This repository has been archived by the owner on May 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Proxy.cs
131 lines (120 loc) · 4.83 KB
/
Proxy.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using System.Text;
namespace TrifleJS
{
/// <summary>
/// Static class used for tracking and modifying proxy information.
/// Proxy information used by IE is held in the registry.
/// </summary>
public static class Proxy
{
/// <summary>
/// Default settings path for IE
/// </summary>
public static string defaultIESettingsPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings";
/// <summary>
/// Proxy server information (ie "192.168.0.255:8888")
/// </summary>
public static string server = null;
/// <summary>
/// Proxy authentication (ie "jsmith:passwd")
/// </summary>
public static string auth = null;
/// <summary>
/// Type of proxy server (ie [http|socks5|none)
/// </summary>
public static string type = "http";
/// <summary>
/// Tracks wether the proxy has been set inside the app or not
/// </summary>
public static bool isSet = false;
/// <summary>
/// Sets the IE Proxy at windows level
/// </summary>
public static void Set() {
Backup.Save();
if (type == "none")
{
Utils.TryWriteRegistryKey(defaultIESettingsPath, "ProxyEnable", 0, Microsoft.Win32.RegistryValueKind.DWord);
}
else {
Utils.TryWriteRegistryKey(defaultIESettingsPath, "ProxyEnable", 1, Microsoft.Win32.RegistryValueKind.DWord);
Utils.TryWriteRegistryKey(defaultIESettingsPath, "ProxyServer", GetParsedServerInfo(), Microsoft.Win32.RegistryValueKind.String);
Utils.TryWriteRegistryKey(defaultIESettingsPath, "ProxyOverride", "", Microsoft.Win32.RegistryValueKind.String);
}
Proxy.isSet = true;
}
private static string GetParsedServerInfo()
{
switch (type)
{
case "http":
// Include 'http' & 'https' by simply using the host:port configuration
return server;
case "socks5":
// Limit connection to socks only
return String.Format("socks={0}", server);
case "none":
// Blank server
return "";
default:
Console.Error.WriteLine(String.Format("Incorrect --proxy-type option: \"{0}\"", type));
return "";
}
}
/// <summary>
/// Gets the proxy username
/// </summary>
public static string Username {
get {
if (auth == null) {
return null;
};
return auth.Split(':')[0];
}
}
/// <summary>
/// Gets the proxy password
/// </summary>
public static string Password {
get {
if (auth == null || !auth.Contains(":")) {
return null;
}
return auth.Split(':')[1];
}
}
/// <summary>
/// Encapsulates functionality for backing up and restoring existing proxy information
/// </summary>
public static class Backup
{
/// <summary>
/// Saves the existing proxy information
/// </summary>
public static void Save() {
MigrateProxy = Utils.TryReadRegistryKey(defaultIESettingsPath, "MigrateProxy");
ProxyEnable = Utils.TryReadRegistryKey(defaultIESettingsPath, "ProxyEnable");
ProxyServer = Utils.TryReadRegistryKey(defaultIESettingsPath, "ProxyServer");
ProxyOverride = Utils.TryReadRegistryKey(defaultIESettingsPath, "ProxyOverride");
}
/// <summary>
/// Restores previous proxy information
/// </summary>
public static void Restore() {
if (Proxy.isSet)
{
Utils.TryWriteRegistryKey(defaultIESettingsPath, "MigrateProxy", MigrateProxy, Microsoft.Win32.RegistryValueKind.DWord);
Utils.TryWriteRegistryKey(defaultIESettingsPath, "ProxyEnable", ProxyEnable, Microsoft.Win32.RegistryValueKind.DWord);
Utils.TryWriteRegistryKey(defaultIESettingsPath, "ProxyServer", ProxyServer, Microsoft.Win32.RegistryValueKind.String);
Utils.TryWriteRegistryKey(defaultIESettingsPath, "ProxyOverride", ProxyOverride, Microsoft.Win32.RegistryValueKind.String);
}
}
public static object MigrateProxy;
public static object ProxyEnable;
public static object ProxyServer;
public static object ProxyOverride;
}
}
}