Exemplo n.º 1
0
	HERRCODE ChatClient::loginImpl(int type, const std::wstring& username, const std::wstring& credential)
	{
		sockaddr_in addr = { 0 };
		addr.sin_port = htons(serverPort_);
		addr.sin_family = AF_INET;
		IN_ADDR in_addr;
		InetPton(AF_INET, serverAddr_.c_str(), &in_addr);
		addr.sin_addr = in_addr;
		if (::connect(sock_, (const sockaddr*)&addr, sizeof(sockaddr_in)) != 0) {
			return H_NETWORK_ERROR;
		}

		SockStream stream;
		stream.writeInt(net::kCommandType_Login);
		stream.writeInt(0);
		stream.writeInt(type);
		stream.writeString(username);
		stream.writeString(credential);
		stream.flushSize();
		auto ret = ::send(sock_, stream.getBuf(), stream.getSize(), 0);
		if (ret == SOCKET_ERROR) {
			return H_NETWORK_ERROR;
		}
		buffer buf(200);
		int rc = ::recv(sock_, buf.data(), buf.size(), 0);
		if (rc) {
			SockStream ss(buf.data(), rc);
			assert(net::kCommandType_LoginAck == ss.getInt());
			auto size = ss.getInt();
			auto ack = ss.getInt();

			if (ack == net::kLoginAck_Succeeded) {
				email_ = username;
				authKey_ = ss.getString();
				auto hr = initSocks(sock_);
				if (hr != H_OK)
					return hr;
				return H_OK;
			} else {
				return H_AUTH_FAILED;
			}
		} else {
			return H_NETWORK_ERROR;
		}
	}
Exemplo n.º 2
0
int
main(int argc, char **argv)
{
    FdEventHandlerPtr listener;
    int i;
    int rc;
    int expire = 0, printConfig = 0;

    initAtoms();
    CONFIG_VARIABLE(daemonise, CONFIG_BOOLEAN, "Run as a daemon");
    CONFIG_VARIABLE(pidFile, CONFIG_ATOM, "File with pid of running daemon.");

    preinitChunks();
    preinitLog();
    preinitObject();
    preinitIo();
    preinitDns();
    preinitServer();
    preinitHttp();
    preinitDiskcache();
    preinitLocal();
    preinitForbidden();
    preinitSocks();
	preinitOffline();

    i = 1;
    while(i < argc) {
        if(argv[i][0] != '-')
            break;
        if(strcmp(argv[i], "--") == 0) {
            i++;
            break;
        } else if(strcmp(argv[i], "-h") == 0) {
            usage(argv[0]);
            exit(0);
        } else if(strcmp(argv[i], "-v") == 0) {
            printConfig = 1;
            i++;
        } else if(strcmp(argv[i], "-x") == 0) {
            expire = 1;
            i++;
        } else if(strcmp(argv[i], "-c") == 0) {
            i++;
            if(i >= argc) {
                usage(argv[0]);
                exit(1);
            }
            if(configFile)
                releaseAtom(configFile);
            configFile = internAtom(argv[i]);
            i++;
        } else {
            usage(argv[0]);
            exit(1);
        }
    }

    if(configFile)
        configFile = expandTilde(configFile);

    if(configFile == NULL) {
        configFile = expandTilde(internAtom("~/.polipo"));
        if(configFile)
            if(access(configFile->string, F_OK) < 0) {
                releaseAtom(configFile);
                configFile = NULL;
            }
    }

    if(configFile == NULL) {
        if(access("/etc/polipo/config", F_OK) >= 0)
            configFile = internAtom("/etc/polipo/config");
        if(configFile && access(configFile->string, F_OK) < 0) {
            releaseAtom(configFile);
            configFile = NULL;
        }
    }

    rc = parseConfigFile(configFile);
    if(rc < 0)
        exit(1);

    while(i < argc) {
        rc = parseConfigLine(argv[i], "command line", 0, 0);
        if(rc < 0)
            exit(1);
        i++;
    }

    initChunks();
    initLog();
    initObject();
    if(!expire && !printConfig)
        initEvents();
    initIo();
    initDns();
    initHttp();
    initServer();
    initDiskcache();
    initForbidden();
    initSocks();
	initOffline();

    if(printConfig) {
        printConfigVariables(stdout, 0);
        exit(0);
    }

    if(expire) {
        expireDiskObjects();
        exit(0);
    }

    if(daemonise)
        do_daemonise(logFile == NULL || logFile->length == 0);

    if(pidFile)
        writePid(pidFile->string);

    listener = create_listener(proxyAddress->string, 
                               proxyPort, httpAccept, NULL);
    if(!listener) {
        if(pidFile) unlink(pidFile->string);
        exit(1);
    }

    eventLoop();

    if(pidFile) unlink(pidFile->string);
    return 0;
}
Exemplo n.º 3
0
int psiphonMain(
        int bindAll,
        int proxyPortParam,
        int localParentProxyPortParam)
{
    FdEventHandlerPtr listener;

    initAtoms();
    CONFIG_VARIABLE(daemonise, CONFIG_BOOLEAN, "Run as a daemon");
    CONFIG_VARIABLE(pidFile, CONFIG_ATOM, "File with pid of running daemon.");

    preinitChunks();
    preinitLog();
    preinitObject();
    preinitIo();
    preinitDns();
    preinitServer();
    preinitHttp();
    preinitDiskcache();
    preinitLocal();
    preinitForbidden();
    preinitSocks();

    const int MAX_SIZE = 80;
    char proxyAddressParamLine[MAX_SIZE];
    char proxyPortParamLine[MAX_SIZE];
    char localParentProxyPortParamLine[MAX_SIZE];
    snprintf(
        proxyAddressParamLine,
        MAX_SIZE,
        (0 == bindAll) ? "proxyAddress=127.0.0.1" : "proxyAddress=0.0.0.0");
    snprintf(
        proxyPortParamLine,
        MAX_SIZE,
        "proxyPort=%d",
        proxyPortParam);
    snprintf(
        localParentProxyPortParamLine,
        MAX_SIZE,
        "socksParentProxy=127.0.0.1:%d",
        localParentProxyPortParam);

    if (0 > parseConfigLine(proxyAddressParamLine, "psiphon", 0, 0)
        || 0 > parseConfigLine(proxyPortParamLine, "psiphon", 0, 0)
        || 0 > parseConfigLine(localParentProxyPortParamLine, "psiphon", 0, 0)
        || 0 > parseConfigLine("disableLocalInterface=true", "psiphon", 0, 0)
        || 0 > parseConfigLine("logLevel=1", "psiphon", 0, 0)
        /* Allow HTTPS on all ports */
        || 0 > parseConfigLine("tunnelAllowedPorts=1-65535", "psiphon", 0, 0))
    {
        return -1;
    }

    initChunks();
    initLog();
    initObject();
    initEvents();
    initIo();
    initDns();
    initHttp();
    initServer();
    initDiskcache();
    initForbidden();
    initSocks();

    listener = create_listener(
                    proxyAddress->string,
                    proxyPort,
                    httpAccept,
                    NULL);
    if (!listener)
    {
        return -1;
    }

    eventLoop();

    return 0;
}