summaryrefslogtreecommitdiff
path: root/main2.c
diff options
context:
space:
mode:
authorfadhil riyanto <me@fadev.org>2024-10-05 21:01:28 +0700
committerfadhil riyanto <me@fadev.org>2024-10-05 21:01:28 +0700
commit97a8cba0a7ea0d2c79dfd5049695463c804a7aa2 (patch)
tree8b7f9330f27e3dade2a78f2a337011bf05a43dd4 /main2.c
parentd1c41c5bcad2ddd3124ff20d92e50795ab90e19d (diff)
add data exchange
Signed-off-by: fadhil riyanto <me@fadev.org>
Diffstat (limited to 'main2.c')
-rw-r--r--main2.c53
1 files changed, 41 insertions, 12 deletions
diff --git a/main2.c b/main2.c
index a192ab1..c58038b 100644
--- a/main2.c
+++ b/main2.c
@@ -470,17 +470,23 @@ static int socks5_handshake(int fd, char* buf, struct socks5_session *socks5_ses
socks5_session->is_auth = 1;
}
-static int socks5_send_connstate(int fd, char* buf, struct socks5_session *socks5_session)
+static int socks5_send_connstate(int fd, u_int8_t state, u_int8_t atyp, u_int8_t *addr, uint16_t port)
{
- struct socks5_client_hello *c_hello = (struct socks5_client_hello*)buf;
- printf("SOCKS_HANDSHAKE version: %d; nmethods: %d; methods: %d\n", c_hello->ver, c_hello->nmethods, c_hello->methods);
-
- struct socks5_server_hello s_hello;
- s_hello.ver = 5;
- s_hello.method = 0;
- send(fd, (void*)&s_hello, sizeof(struct socks5_server_hello), 0);
+ int ret = 0;
+ struct socks5_server_reply s_state;
+ s_state.ver = 5;
+ s_state.rep = state;
+ s_state.reserved = 0;
+ s_state.atyp = atyp;
+ s_state.bind_addr = addr;
+ s_state.bind_port = port;
+
+ ret = send(fd, (void*)&s_state, sizeof(struct socks5_server_reply), 0);
+ if (ret == -1) {
+ return -1;
+ }
+ return 0;
- socks5_session->is_auth = 1;
}
// static int debug_tcp(int fd)
@@ -554,6 +560,26 @@ static int create_server2server_conn(int *fdptr, int atyp, u_int8_t *addr, u_int
return 0;
}
+static void start_exchange_data(int client_fd, int target_fd)
+{
+ int ret;
+ u_int8_t buf[4096];
+
+ do {
+ memset(buf, 0, 4096);
+ ret = recv(client_fd, buf, 4096, 0);
+ if (ret == 0) { /* prevent close connection */
+ return;
+ }
+ printf("recv from client: %d bytes\n", ret);
+ send(target_fd, buf, ret, 0);
+ ret = recv(target_fd, buf, 4096, 0);
+ printf("recv from server: %d bytes\n", ret);
+ printf("data: %s\n", buf);
+ send(client_fd, buf, ret, 0);
+ } while (ret != 0);
+}
+
static int start_unpack_packet_no_epl(int fd, void* reserved, struct socks5_session *socks5_session)
{
char buf[4096];
@@ -568,9 +594,6 @@ static int start_unpack_packet_no_epl(int fd, void* reserved, struct socks5_sess
socks5_handshake(fd, buf, socks5_session);
} else {
-
-
-
if (buf[3] == 1) {
struct next_req_ipv4 *next_req = (struct next_req_ipv4*)buf;
int cur_conn_clientfd = 0;
@@ -582,7 +605,13 @@ static int start_unpack_packet_no_epl(int fd, void* reserved, struct socks5_sess
ret = create_server2server_conn(&cur_conn_clientfd, next_req->atyp, next_req->dest, next_req->port);
if (ret == 0) {
+ socks5_send_connstate(fd, 0, next_req->atyp, next_req->dest,
+ next_req->port);
+
+ start_exchange_data(fd, cur_conn_clientfd);
+ close(cur_conn_clientfd);
+
}
}