Skip to content

Commit

Permalink
调整socksv4的获取方法
Browse files Browse the repository at this point in the history
  • Loading branch information
b95678 committed Dec 19, 2019
1 parent a49a8e2 commit fca77d4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 35 deletions.
76 changes: 45 additions & 31 deletions BetterHttpClient/Socks/Socks4HttpWebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ private SocksHttpWebResponse InternalGetResponse()

var proxyUri = Proxy.GetProxy(requestUri);
var ipAddress = GetProxyIpAddress(proxyUri);
var response = new List<byte>();
var response2 = new List<byte>();

using (var client = new TcpClient(ipAddress.ToString(), proxyUri.Port))
{
Expand All @@ -336,40 +336,54 @@ private SocksHttpWebResponse InternalGetResponse()
client.SendTimeout = timeout;
var networkStream = client.GetStream();
// auth
var buf = new byte[300];
int index = 0;
buf[index++] = 0x04; // Version
buf[index++] = 0x01; // NMETHODS
// +----+----+----+----+----+----+----+----+----+----+....+----+
// | VN | CD | DSTPORT | DSTIP | USERID |NULL|
// +----+----+----+----+----+----+----+----+----+----+....+----+
// 1 1 2 4 variable 1
var request = new byte[9];
byte[] userId = new byte[0];

request[0] = 0x04; // Version
request[1] = 0x01; // NMETHODS
var destIP = Dns.GetHostEntry(requestUri.DnsSafeHost).AddressList[0];
var rawBytes = destIP.GetAddressBytes();
var portBytes = BitConverter.GetBytes(Uri.UriSchemeHttps == requestUri.Scheme ? 443 : 80);
for (var i = portBytes.Length - 3; i >= 0; i--)
buf[index++] = portBytes[i];

rawBytes.CopyTo(buf, index);
index += (ushort)rawBytes.Length;
networkStream.Write(buf, 0, index);

networkStream.Read(buf, 0, 2);
if (buf[0] != 0)
var ipBytes = destIP.GetAddressBytes();
var port = Uri.UriSchemeHttps == requestUri.Scheme ? 443 : 80;
request[2] = (byte)(port / 256);
request[3] = (byte)(port % 256);
ipBytes.CopyTo(request, 4);
request[8 + userId.Length] = 0x00;

//index += (ushort)ipBytes.Length;
networkStream.Write(request, 0, request.Length);

// response
// +----+----+----+----+----+----+----+----+
// | VN | CD | DSTPORT | DSTIP |
// +----+----+----+----+----+----+----+----+
// 1 1 2

byte[] response = new byte[8];

networkStream.Read(response, 0, response.Length);
if (response[0] != 0)
{
throw new IOException("Invalid Socks Version");
}
if (buf[1] == 91|| buf[1] == 92|| buf[1] == 93)
{
throw new IOException("Socks Server does not support no-auth");
}
if (buf[1] != 90)
//if (response[1] == 91|| response[1] == 92|| response[1] == 93)
//{
// throw new IOException("Socks Server does not support no-auth");
//}
if (response[1] != 0x5a)
{
throw new Exception("Socks Server did choose bogus auth");
}
networkStream.Read(buf, 0, 2);
var rport = (ushort)IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(buf, 0));
//networkStream.Read(request, 0, 2);
//var rport = (ushort)IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(request, 0));

var rdest = string.Empty;
networkStream.Read(buf, 0, 4);
var v4 = BitConverter.ToUInt32(buf, 0);
rdest = new IPAddress(v4).ToString();
//var rdest = string.Empty;
//networkStream.Read(request, 0, 4);
//var v4 = BitConverter.ToUInt32(request, 0);
//rdest = new IPAddress(v4).ToString();


Stream readStream = null;
Expand All @@ -386,8 +400,8 @@ private SocksHttpWebResponse InternalGetResponse()

string requestString = BuildHttpRequestMessage(requestUri);

var request = Encoding.ASCII.GetBytes(requestString);
readStream.Write(request, 0, request.Length);
var request1 = Encoding.ASCII.GetBytes(requestString);
readStream.Write(request1, 0, request1.Length);
readStream.Flush();

var buffer = new byte[client.ReceiveBufferSize];
Expand All @@ -396,13 +410,13 @@ private SocksHttpWebResponse InternalGetResponse()
do
{
readlen = readStream.Read(buffer, 0, buffer.Length);
response.AddRange(buffer.Take(readlen));
response2.AddRange(buffer.Take(readlen));
} while (readlen != 0);

readStream.Close();
}

var webResponse = new SocksHttpWebResponse(requestUri, response.ToArray());
var webResponse = new SocksHttpWebResponse(requestUri, response2.ToArray());

if (webResponse.StatusCode == HttpStatusCode.Moved || webResponse.StatusCode == HttpStatusCode.MovedPermanently)
{
Expand Down
9 changes: 5 additions & 4 deletions UnitTestBetterHttpClient/UnitTestHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void TestHttpsProxy()
[TestMethod]
public void TestSocks5Proxy()
{
Proxy proxy = new Proxy("124.207.126.15", 1080, ProxyTypeEnum.Socks);
Proxy proxy = new Proxy("47.94.19.105", 3001, ProxyTypeEnum.Socks);
HttpClient client = new HttpClient(proxy)
{
UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0",
Expand All @@ -121,7 +121,8 @@ public void TestSocks5Proxy()
[TestMethod]
public void TestSocks4Proxy()
{
Proxy proxy = new Proxy("115.29.161.103", 1080, ProxyTypeEnum.Socks4);
string ip = "61.142.72.150";
Proxy proxy = new Proxy(ip, 33235, ProxyTypeEnum.Socks4);

HttpClient client = new HttpClient(proxy)
{
Expand All @@ -130,8 +131,8 @@ public void TestSocks4Proxy()
//AcceptEncoding = "deflate"
};

string page = client.Get("http://proxy.mimvp.com/free.php?proxy=in_socks");
Assert.IsTrue(page.Contains("\"origin\": \"115.29.161.103\""));
string page = client.Get("http://httpbin.org/get");
Assert.IsTrue(page.Contains(ip));
}
[TestMethod]
public void TestSocks5ProxyGetIPFromProxyServer()
Expand Down

0 comments on commit fca77d4

Please sign in to comment.