-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSeleniumProxyServer.cs
74 lines (65 loc) · 2.38 KB
/
SeleniumProxyServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using SeleniumProxyAuth.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using Titanium.Web.Proxy;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Models;
namespace SeleniumProxyAuth
{
public class SeleniumProxyServer : IDisposable
{
private Dictionary<int, ProxyAuth> proxyAuths;
private ProxyServer proxyServer;
public SeleniumProxyServer()
{
proxyAuths = new Dictionary<int, ProxyAuth>();
proxyServer = new ProxyServer();
proxyServer.BeforeRequest += OnRequest;
proxyServer.Start();
}
/// <summary>
/// Adds a new endpoint to the local proxy server
/// </summary>
/// <param name="auth">ProxyAuth</param>
/// <returns>Port where the new proxy will be opened</returns>
public int AddEndpoint(ProxyAuth auth)
{
IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] conArr = ipGlobalProperties.GetActiveTcpListeners();
for (int i = 50000; i < 60000; i++)
{
if (conArr.Any(x => x.Port == i)) continue;
proxyServer.AddEndPoint(new ExplicitProxyEndPoint(IPAddress.Any, i, true));
proxyAuths.Add(i, auth);
return i;
}
throw new Exception("Couldn't find any available tcp port!");
}
/// <summary>
/// When a new request is received, set up the upstream proxy based on the port of the request
/// </summary>
private async Task OnRequest(object sender, SessionEventArgs e)
{
if (!proxyAuths.TryGetValue(e.ClientLocalEndPoint.Port, out var auth))
{
e.Ok("<html><h>Error with proxy</h></html>");
return;
}
e.CustomUpStreamProxy = new ExternalProxy(auth.Proxy, auth.Port, auth.Username, auth.Password)
{
ProxyType = ExternalProxyType.Http
};
}
/// <summary>
/// Dispose the proxy server
/// </summary>
public void Dispose()
{
this.proxyServer.Dispose();
}
}
}