Beispiel #1
0
 private int IsHandle(byte[] firstPacket, int length, Socket socket)
 {
     if (length >= 7 && _config.proxyRuleMode != (int)ProxyRuleMode.Disable)
     {
         IPAddress ipAddress = null;
         if (firstPacket[0] == 1)
         {
             byte[] addr = new byte[4];
             Array.Copy(firstPacket, 1, addr, 0, addr.Length);
             ipAddress = new IPAddress(addr);
         }
         else if (firstPacket[0] == 3)
         {
             int    len  = firstPacket[1];
             byte[] addr = new byte[len];
             if (length >= len + 2)
             {
                 Array.Copy(firstPacket, 2, addr, 0, addr.Length);
                 string host = Encoding.UTF8.GetString(firstPacket, 2, len);
                 if (IPAddress.TryParse(host, out ipAddress))
                 {
                     //pass
                 }
                 else
                 {
                     if ((_config.proxyRuleMode == (int)ProxyRuleMode.BypassLanAndChina || _config.proxyRuleMode == (int)ProxyRuleMode.BypassLanAndNotChina) && _IPRange != null || _config.proxyRuleMode == (int)ProxyRuleMode.UserCustom)
                     {
                         if (!IPAddress.TryParse(host, out ipAddress))
                         {
                             if (_config.proxyRuleMode == (int)ProxyRuleMode.UserCustom)
                             {
                                 Shadowsocks.Model.HostMap hostMap = HostMap.Instance();
                                 if (hostMap.GetHost(host, out var host_addr))
                                 {
                                     if (!string.IsNullOrEmpty(host_addr))
                                     {
                                         string lower_host_addr = host_addr.ToLower();
                                         if (lower_host_addr.StartsWith("reject") ||
                                             lower_host_addr.StartsWith("direct")
                                             )
                                         {
                                             return(CONNECT_DIRECT);
                                         }
                                         else if (lower_host_addr.StartsWith("localproxy"))
                                         {
                                             return(CONNECT_LOCALPROXY);
                                         }
                                         else if (lower_host_addr.StartsWith("remoteproxy"))
                                         {
                                             return(CONNECT_REMOTEPROXY);
                                         }
                                         else if (lower_host_addr.IndexOf('.') >= 0 || lower_host_addr.IndexOf(':') >= 0)
                                         {
                                             if (!IPAddress.TryParse(lower_host_addr, out ipAddress))
                                             {
                                                 //
                                             }
                                         }
                                     }
                                 }
                             }
                             if (ipAddress == null)
                             {
                                 ipAddress = Utils.DnsBuffer.Get(host);
                             }
                         }
                         if (ipAddress == null)
                         {
                             ipAddress = Utils.QueryDns(host, host.IndexOf('.') >= 0 ? _config.dnsServer : null);
                             if (ipAddress != null)
                             {
                                 Utils.DnsBuffer.Set(host, new IPAddress(ipAddress.GetAddressBytes()));
                                 if (host.IndexOf('.') >= 0)
                                 {
                                     if (Utils.isLAN(ipAddress)) // assume that it is polution if return LAN address
                                     {
                                         return(CONNECT_REMOTEPROXY);
                                     }
                                 }
                             }
                             else
                             {
                                 Logging.Log(LogLevel.Debug, "DNS query fail: " + host);
                             }
                         }
                     }
                 }
             }
         }
         else if (firstPacket[0] == 4)
         {
             byte[] addr = new byte[16];
             Array.Copy(firstPacket, 1, addr, 0, addr.Length);
             ipAddress = new IPAddress(addr);
         }
         if (ipAddress != null)
         {
             if (_config.proxyRuleMode == (int)ProxyRuleMode.UserCustom)
             {
                 Shadowsocks.Model.HostMap hostMap = HostMap.Instance();
                 string host_addr;
                 if (hostMap.GetIP(ipAddress, out host_addr))
                 {
                     string lower_host_addr = host_addr.ToLower();
                     if (lower_host_addr.StartsWith("reject") ||
                         lower_host_addr.StartsWith("direct")
                         )
                     {
                         return(CONNECT_DIRECT);
                     }
                     else if (lower_host_addr.StartsWith("localproxy"))
                     {
                         return(CONNECT_LOCALPROXY);
                     }
                     else if (lower_host_addr.StartsWith("remoteproxy"))
                     {
                         return(CONNECT_REMOTEPROXY);
                     }
                 }
             }
             else
             {
                 if (Util.Utils.isLAN(ipAddress))
                 {
                     return(CONNECT_DIRECT);
                 }
                 if ((_config.proxyRuleMode == (int)ProxyRuleMode.BypassLanAndChina || _config.proxyRuleMode == (int)ProxyRuleMode.BypassLanAndNotChina) && _IPRange != null &&
                     ipAddress.AddressFamily == AddressFamily.InterNetwork
                     )
                 {
                     if (_IPRange.IsInIPRange(ipAddress))
                     {
                         return(CONNECT_LOCALPROXY);
                     }
                     Utils.DnsBuffer.Sweep();
                 }
             }
         }
     }
     return(CONNECT_REMOTEPROXY);
 }
Beispiel #2
0
        public static bool LogSocketException(string remarks, string server, Exception e)
        {
            if (DateTime.Now.ToString("yyyy-MM") != date)
            {
                OpenLogFile();
            }
            // just log useful exceptions, not all of them
            if (e is ObfsException)
            {
                ObfsException oe = (ObfsException)e;
                Error("Proxy server [" + remarks + "(" + server + ")] "
                      + oe.Message);
                return(true);
            }
            else if (e is NullReferenceException)
            {
                return(true);
            }
            else if (e is ObjectDisposedException)
            {
                // ignore
                return(true);
            }
            else if (e is SocketException)
            {
                SocketException se = (SocketException)e;
                if ((uint)se.SocketErrorCode == 0x80004005)
                {
                    // already closed
                    return(true);
                }
                else if (se.ErrorCode == 11004)
                {
                    Logging.Log(LogLevel.Warn, "Proxy server [" + remarks + "(" + server + ")] "
                                + "DNS lookup failed");
                    return(true);
                }
                else if (se.SocketErrorCode == SocketError.HostNotFound)
                {
                    Logging.Log(LogLevel.Warn, "Proxy server [" + remarks + "(" + server + ")] "
                                + "Host not found");
                    return(true);
                }
                else if (se.SocketErrorCode == SocketError.ConnectionRefused)
                {
                    Logging.Log(LogLevel.Warn, "Proxy server [" + remarks + "(" + server + ")] "
                                + "connection refused");
                    return(true);
                }
                else if (se.SocketErrorCode == SocketError.NetworkUnreachable)
                {
                    Logging.Log(LogLevel.Warn, "Proxy server [" + remarks + "(" + server + ")] "
                                + "network unreachable");
                    return(true);
                }
                else if (se.SocketErrorCode == SocketError.TimedOut)
                {
                    //Logging.Log(LogLevel.Warn, "Proxy server [" + remarks + "(" + server + ")] "
                    //    + "connection timeout");
                    return(true);
                }
                else if (se.SocketErrorCode == SocketError.Shutdown)
                {
                    return(true);
                }
                else
                {
                    Logging.Log(LogLevel.Info, "Proxy server [" + remarks + "(" + server + ")] "
                                + Convert.ToString(se.SocketErrorCode) + ":" + se.Message);

                    Debug(ToString(new StackTrace().GetFrames()));

                    return(true);
                }
            }
            return(false);
        }
Beispiel #3
0
        public static bool LogSocketException(string remarks, string server, Exception e)
        {
            // just log useful exceptions, not all of them
            if (e is ObfsException)
            {
                ObfsException oe = (ObfsException)e;
                Logging.Log(LogLevel.Error, "Proxy server [" + remarks + "(" + server + ")] "
                            + oe.Message);
                return(true);
            }
            else if (e is ObjectDisposedException)
            {
                // ignore
                return(true);
            }
            else if (e is SocketException)
            {
                SocketException se = (SocketException)e;
                if (se.SocketErrorCode == SocketError.ConnectionAborted)
                {
                    // closed by browser when sending
                    // normally happens when download is canceled or a tab is closed before page is loaded
                }
                else if (se.SocketErrorCode == SocketError.ConnectionReset)
                {
                    // received rst
                }
                else if (se.SocketErrorCode == SocketError.NotConnected)
                {
                    // close when not connected
                }
                else if ((uint)se.SocketErrorCode == 0x80004005)
                {
                    // already closed
                    return(true);
                }
                else if (se.ErrorCode == 11004)
                {
                    Logging.Log(LogLevel.Warn, "Proxy server [" + remarks + "(" + server + ")] "
                                + "DNS lookup failed");
                    return(true);
                }
                else if (se.SocketErrorCode == SocketError.HostNotFound)
                {
                    Logging.Log(LogLevel.Warn, "Proxy server [" + remarks + "(" + server + ")] "
                                + "Host not found");
                    return(true);
                }
                else if (se.SocketErrorCode == SocketError.ConnectionRefused)
                {
                    Logging.Log(LogLevel.Warn, "Proxy server [" + remarks + "(" + server + ")] "
                                + "connection refused");
                    return(true);
                }
                else if (se.SocketErrorCode == SocketError.NetworkUnreachable)
                {
                    Logging.Log(LogLevel.Warn, "Proxy server [" + remarks + "(" + server + ")] "
                                + "network unreachable");
                    return(true);
                }
                else if (se.SocketErrorCode == SocketError.TimedOut)
                {
                    //Logging.Log(LogLevel.Warn, "Proxy server [" + remarks + "(" + server + ")] "
                    //    + "connection timeout");
                    return(true);
                }
                else if (se.SocketErrorCode == SocketError.Shutdown)
                {
                    return(true);
                }
                else
                {
                    Logging.Log(LogLevel.Info, "Proxy server [" + remarks + "(" + server + ")] "
                                + Convert.ToString(se.SocketErrorCode) + ":" + se.Message);
//#if DEBUG
                    Console.WriteLine(ToString(new StackTrace().GetFrames()));
//#endif
                    return(true);
                }
            }
            return(false);
        }