Code Monkey home page Code Monkey logo

hev-socks5-core's Introduction

HevSocks5Core

HevSocks5Core is a simple, lightweight socks5 library.

Features

  • IPv4/IPv6. (dual stack)
  • Standard CONNECT command.
  • Standard UDP ASSOCIATE command.
  • Extended FWD UDP command. (UDP in TCP)
  • Multiple username/password authentication.

Dependencies

Examples

Server

#include <unistd.h>

#include <hev-task.h>
#include <hev-task-io.h>
#include <hev-task-io-socket.h>
#include <hev-task-dns.h>
#include <hev-task-system.h>

#include <hev-socks5-server.h>

static void
server_entry (void *data)
{
    HevSocks5Server *server = data;
    hev_socks5_server_run (server);
    hev_object_unref (HEV_OBJECT (server));
}

static void
listener_entry (void *data)
{
    struct addrinfo hints = { 0 };
    struct addrinfo *result;
    int fd;

    hints.ai_family = AF_INET6;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;

    hev_task_dns_getaddrinfo (NULL, "1080", &hints, &result);
    fd = hev_task_io_socket_socket (AF_INET6, SOCK_STREAM, 0);
    bind (fd, result->ai_addr, result->ai_addrlen);
    freeaddrinfo (result);
    listen (fd, 5);

    hev_task_add_fd (hev_task_self (), fd, POLLIN);

    for (;;) {
        HevSocks5Server *server;
        HevTask *task;
        int nfd;

        nfd = hev_task_io_socket_accept (fd, NULL, NULL, NULL, NULL);

        task = hev_task_new (-1);
        server = hev_socks5_server_new (nfd);
        hev_task_run (task, server_entry, server);
    }

    close (fd);
}

int
main (int argc, char *argv[])
{
    HevTask *task;

    hev_task_system_init ();

    task = hev_task_new (-1);
    hev_task_run (task, listener_entry, NULL);

    hev_task_system_run ();

    hev_task_system_fini ();

    return 0;
}

Client

#include <stddef.h>

#include <hev-task.h>
#include <hev-task-system.h>
#include <hev-socks5-client-tcp.h>
#include <hev-socks5-client-udp.h>

static void
tcp_client_entry (void *data)
{
    HevSocks5ClientTCP *tcp;

    tcp = hev_socks5_client_tcp_new ("www.google.com", 443);
    hev_socks5_client_connect (HEV_SOCKS5_CLIENT (tcp), "127.0.0.1", 1080);
    hev_socks5_client_handshake (HEV_SOCKS5_CLIENT (tcp));

    /*
     * splice data to/from a socket fd:
     *     hev_socks5_tcp_splice (HEV_SOCKS5_TCP (tcp), fd);
     */

    hev_object_unref (HEV_OBJECT (tcp));
}

static void
udp_client_entry (void *data)
{
    HevSocks5ClientUDP *udp;

    udp = hev_socks5_client_udp_new (HEV_SOCKS5_TYPE_UDP_IN_TCP);
    hev_socks5_client_connect (HEV_SOCKS5_CLIENT (udp), "127.0.0.1", 1080);
    hev_socks5_client_handshake (HEV_SOCKS5_CLIENT (udp));

    /*
     * send udp packet:
     *     hev_socks5_udp_sendto (HEV_SOCKS5_UDP (udp), data, len, addr);
     *
     * recv udp packet: (with source address family AF_INET6)
     *     addr.sa_family = AF_INET6;
     *     hev_socks5_udp_recvfrom (HEV_SOCKS5_UDP (udp), data, len, addr);
     *
     * recv udp packet: (with source address family AF_INET for IPv4 only)
     *     addr.sa_family = AF_INET;
     *     hev_socks5_udp_recvfrom (HEV_SOCKS5_UDP (udp), data, len, addr);
     */

    hev_object_unref (HEV_OBJECT (udp));
}

int
main (int argc, char *argv[])
{
    HevTask *task;

    hev_task_system_init ();

    task = hev_task_new (-1);
    hev_task_run (task, tcp_client_entry, NULL);

    task = hev_task_new (-1);
    hev_task_run (task, udp_client_entry, NULL);

    hev_task_system_run ();

    hev_task_system_fini ();

    return 0;
}

Users

Contributors

License

MIT

hev-socks5-core's People

Contributors

heiher avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.