123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659 |
- #include <fcntl.h>
- #include <pty.h>
- #include <stdio.h>
- #include <string.h>
- #include <sys/select.h>
- #include <sys/wait.h>
- #include <termios.h>
- #include <unistd.h>
- #include <fstream>
- #include <iostream>
- #include <sstream>
- #include <string>
- std::string username;
- std::string fullname;
- using namespace std;
- #include <strings.h> // strcasecmp
- #include <time.h>
- #include <ctype.h>
- #include <stdlib.h> // random()
- #include "zf_log.h"
- FILE *g_log_file;
- static void file_output_callback(const zf_log_message *msg, void *arg) {
- (void)arg;
- *msg->p = '\n';
- fwrite(msg->buf, msg->p - msg->buf + 1, 1, g_log_file);
- fflush(g_log_file);
- }
- static void file_output_close(void) { fclose(g_log_file); }
- static int file_output_open(const char *const log_path) {
- g_log_file = fopen(log_path, "a");
- if (!g_log_file) {
- ZF_LOGW("Failed to open log file %s", log_path);
- return 0;
- }
- atexit(file_output_close);
- zf_log_set_output_v(ZF_LOG_PUT_STD, 0, file_output_callback);
- return 1;
- }
- void log_flush(void) { fflush(g_log_file); }
- #include "terminal.h"
- #include "utils.h"
- struct console_details console;
- #include "wordplay.h"
- #define TARGET "./mySTIC"
- #define BSIZE 1024
- int node;
- int locate_user(const char *alias) {
- FILE *user;
- char buffer[0x600];
- char temp[100];
- user = fopen("data/users.dat", "rb");
- if (user == NULL)
- return 0;
-
- while (fread(buffer, 0x600, 1, user) == 1) {
- pcopy(buffer + 0x6d, temp);
- if (strcasecmp(temp, username.c_str()) == 0) {
- pcopy(buffer + 0x8c, temp);
- fullname.assign(temp);
- break;
- }
-
- }
- fclose(user);
- return 1;
- }
- ifstream logfile;
- streampos log_pos;
- void open_mystic_log(void) {
- string mystic_logfile;
- {
- ostringstream buffer;
- buffer << "logs/node" << node << ".log";
- mystic_logfile = buffer.str();
- };
- logfile.open(mystic_logfile, ios_base::in | ios_base::ate);
-
- if (logfile.is_open()) {
- ZF_LOGD("Log %s open", (const char *)mystic_logfile.c_str());
- log_pos = logfile.tellg();
- } else {
- ZF_LOGE("Failed to open: %s", (const char *)mystic_logfile.c_str());
- }
- }
- void scan_mystic_log(void) {
- if (logfile.is_open()) {
- int again = 0;
- do {
- string line = find_new_text(logfile, log_pos);
- if (line.empty())
- return;
- again = 1;
- ZF_LOGD("mystic log: %s", (const char *)line.c_str());
-
- if (line.find("New user application") != string::npos) {
- ZF_LOGE("New User");
- }
- size_t pos;
- pos = line.find("Created Account: ");
- if (pos != string::npos) {
- pos += 18 - 1;
-
- size_t len = line.find('#', pos);
- if (len != string::npos) {
- username = line.substr(pos, len - pos - 1);
- ZF_LOGE("New User: %s", (const char *)username.c_str());
-
- locate_user(username.c_str());
- ZF_LOGD("Username: [%s] A.K.A. [%s]", (const char *)username.c_str(),
- (const char *)fullname.c_str());
- }
- }
- pos = line.find(" logged in");
- if (pos != string::npos) {
- --pos;
- size_t len = line.rfind(' ', pos);
- if (len != string::npos) {
- len++;
- username = line.substr(len, pos + 1 - len);
- ZF_LOGE("User: %s", (const char *)username.c_str());
-
- locate_user(username.c_str());
- ZF_LOGD("Username: [%s] A.K.A. [%s]", (const char *)username.c_str(),
- (const char *)fullname.c_str());
- }
- }
- } while (again);
- }
- }
- int main(int argc, char *argv[]) {
- int master;
- pid_t pid;
- node = -1;
- init_harry();
- srandom(time(NULL));
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- for (int x = 0; x < argc; x++) {
- if (strncmp("-U", argv[x], 2) == 0) {
- username.assign(argv[x] + 2);
- }
-
-
- if (strncmp("-TID", argv[x], 4) == 0) {
- node = (atoi(argv[x] + 4) - 5) / 2;
- }
- }
- if (node == -1) {
-
- char *args[20];
- int x;
- char new_exec[] = TARGET;
-
- args[0] = new_exec;
- for (x = 1; x < argc; x++) {
- args[x] = argv[x];
- };
-
- args[x] = NULL;
-
- execvp(TARGET, args);
- return 2;
- }
- string logfile;
- {
- ostringstream buffer;
- buffer << "horrible_harry_" << node << ".log";
- logfile = buffer.str();
- };
- if (!file_output_open((const char *)logfile.c_str()))
- return 2;
- for (auto cit = CONFIG.begin(); cit != CONFIG.end(); ++cit) {
- ZF_LOGD("Config {%s}:{%s}", (const char *)cit->first.c_str(),
- (const char *)cit->second.c_str());
- }
- ZF_LOGI("Node: %d", node);
- if (!username.empty()) {
- locate_user(username.c_str());
- ZF_LOGD("Username: [%s] A.K.A. [%s]", (const char *)username.c_str(),
- (const char *)fullname.c_str());
- }
- open_mystic_log();
- pid = forkpty(&master, NULL, NULL, NULL);
-
- if (pid < 0) {
- return 1;
- }
-
- else if (pid == 0) {
- char *args[20];
- int x;
- char new_exec[] = TARGET;
-
- args[0] = new_exec;
- for (x = 1; x < argc; x++) {
- args[x] = argv[x];
- };
-
- args[x] = NULL;
-
- execvp(TARGET, args);
- }
-
- else {
- struct termios tios, orig1;
- struct timeval timeout;
- time_t last_logscan = time(NULL);
- ZF_LOGD("starting");
- tcgetattr(master, &tios);
- tios.c_lflag &= ~(ECHO | ECHONL | ICANON);
-
- tcsetattr(master, TCSAFLUSH, &tios);
- tcgetattr(1, &orig1);
- tios = orig1;
- tios.c_iflag &= ~(ICRNL | IXON | BRKINT);
- tios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
- tios.c_oflag &= ~(OPOST);
-
- tcsetattr(1, TCSAFLUSH, &tios);
-
- string buffer;
- buffer.reserve(BSIZE * 2);
- string play;
- play.reserve(4096);
- int zmodem = 0;
-
- for (;;) {
- int time_idle;
-
-
- fd_set read_fd;
- fd_set write_fd;
- fd_set except_fd;
-
- FD_ZERO(&read_fd);
- FD_ZERO(&write_fd);
- FD_ZERO(&except_fd);
-
- FD_SET(master, &read_fd);
-
- FD_SET(STDIN_FILENO, &read_fd);
-
-
-
- if (buffer.size() == 0) {
-
- if (zmodem) {
- timeout.tv_sec = 5;
- } else {
- timeout.tv_sec = randrange(10, 20);
- };
- timeout.tv_usec = 0;
- time_idle = 1;
- } else {
-
- timeout.tv_sec = 0;
- timeout.tv_usec = 1;
- time_idle = 0;
- }
- if (last_logscan < time(NULL)) {
- scan_mystic_log();
- if (username.empty())
- last_logscan = time(NULL) + 2;
- else
- last_logscan = time(NULL) + 10;
- }
- if (select(master + 1, &read_fd, &write_fd, &except_fd, &timeout) == 0) {
- ZF_LOGI("TIMEOUT");
-
- if (time_idle) {
- if (harry_level() && !zmodem)
- harry_idle_event(STDOUT_FILENO);
- } else {
- ZF_LOGV("TIMEOUT buffer: %s", logrepr(buffer.c_str()));
-
- play.assign(buffer);
- if (harry_level())
- mangle(STDOUT_FILENO, play);
- else {
- write(STDOUT_FILENO, play.data(), play.size());
- console_receive(&console, play);
- }
-
- ZF_LOGI("buffer clear");
- buffer.clear();
-
-
- }
- }
-
- if (FD_ISSET(master, &read_fd)) {
-
-
- char read_buffer[BSIZE + 1];
- int total;
-
- if ((total = read(master, read_buffer, BSIZE)) != -1) {
-
- ZF_LOGV("Read %d bytes", total);
- buffer.append(read_buffer, total);
- if (zmodem) {
-
- size_t zend = buffer.find("\x1b[0m");
- if (zend != string::npos)
- zmodem = 0;
- zend = buffer.find("\x1b[1;1H");
- if (zend != string::npos)
- zmodem = 0;
- if (!zmodem)
- ZF_LOGD("Zmodem end");
- } else {
-
- size_t zstart = buffer.find("**\x18"
- "B0");
- if (zstart != string::npos) {
- zmodem = 1;
- ZF_LOGD("Zmodem start");
- }
- }
- if (zmodem) {
-
- write(STDOUT_FILENO, buffer.data(), buffer.size());
-
- buffer.clear();
- } else {
-
-
-
- size_t pos = buffer.rfind("\r\n");
-
-
- if (pos != string::npos) {
-
- pos += 2;
-
-
- play.assign(buffer, 0, pos);
- ZF_LOGI("play %lu size, %lu cap", play.size(), play.capacity());
-
-
- buffer.erase(0, pos);
- mangle(STDOUT_FILENO, play);
-
-
-
-
-
-
- }
-
- if (buffer.size() > BSIZE) {
-
-
-
- ZF_LOGI("Buffer %lu bytes, write only...", buffer.size());
- write(STDOUT_FILENO, buffer.data(), buffer.size());
- console_receive(&console, buffer);
- buffer.clear();
- }
- }
- } else
- break;
- }
-
- if (FD_ISSET(STDIN_FILENO, &read_fd)) {
-
- char input[BSIZE];
- int r = read(STDIN_FILENO, &input, BSIZE);
- input[r] = 0;
-
- if (!zmodem) {
- if (r > 50) {
- ZF_LOGI("<< %d bytes", r);
- } else {
- ZF_LOGI("<< %s", repr(input));
- }
- }
- write(master, &input, r);
-
-
- }
- }
-
- tcsetattr(1, TCSAFLUSH, &orig1);
- ZF_LOGD("exit");
- }
- return 0;
- }
|