public Connection AcceptConnection(out string uri, BandwidthLimit bandwidthLimit) { uri = null; List <IDisposable> garbages = new List <IDisposable>(); try { Connection connection = null; foreach (var type in (new int[] { 0, 1 }).Randomize()) { if (this.State == ManagerState.Stop) { return(null); } if (type == 0) { lock (this.ThisLock) { foreach (var item in _tcpListeners) { if (item.Value.Pending()) { var socket = item.Value.AcceptTcpClient().Client; garbages.Add(socket); { var remoteEndPoint = (IPEndPoint)socket.RemoteEndPoint; uri = string.Format("tcp:{0}:{1}", remoteEndPoint.Address, remoteEndPoint.Port); } if (!this.OnCheckUriEvent(uri)) { _blockedCount.Increment(); continue; } var cap = new SocketCap(socket); garbages.Add(cap); connection = new BaseConnection(cap, bandwidthLimit, _maxReceiveCount, _bufferManager); garbages.Add(connection); } } } } else if (type == 1) { // Overlay network var cap = this.OnAcceptCapEvent(out uri); if (cap == null) { continue; } garbages.Add(cap); connection = new BaseConnection(cap, bandwidthLimit, _maxReceiveCount, _bufferManager); garbages.Add(connection); } if (connection != null) { break; } } if (connection == null) { return(null); } var secureConnection = new SecureConnection(SecureConnectionVersion.Version3, SecureConnectionType.Accept, connection, null, _bufferManager); garbages.Add(secureConnection); secureConnection.Connect(new TimeSpan(0, 0, 30)); var compressConnection = new CompressConnection(secureConnection, _maxReceiveCount, _bufferManager); garbages.Add(compressConnection); compressConnection.Connect(new TimeSpan(0, 0, 10)); return(compressConnection); } catch (Exception) { foreach (var item in garbages) { item.Dispose(); } } return(null); }
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); }
public Cap ConnectCap(string uri) { if (_disposed) { 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); }