Home > other >  Questions about SOCKS5 communication of the client
Questions about SOCKS5 communication of the client

Time:09-22

I compile environment is win10 + visual studio 2015
The client is written in c # console program
When I was debugging is a proxy server has been set up in the unit, so both agent address is 192.168.0.109:1002
On the first code
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* the Author: CYhyq
* E-mail: 421160052 @qq.com
* a DateTime: the 2018-08-21 15:41:52
* Description: summary of the core code Description
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
using System;
using System.Text;
Using System.Net.Sockets;
Using System.Net;

The namespace Socks5. Proxy
{
Class Program
{
The static void Main (string [] args)
{
The Proxy Proxy=new Proxy ();
Proxy. SendFirstMessage ();
Console. WriteLine (" if you want to stop the service, please press the return key!! ");
Console.ReadLine();
}
Public class Proxy
{
The static string proxyIP="192.168.0.109";
Static int proxyPort=1002;
IPEndPoint proxyEndPoint=new IPEndPoint (IPAddress. Parse (proxyIP), proxyPort);//proxy IP
Socket s=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);
Socket receiveSocket=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);
Byte [] request=new byte [1000].//request information
Byte [] sreceive=new byte [10000].//return message
String userName="XXXX";//account
String password="XXXX";//password
Byte [] rawBytes=new byte [1024].//conversion container
Int nIndex=0;//the subscript
String destAddress="52.235.21.1";//destination url
Static int dest_port=13307;//destination port
The static byte [] result=new byte [1024].//the result set
Static int port;//conversion of port
StringBuilder sb=new StringBuilder();//the result of the receiving data
The static string Result;//the result - string
Public void SendFirstMessage ()
{
//open a TCP connection to the SOCKS server...
S.C onnect (proxyEndPoint);
//for the first time send information
NIndex=0;
Request [nIndex++]=0 x05;//Version 5.
Request [nIndex++]=0 x01;//METHOD.
Request [nIndex++]=0 x02;//USERNAME/PASSWORD
S.S end (request, 3, SocketFlags. None).
Int a=s.R eceive (sreceive, sreceive. Length, SocketFlags. None).
//Username/Password Authentication protocol

//the second send validation message
Byte [] request1=new byte [userName + password Length. The Length + 3);
NIndex=0;
Request1 [nIndex++]=0 x01;//Version 5.
//add user name Length
Request1 [nIndex++]=(byte) userName. Length;
RawBytes=Encoding. The Default. GetBytes (userName);
//account itself
RawBytes. CopyTo (request1 nIndex);
NIndex +=(ushort) rawBytes. Length;
//add the password Length
Request1 [nIndex++]=(byte) password. Length;
RawBytes=Encoding. The Default. GetBytes (password);
//the password itself
RawBytes. CopyTo (request1 nIndex);
NIndex +=(ushort) rawBytes. Length;
//Send the Username/Password request
S.S end (request1, request1. Length, SocketFlags. None).
A=s.R eceive (sreceive, sreceive. Length, SocketFlags. None).
If (sreceive [1].=0)
{
Console. WriteLine (" proxy server validation fails, ");
}
The else
{
Console. WriteLine (" proxy authentication is successful, ");
//continue to send the request
//Send the connect request now...
NIndex=0;

String [] IP_Array=destAddress. Split ('. ');
Request [nIndex++]=0 x05;//version 5.
Request [nIndex++]=0 x01;//1=connect. 2=bind. 3=UDP
Request [nIndex++]=0 x00;//Reserve=must be 0 x00 the function to retain
//request/nIndex++=0 x04;//ATYP=IPV6
Request [nIndex++]=0 x01;//ATYP=IPV4

//server IP *
Request [nIndex++]=the Convert. ToByte (IP_Array [0]).//connect the destination address (server)
Request [nIndex++]=the Convert. ToByte (IP_Array [1]).
Request [nIndex++]=the Convert. ToByte (IP_Array [2]).
Request [nIndex++]=the Convert. ToByte (IP_Array [3]).
//byte into int

//server port dest_port
Int port=the Convert. ToInt32 (dest_port);
String hexOutput=the string. Format ("} {0: X ", the port).
//the Console. WriteLine (" the Hexadecimal value of {0} is {1} ", port, hexOutput);
Request [nIndex++]=(byte) (dest_port/256);
Request [nIndex++]=(byte) (dest_port % 256);
S.S end (request, nIndex SocketFlags. None).

A=s.R eceive (sreceive, sreceive. Length, SocketFlags. None).
Console. WriteLine (" return messages below: ");
Console. WriteLine (" a="+ a);//returns the length of the information
Console. WriteLine (" VER="+ sreceive [0]).
Console. WriteLine (" REP="+ sreceive [1]).
Console. WriteLine (" RSV="+ sreceive [2]).
Console. WriteLine (" ATYP="+ sreceive [3]).
Console. WriteLine (" return information IP="+ sreceive [4]);
Console. WriteLine (" return information IP="+ sreceive [5]);
Console. WriteLine (" return information IP="+ sreceive [6]);
Console. WriteLine (" return information IP="+ sreceive [7]);
Console. WriteLine (" port information 1="+ sreceive [8]).
Console. WriteLine (" port information 2="+ sreceive [9]);
Console. WriteLine (" ");
If (sreceive [1]==0)
{
Try
{
Let the proxy server forwarding messages sent//
String message="{' head ':' login ', 'name', '2535', 'mm' : '123'}";
S.S end (Encoding. The Default. GetBytes (the message));
//receive the proxy server returned message
While ((a=s.R eceive (result)) & gt; 0)
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related