/// <summary>获取或添加</summary> /// <param name="name"></param> /// <returns></returns> public ProxyItem GetOrAdd(String name) { if (name.IsNullOrEmpty()) { return(null); } var mi = Items.FirstOrDefault(e => e.Name.EqualIgnoreCase(name)); if (mi != null) { return(mi); } lock (this) { var list = new List <ProxyItem>(Items); mi = list.FirstOrDefault(e => e.Name.EqualIgnoreCase(name)); if (mi != null) { return(mi); } mi = new ProxyItem { Name = name }; list.Add(mi); Items = list.ToArray(); return(mi); } }
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); }
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); }
/// <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(); }