示例#1
0
void accept_cb(EV_P_ ev_io *w, int revents)
{
    listen_ctx_t *listener = (listen_ctx_t *)w;
    int serverfd           = accept(listener->fd, NULL, NULL);
    if (serverfd == -1) {
        ERROR("accept");
        return;
    }
    setnonblocking(serverfd);
    int opt = 1;
    setsockopt(serverfd, SOL_TCP, TCP_NODELAY, &opt, sizeof(opt));
#ifdef SO_NOSIGPIPE
    setsockopt(serverfd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
#endif

    server_t *server = new_server(serverfd, listener->method);
    server->listener = listener;
    // SSR beg
    server->obfs_plugin = new_obfs_class(listener->obfs_name);
    if (server->obfs_plugin) {
        server->obfs = server->obfs_plugin->new_obfs();
    }
    server->protocol_plugin = new_obfs_class(listener->protocol_name);
    if (server->protocol_plugin) {
        server->protocol = server->protocol_plugin->new_obfs();
    }
    // SSR end

    ev_io_start(EV_A_ & server->recv_ctx->io);
}
示例#2
0
int
init_udprelay(const char *server_host, const char *server_port,
#ifdef MODULE_LOCAL
              const struct sockaddr *remote_addr, const int remote_addr_len,
              const ss_addr_t tunnel_addr,
#endif
              int mtu, int timeout, const char *iface,
              cipher_env_t* cipher_env, const char *protocol, const char *protocol_param)
{
    // Initialize ev loop
    struct ev_loop *loop = EV_DEFAULT;

    // Initialize MTU
    if (mtu > 0) {
        packet_size = mtu - 1 - 28 - 2 - 64;
        buf_size    = packet_size * 2;
    }

    // Initialize cache
    struct cache *conn_cache;
    cache_create(&conn_cache, MAX_UDP_CONN_NUM, free_cb);

    // ////////////////////////////////////////////////
    // Setup server context

    // Bind to port
    int serverfd = create_server_socket(server_host, server_port);
    if (serverfd < 0) {
        FATAL("[udp] bind() error");
    }
    setnonblocking(serverfd);

    server_ctx_t *server_ctx = new_server_ctx(serverfd);

    server_ctx->cipher_env = cipher_env;
#ifdef MODULE_REMOTE
    server_ctx->loop = loop;
#endif
    server_ctx->timeout    = max(timeout, MIN_UDP_TIMEOUT);
    server_ctx->iface      = iface;
    server_ctx->conn_cache = conn_cache;
#ifdef MODULE_LOCAL
    server_ctx->remote_addr     = remote_addr;
    server_ctx->remote_addr_len = remote_addr_len;
    //SSR beg
    server_ctx->protocol_plugin = new_obfs_class((char *)protocol);
    if (server_ctx->protocol_plugin) {
        server_ctx->protocol = server_ctx->protocol_plugin->new_obfs();
        server_ctx->protocol_global = server_ctx->protocol_plugin->init_data();
    }

    server_info _server_info;
    memset(&_server_info, 0, sizeof(server_info));
    strcpy(_server_info.host, server_host);
    _server_info.port = atoi(server_port);
    _server_info.g_data = server_ctx->protocol_global;
    _server_info.param = (char *)protocol_param;
    _server_info.key = enc_get_key(cipher_env);
    _server_info.key_len = enc_get_key_len(cipher_env);

    if (server_ctx->protocol_plugin)
        server_ctx->protocol_plugin->set_server_info(server_ctx->protocol, &_server_info);
    //SSR end
    server_ctx->tunnel_addr = tunnel_addr;
#endif

    ev_io_start(loop, &server_ctx->io);

    server_ctx_list[server_num++] = server_ctx;

    return 0;
}