示例#1
0
int tcp_connection_connect_with_proxy(FetionConnection* connection 
		, const char* ipaddress , const int port , Proxy *proxy)
{
	struct sockaddr_in addr;
	
	char *ip = get_ip_by_name(proxy->proxyHost);
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = inet_addr(ip);
	free(ip);
	addr.sin_port = htons(proxy->proxyPort);
	strcpy(connection->remote_ipaddress , ipaddress);
	connection->remote_port = port;

	unsigned int n = MAX_RECV_BUF_SIZE;
	int ret = setsockopt(connection->socketfd , SOL_SOCKET , SO_RCVBUF , (const char*)&n , sizeof(n));
	if(ret == -1) return -1;
	ret = connect(connection->socketfd , (struct sockaddr*)&addr , sizeof(struct sockaddr));
	if(ret == -1) return -1;
	printf("%s:%d\n", ipaddress, port);
	
	char http[1024] , code[5] , *pos = NULL;
	unsigned char authentication[1024];
	char authen[1024];
	char authorization[1024];
	
	memset(authorization, 0, sizeof(authorization));
	if(strlen(proxy->proxyUser) != 0 && strlen(proxy->proxyPass) != 0)
	{
		memset(authen, 0, sizeof(authen));
		sprintf(authen , "%s:%s" , proxy->proxyUser , proxy->proxyPass);
		EVP_EncodeBlock(authentication , (unsigned char*)authen , strlen(authen));
		sprintf(authorization , "Proxy-Authorization: Basic %s\r\n" , (char*)authentication);
	}

	memset(http, 0, sizeof(http));
	snprintf(http , sizeof(http)-1 , "CONNECT %s:%d HTTP/1.1\r\n"
				   "Host: %s:%d\r\n%s"
				   "User-Agent: OpenFetion\r\n\r\n"
				 , ipaddress , port , ipaddress , port , authorization);

	tcp_connection_send(connection , http , strlen(http));

	memset(http, 0, sizeof(http));

	tcp_connection_recv(connection , http , sizeof(http));

	pos = strstr(http , " ");
	if(pos == NULL){
		return -1;
	}
	pos++;
	n = strlen(pos) - strlen(strstr(pos , " "));
	memset(code, 0, sizeof(code));
	strncpy(code, pos, (sizeof(code)-1 < n) ? (sizeof(code)-1) : n);
	code[sizeof(code)-1]='\0';

	if(strcmp(code , "200") != 0)
		return -1;

	return 1;
}
示例#2
0
文件: main.cpp 项目: Zhanyin/taomee
int main(int argc, char* argv[])
{
    arg_start = argv[0];
    arg_end = argv[argc-1] + strlen(argv[argc - 1]) + 1;
    env_start = environ[0];

    if (argc != 2)
        exit(-1);

    daemon_start();

    INFO_LOG ("async_server %s, report bugs to <*****@*****.**>", VERSION);

    load_config_file(argv[1]);

    check_single();

    if (config_get_strval("proc_name", NULL))
    {
        set_title("%s-MAIN", config_get_strval("proc_name", NULL));
    }
    else
    {
        set_title("%s-MAIN", arg_start);
    }

    load_work_file(config_get_strval("work_conf", ""));

    if (-1 == log_init(config_get_strval("log_dir", "./"), (log_lvl_t)config_get_intval("log_level", 8),
                       config_get_intval("log_size", 33554432),
                       config_get_intval("log_maxfiles", 100),
                       "main_")) {
        BOOT_LOG(-1, "log init");
    }

    init_warning_system();

    plugin_load(config_get_strval("plugin_file", ""));


    shmq_init(g_work_confs.size(), config_get_intval("shmq_size", 8388608));

    int max_connect = config_get_intval("max_connect", 10000);
    if (max_connect <= 4096)
        max_connect = 4096;

    g_max_connect = max_connect;

    int max_pkg_len = config_get_intval("max_pkg_len", 16384);
    if (max_pkg_len <= 4096)
        max_pkg_len = 4096;

    g_max_pkg_len = max_pkg_len;


    const char * bind_ip = config_get_strval("bind_ip", NULL);
    if (NULL == bind_ip)
    {
        BOOT_LOG(-1, "unspecified bind_ip");
        return -1;
    }

    if (0 != get_ip_by_name(bind_ip, g_bind_ip))
    {
        strncpy(g_bind_ip, bind_ip, 16);
    }


    if (g_plugin.plugin_init) 
    {
        if (g_plugin.plugin_init(PROC_MAIN) != 0)
            BOOT_LOG(-1, "plugin_init init failed PROC_MAIN");
    }


    for (uint32_t i = 0; i < g_work_confs.size(); ++i)
        g_work_confs[i].pid = fork_work(i);

    pid_t conn_pid = fork_conn();

    while (!g_stop) {
        int status = 0;
        pid_t p = waitpid(-1, &status, 0);
        if(-1 == p)
            continue;

        if (WEXITSTATUS(status) == 10) {
            if (p == conn_pid) {
                conn_pid = -1;
            } else {
                for (uint32_t i = 0; i < g_work_confs.size(); ++i) {
                    if (g_work_confs[i].pid == p) {
                        g_work_confs[i].pid = -1;
                        break;
                    }
                }
            }
        } else {
            if (p == conn_pid) {
                conn_pid = fork_conn();
                send_warning_msg("conn core", 0, 0, 0, config_get_strval("bind_ip", "0.0.0.0"));
                ERROR_LOG("conn core");
            } else {
                for (uint32_t i = 0; i < g_work_confs.size(); ++i) {
                    if (g_work_confs[i].pid == p) {
                        ERROR_LOG("work core, id: %u, pid: %d", g_work_confs[i].id, p);
                        g_work_confs[i].pid = fork_work(i);
                        send_warning_msg("work core", g_work_confs[i].id, 0, 0, config_get_strval("bind_ip", "0.0.0.0"));
                        break;
                    }
                }
            }
        }
    }

    for (uint32_t i = 0; i < g_work_confs.size(); ++i) {
        if (g_work_confs[i].pid != -1)
            kill(g_work_confs[i].pid, SIGTERM);
    }

    if (conn_pid != -1)
        kill(conn_pid, SIGTERM);

    while (true) {
        int ret = 0;
        for (uint32_t i = 0; i < g_work_confs.size(); ++i) {
            if (g_work_confs[i].pid != -1)
                ret = 1;
        }

        if (ret || conn_pid != -1) {
            int status = 0;
            pid_t p = waitpid(-1, &status, 0);
            if(-1 == p)
                continue;

            if (p == conn_pid) {
                conn_pid = -1;
            } else {
                for (uint32_t i = 0; i < g_work_confs.size(); ++i) {
                    if (g_work_confs[i].pid == p) {
                        g_work_confs[i].pid = -1;
                        break;
                    }
                }
            }
        } else {
            break;
        }
    }

    if (g_plugin.plugin_fini)
        g_plugin.plugin_fini(PROC_MAIN);
}