Created
August 11, 2018 13:43
-
-
Save d3v3us/cbce18e18e8417e23a014e6f65a33bdf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SocksWebProxy : IWebProxy, IDisposable | |
{ | |
private static object locker = new object(); | |
private static List<ProxyListener> listeners; | |
private static bool allowBypass; | |
private ProxyListener GetListener(ProxyConfig config, bool allowBypass = true) | |
{ | |
lock (locker) | |
{ | |
SocksWebProxy.allowBypass = allowBypass; | |
if (listeners == null) | |
listeners = new List<ProxyListener>(); | |
var listener = listeners.Where(x => x.Port == config.HttpPort).FirstOrDefault(); | |
if (listener == null) | |
{ | |
listener = new ProxyListener(config); | |
listener.Start(); | |
listeners.Add(listener); | |
} | |
if (listener.Version != config.Version) | |
throw new Exception("Socks Version Mismatch for Port " + config.HttpPort); | |
return listener; | |
} | |
} | |
private ProxyConfig Config { get; set; } | |
/// <summary> | |
/// Creates a new SocksWebProxy | |
/// </summary> | |
/// <param name="config">Proxy settings</param> | |
/// <param name="allowBypass">Whether to allow bypassing the proxy server. | |
/// The current implementation to allow bypassing the proxy server requiers elevated privileges. | |
/// If you want to use the library in an environment with limited privileges (like Azure Websites or Azure Webjobs), set allowBypass = false</param> | |
/// <returns></returns> | |
public SocksWebProxy(ProxyConfig config = null, bool allowBypass = true) | |
{ | |
Config = config; | |
GetListener(config, allowBypass); | |
} | |
private ICredentials cred = null; | |
public ICredentials Credentials | |
{ | |
get | |
{ | |
return cred; | |
} | |
set | |
{ | |
cred = value; | |
} | |
} | |
public Uri GetProxy(Uri destination) | |
{ | |
return new Uri("http://127.0.0.1:" + Config.HttpPort); | |
} | |
/// <summary> | |
/// Indicates whether to use the proxy server for the specified host. | |
/// </summary> | |
/// <param name="host"></param> | |
/// <returns></returns> | |
public bool IsBypassed(Uri host) | |
{ | |
if (allowBypass) | |
{ | |
return !IsActive(); | |
} | |
return false; | |
} | |
public bool IsActive() | |
{ | |
var isSocksPortListening = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners().Any(x => x.Port == Config.SocksPort); | |
return isSocksPortListening; | |
} | |
#region IDisposable Support | |
private bool disposedValue = false; | |
public void Dispose() | |
{ | |
if (!disposedValue) | |
{ | |
disposedValue = true; | |
var itemsToDispose = listeners; | |
listeners = null; | |
itemsToDispose.ForEach(x => x.Dispose()); | |
} | |
GC.SuppressFinalize(this); | |
} | |
~SocksWebProxy() | |
{ | |
Dispose(); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment