int CNetConsole::Update() { NETSOCKET Socket; NETADDR Addr; if(net_tcp_accept(m_Socket, &Socket, &Addr) > 0) { // check if we just should drop the packet char aBuf[128]; if(NetBan() && NetBan()->IsBanned(&Addr, aBuf, sizeof(aBuf))) { // banned, reply with a message and drop net_tcp_send(Socket, aBuf, str_length(aBuf)); net_tcp_close(Socket); } else AcceptClient(Socket, &Addr); } for(int i = 0; i < NET_MAX_CONSOLE_CLIENTS; i++) { if(m_aSlots[i].m_Connection.State() == NET_CONNSTATE_ONLINE) m_aSlots[i].m_Connection.Update(); if(m_aSlots[i].m_Connection.State() == NET_CONNSTATE_ERROR) Drop(i, m_aSlots[i].m_Connection.ErrorString()); } return 0; }
int CNetConsole::Close() { for(int i = 0; i < NET_MAX_CONSOLE_CLIENTS; i++) m_aSlots[i].m_Connection.Disconnect("closing console"); net_tcp_close(m_Socket); return 0; }
void CSpoofRemote::Disconnect() { #if defined(CONF_SPOOFING) Console()->Print(0, "spfrmt", "disconnecting from zervor!", false); Console()->Print(0, "spfrmt", "requesting threads to terminate...", false); Reset(); Console()->Print(0, "spfrmt", "closing socket...", false); net_tcp_close(m_Socket); #endif }
void CConsoleNetConnection::Disconnect(const char *pReason) { if(State() == NET_CONNSTATE_OFFLINE) return; if(pReason && pReason[0]) Send(pReason); net_tcp_close(m_Socket); Reset(); }
void CSpoofRemote::StartConnection(void *pUserData) { #if defined(CONF_SPOOFING) CSpoofRemote *pSelf = (CSpoofRemote *)pUserData; pSelf->m_ConnState = CONNSTATE_CONNECTING; pSelf->Console()->Print(0, "spfrmt", "Connecting to zervor...", false); NETADDR BindAddr; mem_zero(&pSelf->m_HostAddress, sizeof(pSelf->m_HostAddress)); mem_zero(&BindAddr, sizeof(BindAddr)); // lookup if(net_host_lookup(g_Config.m_ClSpoofSrvIP, &pSelf->m_HostAddress, NETTYPE_IPV4) != 0) { pSelf->Console()->Printf(IConsole::OUTPUT_LEVEL_STANDARD, "spfrmt", "ERROR: Can't resolve %s", g_Config.m_ClSpoofSrvIP); pSelf->m_ConnState = CONNSTATE_CONNECTING; return; } pSelf->m_HostAddress.port = (unsigned short)g_Config.m_ClSpoofSrvPort; // connect BindAddr.type = NETTYPE_IPV4; pSelf->m_Socket = net_tcp_create(BindAddr); if(net_tcp_connect(pSelf->m_Socket, &pSelf->m_HostAddress) != 0) { net_tcp_close(pSelf->m_Socket); char aBuf[128]; net_addr_str(&pSelf->m_HostAddress, aBuf, sizeof(aBuf), 0); pSelf->Console()->Printf(IConsole::OUTPUT_LEVEL_STANDARD, "spfrmt", "ERROR: Can't connect to '%s:%d' on type=%i", aBuf, pSelf->m_HostAddress.port, pSelf->m_HostAddress.type); pSelf->m_ConnState = CONNSTATE_DISCONNECTED; int error = net_errno(); #if defined(CONF_FAMILY_WINDOWS) dbg_msg("spfrmt", " : (errorcode=%d)", error); #else dbg_msg("spfrmt", " : (%d '%s')", error, strerror(error)); #endif return; } pSelf->m_LastAck = time_get(); pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "spfrmt", "connected, creating threads...", false); pSelf->m_ConnState = CONNSTATE_CONNECTED; pSelf->m_pWorkerThread = thread_init(CSpoofRemote::Worker, pUserData); pSelf->m_pListenerThread = thread_init(CSpoofRemote::Listener, pUserData); #endif }
void CGeoIP::Search(InfoGeoIPThread *pGeoInfo) { if (!m_Active) return; if (m_pGeoIPThread) { net_tcp_close(m_Socket); thread_destroy(m_pGeoIPThread); m_pGeoIPThread = 0x0; } m_pGeoIPThread = thread_init(ThreadGeoIP, pGeoInfo); }
int CNetConsole::AcceptClient(NETSOCKET Socket, const NETADDR *pAddr) { char aError[256] = { 0 }; int FreeSlot = -1; // look for free slot or multiple client for(int i = 0; i < NET_MAX_CONSOLE_CLIENTS; i++) { if(FreeSlot == -1 && m_aSlots[i].m_Connection.State() == NET_CONNSTATE_OFFLINE) FreeSlot = i; if(m_aSlots[i].m_Connection.State() != NET_CONNSTATE_OFFLINE) { if(net_addr_comp(pAddr, m_aSlots[i].m_Connection.PeerAddress()) == 0) { str_copy(aError, "only one client per IP allowed", sizeof(aError)); break; } } } // accept client if(!aError[0] && FreeSlot != -1) { m_aSlots[FreeSlot].m_Connection.Init(Socket, pAddr); if(m_pfnNewClient) m_pfnNewClient(FreeSlot, m_UserPtr); return 0; } // reject client if(!aError[0]) str_copy(aError, "no free slot available", sizeof(aError)); net_tcp_send(Socket, aError, str_length(aError)); net_tcp_close(Socket); return -1; }
IGeoIP::GeoInfo CGeoIP::GetInfo(std::string ip) { dbg_msg("GeoIP", "Searching geolocation of '%s'...", ip.c_str()); NETADDR bindAddr; mem_zero(&bindAddr, sizeof(bindAddr)); char aNetBuff[1024]; std::string jsonData; IGeoIP::GeoInfo rInfo; //Connect bindAddr.type = NETTYPE_IPV4; m_Socket = net_tcp_create(bindAddr); net_set_non_blocking(m_Socket); if(net_tcp_connect(m_Socket, &m_HostAddress) < 0) { if (net_errno() != EINPROGRESS) { dbg_msg("GeoIP","ERROR: Can't connect."); net_tcp_close(m_Socket); return rInfo; } } net_socket_read_wait(m_Socket, 1); net_set_blocking(m_Socket); //Send request str_format(aNetBuff, sizeof(aNetBuff), "GET /geoip/%s HTTP/1.0\r\nHost: www.telize.com\r\n\r\n", ip.c_str()); net_tcp_send(m_Socket, aNetBuff, strlen(aNetBuff)); //read data bool errors = true; std::string NetData; int TotalRecv = 0; int TotalBytes = 0; int CurrentRecv = 0; bool isHead = true; int enterCtrl = 0; while ((CurrentRecv = net_tcp_recv(m_Socket, aNetBuff, sizeof(aNetBuff))) > 0) { for (int i=0; i<CurrentRecv ; i++) { if (isHead) { if (aNetBuff[i]=='\n') { enterCtrl++; if (enterCtrl == 2) { isHead = false; NetData.clear(); continue; } std::transform(NetData.begin(), NetData.end(), NetData.begin(), ::tolower); if (NetData.find("content-length:") != std::string::npos) { sscanf(NetData.c_str(), "content-length:%d", &TotalBytes); if (TotalBytes == 0) sscanf(NetData.c_str(), "content-length: %d", &TotalBytes); } NetData.clear(); continue; } else if (aNetBuff[i]!='\r') enterCtrl=0; NetData+=aNetBuff[i]; } else { if (TotalBytes == 0) { net_tcp_close(m_Socket); dbg_msg("GeoIP","ERROR: Error with size received data."); break; } jsonData += aNetBuff[i]; TotalRecv++; if (TotalRecv == TotalBytes) { errors = false; break; } } } } //Finish net_tcp_close(m_Socket); if (!errors) { // parse json data json_settings JsonSettings; mem_zero(&JsonSettings, sizeof(JsonSettings)); char aError[256]; json_value *pJsonData = json_parse_ex(&JsonSettings, jsonData.c_str(), jsonData.length(), aError); if (pJsonData == 0) { dbg_msg("GeoIP", "Error: %s", aError); return rInfo; } // generate configurations const json_value &countryCode = (*pJsonData)["country_code"]; if (countryCode.type == json_string) str_copy(rInfo.m_aCountryCode, (const char *)countryCode, sizeof(rInfo.m_aCountryCode)); const json_value &countryName = (*pJsonData)["country"]; if (countryName.type == json_string) str_copy(rInfo.m_aCountryName, (const char *)countryName, sizeof(rInfo.m_aCountryName)); //const json_value &isp = (*pJsonData)["isp"]; //if (isp.type == json_string) geoInfo->m_Isp = (const char *)isp; } return rInfo; }
bool CHttpDownloader::GetToFile(const char *url, const char *dest, unsigned timeOut, unsigned downloadSpeed) { if (!dest || dest[0] == 0) return false; int64 downloadTime = time_get(); unsigned chunkBytes = 0; NETSOCKET sock; NETADDR nadd, bindAddr; mem_zero(&nadd, sizeof(nadd)); mem_zero(&bindAddr, sizeof(bindAddr)); bindAddr.type = NETTYPE_IPV4; NETURL NetUrl = CreateUrl(url); if (net_host_lookup(NetUrl.m_aHost, &nadd, NETTYPE_IPV4) != 0) { dbg_msg("HttpDownloader", "Error can't found '%s'...", NetUrl.m_aHost); return false; } nadd.port = 80; sock = net_tcp_create(bindAddr); if (net_tcp_connect(sock, &nadd) != 0) { dbg_msg("HttpDownloader", "Error can't connect with '%s'...", NetUrl.m_aHost); net_tcp_close(sock); return false; } net_socket_rcv_timeout(sock, timeOut); char aBuff[512] = {0}; str_format(aBuff, sizeof(aBuff), "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", NetUrl.m_aSlug, NetUrl.m_aHost); net_tcp_send(sock, aBuff, str_length(aBuff)); std::string NetData; unsigned TotalRecv = 0; unsigned TotalBytes = 0; int CurrentRecv = 0; unsigned nlCount = 0; char aNetBuff[1024] = {0}; IOHANDLE dstFile = NULL; do { // Limit Speed if (downloadSpeed > 0) { int64 ctime = time_get(); if (ctime - downloadTime <= time_freq()) { if (chunkBytes >= downloadSpeed) { int tdff = (time_freq() - (ctime - downloadTime)) / 1000; thread_sleep(tdff); continue; } } else { chunkBytes = 0; downloadTime = time_get(); } } // CurrentRecv = net_tcp_recv(sock, aNetBuff, sizeof(aNetBuff)); chunkBytes += CurrentRecv; for (int i=0; i<CurrentRecv ; i++) { if (nlCount < 2) { if (aNetBuff[i] == '\r' || aNetBuff[i] == '\n') { ++nlCount; if (NetData.size() > 0) { std::transform(NetData.begin(), NetData.end(), NetData.begin(), ::tolower); if (NetData.find("404 not found") != std::string::npos) { dbg_msg("HttpDownloader", "ERROR 404: '%s' not found...", NetUrl.m_aFile); net_tcp_close(sock); return false; } else if (NetData.find("content-length:") != std::string::npos) { char aFileSize[64]; str_copy(aFileSize, NetData.substr(15).c_str(), sizeof(aFileSize)); str_trim(aFileSize); TotalBytes = atoi(aFileSize); } NetData.clear(); } if (aNetBuff[i] == '\r') ++i; continue; } nlCount = 0; NetData += aNetBuff[i]; } else { if (nlCount == 2) { if (TotalBytes <= 0) { dbg_msg("HttpDownloader", "Error downloading '%s'...", NetUrl.m_aFile); net_tcp_close(sock); return false; } dstFile = io_open(dest, IOFLAG_WRITE); if(!dstFile) { dbg_msg("HttpDownloader", "Error creating '%s'...", dest); net_tcp_close(sock); return false; } ++nlCount; } io_write(dstFile, &aNetBuff[i], 1); TotalRecv++; if (TotalRecv == TotalBytes) break; } } } while (CurrentRecv > 0); net_tcp_close(sock); if (TotalRecv > 0) { io_close(dstFile); return true; } return false; }
unsigned CHttpDownloader::GetFileSize(const char *url, unsigned timeOut) { NETSOCKET sock; NETADDR nadd, bindAddr; mem_zero(&nadd, sizeof(nadd)); mem_zero(&bindAddr, sizeof(bindAddr)); bindAddr.type = NETTYPE_IPV4; NETURL NetUrl = CreateUrl(url); if (net_host_lookup(NetUrl.m_aHost, &nadd, NETTYPE_IPV4) != 0) { dbg_msg("HttpDownloader", "Error can't found '%s'...", NetUrl.m_aHost); return false; } nadd.port = 80; sock = net_tcp_create(bindAddr); net_socket_rcv_timeout(sock, timeOut); if (net_tcp_connect(sock, &nadd) != 0) { dbg_msg("HttpDownloader", "Error can't connect with '%s'...", NetUrl.m_aHost); net_tcp_close(sock); return false; } char aBuff[512] = {0}; str_format(aBuff, sizeof(aBuff), "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", NetUrl.m_aSlug, NetUrl.m_aHost); net_tcp_send(sock, aBuff, str_length(aBuff)); std::string NetData; int TotalBytes = 0; int CurrentRecv = 0; int nlCount = 0; char aNetBuff[1024] = {0}; do { CurrentRecv = net_tcp_recv(sock, aNetBuff, sizeof(aNetBuff)); for (int i=0; i<CurrentRecv ; i++) { if (nlCount < 2) { if (aNetBuff[i] == '\r' || aNetBuff[i] == '\n') { ++nlCount; if (NetData.size() > 0) { std::transform(NetData.begin(), NetData.end(), NetData.begin(), ::tolower); if (NetData.find("404 not found") != std::string::npos) { dbg_msg("HttpDownloader", "ERROR 404: '%s' not found...", NetUrl.m_aFile); net_tcp_close(sock); return false; } else if (NetData.find("content-length:") != std::string::npos) { char aFileSize[64]; str_copy(aFileSize, NetData.substr(15).c_str(), sizeof(aFileSize)); str_trim(aFileSize); TotalBytes = atoi(aFileSize); if (TotalBytes <= 0) { dbg_msg("HttpDownloader", "Error getting size of '%s'...", NetUrl.m_aFile); net_tcp_close(sock); return 0; } net_tcp_close(sock); return TotalBytes; } NetData.clear(); } if (aNetBuff[i] == '\r') ++i; continue; } nlCount = 0; NetData += aNetBuff[i]; } else { dbg_msg("HttpDownloader", "Error getting size of '%s'...", NetUrl.m_aFile); net_tcp_close(sock); return 0; } } } while (CurrentRecv > 0); return 0; }
// TODO: Ugly but works bool CUpdater::GetFile(const char *url, const char *path) { NETSOCKET Socket = invalid_socket; NETADDR HostAddress, BindAddr; mem_zero(&HostAddress, sizeof(HostAddress)); mem_zero(&BindAddr, sizeof(BindAddr)); char aNetBuff[1024]; //Lookup if(net_host_lookup(UPDATES_HOST, &HostAddress, NETTYPE_IPV4) != 0) { dbg_msg("autoupdate", "Error running host lookup"); return false; } HostAddress.port = 80; //Connect BindAddr.type = NETTYPE_IPV4; Socket = net_tcp_create(BindAddr); if(net_tcp_connect(Socket, &HostAddress) != 0) { net_tcp_close(Socket); dbg_msg("autoupdate","Error connecting to host"); return false; } //Send request str_format(aNetBuff, sizeof(aNetBuff), "GET "UPDATES_BASE_PATH"%s HTTP/1.0\r\nHost: "UPDATES_HOST"\r\n\r\n", url); net_tcp_send(Socket, aNetBuff, str_length(aNetBuff)); //read data IOHANDLE dstFile = 0; std::string NetData; int TotalRecv = 0, TotalBytes = 0, CurrentRecv = 0, enterCtrl = 0; bool isHead = true, isStatusLine = true; while ((CurrentRecv = net_tcp_recv(Socket, aNetBuff, sizeof(aNetBuff))) > 0) { for (int i=0; i<CurrentRecv ; i++) { if (isHead) { if (aNetBuff[i]=='\n') { if (++enterCtrl == 2) // Go To Body Part { dstFile = io_open(path, IOFLAG_WRITE); if (!dstFile) { net_tcp_close(Socket); dbg_msg("autoupdate","Error writing to disk"); return false; } str_copy(m_CurrentDownloadFileName, url, sizeof(m_CurrentDownloadFileName)); isHead = false; NetData.clear(); continue; } std::transform(NetData.begin(), NetData.end(), NetData.begin(), ::tolower); // Check Status if (isStatusLine) { bool isCodeOk = NetData.find("200") != std::string::npos; bool isStatusOk = NetData.find("ok") != std::string::npos; if (!isCodeOk || !isStatusOk) { net_tcp_close(Socket); dbg_msg("autoupdate","Server Host returns error code"); return false; } } isStatusLine = false; // Get File Size std::size_t fpos = std::string::npos; if ((fpos=NetData.find("content-length:")) != std::string::npos) sscanf(NetData.substr(fpos+15).c_str(), "%d", &TotalBytes); NetData.clear(); continue; } else if (aNetBuff[i]!='\r') enterCtrl=0; NetData+=aNetBuff[i]; } else // Body Part { if (TotalBytes <= 0) { io_close(dstFile); net_tcp_close(Socket); dbg_msg("autoupdate","Error receiving file"); return false; } m_CurrentDownloadProgress = (float)TotalRecv/(float)TotalBytes; io_write(dstFile, &aNetBuff[i], 1); TotalRecv++; if (TotalRecv == TotalBytes) break; } } } //Finish io_close(dstFile); net_tcp_close(Socket); return true; }