#include #include #include #include #include "../Servers/src/NetServer.h" #include "../Servers/src/GWLPLog.h" #include "../Servers/src/Utility.h" #include "../Servers/src/IC.h" using namespace std; #undef ALWAYS_TEXT #define ALWAYS_TEXT OPT_ALWAYS_LEVEL | OPT_APP_TIME | OPT_NEWLINE char accName[100]; char pass[100]; char storedCmd[150]; bool prompt=false; bool promptWait=false; bool loggedIn=false; bool writingFileASCII=false; char filename[100]; vector leftOverPackets; ofstream fout; int main( int argc, char *argv[] ) { short port=0; unsigned char ip[4]; char ipString[25]; GWLPLog log; log.SetSpecialDefaults(CATEGORY_OPT,SECTION_OPT,SECTION_OPT_OK,SECTION_OPT_FAIL); log.SetLevelDefaults(DEBUG_TEXT,WARNING_TEXT,NOTICE_TEXT,ERROR_TEXT,ALWAYS_TEXT); log.SetFileHeader("GWLP Admin Console","1.0"); log.Open("AdminCon.txt",LOG_WARNINGS | LOG_NOTICES | LOG_ERRORS); if(argc==3) { strcpy(ipString,argv[1]); port=atoi(argv[2]); } else { printf("Server IP: "); cin >> ipString; printf("Server Port: "); cin >> port; } char temp[50]; strcpy(temp,ipString); char *p=strtok(temp,"."); for(int i=0; i<4; i++) { ip[i]=(char)atoi(p); p=strtok(0,"."); } //Connect to the server log.notice("Connecting to %d.%d.%d.%d:%d...",ip[0],ip[1],ip[2],ip[3],port); Server *server=new Server(); if(server->Init(port,"ICC",ip,true,&log)==-1) { if(server!=NULL){delete server;} log.error("Failed to Initialize!"); return -1; } SERVERCONFIG sc; sc.maxConnections=10; server->UpdateServerConfig(sc); if(server->Connect(ip)!=0) { if(server!=NULL){delete server;} log.error("Failed to connect!"); return -1; } log.notice("Connected.\n\n"); bool exitAC=false; while(true) { server->Update(); //We are now connected to the login server =D NetMessage d(0,0,0); while(server->GetNextNetMessage(&d)==0) { if(d.dataSize==CLIENT_DISCONNECTED) { log.notice("Disconnected."); exitAC=true; break; } //Verify the header and the received packet size switch(CheckHeaderAndSize(&d)) { //First check to see if the received packet size is //smaller than what is specified in the header. case HOLDING_LOWSIZE: ProcessLowSize(&d,&leftOverPackets,NULL); break; //We have received more data than the packet contains. I.E. We have received 1.x packets. Split the data //into two packets and store one as incomplete. Does not need a break because it generates a proper packet. case HOLDING_HIGHSIZE: ProcessHighSize(&d,&leftOverPackets,NULL); //log.LogKnownData("sdf",d.data,d.dataSize); //There are no problems with the data we have received. case NOT_HOLDING: if(d.dataSize>0) { //This is the first packet which requests the account name and password hash if(d.pData[0]==0x01) { system("cls"); memset(pass,0,100); //Query the user for the username and password printf("Account Name: "); cin >> accName; printf("Password: "); cin >> pass; cin.sync(); unsigned char passHash[32]; Util::Hash((unsigned char*)pass,passHash,strlen(pass)); memset(pass,0,100); //Send the message CompiledPacket pck; pck.AddByte(0xFE); pck.AddByte(strlen(accName)+32); pck.Add(accName); pck.Add(passHash,32,false); Util::SendCompiledPacket(&pck,d.networkId,server); system("cls"); } //Check for log in fail if(d.pData[0]==0x0c) { log.error("This account does not have access to the admin console!"); exitAC=true; break; } else if(d.pData[0]==0x0E) { log.error("Incorrect password, password does not exist, or database failure!"); exitAC=true; break; } else if(d.pData[0]==0x0F) { log.error("Account not found!"); exitAC=true; break; } else if(d.pData[0]==0x0D) { prompt=true; loggedIn=true; printf("GWLP Admin Console\nDefault Commands: 'help' and 'logout'\n"); } else if(d.pData[0]==0xFF && loggedIn) { //Extract the data char buf[251]; memset(buf,0,251); memcpy(buf,d.pData+2,d.pData[1]); //ASCII file stuff if(strstr(buf,"FILEASCII")!=0) { char *t=strstr(buf," "); fout.open(t); strcpy(filename,t); writingFileASCII=true; } else if(strcmp(buf,"FILEEND")==0) { fout.close(); writingFileASCII=false; printf("Done writing file: %s\n",filename); } else if(writingFileASCII) { fout << buf; } else if(strcmp(buf,"TERMINATOR")!=0) { /*printf("%s\n",buf);*/ cout << buf << endl; cout.flush(); } else if(leftOverPackets.size()==0) { if(strstr(buf,":csp:")==NULL) { printf("\n"); } else { memset(storedCmd,0,150); memcpy(storedCmd,buf+5,d.pData[1]-5); } prompt=true; } } } } if(d.pData!=NULL){delete [] d.pData;} RepopulatePacketQueue(server,&leftOverPackets); } //Output the command prompt and wait for input if(prompt && !promptWait) { prompt=false; #ifdef WIN32 HANDLE hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hConsoleHandle,FOREGROUND_RED | FOREGROUND_GREEN); #endif char cmdLine[2500]; printf("%s",accName); #ifdef WIN32 SetConsoleTextAttribute(hConsoleHandle,FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); #endif printf(">"); promptWait=true; cin.getline(cmdLine,250); //cin >> cmdLine; if(strlen(cmdLine)>250) { printf("Command too long.\n"); prompt=true; } else { if(!(cmdLine[0]>=48 && cmdLine[0]<=57)) { CompiledPacket pck; pck.AddByte(0xFF); pck.AddByte(strlen(cmdLine)); pck.Add(cmdLine); Util::SendCompiledPacket(&pck,server->GetClientModeId(),server); } else { CompiledPacket pck; pck.AddByte(0xFF); pck.AddByte(strlen(cmdLine)+strlen(storedCmd)+1); pck.Add(storedCmd); pck.AddByte(' '); pck.Add(cmdLine); Util::SendCompiledPacket(&pck,server->GetClientModeId(),server); memset(storedCmd,0,150); } } promptWait=false; } //If the condition is true to exit if(server->mFatalError || exitAC) { break; } } server->Shutdown(true); delete server; log.Close(); }