// ProxyTray.cpp : Defines the entry point for the application. // //#include "vld/vld.h" #include "stdafx.h" #include "ProxyTray.h" #define MAX_LOADSTRING 100 #define MYMSG_NOTIFYICON (WM_APP + 100) short lsPort=0; short gsPort=0; int gFileServer=0; unsigned char serverIP[4]={192,168,1,109}; char ipAddr[30]; struct CLIENT { Server *client; short NetworkID; char bind; }; //Null the client binds std::vector clients; short loginID=0; short gameID=0; // Global Variables: HINSTANCE hInst; // current instance TCHAR szTitle[MAX_LOADSTRING]; // The title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name HWND hWindow; HANDLE mutex; bool closing=false; HANDLE hProc; // Forward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void SetTrayIcon(HINSTANCE hInstance, char *tooltip, HWND hWnd, DWORD dwMsgType, UINT iconNum); DWORD WINAPI MainProgram(LPVOID param); LONG OnTrayNotification(HINSTANCE hInstance, HWND hWnd, WPARAM wParam, LPARAM lParam); HANDLE appMutex; HANDLE hThread=NULL; int lag=0; int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { if(lpCmdLine[0]!=0) { lag=atoi(lpCmdLine); } appMutex=OpenMutex(MUTEX_ALL_ACCESS,false,"gwlpproxytrayapp"); if(appMutex!=NULL) { return -1; } appMutex=CreateMutex(NULL,true,"gwlpproxytrayapp"); std::ifstream fin("GWLPBin/proxy.ini"); if(fin.fail()) { MessageBox(0,"Proxy.ini not found!","Fatal Error",MB_OK); return 0; } fin >> ipAddr; fin >> lsPort; fin >> gsPort; fin >> gFileServer; fin.close(); mutex=CreateMutex(NULL,false,NULL); char temp[50]; strcpy(temp,ipAddr); char *p=strtok(temp,"."); for(int i=0; i<4; i++) { serverIP[i]=(char)atoi(p); p=strtok(0,"."); } // TODO: Place code here. MSG msg; HACCEL hAccelTable; // Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_PROXYTRAY, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_PROXYTRAY); // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // FUNCTION: MyRegisterClass() // // PURPOSE: Registers the window class. // // COMMENTS: // // This function and its usage are only necessary if you want this code // to be compatible with Win32 systems prior to the 'RegisterClassEx' // function that was added to Windows 95. It is important to call this function // so that the application will get 'well formed' small icons associated // with it. // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_PROXYTRAY); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = (LPCTSTR)IDC_PROXYTRAY; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex); } // // FUNCTION: InitInstance(HANDLE, int) // // PURPOSE: Saves instance handle and creates main window // // COMMENTS: // // In this function, we save the instance handle in a global variable and // create and display the main program window. // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // Store instance handle in our global variable hWindow = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWindow) { return FALSE; } //ShowWindow(hWindow, nCmdShow); UpdateWindow(hWindow); SetTrayIcon(hInst,"Listening for connections...",hWindow,NIM_ADD,IDI_DISCONN); DWORD threadID=0; hThread=CreateThread(NULL,0,MainProgram,0,0,&threadID); return TRUE; } void SetTrayIcon(HINSTANCE hInstance, char *tooltip, HWND hWnd, DWORD dwMsgType, UINT iconNum) { HICON hIconAtIndex = LoadIcon(hInstance, (LPCTSTR) MAKEINTRESOURCE(iconNum)); NOTIFYICONDATA IconData; memset(&IconData,0,sizeof(NOTIFYICONDATA)); IconData.cbSize = sizeof(NOTIFYICONDATA); IconData.hIcon = hIconAtIndex; IconData.hWnd = hWnd; lstrcpyn(IconData.szTip,tooltip, (int) strlen(tooltip)+1); IconData.uCallbackMessage = MYMSG_NOTIFYICON; IconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; Shell_NotifyIcon(dwMsgType, &IconData); SendMessage(hWnd, WM_SETICON, NULL, (long) hIconAtIndex); if(hIconAtIndex) DestroyIcon(hIconAtIndex); } // // FUNCTION: WndProc(HWND, unsigned, WORD, LONG) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_EXIT: DestroyWindow(hWnd); break; case ID_EXIT_EXIT: SetTrayIcon(hInst,"Exiting",hWnd,NIM_DELETE,IDI_DISCONN); DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... EndPaint(hWnd, &ps); break; case WM_DESTROY: WaitForSingleObject(mutex,INFINITE); closing=true; ReleaseMutex(mutex); while(true) { WaitForSingleObject(mutex,INFINITE); if(closing==false) { break; } ReleaseMutex(mutex); } ReleaseMutex(appMutex); PostQuitMessage(0); break; case MYMSG_NOTIFYICON: // System Tray Icon notification will be executed.. OnTrayNotification(hInst, hWnd, wParam, lParam); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } DWORD WINAPI MainProgram(LPVOID param) { GWLPLog log; log.SetFileHeader("GWLP Proxy","1.0"); log.Open("proxy.txt",LOG_DEBUGS);//LOG_ERRORS | LOG_NOTICES log.SetSpecialDefaults(CATEGORY_OPT,SECTION_OPT,SECTION_OPT_OK,SECTION_OPT_FAIL); log.SetLevelDefaults(DEBUG_TEXT,WARNING_TEXT,NOTICE_TEXT,ERROR_TEXT,ALWAYS_TEXT); //Start up the server binds Server *loginBind=new Server(6112,"Login Bind",0,false,&log); //Output the file for the login server proxy ip std::ofstream fout("GWLPBin/pip.txt"); char lip[100]; loginBind->GetIpString(lip); fout << lip << '\n'; fout << gFileServer; fout.close(); SERVERCONFIG sc; sc.maxConnections=10; sc.notifyOnConnect=true; loginBind->UpdateServerConfig(sc); unsigned char ip[4]={127,0,0,1}; Server *gameServBind=new Server(6112,"Game Bind",ip,false,&log); gameServBind->UpdateServerConfig(sc); TimerInit(); int sysTrayUpdate=0; while(true) { std::vector::iterator itr; for(itr=clients.begin(); itr!=clients.end(); itr++) { (itr)->client->Update(); } if(closing){break;} loginBind->Update(); gameServBind->Update(); if(loginBind->mFatalError || gameServBind->mFatalError) { break; } if(TimerGetTime()-sysTrayUpdate>1000) { int ret=0; loginBind->GetNumClients(&ret); if(ret==0) { SetTrayIcon(hInst,"Listening for connections...",hWindow,NIM_MODIFY,IDI_DISCONN); } else if(ret>0) { char tt[200]; sprintf(tt,"Connected to proxy...server could not be found. Connections: %d",ret); SetTrayIcon(hInst,tt,hWindow,NIM_MODIFY,IDI_PARTCONN); } if(clients.size()>0) { SetTrayIcon(hInst,"Connected",hWindow,NIM_MODIFY,IDI_CONNECTED); } sysTrayUpdate=(int)TimerGetTime(); } //Check for updates from the login server bind NetMessage d; while(loginBind->GetNextNetMessage(&d)==0) { if(d.dataSize==CLIENT_CONNECTED) { SetTrayIcon(hInst,"Connecting...",hWindow,NIM_MODIFY,IDI_PARTCONN); CLIENT c; c.bind=0; c.NetworkID=d.networkId; loginID=d.networkId; c.client=new Server(lsPort,"login client",serverIP,true,&log); SERVERCONFIG sc2; sc2.maxConnections=10; c.client->UpdateServerConfig(sc2); if(c.client->Connect(serverIP)!=0) { c.client->Shutdown(false); delete c.client; c.client=NULL; char mess[500]; sprintf(mess,"Could not connect to login server(%s)! Please close Guild Wars, check your configuration settings, and try again.",ipAddr); log.error(mess); MessageBox(0,mess,"GWLP Proxy Error",MB_OK | MB_ICONSTOP | MB_SYSTEMMODAL); } else { clients.push_back(c); } } else if(d.dataSize==CLIENT_DISCONNECTED) { std::vector::iterator itr; for(itr=clients.begin(); itr!=clients.end(); itr++) { if(itr->NetworkID==d.networkId && itr->bind==0) { itr->client->Shutdown(false); delete itr->client; clients.erase(itr); break; } } } else if(d.dataSize>0) { bool sent=false; std::vector::iterator itr; for(itr=clients.begin(); itr!=clients.end(); itr++) { if(itr->NetworkID==d.networkId && itr->bind==0) { d.networkId=itr->client->GetClientModeId(); BIT_SET(d.flags,NETMESS_BOOL_DEL); unsigned char *ddata=d.pData; itr->client->SendData(&d,lag); d.pData=NULL; if(ddata!=NULL){delete [] ddata;} sent=true; break; } } } //---------------------------------------------------------------- //free the memory used by the NetMessage. Deletes the original data. //---------------------------------------------------------------- if(d.pData!=0) delete [] d.pData; } //Check for updates from the game server bind while(gameServBind->GetNextNetMessage(&d)==0) { if(d.dataSize==CLIENT_CONNECTED) { CLIENT c; c.NetworkID=d.networkId; c.bind=1; gameID=d.networkId; c.client=new Server(gsPort,"game client",serverIP,true,&log); SERVERCONFIG sc2; sc2.maxConnections=10; c.client->UpdateServerConfig(sc2); if(c.client->Connect(serverIP)!=0) { c.client->Shutdown(false); delete c.client; c.client=NULL; char mess[500]; sprintf(mess,"Could not connect to game server(%s)! Please close Guild Wars, check your configuration settings, and try again.",ipAddr); log.error(mess); MessageBox(0,mess,"GWLP Proxy Error",MB_OK | MB_ICONSTOP | MB_SYSTEMMODAL); } else { clients.push_back(c); } } else if(d.dataSize==CLIENT_DISCONNECTED) { std::vector::iterator itr; for(itr=clients.begin(); itr!=clients.end(); itr++) { if(itr->NetworkID==d.networkId && itr->bind==1) { itr->client->Shutdown(false); delete itr->client; clients.erase(itr); break; } } } else if(d.dataSize>0) { std::vector::iterator itr; for(itr=clients.begin(); itr!=clients.end(); itr++) { if(itr->NetworkID==d.networkId && itr->bind==1) { d.networkId=itr->client->GetClientModeId(); BIT_SET(d.flags,NETMESS_BOOL_DEL); unsigned char *ddata=d.pData; itr->client->SendData(&d,lag); if(ddata!=NULL){delete [] ddata;} d.pData=NULL; break; } } } //---------------------------------------------------------------- //free the memory used by the NetMessage. Deletes the original data. //---------------------------------------------------------------- if(d.pData!=0) delete [] d.pData; } for(itr=clients.begin(); itr!=clients.end(); itr++) { while(itr->client->GetNextNetMessage(&d)==0) { if(d.dataSize==CLIENT_DISCONNECTED) { if(itr->bind==0) { loginBind->Kick(itr->NetworkID); } else { gameServBind->Kick(itr->NetworkID); } } else { d.networkId=itr->NetworkID; BIT_SET(d.flags,NETMESS_BOOL_DEL); if(itr->bind==0) { unsigned char *ddata=d.pData; loginBind->SendData(&d,lag); if(ddata!=NULL){delete [] ddata;} } else { unsigned char *ddata=d.pData; gameServBind->SendData(&d,lag); if(ddata!=NULL){delete [] ddata;} } d.pData=NULL; } if(d.pData!=0){delete [] d.pData;} } } int ret; loginBind->GetNumClients(&ret); if(ret==0) { Sleep(100); } else { Sleep(1); } WaitForSingleObject(mutex,INFINITE); if(closing) { break; } ReleaseMutex(mutex); } //Clean up loginBind->Shutdown(false); gameServBind->Shutdown(true); delete loginBind; delete gameServBind; log.Close(); std::vector::iterator itr; for(itr=clients.begin(); itr!=clients.end(); itr++) { itr->client->Shutdown(false); delete itr->client; } clients.clear(); WaitForSingleObject(mutex,INFINITE); closing=false; ReleaseMutex(mutex); return 0; } LONG OnTrayNotification(HINSTANCE hInstance, HWND hWnd, WPARAM wParam, LPARAM lParam) { HMENU hMenu; switch(lParam) { case WM_RBUTTONDOWN: { /** Load and verify the menu**/ if(hMenu = LoadMenu(hInstance, (LPCTSTR) MAKEINTRESOURCE(IDR_MENU1))) { hMenu = GetSubMenu(hMenu, 0); if(hMenu != NULL) { POINT pt; GetCursorPos(&pt); SetForegroundWindow(hWnd); TrackPopupMenu(hMenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, 0, hWnd, NULL); } } break ; } } return 0; }