Example #1
0
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
}
Example #2
0
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;
}