Skip to content

Commit

Permalink
add dokodemo
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengyao-lin committed Dec 13, 2018
1 parent 860ede5 commit 022daa3
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 5 deletions.
9 changes: 9 additions & 0 deletions app/config/dokodemo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[inbound]
proto = "dokodemo"
local = "127.0.0.1"
port = "3134"

[outbound]
proto = "dokodemo"
proxy = "127.0.0.1"
port = "3135"
34 changes: 34 additions & 0 deletions app/vmecs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include "proto/socks/outbound.h"
#include "proto/socks/tcp.h"

#include "proto/dokodemo/inbound.h"
#include "proto/dokodemo/outbound.h"

#include "proto/native/outbound.h"

#define HELP \
Expand Down Expand Up @@ -276,6 +279,36 @@ tcp_outbound_t *socks5_outbound_builder(toml_object_t *config)
return (tcp_outbound_t *)outbound;
}

tcp_inbound_t *dokodemo_inbound_builder(toml_object_t *config)
{
target_id_t *local;
dokodemo_tcp_inbound_t *inbound;

local = load_inbound_param(config);
if (!local) return NULL;

inbound = dokodemo_tcp_inbound_new(local);

target_id_free(local);

return (tcp_inbound_t *)inbound;
}

tcp_outbound_t *dokodemo_outbound_builder(toml_object_t *config)
{
target_id_t *proxy;
dokodemo_tcp_outbound_t *outbound;

proxy = load_outbound_param(config);
if (!proxy) return NULL;

outbound = dokodemo_tcp_outbound_new(proxy);

target_id_free(proxy);

return (tcp_outbound_t *)outbound;
}

tcp_outbound_t *native_outbound_builder(toml_object_t *config)
{
return (tcp_outbound_t *)native_tcp_outbound_new();
Expand All @@ -290,6 +323,7 @@ struct {
{ "socks", socks_inbound_builder, NULL },
{ "socks4", socks_inbound_builder, socks4_outbound_builder },
{ "socks5", socks_inbound_builder, socks5_outbound_builder },
{ "dokodemo", dokodemo_inbound_builder, dokodemo_outbound_builder },
{ "native", NULL, native_outbound_builder }
};

Expand Down
2 changes: 1 addition & 1 deletion proto/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# proto

add_lib_batch(vmecs-proto STATIC "*.c" "vmess/*.c" "native/*.c" "relay/*.c" "socks/*.c")
add_lib_batch(vmecs-proto STATIC "*.c" "vmess/*.c" "native/*.c" "relay/*.c" "socks/*.c" "dokodemo/*.c")

target_link_libraries(vmecs-proto vmecs-crypto vmecs-pub)
42 changes: 42 additions & 0 deletions proto/dokodemo/inbound.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "pub/socket.h"

#include "proto/common.h"
#include "proto/native/tcp.h"

#include "inbound.h"

static tcp_socket_t *
_dokodemo_tcp_inbound_server(tcp_inbound_t *_inbound)
{
dokodemo_tcp_inbound_t *inbound = (dokodemo_tcp_inbound_t *)_inbound;
tcp_socket_t *sock = (tcp_socket_t *)native_tcp_socket_new();

while (tcp_socket_bind_target(sock, inbound->local))
sleep(1);

return sock;
}

static void
_dokodemo_tcp_inbound_free(tcp_inbound_t *_inbound)
{
dokodemo_tcp_inbound_t *inbound = (dokodemo_tcp_inbound_t *)_inbound;

if (inbound) {
target_id_free(inbound->local);
free(inbound);
}
}

dokodemo_tcp_inbound_t *
dokodemo_tcp_inbound_new(target_id_t *local)
{
dokodemo_tcp_inbound_t *ret = malloc(sizeof(*ret));
ASSERT(ret, "out of mem");

ret->server_func = _dokodemo_tcp_inbound_server;
ret->free_func = _dokodemo_tcp_inbound_free;
ret->local = target_id_copy(local);

return ret;
}
16 changes: 16 additions & 0 deletions proto/dokodemo/inbound.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef _PROTO_DOKODEMO_INBOUND_H_
#define _PROTO_DOKODEMO_INBOUND_H_

#include "pub/type.h"

#include "proto/relay/inbound.h"

typedef struct {
TCP_INBOUND_HEADER
target_id_t *local; // local bind address
} dokodemo_tcp_inbound_t;

dokodemo_tcp_inbound_t *
dokodemo_tcp_inbound_new(target_id_t *local);

#endif
43 changes: 43 additions & 0 deletions proto/dokodemo/outbound.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "proto/native/tcp.h"

#include "outbound.h"

static tcp_socket_t *
_dokodemo_tcp_outbound_client(tcp_outbound_t *_outbound, const target_id_t *_)
{
dokodemo_tcp_outbound_t *outbound = (dokodemo_tcp_outbound_t *)_outbound;
tcp_socket_t *sock = (tcp_socket_t *)native_tcp_socket_new();

if (tcp_socket_connect_target(sock, outbound->target)) {
tcp_socket_close(sock);
tcp_socket_free(sock);
return NULL;
}

return sock;
}

static void
_dokodemo_tcp_outbound_free(tcp_outbound_t *_outbound)
{
dokodemo_tcp_outbound_t *outbound = (dokodemo_tcp_outbound_t *)_outbound;

if (outbound) {
target_id_free(outbound->target);
free(outbound);
}
}

dokodemo_tcp_outbound_t *
dokodemo_tcp_outbound_new(target_id_t *target)
{
dokodemo_tcp_outbound_t *ret = malloc(sizeof(*ret));
ASSERT(ret, "out of mem");

ret->client_func = _dokodemo_tcp_outbound_client;
ret->free_func = _dokodemo_tcp_outbound_free;

ret->target = target_id_copy(target);

return ret;
}
16 changes: 16 additions & 0 deletions proto/dokodemo/outbound.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef _PROTO_DOKODEMO_OUTBOUND_H_
#define _PROTO_DOKODEMO_OUTBOUND_H_

#include "pub/type.h"

#include "proto/relay/outbound.h"

typedef struct {
TCP_OUTBOUND_HEADER
target_id_t *target;
} dokodemo_tcp_outbound_t;

dokodemo_tcp_outbound_t *
dokodemo_tcp_outbound_new(target_id_t *target);

#endif
8 changes: 7 additions & 1 deletion proto/native/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ _native_tcp_socket_free(tcp_socket_t *_sock)
}
}

static target_id_t *
_native_tcp_socket_target(tcp_socket_t *_sock)
{
return NULL;
}

native_tcp_socket_t *
native_tcp_socket_new_fd(fd_t fd)
{
Expand All @@ -116,7 +122,7 @@ native_tcp_socket_new_fd(fd_t fd)
ret->revent_func = _native_tcp_socket_revent;
ret->close_func = _native_tcp_socket_close;
ret->free_func = _native_tcp_socket_free;
ret->target_func = NULL;
ret->target_func = _native_tcp_socket_target;

ret->sock = fd;

Expand Down
9 changes: 7 additions & 2 deletions proto/relay/etcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,14 @@ etcp_handle(epoll_t epfd, tcp_outbound_t *outbound, etcp_relay_conn_t *conn, siz
return;
}

target = tcp_socket_target(new_in);
target = tcp_socket_target(new_in); // NOTE target could be NULL
fprintf(stderr, "thread %ld: ", id);
print_target("request", target);

if (target) {
print_target("request", target);
} else {
fprintf(stderr, "request: NULL\n");
}

if (!(new_out = tcp_outbound_client(outbound, target))) {
// failed to connect
Expand Down
7 changes: 6 additions & 1 deletion proto/relay/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ _tcp_relay_handler(void *arg)
}

target = tcp_socket_target(job->in_sock);
print_target("request", target);

if (target) {
print_target("request", target);
} else {
fprintf(stderr, "request: NULL\n");
}

while (!(job->out_sock = tcp_outbound_client(job->outbound, target))) {
perror("connect");
Expand Down
2 changes: 2 additions & 0 deletions pub/fd.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <unistd.h>
#include <fcntl.h>

#include "type.h"

typedef int fd_t;

// read/write with retry on EINTR or EAGAIN
Expand Down

0 comments on commit 022daa3

Please sign in to comment.