Esempio n. 1
0
        private Task handleConnectProxy(HttpConnection p)
        {
            if (@out == null)
            {
                Logger.info($"unhandled tunnel request (no 'out'): {p.Method} {p.Url}");
                return(AsyncHelper.CompletedTask);
            }
            p.EnableKeepAlive = false;
            var    dest     = AddrPort.Parse(p.Url);
            var    stream   = p.SwitchProtocol();
            var    mystream = getStream(p);
            string str      = "(tunnel) " + p.epPair.ToString();
            var    inc      = InConnectionTcp.Create(this, dest, async(r) => {
                if (r.Ok)
                {
                    await mystream.WriteAsync(ConnectedResponse);
                    return(mystream);
                }
                else
                {
                    mystream.Close().Forget();
                    return(null);
                }
            }, () => str);

            return(HandleIncommingConnection(inc));
        }
Esempio n. 2
0
 public static AddrPort Get()
 {
     try {
         using (var reg = OpenProxyReg()) {
             if (reg.GetValue("ProxyEnable") is int enabled && enabled != 0)
             {
                 var str = reg.GetValue("ProxyServer") as string;
                 if (str?.Length == 0)
                 {
                     str = null;
                 }
                 return(AddrPort.Parse(str));
             }
         }
     } catch (Exception e) {
         Logging.exception(e, Logging.Level.Warning, "Error getting proxy setting");
     }
     return(AddrPort.Empty);
 }
Esempio n. 3
0
            public static Request Parse(byte[] buf, ref int c)
            {
                int sumbegin = c;

                c++;
                var  result = new Request(AddrPort.Parse(buf, ref c));
                byte sum    = buf[c++];

                for (int i = sumbegin; i < c - 1; i++)
                {
                    sum -= buf[i];
                }
                if (sum != 233)
                {
                    throw new Exception("checksum failed");
                }
                sumbegin = c;
                if (c < buf.Length) // version 2
                {
                    result.additionalString = Pack.ParseString(Encoding, buf, ref c);
                }
                if (c < buf.Length)   // version 3
                {
                    int stringsCount = buf[c++];
                    var exStrs       = result.extraStrings = new string[stringsCount];
                    for (int i = 0; i < stringsCount; i++)
                    {
                        exStrs[i] = Pack.ParseString(Encoding, buf, ref c);
                    }
                    sum = buf[c++];
                    for (int i = sumbegin; i < c - 1; i++)
                    {
                        sum -= buf[i];
                    }
                    if (sum != 66)
                    {
                        throw new Exception("checksum failed");
                    }
                }
                return(result);
            }
Esempio n. 4
0
            public static Reply Parse(byte[] buf)
            {
                var c      = 0;
                var result = new Reply();

                c++;
                result.remoteEP = AddrPort.Parse(buf, ref c);
                result.status   = buf[c++];
                byte sum = buf[c++];

                for (int i = 0; i < c - 1; i++)
                {
                    sum -= buf[i];
                }
                if (sum != 233)
                {
                    throw new Exception("checksum failed");
                }
                if (c < buf.Length)
                {
                    result.additionalString = Pack.ParseString(Encoding, buf, ref c);
                }
                return(result);
            }
Esempio n. 5
0
 private static TomlSettings CreateTomlSettings(List <AdapterRef> refs)
 {
     return(TomlSettings.Create(cfg => cfg
                                .AllowNonstandard(true)
                                .ConfigureType <IPEndPoint>(type => type
                                                            .WithConversionFor <TomlString>(convert => convert
                                                                                            .ToToml(custom => custom.ToString())
                                                                                            .FromToml(tmlString => Utils.CreateIPEndPoint(tmlString.Value))))
                                .ConfigureType <AddrPort>(type => type
                                                          .WithConversionFor <TomlString>(convert => convert
                                                                                          .ToToml(custom => custom.ToString())
                                                                                          .FromToml(tmlString => AddrPort.Parse(tmlString.Value))))
                                .ConfigureType <AdapterRef>(type => type
                                                            .WithConversionFor <TomlString>(convert => convert
                                                                                            .ToToml(custom => custom.Ref.ToString())
                                                                                            .FromToml(tmlString => {
         var a = new AdapterRef {
             IsName = true, Ref = tmlString.Value
         };
         refs.Add(a);
         return a;
     }))
                                                            .WithConversionFor <TomlTable>(convert => convert
                                                                                           .FromToml(tml => {
         var a = new AdapterRef {
             IsTable = true, Ref = tml
         };
         refs.Add(a);
         return a;
     })))
                                .ConfigureType <AdapterRefOrArray>(type => type
                                                                   .WithConversionFor <TomlString>(convert => convert
                                                                                                   .FromToml(tmlString => {
         var str = tmlString.Value;
         if (str.Contains('|'))
         {
             var splits = str.Split('|');
             var aarr = new AdapterRef[splits.Length];
             for (int i = 0; i < splits.Length; i++)
             {
                 var a = new AdapterRef {
                     IsName = true, Ref = splits[i]
                 };
                 refs.Add(a);
                 aarr[i] = a;
             }
             return new AdapterRefOrArray {
                 obj = aarr
             };
         }
         else
         {
             return new AdapterRefOrArray {
                 obj = tmlString.Get <AdapterRef>()
             };
         }
     }))
                                                                   .WithConversionFor <TomlTable>(convert => convert
                                                                                                  .FromToml(tmlTable => {
         var a = new AdapterRefOrArray {
             obj = tmlTable.Get <AdapterRef>()
         };
         return a;
     }))
                                                                   .WithConversionFor <TomlArray>(convert => convert
                                                                                                  .FromToml(tmlTable => {
         var a = new AdapterRefOrArray {
             obj = tmlTable.Get <AdapterRef[]>()
         };
         return a;
     }))
                                                                   .WithConversionFor <TomlTableArray>(convert => convert
                                                                                                       .FromToml(tmlTable => {
         var a = new AdapterRefOrArray {
             obj = tmlTable.Get <AdapterRef[]>()
         };
         return a;
     })))
                                .ConfigureType <StringOrArray>(type => type
                                                               .WithConversionFor <TomlString>(convert => convert
                                                                                               .FromToml(tmlString => {
         return new StringOrArray {
             obj = tmlString.Get <string>()
         };
     }))
                                                               .WithConversionFor <TomlTable>(convert => convert
                                                                                              .FromToml(tmlTable => {
         return new StringOrArray {
             obj = tmlTable.Get <string>()
         };
     }))
                                                               .WithConversionFor <TomlArray>(convert => convert
                                                                                              .FromToml(tmlArray => {
         return new StringOrArray {
             obj = tmlArray.Get <string[]>()
         };
     })))
                                ));
 }