我的編譯環境是win10+visual studio 2015
客戶端是用C#寫的控制臺程式
我除錯的時候是在本機上架設了一個代理服務器,所以代理端地址是192.168.0.109:1002
先上代碼
/* ***********************************************
* Author : CYhyq
* Email : [email protected]
* DateTime : 2018-08-21 15:41:52
* Description : 核心代碼 的摘要說明
*
* ***********************************************/
using System;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace Socks5.proxy
{
class Program
{
static void Main(string[] args)
{
Proxy proxy = new Proxy();
proxy.SendFirstMessage();
Console.WriteLine("如果要停止服務,請按回車鍵!!");
Console.ReadLine();
}
public class Proxy
{
static string proxyIP = "192.168.0.109";
static int proxyPort = 1002;
IPEndPoint proxyEndPoint = new IPEndPoint(IPAddress.Parse(proxyIP), proxyPort); //代理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]; //請求資訊
byte[] sreceive = new byte[10000]; //回傳訊息
string userName = "xxxx"; //賬戶
string password = "xxxx"; //密碼
byte[] rawBytes = new byte[1024]; //轉換容器
int nIndex = 0; //下標
string destAddress = "52.235.21.1"; //目的地網址
static int dest_port = 13307; //目的埠
static byte[] result = new byte[1024]; //結果集
static int port; //轉換的埠
StringBuilder sb = new StringBuilder(); //接收資料的結果
static string Result; //結果--字串
public void SendFirstMessage()
{
// open a TCP connection to SOCKS server...
s.Connect(proxyEndPoint);
//第一次發送資訊
nIndex = 0;
request[nIndex++] = 0x05; // Version 5.
request[nIndex++] = 0x01; // METHOD.
request[nIndex++] = 0x02; // USERNAME/PASSWORD
s.Send(request, 3, SocketFlags.None);
int a = s.Receive(sreceive, sreceive.Length, SocketFlags.None);
//Username / Password Authentication protocol
//第二次發送驗證訊息
byte[] request1 = new byte[userName.Length+ password.Length+3];
nIndex = 0;
request1[nIndex++] = 0x01; // Version 5.
// add user name Length
request1[nIndex++] = (byte)userName.Length;
rawBytes = Encoding.Default.GetBytes(userName);
// 賬戶本身
rawBytes.CopyTo(request1, nIndex);
nIndex += (ushort)rawBytes.Length;
// add password Length
request1[nIndex++] = (byte)password.Length;
rawBytes = Encoding.Default.GetBytes(password);
// 密碼本身
rawBytes.CopyTo(request1, nIndex);
nIndex += (ushort)rawBytes.Length;
// Send the Username/Password request
s.Send(request1, request1.Length, SocketFlags.None);
a = s.Receive(sreceive, sreceive.Length, SocketFlags.None);
if (sreceive[1] !=0)
{
Console.WriteLine("代理服務器驗證失敗。");
}
else
{
Console.WriteLine("代理服務器驗證成功。");
//繼續發送請求
// Send connect request now...
nIndex = 0;
string [] IP_Array= destAddress.Split('.');
request[nIndex++] = 0x05; // version 5.
request[nIndex++] = 0x01; // 1 = connect. 2 = bind . 3 = UDP
request[nIndex++] = 0x00; // Reserve = must be 0x00 此功能為保留功能
//request[nIndex++] = 0x04; // ATYP=IPV6
request[nIndex++] = 0x01; // ATYP=IPV4
//服務器IP*
request[nIndex++] = Convert.ToByte(IP_Array[0]); //連接目的地址(服務器)
request[nIndex++] = Convert.ToByte(IP_Array[1]);
request[nIndex++] = Convert.ToByte(IP_Array[2]);
request[nIndex++] = Convert.ToByte(IP_Array[3]);
//byte轉化為int
//服務器埠dest_port
int port = Convert.ToInt32(dest_port);
string hexOutput = String.Format("{0:X}", port);
//Console.WriteLine("Hexadecimal value of {0} is {1}", port, hexOutput);
request[nIndex++] = (byte)(dest_port / 256);
request[nIndex++] = (byte)(dest_port % 256);
s.Send(request, nIndex, SocketFlags.None);
a = s.Receive(sreceive, sreceive.Length, SocketFlags.None);
Console.WriteLine("回傳訊息以下:");
Console.WriteLine("a =" + a); //回傳資訊的長度
Console.WriteLine("VER =" + sreceive[0]);
Console.WriteLine("REP =" + sreceive[1]);
Console.WriteLine("RSV =" + sreceive[2]);
Console.WriteLine("ATYP=" + sreceive[3]);
Console.WriteLine("回傳資訊IP=" + sreceive[4]);
Console.WriteLine("回傳資訊IP=" + sreceive[5]);
Console.WriteLine("回傳資訊IP=" + sreceive[6]);
Console.WriteLine("回傳資訊IP=" + sreceive[7]);
Console.WriteLine("埠資訊1=" + sreceive[8]);
Console.WriteLine("埠資訊2=" + sreceive[9]);
Console.WriteLine("");
if (sreceive[1]==0)
{
try
{
//發送讓代理服務器轉發的訊息
string message = "{'head':'login','name':'2535','mm':'123'}";
s.Send(Encoding.Default.GetBytes(message));
//接收代理服務器回傳的訊息
while ((a = s.Receive(result)) > 0)
{
sb.Append(Encoding.Default.GetString(result, 0, a));
}
}
catch
{
//通信程序出問題,例外關閉socket
s.Close();
}
Result = sb.ToString();
Console.WriteLine("Result="+ Result);
//資訊收發完關閉與代理服務器連接
s.Close();
Console.WriteLine("代理服務結束");
}
}
}
}
}
}
目的IP(destAddress )和埠 是我后臺服務器所在的IP和埠,向后臺發送訊息后收到的訊息無論如何都不該是空,因為后臺是會判斷訊息后回傳不會空的訊息,但是實際運行后的結果是這樣:

Result最后的回傳結果應該就是代理服務器訪問了destAddress 以后回傳回來的資訊吧,但是最后結果是空,希望大神能夠給個建議,研究了很久沒發現是哪里的問題,小弟在此謝過。
uj5u.com熱心網友回復:
使用驅動攔截網路資料,然后R3用SOCKS5轉發到你的代理服務器。可以達到每個行程不同IP的效果,上個圖吧。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/57597.html
標籤:網絡通信
