///<summary>Called when there's an incoming client connection waiting to be accepted.</summary> ///<param name="ar">The result of the asynchronous operation.</param> public override void OnAccept(IAsyncResult ar) { try { Socket NewSocket = ListenSocket.EndAccept(ar); //Console.WriteLine("Connection from " + NewSocket.RemoteEndPoint); if (NewSocket != null) { if (WhitelistIPs != null && WhitelistIPs.Count > 0 && !WhitelistIPs.Contains((NewSocket.RemoteEndPoint as IPEndPoint).Address.ToString())) { //TODO: Log this Console.WriteLine("Invalid connection from " + NewSocket.RemoteEndPoint); try { NewSocket.Close(); } catch { } } else { SocksClient NewClient = new SocksClient(NewSocket, new DestroyDelegate(this.RemoveClient), AuthList); AddClient(NewClient); NewClient.StartHandshake(); } } } catch {} try { //Restart Listening ListenSocket.BeginAccept(new AsyncCallback(this.OnAccept), ListenSocket); } catch { Dispose(); } }
public async Task PopulateWhitelistedIps() { if (!WhitelistEnabled) { return; } if (string.IsNullOrEmpty(WhitelistUrl) && WhitelistIPs != null && !WhitelistIPs.Any()) { Log.Warning($"Service '{Name}' has no whitelisting setup!"); return; } if (string.IsNullOrEmpty(WhitelistJsonPath)) { WhitelistJsonPath = "[*]"; } if (WhitelistIPs != null && WhitelistIPs.Any() && !AllowedNetworks.Any()) { Log.Debug("Setting {count} static CIDR ranges for service {service}", WhitelistIPs.Count(), Name); AllowedNetworks = WhitelistIPs.Select(IPNetwork.Parse); } if (WhitelistIPs != null && WhitelistIPs.Any() && AllowedNetworks.Any()) { return; } Log.Debug("Fetching IPs for service {Name}", Name); var req = await _httpClient.GetAsync(WhitelistUrl); if (!req.IsSuccessStatusCode) { Log.Warning("Unable to get IP Addresses for service {Name}; Server returned a {StatusCode}", Name, req.StatusCode); return; } var body = await req.Content.ReadAsStringAsync(); JObject responseJson = JObject.Parse(body); AllowedNetworks = responseJson.SelectTokens(WhitelistJsonPath) .Select(x => IPNetwork.Parse(x.Value<string>())); Log.Debug("Fetched {Count} CIDR ranges for service {Name}", AllowedNetworks.Count(), Name); }