Пример #1
0
 private void CloseSocket(ProxySocketTun sock)
 {
     lock (this)
     {
         if (sock != null)
         {
             ProxySocketTun s = sock;
             try
             {
                 s.Shutdown(SocketShutdown.Both);
             }
             catch { }
             try
             {
                 s.Close();
             }
             catch { }
         }
     }
 }
Пример #2
0
            private void Connect()
            {
                try
                {
                    IPAddress ipAddress   = null;
                    int       _targetPort = 0;
                    {
                        if (_firstPacket[0] == 1)
                        {
                            byte[] addr = new byte[4];
                            Array.Copy(_firstPacket, 1, addr, 0, addr.Length);
                            ipAddress    = new IPAddress(addr);
                            _targetPort  = (_firstPacket[5] << 8) | _firstPacket[6];
                            _remote_host = ipAddress.ToString();
                            Logging.Info("Direct" + " connect " + _remote_host + ":" + _targetPort.ToString());
                        }
                        else if (_firstPacket[0] == 4)
                        {
                            byte[] addr = new byte[16];
                            Array.Copy(_firstPacket, 1, addr, 0, addr.Length);
                            ipAddress    = new IPAddress(addr);
                            _targetPort  = (_firstPacket[17] << 8) | _firstPacket[18];
                            _remote_host = ipAddress.ToString();
                            Logging.Info("Direct" + " connect " + _remote_host + ":" + _targetPort.ToString());
                        }
                        else if (_firstPacket[0] == 3)
                        {
                            int    len  = _firstPacket[1];
                            byte[] addr = new byte[len];
                            Array.Copy(_firstPacket, 2, addr, 0, addr.Length);
                            _remote_host = Encoding.UTF8.GetString(_firstPacket, 2, len);
                            _targetPort  = (_firstPacket[len + 2] << 8) | _firstPacket[len + 3];
                            Logging.Info("Direct" + " connect " + _remote_host + ":" + _targetPort.ToString());

                            if (!IPAddress.TryParse(_remote_host, out ipAddress))
                            {
                                if (ipAddress == null)
                                {
                                    ipAddress = Utils.DnsBuffer.Get(_remote_host);
                                }
                            }
                            if (ipAddress == null)
                            {
                                if (_remote_host.IndexOf('.') >= 0)
                                {
                                    ipAddress = Utils.QueryDns(_remote_host, _config.dnsServer);
                                }
                                else
                                {
                                    ipAddress = Utils.QueryDns(_remote_host, null);
                                }
                            }
                            if (ipAddress != null)
                            {
                                Utils.DnsBuffer.Set(_remote_host, new IPAddress(ipAddress.GetAddressBytes()));
                                Utils.DnsBuffer.Sweep();
                            }
                            else
                            {
                                throw new SocketException((int)SocketError.HostNotFound);
                            }
                        }
                        _remote_port = _targetPort;
                    }

                    // ProxyAuth recv only socks5 head, so don't need to save anything else
                    IPEndPoint remoteEP = new IPEndPoint(ipAddress, _targetPort);

                    _remote = new ProxySocketTun(ipAddress.AddressFamily,
                                                 SocketType.Stream, ProtocolType.Tcp);
                    _remote.GetSocket().NoDelay = true;

                    // Connect to the remote endpoint.
                    _remote.BeginConnect(remoteEP,
                                         new AsyncCallback(ConnectCallback), null);
                }
                catch (Exception e)
                {
                    Logging.LogUsefulException(e);
                    Close();
                }
            }