public PortMapConfig Clone() { PortMapConfig pmc = new PortMapConfig(); pmc.enable = enable; pmc.type = type; pmc.id = id; pmc.server_addr = server_addr; pmc.server_port = server_port; pmc.remarks = remarks; return(pmc); }
public void FlushPortMapCache() { portMapCache = new Dictionary <int, PortMapConfigCache>(); Dictionary <string, Server> id2server = new Dictionary <string, Server>(); Dictionary <string, int> server_group = new Dictionary <string, int>(); foreach (Server s in configs) { id2server[s.id] = s; if (s.group != null && s.group.Length > 0) { server_group[s.group] = 1; } } foreach (KeyValuePair <string, PortMapConfig> pair in portMap) { int key = 0; PortMapConfig pm = pair.Value; if (!pm.enable) { continue; } if (id2server.ContainsKey(pm.id) || server_group.ContainsKey(pm.id) || pm.id == null || pm.id.Length == 0) { } else { continue; } try { key = int.Parse(pair.Key); } catch (FormatException) { continue; } portMapCache[key] = new PortMapConfigCache { type = pm.type, id = pm.id, server = id2server.ContainsKey(pm.id) ? id2server[pm.id] : null, server_addr = pm.server_addr, server_port = pm.server_port }; } }
public void FlushPortMapCache() { portMapCache = new Dictionary <int, PortMapConfigCache>(); Dictionary <string, Server> id2server = new Dictionary <string, Server>(); foreach (Server s in configs) { id2server[s.id] = s; } foreach (KeyValuePair <string, PortMapConfig> pair in portMap) { int key = 0; PortMapConfig pm = pair.Value; if (!pm.enable) { continue; } if (!id2server.ContainsKey(pm.id)) { continue; } try { key = int.Parse(pair.Key); } catch (FormatException) { continue; } portMapCache[key] = new PortMapConfigCache { type = pm.type, id = pm.id, server = id2server[pm.id], server_addr = pm.server_addr, server_port = pm.server_port }; } }
public void FlushPortMapCache() { portMapCache = new Dictionary <int, PortMapConfigCache>(); Dictionary <string, Server> id2server = new Dictionary <string, Server>(); Dictionary <string, int> server_group = new Dictionary <string, int>(); foreach (Server s in configs) { id2server[s.id] = s; if (!string.IsNullOrEmpty(s.group)) { server_group[s.group] = 1; } } foreach (KeyValuePair <string, PortMapConfig> pair in portMap) { int key = 0; PortMapConfig pm = pair.Value; if (!pm.enable) { continue; } if (id2server.ContainsKey(pm.id) || server_group.ContainsKey(pm.id) || pm.id == null || pm.id.Length == 0) { } else { continue; } try { key = int.Parse(pair.Key); } catch (FormatException) { continue; } portMapCache[key] = new PortMapConfigCache { type = pm.type, id = pm.id, server = id2server.ContainsKey(pm.id) ? id2server[pm.id] : null, server_addr = pm.server_addr, server_port = pm.server_port }; } lock (serverStrategyMap) { List <int> remove_ports = new List <int>(); foreach (KeyValuePair <int, ServerSelectStrategy> pair in serverStrategyMap) { if (portMapCache.ContainsKey(pair.Key)) { continue; } remove_ports.Add(pair.Key); } foreach (int port in remove_ports) { serverStrategyMap.Remove(port); } if (!portMapCache.ContainsKey(localPort)) { serverStrategyMap.Remove(localPort); } } uricache.Clear(); }
public void FixConfiguration() { if (localPort == 0) { localPort = 1080; } if (keepVisitTime == 0) { keepVisitTime = 180; } if (portMap == null) { portMap = new Dictionary <string, object>(); } if (token == null) { token = new Dictionary <string, string>(); } if (connect_timeout == 0) { connect_timeout = 10; reconnectTimes = 2; TTL = 180; keepVisitTime = 180; } if (localAuthPassword == null || localAuthPassword.Length < 16) { localAuthPassword = randString(20); } Dictionary <string, int> id = new Dictionary <string, int>(); foreach (Server server in configs) { if (id.ContainsKey(server.id)) { byte[] new_id = new byte[16]; Util.Utils.RandBytes(new_id, new_id.Length); server.id = BitConverter.ToString(new_id).Replace("-", ""); } else { id[server.id] = 0; } } bool type_err = false; foreach (KeyValuePair <string, object> pair in portMap) { if (pair.Value.GetType() != typeof(PortMapConfig)) { type_err = true; } } if (type_err) { Dictionary <string, object> new_portMap = new Dictionary <string, object>(); foreach (KeyValuePair <string, object> pair in portMap) { if (pair.Value.GetType() != typeof(PortMapConfig)) { PortMapConfig pm; Type type = typeof(PortMapConfig); object obj = Activator.CreateInstance(type); System.Reflection.FieldInfo[] field = null; field = type.GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); pm = new PortMapConfig(); foreach (System.Reflection.FieldInfo fi in field) { if (((SimpleJson.JsonObject)pair.Value).ContainsKey(fi.Name)) { if (fi.FieldType == typeof(int)) { int val = (int)(Int64)((SimpleJson.JsonObject)pair.Value)[fi.Name]; fi.SetValue(pm, val); } else { fi.SetValue(pm, ((SimpleJson.JsonObject)pair.Value)[fi.Name]); } } } new_portMap[pair.Key] = pm; } else { new_portMap[pair.Key] = pair.Value; } } portMap = new_portMap; } }