SOCK5 agent implementation under UDP

xiaoxiao2021-03-06  38

Author: zfive5 (zhaozidong) email: zfive5@yahoo.com.cn

A few more time ago, the previous colleague called me to help write a SOCK5 agent under UPD, because some reasons did not help him, I said to him again! About the SOCK5 agent package and data format can refer to the following URL Content: http://www.vckbase.net/document/viewdoc/?id=853 http://www.vckbase.net/document/viewdoc/?id=852 http://www.china-pub.com /computers/emook/emooknew/rfctxt/rfc1928.txt

The teeth are still can't be unfortunate! There is no one in the hospital's stomatology, there is no one! ! ! Big morning morning, I will come back to 8 rounds!

Package data under UPD SOCK5 Agent:

Client: Ver (1 Byte) Num Method (1 Byte) Methods (1 -255 Byte) (05 02 00 02)

Server: Ver (1 Byte) Num Method (1 Byte) (05 00) OR (05 02)

* Client (when returned 02): Ver (1 byte) name len (1 byte) Name (1-255 Byte) pass Len (1 byte) Password (1 -255 byte) (01 01 61 01 61 )

* Server (when returning 02): Ver (1 byte) status (1 byte) (05 00) OR (05 xx)

Client: Ver (1 Byte) CMD (1 Byte) RSV (1 Byte) AddRss Type (1 Byte) AddRSS (?? Byte) Port (2 Byte)

(05 03 00 01 7F 00 00 01 0F A0)

Server: Ver (1 Byte) Reponse (1 Byte) RSV (1 Byte) Address Type (1 Byte) Address (?? Byte) Port (2 Byte) (05 00 00 01 7F 00 00 01 0F A0 )

Client: RSV (2 Byte) Fragment (1 Byte) AddRESS TYPE (1 BYTE) AddR (?? Byte) Port (2 byte) Data (??) (00 00 00 01 7F 00 00 01 0F A0 .....) Server: rsv (2 byte) fragment (1 byte) addR (1 byte) addr (?? Byte) port (2 byte) data (??) (00 00 01 7F 00 00 01 0f A0 .....) .....

test server Code: SOCKET s = socket (AF_INET, SOCK_DGRAM, 0); if (s == INVALID_SOCKET) {closesocket (s); return 0;} struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_addr.s_addr = ADDR_ANY addr.sin_port = htons (4000); if (Bind (S, Struct SockAddr *) & addr, sizeof (struct sockaddr)) == SOCKET_ERROR) {CloseSocket (s); return 0;} while (1 == 1) {Char SZ_BUF [1024]; INT N_LEN = SIZEOF (Struct SockAddr); IF (Recvfrom (S, S, SZ_BUF, 1024, 0, (Struct SockAddr *) & in, & n_len) == Socket_ERROR) {CloseSocket (s) Return 0;}

IF (Sendto (S, "Hello World", Strlen ("Hello World", 0, (Struct SockAddr *) & IN, SIZEOF (STRUCKADDR_IN) == SOCKET_ERROR) {CloseSocket (s); returnograph 0;}}

CloseSocket (s); return 0;

Test Client:

Socket S = Socket (AF_INET, SOCK_DGRAM, 0); if (s == invalid_socket) {CloseSocket (s); Return;}

struct sockaddr_in addr1; addr1.sin_family = AF_INET; addr1.sin_addr.s_addr = ADDR_ANY; addr1.sin_port = htons (4001); if (bind (s, (struct sockaddr *) & addr1, sizeof (struct sockaddr_in)) == SOCKET_ERROR) {CloseSocket (S); Return;}

ZF5_SOCK5_UDP UDP; STRCPY (UDP.M_SZ_NAME, "A"); STRCPY (UDP.M_SZ_PASS, "A"); if (udp.connect_sock5_udp ("127.0.0.1", 1080, & addr1) <0) {CloseSocket (s); Return;}

struct sockaddr_in addr2; addr2.sin_family = AF_INET; addr2.sin_addr.s_addr = inet_addr ( "127.0.0.1"); addr2.sin_port = htons (4000); if (udp.send_sock5_udp (s, "hello", strlen ( "hello "), & addr2) <0) {CloseSocket (s); return;}

CHAR SZ_BUF [1024] = ""; MEMSET (SZ_BUF, 0, 1024); struct sockaddr_in addr3; if (udp.recv_sock5_udp (s, sz_buf, & addr3) <0) {clossoSocket (s); return;} AFXMESSAGEBOX (SZ_BUF) Return;

ZF5_SOCK5_UDP.H file:

#pragma overce #pragma Comment (Lib, "WS2_32.LIB")

#include #define zf5_buffer_size1 255 #define zf5_buffer_size2 1024 #define zf5_buffer_size3 8192 Class Zf5_sock5_udp {public:

// Constructor ZF5_SOCK5_UDP (VOID);

// Destructor Virtual ~ ZF5_SOCK5_UDP (VOID);

// Create a UPD agent INT connect_sock5_udp (CHAR * SZ_ADDR_PROXY, INT N_PORT_PROXY, STRUCT SOCKADDR_IN * P_ADDR);

// Send UDP data intlected by SOCK5 INT sent_sock5_udp (int s_udp, char * sz_buf, int n_len, struct sockaddr_in * p_addr);

Udp data received by sock5 // int recv_sock5_udp (int s_udp, char * sz_buf, struct sockaddr_in * p_addr); // Close the sock5 proxy udp int close_sock5_udp (); // proxy username char m_sz_name [ZF5_BUFFER_SIZE1 2]; / / Proxy server user password char m_sz_pass [zf5_buffer_size1 2];

PRIVATE: / / Remote UDP server address char m_sz_udp_srv [zf5_buffer_size2 2]; // Remote UDP server port int m_n_port; // address type int m_n_addrtype; // sock5 agent TCP connection int m_n_sock5;};

ZF5_SOCK5_UDP.CPP file:

#include "stdafx.h" #include "./zf5_sock5_udp.h"

// constructor zf5_sock5_udp :: zf5_sock5_udp (void) {WSADATA wsa; WSAStartup (MAKEWORD (1,1), & wsa); m_n_addrtype = -1; m_n_port = 0; m_n_sock5 = INVALID_SOCKET; m_sz_name [0] = 0; m_sz_pass [0 ] = 0; M_SZ_UDP_SRV [0] = 0;

}

// Destructor ZF5_SOCK5_UDP :: ~ ZF5_SOCK5_UDP (VOID) {Close_Sock5_UDP (); wsacleanup ();}

// Create a SOCK5 UPD agent int ZF5_SOCK5_UDP :: Connect_Sock5_UDP (CHAR * SZ_ADDR_PROXY, INT N_PORT_PROXY, STRUCKADDR_IN * P_ADDR) {socket s_sock5_tcp = invalid_socket;

s_sock5_tcp = socket (AF_INET, SOCK_STREAM, 0); if (s_sock5_tcp == INVALID_SOCKET) {return -1;} timeval tv; tv.tv_sec = 10; tv.tv_usec = 0; if (setsockopt (s_sock5_tcp, SOL_SOCKET, SO_RCVTIMEO, ( char *) & tv, sizeof (timeval)) == SOCKET_ERROR) {closesocket (s_sock5_tcp); return -1;} struct sockaddr_in sa_1; sa_1.sin_family = AF_INET; sa_1.sin_addr.s_addr = inet_addr (sz_addr_proxy); sa_1.sin_port = htons (n_port_proxy); if (sa_1.sin_addr.s_addr == INADDR_NONE) {struct hostent * p = gethostbyname (sz_addr_proxy); if (p == NULL) {closesocket (s_sock5_tcp); return -1;} sa_1.sin_addr.s_addr = ((Lpin_addr) p-> h_addr) -> s_addr;}

IF (Connect (S_Sock5_TCP, (Struct SockAddr *) & SA_1, SIZEOF (STRUCT SOCKADDR_IN)) == Socket_ERROR) {CloseSocket (s_sock5_tcp); Return -1;}

CHAR SZ_BUF [ZF5_Buffer_Size2 2];

MEMSET (SZ_BUF, 0, ZF5_BUFFER_SIZE2); SZ_BUF [0] = 0x05; S_BUF [1] = 0x02; SZ_BUF [2] = 0x00; SZ_BUF [3] = 0x02; IF (S_Sock5_tcp, SZ_BUF, 4, 0) = = Socket_ERROR) {CloseSocket (S_SOCK5_TCP); Return-1;}

MEMSET (SZ_BUF, 0, ZF5_BUFFER_SIZE2); if (RECV (S_Sock5_TCP, SZ_BUF, ZF5_Buffer_Size2, 0) == SOCKET_ERROR) {CloseSocket (s_sock5_tcp); Return -1;}

IF (SZ_BUF [0] == 0x05 && Sz_buf [INT N_TEMP1 = 0; INT N_TEMP2 = 0; MEMSET (SZ_BUF, 0, ZF5_Buffer_Size2); N_TEMP1 = Strlen (M_SZ_NAME); SZ_BUF [0] = 0x01; SZ_BUF [1] = n_temp1; memcpy ((void *) & (void *) & (m_sz_name [0]), n_temp1); n_temp2 = strlen (m_sz_pass); SZ_BUF [1 n_temp1 1 ] = n_temp2; Memcpy ((void *) & (SZ_BUF [1 N_TEMP1 2]), (Void *) & (M_SZ_Pass [0]), N_TEMP2); if (Send (S_Sock5_TCP, SZ_BUF, N_TEMP1 N_TEMP2 3 , 0) == SOCKET_ERROR) {closesocket (s_sock5_tcp); return -1;} memset (sz_buf, 0, ZF5_BUFFER_SIZE2); if (recv (s_sock5_tcp, sz_buf, ZF5_BUFFER_SIZE2,0) == SOCKET_ERROR) {closesocket (s_sock5_tcp); return -1; }

IF (SZ_BUF [0]! = 0x01 || SZ_BUF [1]! = 0x00) {CloseSocket (S_SOCK5_TCP); Return -1;}} else if (SZ_BUF [0]! = 0x05 || SZ_BUF [1]! = 0x00 ) {CloseSocket (S_SOCK5_TCP); Return-1;}

MEMSET (SZ_BUF, 0, ZF5_BUFFER_SIZE2); SZ_BUF [0] = 0x05; SZ_BUF [1] = 0x03; SZ_BUF [2] = 0x00; SZ_BUF [3] = 0x01;

* (INT *) (& SZ_BUF [4])) = p_addr-> sin_addr.s_addr; * ((SHORT *) (& (SZ_BUF [8]))) = (short) p_addr-> sin_port;

IF (Send (S_Sock5_TCP, SZ_BUF, 10, 0) == Socket_ERROR) {CloseSocket (s_sock5_tcp); Return -1;}

MEMSET (SZ_BUF, 0, ZF5_BUFFER_SIZE2); if (RECV (S_Sock5_TCP, SZ_BUF, ZF5_Buffer_Size2, 0) == SOCKET_ERROR) {CloseSocket (s_sock5_tcp); Return -1;}

IF (SZ_BUF [0]! = 0x05 || SZ_BUF [1]! = 0x00) {CloseSocket (s_sock5_tcp); Return -1;}

IF (SZ_BUF [3] == 0x01) {INT N_IP = * (INT *) & SZ_BUF [4]); struct in_addr in; in.s_un.s_addr = n_ip; memset ((void *) m_sz_udp_srv, 0, Sizeof m_sz_udp_srv)); STRCPY (M_SZ_UDP_SRV, INET_NTOA (IN)); m_n_port = htons (* (SHORT *) & (SZ_BUF [8]))); m_n_addrtype = 0x01;} else if (SZ_BUF [3] == 0 × 03) {MEMSET ((void *) m_SZ_UDP_SRV, 0, SIZEOF (M_SZ_UDP_SRV)); STRNCPY (m_sz_udp_srv, & sz_buf [5], SZ_BUF [4]); m_n_port = htons (* (SHORT *) & (SZ_BUF [5 SZ_BUF [ 4]]))); m_n_addrtype = 0x03;} else {clossoCket; return -1;} m_n_sock5 = s_sock5_tcp;

Return 0;}

// udp data transmission by sock5 int zf5_sock5_udp :: send_sock5_udp (int s_udp, char * sz_buf, int n_len, struct sockaddr_in * p_addr) {char sz_buf1 [ZF5_BUFFER_SIZE3 2]; int n_len1 = 0; sz_buf1 [0] = 0x00; sz_buf1 [1] = 0x00; SZ_BUF1 [2] = 0x00; SZ_BUF1 [3] = 0x01; Memcpy ((void *) & Sz_buf1 [4], (void *) & (p_addr-> sin_addr.s_addr), 4); Memcpy (void *) & sz_buf1 [8], (void *) & (p_addr-> sin_port), 2); struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr (m_sz_udp_srv); addr.sin_port = htons (m_n_port); if (addr.sin_addr.s_addr == @addr_none) {struct hostent * p = gethostByname (m_sz_udp_srv); if (p == null) {return -1;} addr.sin_addr.s_addr = ((lpin_addr) P -> h_addr) -> s_addr;}

Memcpy (& SZ_BUF1 [10], SZ_BUF, N_LEN);

INT N_RET = -1; N_RET = Sendto (S_UDP, SZ_BUF1, N_LEN 10, 0, (Struct SockAddr *) & addr, sizeof (struct sockaddr_in));

Return n_ret;

Udp data received by sock5 // int zf5_sock5_udp :: recv_sock5_udp (int s_udp, char * sz_buf, struct sockaddr_in * p_addr) {char sz_buf1 [ZF5_BUFFER_SIZE3 2]; struct sockaddr_in addr; int n_ret = -1; int n_len1 = 0; int n_len2 = sizeof (struct sockaddr_in); n_ret = recvfrom (s_udp, sz_buf1, ZF5_BUFFER_SIZE3,0, (struct sockaddr *) & addr, & n_len2); if (n_ret == SOCKET_ERROR) {return -1;} if (sz_buf1 [3] = = 0x01) {p_addr-> sin_family = AF_INET; P_ADDR-> SIN_ADDR.S_ADDR = * ((int *) & (SZ_BUF1 [4])); p_addr-> sin_port = * ((Short *) & (SZ_BUF1 [8] )); N_RET- = 10; Memcpy (SZ_BUF, & SZ_BUF1 [10], N_RET);} else if (SZ_BUF1 [3] == 0x03) {char SZ_BUF2 [zf5_buffer_size2 2]; MEMSET (SZ_BUF2, 0, ZF5_Buffer_Size2); STRNCPY (SZ_BUF2, SZ_BUF2, SZ_BUF1 [4]); struct hostent * p = gethostByName (SZ_BUF2); if (p == null) {return -1;} n_len1 = 4 SZ_BUF1 [4]; p_addr-> sin_family = AF_INET P_addr-> sin_addr.s_addr = (LPIN_ADDR) P-> h_addr) -> s_addr; p_addr-> sin_port = * (SHORT *) & (SZ_BUF1 [N_LEN1])); n_ret - = (n_len1 2); Memcpy (void *) SZ_BUF, (void *) & SZ_BUF1 [N_LEN1], N_RET);

n_ret - = (4 2 1 SZ_BUF1 [4]);} else {return -1;}

Return n_ret;

// Close SOCK5 UDP proxy int zf5_sock5_udp :: close_sock5_udp () {if (m_n_sock5! = Invalid_socket) {CloseSocket (m_n_sock5); m_n_sock5 = invalid_socket;}

Return 0;}

转载请注明原文地址:https://www.9cbs.com/read-70848.html

New Post(0)