예제 #1
0
 private void CloseSocket(ProxySocketTun sock)
 {
     lock (this)
     {
         if (sock != null)
         {
             var s = sock;
             try
             {
                 s.Shutdown(SocketShutdown.Both);
             }
             catch
             {
                 // ignored
             }
             try
             {
                 s.Close();
             }
             catch
             {
                 // ignored
             }
         }
     }
 }
예제 #2
0
 private void d()
 {
     try
     {
         IPAddress iPAddress = null;
         int       port      = 0;
         if (this.b[0] == 1)
         {
             byte[] array = new byte[4];
             Array.Copy(this.b, 1, array, 0, array.Length);
             iPAddress = new IPAddress(array);
             port      = ((int)this.b[5] << 8 | (int)this.b[6]);
             this.h    = iPAddress.ToString();
         }
         else if (this.b[0] == 4)
         {
             byte[] array2 = new byte[16];
             Array.Copy(this.b, 1, array2, 0, array2.Length);
             iPAddress = new IPAddress(array2);
             port      = ((int)this.b[17] << 8 | (int)this.b[18]);
             this.h    = iPAddress.ToString();
         }
         else if (this.b[0] == 3)
         {
             int    num    = (int)this.b[1];
             byte[] array3 = new byte[num];
             Array.Copy(this.b, 2, array3, 0, array3.Length);
             this.h = Encoding.UTF8.GetString(this.b, 2, num);
             port   = ((int)this.b[num + 2] << 8 | (int)this.b[num + 3]);
             if (!IPAddress.TryParse(this.h, out iPAddress) && iPAddress == null)
             {
                 iPAddress = global::a.d().Get(this.h);
             }
             if (iPAddress == null)
             {
                 iPAddress = global::a.a(this.h, null, false);
             }
             if (iPAddress != null)
             {
                 global::a.d().Set(this.h, new IPAddress(iPAddress.GetAddressBytes()));
                 global::a.d().Sweep();
             }
             else if (!this.g)
             {
                 throw new SocketException(11001);
             }
         }
         this.i = port;
         IPEndPoint ep = new IPEndPoint(iPAddress, port);
         this.e = new ProxySocketTun(iPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
         this.e.GetSocket().NoDelay = true;
         this.e.BeginConnect(ep, new AsyncCallback(this.c), null);
     }
     catch (Exception arg_1D1_0)
     {
         Logging.LogUsefulException(arg_1D1_0);
         this.e();
     }
 }
예제 #3
0
 private void a(ProxySocketTun A_0)
 {
     lock (this)
     {
         if (A_0 != null)
         {
             try
             {
                 A_0.Shutdown(SocketShutdown.Both);
             }
             catch
             {
             }
             try
             {
                 A_0.Close();
             }
             catch
             {
             }
         }
     }
 }
예제 #4
0
            private void Connect()
            {
                try
                {
                    IPAddress ipAddress   = null;
                    var       _targetPort = 0;
                    {
                        if (_firstPacket[0] == 1)
                        {
                            var 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((_local_proxy ? "Local proxy" : "Direct") + " connect " + _remote_host + ":" + _targetPort);
                        }
                        else if (_firstPacket[0] == 4)
                        {
                            var 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((_local_proxy ? "Local proxy" : "Direct") + " connect " + _remote_host + ":" + _targetPort);
                        }
                        else if (_firstPacket[0] == 3)
                        {
                            int len  = _firstPacket[1];
                            var 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((_local_proxy ? "Local proxy" : "Direct") + " connect " + _remote_host + ":" + _targetPort);

                            //if (!_local_proxy)
                            {
                                if (!IPAddress.TryParse(_remote_host, out ipAddress))
                                {
                                    if (_config.proxyRuleMode == ProxyRuleMode.UserCustom)
                                    {
                                        var hostMap = HostMap.Instance();
                                        if (hostMap.GetHost(_remote_host, out var host_addr))
                                        {
                                            if (!string.IsNullOrEmpty(host_addr))
                                            {
                                                var lower_host_addr = host_addr.ToLower();
                                                if (lower_host_addr.StartsWith("reject"))
                                                {
                                                    Close();
                                                    return;
                                                }

                                                if (lower_host_addr.IndexOf('.') >= 0 || lower_host_addr.IndexOf(':') >= 0)
                                                {
                                                    if (!IPAddress.TryParse(lower_host_addr, out ipAddress))
                                                    {
                                                        //
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    if (ipAddress == null)
                                    {
                                        ipAddress = DnsUtil.LocalDnsBuffer.Get(_remote_host);
                                    }
                                }
                                if (ipAddress == null)
                                {
                                    ipAddress = DnsUtil.QueryDns(_remote_host, _remote_host.IndexOf('.') >= 0 ? _config.localDnsServer : null);
                                }
                                if (ipAddress != null)
                                {
                                    DnsUtil.LocalDnsBuffer.Set(_remote_host, new IPAddress(ipAddress.GetAddressBytes()));
                                    DnsUtil.LocalDnsBuffer.Sweep();
                                }
                                else
                                {
                                    if (!_local_proxy)
                                    {
                                        throw new SocketException((int)SocketError.HostNotFound);
                                    }
                                }
                            }
                        }
                        _remote_port = _targetPort;
                    }
                    if (ipAddress != null && _config.proxyRuleMode == ProxyRuleMode.UserCustom)
                    {
                        var hostMap = HostMap.Instance();
                        if (hostMap.GetIP(ipAddress, out var host_addr))
                        {
                            var lower_host_addr = host_addr.ToLower();
                            if (lower_host_addr.StartsWith("reject")
                                )
                            {
                                Close();
                                return;
                            }
                        }
                    }
                    if (_local_proxy)
                    {
                        IPAddress.TryParse(_config.proxyHost, out ipAddress);
                        _targetPort = _config.proxyPort;
                    }
                    // ProxyAuth recv only socks5 head, so don't need to save anything else
                    var remoteEP = new IPEndPoint(ipAddress ?? throw new InvalidOperationException(), _targetPort);

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

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