public Cap ConnectCap(string uri) { if (_disposed) { return(null); } if (this.State == ManagerState.Stop) { return(null); } if (!uri.StartsWith("tcp:")) { return(null); } var garbages = new List <IDisposable>(); try { var config = this.Config; var result = UriUtils.Parse(uri); if (result == null) { throw new Exception(); } string scheme = result.GetValue <string>("Scheme"); if (scheme != "tcp") { return(null); } string address = result.GetValue <string>("Address"); int port = result.GetValueOrDefault <int>("Port", () => 4050); // Check { IPAddress ipAddress; if (!IPAddress.TryParse(address, out ipAddress)) { return(null); } #if !DEBUG if (!IsGlobalIpAddress(ipAddress)) { return(null); } #endif if (!config.Type.HasFlag(TcpConnectionType.Ipv4) && ipAddress.AddressFamily == AddressFamily.InterNetwork) { return(null); } if (!config.Type.HasFlag(TcpConnectionType.Ipv6) && ipAddress.AddressFamily == AddressFamily.InterNetworkV6) { return(null); } if (!_catharsisManager.Check(ipAddress)) { _blockCount.Increment(); return(null); } } if (!string.IsNullOrWhiteSpace(config.ProxyUri)) { var result2 = UriUtils.Parse(config.ProxyUri); if (result2 == null) { throw new Exception(); } string proxyScheme = result2.GetValue <string>("Scheme"); if (proxyScheme == "socks" || proxyScheme == "socks5") { string proxyAddress = result2.GetValue <string>("Address"); int proxyPort = result2.GetValueOrDefault <int>("Port", () => 1080); var socket = Connect(new IPEndPoint(GetIpAddress(proxyAddress), proxyPort)); garbages.Add(socket); var proxy = new Socks5ProxyClient(address, port); proxy.Create(socket, new TimeSpan(0, 0, 30)); var cap = new SocketCap(socket); garbages.Add(cap); return(cap); } else if (proxyScheme == "http") { string proxyAddress = result2.GetValue <string>("Address"); int proxyPort = result2.GetValueOrDefault <int>("Port", () => 80); var socket = Connect(new IPEndPoint(GetIpAddress(proxyAddress), proxyPort)); garbages.Add(socket); var proxy = new HttpProxyClient(address, port); proxy.Create(socket, new TimeSpan(0, 0, 30)); var cap = new SocketCap(socket); garbages.Add(cap); return(cap); } } else { var socket = Connect(new IPEndPoint(IPAddress.Parse(address), port)); garbages.Add(socket); var cap = new SocketCap(socket); garbages.Add(cap); return(cap); } } catch (Exception) { foreach (var item in garbages) { item.Dispose(); } } return(null); }
private void WatchListenerThread() { for (; ;) { var config = this.Config; foreach (var(uri, tcpListener) in _tcpListeners.ToArray()) { if (config.ListenUris.Contains(uri)) { continue; } tcpListener.Stop(); _tcpListeners.Remove(uri); } foreach (string uri in config.ListenUris) { if (_tcpListeners.ContainsKey(uri)) { continue; } var result = UriUtils.Parse(uri); if (result == null) { throw new Exception(); } string scheme = result.GetValue <string>("Scheme"); string address = result.GetValue <string>("Address"); int port = result.GetValueOrDefault <int>("Port", () => 4050); if (scheme == "tcp") { try { var listener = new TcpListener(IPAddress.Parse(address), port); listener.Start(3); _tcpListeners[uri] = listener; } catch (Exception) { } } } lock (_lockObject) { if (this.Config != config) { continue; } _locationUris.Clear(); _locationUris.AddRange(config.LocationUris); } return; } }
private void WatchThread() { for (; ;) { var config = this.Config; string i2pUri = null; if (config.IsEnabled) { if ((_samManager == null || !_samManager.IsConnected) || _watchSamBridgeUri != config.SamBridgeUri) { try { var result = UriUtils.Parse(config.SamBridgeUri); if (result == null) { throw new Exception(); } string scheme = result.GetValue <string>("Scheme"); if (scheme != "tcp") { throw new Exception(); } string address = result.GetValue <string>("Address"); int port = result.GetValueOrDefault <int>("Port", () => 7656); if (_samManager != null) { _samManager.Dispose(); _samManager = null; } _samManager = new SamManager(address, port, "Amoeba"); _samManager.Start(); _watchSamBridgeUri = config.SamBridgeUri; } catch (Exception) { if (_samManager != null) { _samManager.Dispose(); _samManager = null; } } } if (_samManager.Base32Address != null) { i2pUri = string.Format("i2p:{0}", _samManager.Base32Address); } } else { if (_samManager != null) { _samManager.Dispose(); _samManager = null; } } lock (_lockObject) { if (this.Config != config) { continue; } _locationUris.Clear(); if (i2pUri != null) { _locationUris.Add(i2pUri); } } return; } }
public Cap ConnectCap(string uri) { if (_isDisposed) { return(null); } if (this.State == ManagerState.Stop) { return(null); } var garbages = new List <IDisposable>(); try { var config = this.Config; var result = UriUtils.Parse(uri); if (result == null) { throw new Exception(); } string scheme = result.GetValue <string>("Scheme"); string address = result.GetValue <string>("Address"); int port = result.GetValueOrDefault <int>("Port", () => 4050); var connectionFilter = config.ConnectionFilters.FirstOrDefault(n => n.Scheme == scheme); if (connectionFilter == null || connectionFilter.Type == ConnectionType.None) { return(null); } if (connectionFilter.Type == ConnectionType.Tcp) { // Check { IPAddress ipAddress; if (!IPAddress.TryParse(address, out ipAddress)) { return(null); } #if !DEBUG if (!CheckGlobalIpAddress(ipAddress)) { return(null); } #endif if (!_catharsisManager.Check(ipAddress)) { _blockCount.Increment(); return(null); } } var socket = Connect(new IPEndPoint(IPAddress.Parse(address), port)); garbages.Add(socket); var cap = new SocketCap(socket); garbages.Add(cap); return(cap); } else if (connectionFilter.Type == ConnectionType.Socks5Proxy || connectionFilter.Type == ConnectionType.HttpProxy) { var result2 = UriUtils.Parse(connectionFilter.ProxyUri); if (result2 == null) { throw new Exception(); } string proxyScheme = result2.GetValue <string>("Scheme"); if (proxyScheme != "tcp") { throw new Exception(); } if (connectionFilter.Type == ConnectionType.HttpProxy) { string proxyAddress = result2.GetValue <string>("Address"); int proxyPort = result2.GetValueOrDefault <int>("Port", () => 1080); var socket = Connect(new IPEndPoint(GetIpAddress(proxyAddress), proxyPort)); garbages.Add(socket); var proxy = new HttpProxyClient(address, port); proxy.Create(socket, new TimeSpan(0, 0, 30)); var cap = new SocketCap(socket); garbages.Add(cap); return(cap); } else if (connectionFilter.Type == ConnectionType.Socks5Proxy) { string proxyAddress = result2.GetValue <string>("Address"); int proxyPort = result2.GetValueOrDefault <int>("Port", () => 80); var socket = Connect(new IPEndPoint(GetIpAddress(proxyAddress), proxyPort)); garbages.Add(socket); var proxy = new Socks5ProxyClient(address, port); proxy.Create(socket, new TimeSpan(0, 0, 30)); var cap = new SocketCap(socket); garbages.Add(cap); return(cap); } } } catch (Exception) { foreach (var item in garbages) { item.Dispose(); } } return(null); }