Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.IO;
- namespace SocksForSharp{
- public enum ProxyType{
- Socks4, Socks5
- }
- public class Proxy{
- public ProxyType Type{ get; set; }
- public IPEndPoint EndPoint{ get; set; }
- public IPAddress Address{ get{return EndPoint.Address;} set{EndPoint.Address = value;} }
- public int Port{ get{return EndPoint.Port;} set{EndPoint.Port = value;} }
- public string Username{ get; set; }
- public string Password{ get; set; }
- public Proxy(){
- EndPoint = new IPEndPoint(0, 0);
- Username = Password = "";
- }
- public override string ToString(){
- if(!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password)){
- return string.Format("{0}://{1}:{2}@{3}:{4}", Type, Username, new string('*', Password.Length), Address, Port);
- }
- else if(!string.IsNullOrEmpty(Username)){
- return string.Format("{0}://{1}@{2}:{3}", Type, Username, Address, Port);
- }
- else{
- return string.Format("{0}://{1}:{2}", Type, Address, Port);
- }
- }
- }
- public class SocksError:Exception{
- public override string Message{ get{return _Message;} }
- private string _Message;
- public SocksError(string Message){
- _Message = Message;
- }
- public override string ToString(){
- return string.Format("SocksError: {0}", Message);
- }
- }
- public static class TcpClientEx{
- public static void ConnectProxy(this TcpClient tcpClient, IPEndPoint target, Proxy proxy){
- ConnectProxy(tcpClient, target.Address.ToString(), target.Port, proxy);
- }
- public static void ConnectProxy(this TcpClient tcpClient, IPAddress address, int port, Proxy proxy){
- ConnectProxy(tcpClient, address.ToString(), port, proxy);
- }
- public static void ConnectProxy(this TcpClient tcpClient, string target, int port, Proxy proxy){
- tcpClient.Connect(proxy.EndPoint);
- if(proxy.Type == ProxyType.Socks4){
- IPAddress address;
- MemoryStream ms = new MemoryStream();
- if(!IPAddress.TryParse(target, out address)){
- byte[] target_bytes = Encoding.UTF8.GetBytes(target);
- ms.Write(new byte[] { 4, 1, (byte)((port >> 8) & 0xff), (byte)(port & 0xff), 0, 0, 0, 1, 0 }, 0, 9);
- ms.Write(target_bytes, 0, target_bytes.Length);
- byte[] username_bytes = Encoding.UTF8.GetBytes(proxy.Username);
- ms.Write(username_bytes, 0, username_bytes.Length);
- ms.WriteByte(0);
- }
- else{
- ms.Write(new byte[] { 4, 1, (byte)((port>>8)&0xff), (byte)(port&0xff) }, 0, 4);
- byte[] username_bytes = Encoding.UTF8.GetBytes(proxy.Username);
- ms.Write(username_bytes, 0, username_bytes.Length);
- ms.WriteByte(0);
- byte[] address_bytes = address.GetAddressBytes();
- ms.Write(address_bytes, 0, address_bytes.Length);
- ms.WriteByte(0);
- }
- tcpClient.Client.Send(ms.ToArray(), 0, (int)ms.Length, SocketFlags.None);
- ms.Close();
- ms.Dispose();
- byte[] buffer = new byte[8];
- if(tcpClient.Client.Receive(buffer, 0, 8, SocketFlags.None) != 8){
- tcpClient.Close();
- throw new SocksError("Wrong number of bytes received.");
- }
- if(buffer[0] != 0){
- tcpClient.Close();
- throw new SocksError(string.Format("Wrong protocol version ({0}).", buffer[1]));
- }
- if(buffer[1] != 0x5a){
- tcpClient.Close();
- throw new SocksError(string.Format("Socks connection failed ({0:x}).", buffer[1]));
- }
- }
- else if(proxy.Type == ProxyType.Socks5){
- if(string.IsNullOrEmpty(proxy.Username) || string.IsNullOrEmpty(proxy.Password))
- tcpClient.Client.Send(new byte[]{5, 1, 0});
- else
- tcpClient.Client.Send(new byte[]{5, 2, 0, 2});
- byte[] buffer = new byte[0xfff];
- if(tcpClient.Client.Receive(buffer, 0, buffer.Length, SocketFlags.None) != 2){
- tcpClient.Close();
- throw new SocksError("Wrong number of bytes received.");
- }
- if(buffer[0] != 5){
- tcpClient.Close();
- throw new SocksError(string.Format("Wrong protocol version ({0}).", buffer[0]));
- }
- if(buffer[1] == 0xff){
- tcpClient.Close();
- throw new SocksError("No acceptable authentication method.");
- }
- MemoryStream ms;
- if(buffer[1] == 2){
- ms = new MemoryStream();
- ms.WriteByte(1);
- byte[] username_bytes = Encoding.UTF8.GetBytes(proxy.Username);
- ms.WriteByte((byte)username_bytes.Length);
- ms.Write(username_bytes, 0, username_bytes.Length);
- byte[] password_bytes = Encoding.UTF8.GetBytes(proxy.Password);
- ms.WriteByte((byte)password_bytes.Length);
- ms.Write(password_bytes, 0, password_bytes.Length);
- tcpClient.Client.Send(ms.ToArray(), 0, (int)ms.Length, SocketFlags.None);
- ms.Close();
- ms.Dispose();
- if(tcpClient.Client.Receive(buffer, 0, 2, SocketFlags.None) != 2){
- tcpClient.Close();
- throw new SocksError("Wrong number of bytes received.");
- }
- if(buffer[1] != 0){
- tcpClient.Close();
- throw new SocksError("Authentication failed.");
- }
- }
- ms = new MemoryStream();
- IPAddress address;
- byte[] target_bytes;
- byte address_type = 0xff;
- if(IPAddress.TryParse(target, out address)){
- target_bytes = address.GetAddressBytes();
- address_type = (byte)(target_bytes.Length == 4 ? 1 : 4);
- }
- else{
- target_bytes = Encoding.UTF8.GetBytes(target);
- address_type = 3;
- }
- ms.Write(new byte[]{5,1,0,address_type},0,4);
- if(address_type == 3)
- ms.WriteByte((byte)target_bytes.Length);
- ms.Write(target_bytes, 0, target_bytes.Length);
- ms.Write(new byte[] { (byte)((port >> 8) & 0xff), (byte)(port & 0xff) }, 0, 2);
- tcpClient.Client.Send(ms.ToArray(), 0, (byte)ms.Length, SocketFlags.None);
- int c = tcpClient.Client.Receive(buffer, 0, (byte)ms.Length, SocketFlags.None);
- if(c < 4){
- tcpClient.Close();
- throw new SocksError("Wrong number of bytes received.");
- }
- if(buffer[0] != 5){
- tcpClient.Close();
- throw new SocksError(string.Format("Wrong protocol version ({0}).", buffer[0]));
- }
- if(buffer[1] != 0){
- tcpClient.Close();
- throw new SocksError(string.Format("Socks connection failed ({0}).", buffer[1]));
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement