-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathProxyAuthentication.cs
More file actions
62 lines (55 loc) · 2.57 KB
/
ProxyAuthentication.cs
File metadata and controls
62 lines (55 loc) · 2.57 KB
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
using System.IO;
using System.Runtime.InteropServices;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumProxyAuthentication
{
/// <summary>
/// Auth Proxy For Chrome
/// </summary>
public static class ProxyAuthentication
{
/// <summary>
/// Add Proxy With Extension To Chrome Option (Chrome Only Support Http Auth Proxies (!Didn't Test It With Socks4 Auth Proxies))
/// </summary>
/// <param name="browserOption">You Can Use Edge or Chrome for now</param>
/// <param name="proxy">Your Proxy With This Format host:port or host:port:user:pass as string</param>
/// <param name="crxManifest">Edit Chrome Extension Manifest File (Leave it Empty If You Don't Want To Change It)</param>
public static void AddProxyAuthenticationExtension<T>(this T browserOption, Proxy proxy, [Optional] CrxManifest crxManifest, bool pack = true)
{
//easy set if there is no auth proxy
//if (!proxy.HasCredential)
//{
// (browserOption as ChromeOptions)?.AddArgument($"--proxy-server={GenerateCrx.GetScheme(proxy.ProxyProtocol)}://{proxy.Host}:{proxy.Port}");
// return;
//}
var cachePath = Utilities.GetCachePath();
var tempFolder = Utilities.GetTempFolder(cachePath, proxy);
var crxDetailsFolder = Utilities.GetCrxDetailsFolder(tempFolder);
var manifestLocation = Path.Combine(crxDetailsFolder, "manifest.json");
var backgroundLocation = Path.Combine(crxDetailsFolder, "background.js");
Utilities.WriteDetailsFiles(crxManifest, manifestLocation, backgroundLocation, proxy);
if (browserOption is ChromeOptions chromeOptions)
{
if (pack)
{
chromeOptions.AddExtension(Utilities.CreateExtension(tempFolder, crxDetailsFolder));
}
else
{
chromeOptions.AddArgument($"--load-extension={crxDetailsFolder}");
}
}
}
/// <summary>
/// Delete All Files That Made By Extensions
/// </summary>
/// <param name="driverOptions"></param>
public static void DeleteExtensionsCache(this DriverOptions driverOptions)
{
var cacheFolder = Path.Combine(GenerateCrx.GetAppDataPath(), "ProxyAuthCache");
if (Directory.Exists(cacheFolder))
Directory.Delete(cacheFolder);
}
}
}