public Socket Accept() { byte[] response = new byte[262]; if (_socket.Receive(response) < 10) { throw new SocksClientException("The connection was reset by the remote peer."); } if (response[0] != SocksClient.SOCKS_VERSION) { throw new SocksClientException("Socks version 5 is not supported by the proxy server."); } SocksReplyCode reply = (SocksReplyCode)response[1]; if (reply != SocksReplyCode.Succeeded) { throw new SocksClientException("Socks proxy server request failed: " + reply.ToString()); } _dstEP = new SocksEndPoint(response, 3); return(_socket); }
public int ReceiveFrom(byte[] buffer, int offset, int size, out SocksEndPoint remoteEP) { byte[] datagram = new byte[262 + size]; EndPoint dummyEP = new IPEndPoint(IPAddress.Any, 0); int bytesReceived = _udpSocket.ReceiveFrom(datagram, 0, datagram.Length, SocketFlags.None, ref dummyEP); if (bytesReceived < 10) { throw new SocksClientException("The connection was reset by the remote peer."); } remoteEP = new SocksEndPoint(datagram, 3); int dataOffset = 6 + remoteEP.GetAddressSize(); int dataSize = bytesReceived - dataOffset; if (dataSize > size) { dataSize = size; } Buffer.BlockCopy(datagram, dataOffset, buffer, offset, dataSize); return(dataSize); }
private SocksEndPoint Request(Socket socket, SocksRequestCommand command, SocksEndPoint dstAddr) { socket.Send(dstAddr.CreateRequest(command)); byte[] response = new byte[262]; if (socket.Receive(response) < 10) { throw new SocksClientException("The connection was reset by the remote peer."); } if (response[0] != SOCKS_VERSION) { throw new SocksClientException("Socks version 5 is not supported by the proxy server."); } SocksReplyCode reply = (SocksReplyCode)response[1]; if (reply != SocksReplyCode.Succeeded) { throw new SocksClientException("Socks proxy server request failed: " + reply.ToString()); } return(new SocksEndPoint(response, 3)); }
internal SocksUdpAssociateRequestHandler(Socket controlSocket, Socket udpSocket, SocksEndPoint relayEP) { _controlSocket = controlSocket; _udpSocket = udpSocket; _relayEP = relayEP; _relayEndPoint = _relayEP.GetEndPoint(); _watchThread = new Thread(ControlSocketWatchAsync); _watchThread.IsBackground = true; _watchThread.Start(); }
public SocksBindRequestHandler Bind(SocksEndPoint endpoint, int timeout = 10000) { //connect to proxy server Socket socket = GetProxyConnection(timeout); socket.SendTimeout = 30000; socket.ReceiveTimeout = 30000; try { Negotiate(socket); SocksEndPoint bindEP = Request(socket, SocksRequestCommand.Bind, endpoint); return(new SocksBindRequestHandler(socket, bindEP)); } catch { socket.Dispose(); throw; } }
public SocksConnectRequestHandler Connect(SocksEndPoint remoteEP) { //connect to proxy server Socket socket = GetProxyConnection(); socket.SendTimeout = 30000; socket.ReceiveTimeout = 30000; try { Negotiate(socket); SocksEndPoint bindEP = Request(socket, SocksRequestCommand.Connect, remoteEP); return(new SocksConnectRequestHandler(socket, remoteEP, bindEP)); } catch { socket.Dispose(); throw; } }
public SocksUdpAssociateRequestHandler UdpAssociate(IPEndPoint localEP, int timeout = 10000) { //bind local ep Socket udpSocket = new Socket(localEP.AddressFamily, SocketType.Dgram, ProtocolType.Udp); udpSocket.Bind(localEP); //connect to proxy server Socket socket = GetProxyConnection(timeout); socket.SendTimeout = 30000; socket.ReceiveTimeout = 30000; try { Negotiate(socket); SocksEndPoint relayEP = Request(socket, SocksRequestCommand.UdpAssociate, new SocksEndPoint((IPEndPoint)udpSocket.LocalEndPoint)); return(new SocksUdpAssociateRequestHandler(socket, udpSocket, relayEP)); } catch { if (socket != null) { socket.Dispose(); } if (udpSocket != null) { udpSocket.Dispose(); } throw; } }
public SocksClient(IPEndPoint proxyEndPoint, NetworkCredential credentials = null) { _proxyEP = new SocksEndPoint(proxyEndPoint); Init(credentials); }
public SocksClient(IPAddress proxyAddress, int port = 1080, NetworkCredential credentials = null) { _proxyEP = new SocksEndPoint(proxyAddress, port); Init(credentials); }
public void SendTo(byte[] buffer, int offset, int size, SocksEndPoint remoteEP) { byte[] datagram = remoteEP.CreateUdpDatagram(buffer, offset, size); _udpSocket.SendTo(datagram, _relayEndPoint); }
public SocksBindRequestHandler(Socket socket, SocksEndPoint bindEP) { _socket = socket; _bindEP = bindEP; }
public SocksConnectRequestHandler(Socket socket, SocksEndPoint dstEP, SocksEndPoint bindEP) { _socket = socket; _dstEP = dstEP; _bindEP = bindEP; }
public SocksClient(SocksEndPoint proxyEndPoint, NetworkCredential credentials = null) { _proxyEP = proxyEndPoint; Init(credentials); }