public void TestMethod1()
 {
     var resolver = new ProviderAddrPortResolver();
     var t = new AddrPort("omsg101", 2805);
     var k = new AddrPort("omsg102", 2818);
     var g = new AddrPort("omsg103", 2831);
     var ret = ProviderAddrPortResolver.Concat(ProviderAddrPortResolver.CreateEmptyCircle(), new[] { t, k, g });
     var a = new Elem<AddrPort>(t);
     var b = new Elem<AddrPort>(k);
     var c = new Elem<AddrPort>(g);
     var circle = ProviderAddrPortResolver.CreateEmptyCircle();
     circle = ProviderAddrPortResolver.InsertNext(circle, a);
     circle = ProviderAddrPortResolver.InsertNext(circle, b);
     circle = ProviderAddrPortResolver.InsertNext(circle, c);
     Assert.AreEqual(ret.Circle.ToStr(), circle.ToStr());
 }
Esempio n. 2
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. 3
0
 public static Task <WebSocketClient> ConnectToTlsAsync(AddrPort dest, string path, int timeout)
 => ConnectToTlsAsync(dest, path, timeout, CancellationToken.None);
Esempio n. 4
0
 public static Task <WebSocketClient> ConnectToAsync(AddrPort dest, string path)
 {
     return(ConnectToAsync(dest, path, 15 * 1000));
 }
Esempio n. 5
0
                public Task WriteHeader(AddrPort dest)
                {
                    var header = dest.ToSocks5Bytes();

                    return(SendMsg(new Msg(header)));
                }
Esempio n. 6
0
                public async Task <AddrPort> ReadHeader()
                {
                    var header = (await RecvMsg(null)).Data;

                    return(AddrPort.FromSocks5Bytes(header));
                }
Esempio n. 7
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[]>()
         };
     })))
                                ));
 }
Esempio n. 8
0
 public Reply(AddrPort remoteEP, byte status, string addStr)
 {
     this.remoteEP         = remoteEP;
     this.status           = status;
     this.additionalString = addStr;
 }
Esempio n. 9
0
 public static ConnectRequest Create(IAdapter inAdapter, AddrPort dest, string getInfoStr = null)
 => new ConnectRequestImpl(inAdapter, dest, () => getInfoStr);
Esempio n. 10
0
 public static InConnectionTcp Create(IAdapter inAdapter, AddrPort dest, IMyStream dataStream, string getInfoStr = null)
 => new Impl(inAdapter, dest, async(r) => dataStream, () => getInfoStr);
Esempio n. 11
0
 public static InConnectionTcp Create(IAdapter inAdapter, AddrPort dest, ConnectionCallbackDelegate connectionCallback, Func <string> getInfoStr = null)
 => new Impl(inAdapter, dest, connectionCallback, getInfoStr);
Esempio n. 12
0
        public override async Task HandleTcpConnection(InConnectionTcp connection)
        {
            var host = connection.Dest.Host;
            var name = TryGetName(host);

            if (name != null)
            {
                if (name.Length == 0 || name == list_name)
                {
                    if (connection.Dest.Port == 80)
                    {
                        goto LIST;
                    }
                }
                else
                {
                    var cli = FindClientByName(name);
                    if (cli != null)
                    {
                        await cli.HandleConnection(connection);
                    }
                    else if (if_notfound == null)
                    {
                        await connection.HandleAndGetStream(new ConnectResult(this, ConnectResultEnum.Failed) {
                            FailedReason = "no such client"
                        });
                    }
                    else
                    {
                        connection.RedirectTo(if_notfound);
                    }
                }
            }
            else
            {
                if (enable_ip && AddrPort.TryParseIpv4(host, out var ip))
                {
                    if (ipmap.TryGetValue(ip, out var cli))
                    {
                        await cli.HandleConnection(connection);

                        return;
                    }
                    else if (ip == listIp)
                    {
                        goto LIST;
                    }
                }
                if (if_notmatch != null)
                {
                    connection.RedirectTo(if_notmatch);
                }
            }
            return;

LIST:
            var stream = await connection.HandleAndGetStream(this);

            var httpConnection = CreateHttpConnectionFromMyStream(stream, HttpSvr);

            httpConnection.SetTag("connection", connection);
            await httpConnection.Process();
        }
Esempio n. 13
0
 public Request(AddrPort dest, string addStr)
 {
     this.dest             = dest;
     this.additionalString = addStr;
 }
Esempio n. 14
0
 public Request(AddrPort dest)
 {
     this.dest = dest;
 }
Esempio n. 15
0
        public bool Check(AddrPort dest, string url)
        {
            if (url != null || resultCache == null)   // no cache for connections with a URL
            {
                return(CheckUncached(dest, url));
            }

            var host = dest.Host;

            if (host == null)
            {
                throw new ArgumentNullException("dest.Host");
            }

            if (ReadCache(host, out var cachedHit))
            {
                return(cachedHit);
            }

            bool             computing;
            ManualResetEvent compLock;

            lock (computingLocks) {
                computing = computingLocks.TryGetValue(host, out compLock);
                if (!computing)
                {
                    if (ReadCache(host, out cachedHit))
                    {
                        return(cachedHit);
                    }
                    compLock             = new ManualResetEvent(false);
                    computingLocks[host] = compLock;
                }
            }

            if (computing)
            {
                compLock.WaitOne();
                if (ReadCache(host, out cachedHit))
                {
                    return(cachedHit);
                }
                else
                {
                    throw new Exception("failed to get cached result");
                }
            }

            try {
                var hit = CheckUncached(dest, url);
                resultCacheLock.EnterWriteLock();
                try {
                    resultCache.Add(host, hit);
                } finally {
                    resultCacheLock.ExitWriteLock();
                }
                return(hit);
            } finally {
                compLock.Set();
                lock (computingLocks) {
                    computingLocks.Remove(host);
                }
            }
        }
Esempio n. 16
0
 public ConnectRequestImpl(IAdapter adapter, AddrPort dest, Func <string> getInfoStr = null) : base(adapter)
 {
     _getInfoStr = getInfoStr;
     this.Dest   = dest;
 }
Esempio n. 17
0
        private bool CheckUncached(AddrPort dest, string url)
        {
            var host = dest.Host;

            url = url ?? (dest.Port == 443 ? $"https://{host}/" : $"http://{host}/");
            foreach (var item in blackDomainList)
            {
                if (MatchDomain(host, bigString, item))
                {
                    goto IF_HIT;
                }
            }
            foreach (var item in blackWildcardDomainList)
            {
                if (MatchWildcard(host, bigString, item))
                {
                    goto IF_HIT;
                }
            }
            foreach (var item in blackRegexUrlList)
            {
                if (MatchRegex(url, item))
                {
                    goto IF_HIT;
                }
            }
            foreach (var item in blackWildcardUrlList)
            {
                if (MatchWildcard(url, bigString, item))
                {
                    goto IF_HIT;
                }
            }
            return(false);

IF_HIT:
            foreach (var item in whiteDomainList)
            {
                if (MatchDomain(host, bigString, item))
                {
                    return(false);
                }
            }
            foreach (var item in whiteWildcardDomainList)
            {
                if (MatchWildcard(host, bigString, item))
                {
                    return(false);
                }
            }
            foreach (var item in whiteRegexUrlList)
            {
                if (MatchRegex(url, item))
                {
                    return(false);
                }
            }
            foreach (var item in whiteWildcardUrlList)
            {
                if (MatchWildcard(url, bigString, item))
                {
                    return(false);
                }
            }
            return(true);
        }
Esempio n. 18
0
 public Reply(AddrPort remoteEP, byte status)
 {
     this.remoteEP = remoteEP;
     this.status   = status;
 }