Пример #1
0
        private async Task ShowAll()
        {
            await Task.Delay(500);

            //XTrace.WriteLine("");
            Console.WriteLine();

            var ps = ProxyHelper.GetAll();

            XTrace.WriteLine("共有代理提供者[{0}]:", ps.Length);
            foreach (var item in ps)
            {
                XTrace.WriteLine("{0}\t{1}\t{2}", item.Name, item.GetDisplayName(), item.FullName);
            }

            Console.WriteLine();
            var set = Setting.Current;
            var xs  = set.Items ?? new ProxyItem[0];

            XTrace.WriteLine("共有代理配置[{0}]:", xs.Length);
            foreach (var item in xs)
            {
                XTrace.WriteLine("{0}({1})\t{2}\t{3}=>{4}\t{5}", item.Name, item.Provider, item.Enable, item.Local, item.Remote, item.Value);
            }
        }
Пример #2
0
        private void ShowAll()
        {
            //await Task.Delay(500);
            Thread.Sleep(500);

            //XTrace.WriteLine("");
            Console.WriteLine();

            var ps = ProxyHelper.GetAll();

            XTrace.WriteLine("共有代理提供者[{0}]:", ps.Length);
            foreach (var item in ps)
            {
                var pxy = item.CreateInstance() as ProxyBase;
                XTrace.WriteLine("{0}\t{1}\t{2}", pxy.Name, item.GetDisplayName(), item.FullName);
            }

            Console.WriteLine();
            var set = Setting.Current;
            var xs  = set.Items ?? new ProxyItem[0];

            XTrace.WriteLine("共有代理配置[{0}]:", xs.Length);
            foreach (var item in xs)
            {
                XTrace.WriteLine("{0}\t{1}\t{2}\t{3}=>{4}\t{5}", item.Name, item.Provider, item.Enable, item.Local, item.Remote, item.Config);
            }
        }
Пример #3
0
        private ProxyBase CreateProxy(ProxyItem item, Boolean debug)
        {
            var xs   = ProxyHelper.GetAll();
            var type = xs.FirstOrDefault(e => item.Provider.EqualIgnoreCase(e.Name, e.FullName, e.Name.TrimEnd("Proxy")));

            if (type == null)
            {
                return(null);
            }

            if (type.CreateInstance() is not ProxyBase proxy)
            {
                return(null);
            }

            using var span = _tracer?.NewSpan("CreateProxy", item);

            proxy.Name   = item.Name;
            proxy.Tracer = _tracer;

            XTrace.WriteLine("创建代理 {0}", item.ToJson());

            proxy.Init(item.Config);

            // 配置本地、远程参数。高级参数直接修改这里,解析item.Value
            proxy.Local = new NetUri(item.Local);
            if (proxy is NATProxy nat && !item.Remote.IsNullOrEmpty())
            {
                nat.RemoteServer = new NetUri(item.Remote);
            }

            // 配置日志
            proxy.Log = XTrace.Log;
            if (debug)
            {
                proxy.SessionLog = XTrace.Log;
            }

            // 启动服务
            proxy.Start();

            return(proxy);
        }
Пример #4
0
        private ProxyBase CreateProxy(ProxyItem item, Boolean debug)
        {
            var xs   = ProxyHelper.GetAll();
            var type = xs.FirstOrDefault(e => item.Provider.EqualIgnoreCase(e.Name, e.FullName, e.Name.TrimEnd("Proxy")));

            if (type == null)
            {
                return(null);
            }

            var proxy = type.CreateInstance() as ProxyBase;

            if (proxy == null)
            {
                return(null);
            }

            // 配置本地、远程参数。高级参数直接修改这里,解析item.Value
            proxy.Local = new NetUri(item.Local);
            if (proxy is NATProxy nat && !item.Remote.IsNullOrEmpty())
            {
                nat.RemoteServer = new NetUri(item.Remote);
            }

            // 配置日志
            proxy.Log = XTrace.Log;
            if (debug)
            {
                proxy.SessionLog = XTrace.Log;
            }

            // 启动服务
            proxy.Start();

            return(proxy);
        }
Пример #5
0
        /// <summary>已加载</summary>
        protected override void OnLoaded()
        {
            // 本进程的第一次加载时,补齐缺失项
            if (_proxyArray == null)
            {
                var list = new List <ProxyItem>();

                var ms = Items;
                if (ms != null && ms.Length > 0)
                {
                    list.AddRange(ms);
                }

                var demo = list.Count == 0;

                var idx = 1;
                var arr = ProxyHelper.GetAll();
                foreach (var item in arr)
                {
                    if (!item.IsAbstract && item.GetGenericArguments().Length == 0)
                    {
                        var pxy  = item.CreateInstance() as ProxyBase;
                        var name = pxy.Name.TrimEnd("Proxy");
                        // 如果没有该类型的代理项,则增加一个
                        if (!list.Any(e => e.Provider == name))
                        {
                            var pi = new ProxyItem
                            {
                                Name     = "Demo" + idx++,
                                Provider = name,
                            };

                            if (item == typeof(NATProxy))
                            {
                                pi.Local  = "tcp://0.0.0.0:3388";
                                pi.Remote = "tcp://localhost:3389";
                            }
                            else if (item == typeof(HttpReverseProxy))
                            {
                                pi.Local  = "tcp://0.0.0.0:8081";
                                pi.Remote = "tcp://www.newlifex.com:80";
                            }
                            else if (item == typeof(HttpProxy))
                            {
                                pi.Local  = "tcp://0.0.0.0:1080";
                                pi.Remote = "";
                                pi.Config = "user=;pass=newlife;realm=Access to NewLife-HttpProxy";
                            }

                            // 演示模式时,默认开启所有代理
                            if (demo)
                            {
                                pi.Enable = true;
                            }

                            list.Add(pi);
                        }
                    }
                }

                _proxyArray = arr;

                Items = list.ToArray();
            }

            base.OnLoaded();
        }