-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
320 lines (277 loc) · 8.25 KB
/
Program.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using Microsoft.Win32;
using Spectre.Console;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
class Program
{
static void Main(string[] args)
{
if (!OperatingSystem.IsWindows())
{
AnsiConsole.MarkupLine("[yellow]]Only windows platform supported[/]");
Console.ReadKey();
}
Console.CancelKeyPress += OnExit;
AppDomain.CurrentDomain.ProcessExit += OnExit;
On();
Menu();
}
/// <summary>
/// Registry keys
/// </summary>
const string _proxyServerKey = "ProxyServer";
const string _proxyEnableKey = "ProxyEnable";
static string _configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.ini");
/// <summary>
/// Set proxy one
/// </summary>
static void On()
{
if(ReadConfig().Any())
SetFromDefaultConfig();
RegistryKey Key = GetRegKey();
if (Key != null)
{
Key.SetValue(_proxyEnableKey, "1", RegistryValueKind.DWord);
}
Key.Close();
}
/// <summary>
/// Set proxy off
/// </summary>
static void Off()
{
RegistryKey Key = GetRegKey();
if (Key != null)
{
Key.SetValue(_proxyEnableKey, "0", RegistryValueKind.DWord);
}
Key.Close();
}
/// <summary>
/// Initialize Registry key and value
/// </summary>
/// <returns></returns>
static object ProxyServerInit()
{
RegistryKey Key = GetRegKey();
var value = Key.GetValue(_proxyServerKey);
if (value == null)
{
value = "";
}
Key.Close();
return value;
}
/// <summary>
/// Change host url
/// </summary>
static void SetHost(string host)
{
var value = ProxyServerInit();
var arr = value.ToString().Split(':');
string port = arr.Length > 1 ? arr[1] : "";
SetToRegistry(host, port);
}
/// <summary>
/// Change port url
/// </summary>
static void SetPort(string port)
{
var value = ProxyServerInit();
var arr = value.ToString().Split(':');
string host = arr.Length > 0 ? arr[0] : "";
SetToRegistry(host, port);
}
/// <summary>
/// Store proxy changes
/// </summary>
static void SetToRegistry(string host, string port)
{
RegistryKey Key = GetRegKey();
if (string.IsNullOrWhiteSpace(host) && string.IsNullOrWhiteSpace(port))
{
Key.DeleteValue("ProxyServer");
return;
}
string value = string.IsNullOrWhiteSpace(port) ? host : $"{host}:{port}";
Key.SetValue(_proxyServerKey, value, RegistryValueKind.String);
Key.Close();
}
/// <summary>
/// Get status of proxy config
/// </summary>
static void ShowStatus()
{
RegistryKey Key = GetRegKey();
if (Key != null)
{
var statusObj = Key.GetValue(_proxyEnableKey);
string status = "Off";
if (statusObj != null)
{
status = statusObj.ToString() == "1" ? "[green]On[/]" : "[red]Off[/]";
}
AnsiConsole.MarkupLine($"[yellow]Status[/] {status}");
string host = null;
string port;
var valueObj = Key.GetValue(_proxyServerKey);
string strValue = "";
if (valueObj != null)
{
strValue = valueObj.ToString();
host = strValue.Split(":")[0];
}
if (string.IsNullOrWhiteSpace(host))
{
host = "[red]is empty[/]";
}
AnsiConsole.MarkupLine($"[yellow]Host {host}[/]");
if (strValue.Split(":").Length > 1)
port = strValue.Split(":")[1];
else
port = "[red]is empty[/]";
AnsiConsole.MarkupLine($"[yellow]Port {port}[/]");
}
Key.Close();
}
/// <summary>
/// Get registry key
/// </summary>
static RegistryKey GetRegKey()
{
return Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
}
/// <summary>
/// Store default config
/// </summary>
static void DefineDefault()
{
var config = ReadConfig();
foreach (var item in config)
{
AnsiConsole.MarkupLine($"[yellow]{item.Key} {item.Value}[/]");
}
Console.WriteLine();
string host = AnsiConsole.Ask<string>("[yellow]Enter your host:[/]");
string port = AnsiConsole.Ask<string>("[yellow]Enter your port:[/]");
StoreConfig(new Dictionary<string, string>
{
{ "default host", host },
{ "default port", port }
});
}
/// <summary>
/// Set proxy from default config
/// </summary>
static void SetFromDefaultConfig()
{
var config = ReadConfig();
if (!config.Any())
{
AnsiConsole.MarkupLine($"[red]Default host and port is not set[/]");
AnsiConsole.MarkupLine($"[yellow]Press any key to back...[/]");
Console.ReadKey();
return;
}
if (config["default host"] != null)
{
SetHost(config["default host"]);
}
if (config["default port"] != null)
{
SetPort(config["default port"]);
}
ShowStatus();
}
/// <summary>
/// Store config to file
/// </summary>
static void StoreConfig(Dictionary<string, string> input)
{
var config = ReadConfig();
foreach (var item in input)
{
config[item.Key] = item.Value;
}
File.WriteAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.ini"), config.Select(kvp => kvp.Key + "=" + kvp.Value).ToArray());
}
/// <summary>
/// Read from config file
/// </summary>
static Dictionary<string, string> ReadConfig()
{
if (!File.Exists(_configPath)) return new Dictionary<string, string>();
string[] config = File.ReadAllLines(_configPath);
var dict = config.Select(l => l.Split('=')).ToDictionary(c => c[0], c => c[1]);
return dict;
}
/// <summary>
/// Display menu
/// </summary>
static void Menu()
{
Console.Clear();
AnsiConsole.Write(
new FigletText($"Proxy Switcher")
.Centered()
.Color(Color.DeepSkyBlue3));
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString(3);
AnsiConsole.MarkupLine($"[DeepSkyBlue3]v{version}[/]");
AnsiConsole.MarkupLine($"[DeepSkyBlue3]github.com/HoseinHabibiyan/ProxySwitcher[/]");
Console.WriteLine();
ShowStatus();
Console.WriteLine();
AnsiConsole.MarkupLine("[yellow]Menu[/]");
string choice = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.HighlightStyle(new Style(Color.Yellow))
.AddChoices(new[] {
"Proxy On",
"Proxy Off",
"Set from default config",
"Define default",
"Change host url",
"Change port",
"Exit"
}));
Console.Clear();
switch (choice)
{
case "Proxy On":
On();
break;
case "Proxy Off":
Off();
break;
case "Set from default config":
SetFromDefaultConfig();
break;
case "Define default":
DefineDefault();
break;
case "Change host url":
string host = AnsiConsole.Ask<string>("[yellow]Enter your host:[/]");
SetHost(host);
break;
case "Change port":
string port = AnsiConsole.Ask<string>("[yellow]Enter your port:[/]");
SetPort(port);
break;
case "Exit":
Environment.Exit(0);
break;
}
Menu();
}
/// <summary>
/// On exit set proxy off
/// </summary>
static void OnExit(object sender, EventArgs e)
{
Off();
}
}