我正在为游戏nethack
开发一个AI机器人,但我无法绕过源代码中的“人机验证”。我提到的代码段位于nethack/sys/unix/unixunix.c
:
#ifdef TTY_GRAPHICS /* idea from rpick%[email protected] * prevent automated rerolling of characters * test input (fd0) so that tee'ing output to get a screen dump still * works * also incidentally prevents development of any hack-o-matic programs */ /* added check for window-system type -dlc */ if (!strcmp(windowprocs.name, "tty")) if (!isatty(0)) error("You must play from a terminal.");#endif
我使用的是JavaScript(更具体地说是Node.js),由于上述原因,即使我启动了一个bash shell子进程并告诉它启动nethack
,程序也不允许我玩。我需要找到一种方法在不重新编译源代码的情况下绕过上述检查。
我目前使用的代码是:
"use strict";var env = { TERM: 'tty' };for (var k in process.env) { env[k] = process.env[k];}var terminal = require('child_process').spawn('bash', [], { env: env,});terminal.stdout.on('data', function (data) { console.log('stdout: ' + data);});terminal.on('exit', function (code) { console.log('child process exited with code ' + code);});setTimeout(function() { terminal.stdin.write('nethack'); terminal.stdin.end();}, 1000);
程序的输出是:
stdout: You must play from a terminal.child process exited with code 1
我可以使用哪些Node.js/JavaScript(如果可能的话,不使用其他语言或框架)的“黑魔法”来解决这个问题?
回答:
这种检查有点无效,因为伪终端(ptys)在isatty()中会返回true。伪终端允许程序伪装成终端。这就是Xterm和Screen的工作原理。如果那个检查不允许这些程序通过,你将无法在它们中玩NetHack。
我从未使用过,但pty.js绑定到你会在C代码中使用的东西,并且接口看起来很合理。