-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathWhatEver.cs
426 lines (376 loc) · 16.4 KB
/
WhatEver.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Collections;
namespace WhatEver{
public partial class WhatEver : Form{
private const string Config_File = "XConfig.xml";
private XmlManager Xml;
public WhatEver(){
InitializeComponent();
this.Icon = this.IT.Icon = GetFileIcon(Application.ExecutablePath);
}
public static System.Drawing.Icon GetFileIcon(string name){
Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES | Shell32.SHGFI_SMALLICON;
Shell32.SHGetFileInfo( name,
Shell32.FILE_ATTRIBUTE_NORMAL,
ref shfi,
(uint) System.Runtime.InteropServices.Marshal.SizeOf(shfi),
flags );
System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone();
User32.DestroyIcon( shfi.hIcon );
return icon;
}
private void EnableProxy_CheckedChanged(object sender, EventArgs e){
SkipLocalProxy.Enabled = EnableProxy.Checked;
IEServerSetting.Enabled = EnableProxy.Checked;
IEExceptSetting.Enabled = EnableProxy.Checked;
}
private void WhatEver_Load(object sender, EventArgs e){
if (File.Exists(Config_File) == false){
File.WriteAllText(Config_File, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Config></Config>", Encoding.UTF8);
ShowTips("我在这 ;)");
}
Xml = new XmlManager(Config_File);
LoadConfigToOption();
}
private void LoadConfigToOption(){
ArrayList st = Xml.getAllSettingList();
ConfigList.Items.Clear();
ProxyList.DropDownItems.Clear();
foreach (string[] s in st){
ConfigList.Items.Add(new SettingItem(s));
ToolStripMenuItem item = new ToolStripMenuItem();
item.Name = s[0];
item.Text = s[1];
item.Click += new EventHandler(this.proxyItem_Click);
ProxyList.DropDownItems.Add(item);
}
SaveSetting.Enabled = false;
ApplySetting.Enabled = false;
ShareConfig.Enabled = false;
}
private void proxyItem_Click(object sender, EventArgs e){
applySetting(((ToolStripMenuItem)sender).Name, ((ToolStripMenuItem)sender).Text);
}
private void SaveAsSetting_Click(object sender, EventArgs e){
string name = ShowDialog("", "请输入配置方案名称");
if (name == "") return;
string guid = string.Format("{{{0}}}", Guid.NewGuid().ToString());
Xml.addNewSetting(name, guid, new string[]{
this.EnableProxy.Checked?"1":"0", getProxyServer(), getProxyOverride(), HostContent.Text
});
LoadConfigToOption();
ShowTips("配置保存成功!");
}
private string getProxyOverride(){
if (this.EnableProxy.Checked == false){
return "";
}
return this.ProxyOverride.Text + (SkipLocalProxy.Checked ? ";<local>" : "");
}
private string getProxyServer(){
if (this.EnableProxy.Checked == false){
return "";
}
if (UseAllSameSetting.Checked){
return string.Format("{0}:{1}", HTTP_IP.Text, HTTP_Port.Text);
}
ArrayList ret = new ArrayList();
if (HTTP_IP.Text != ""){
ret.Add("http=" + HTTP_IP.Text + (HTTP_Port.Text != "" ? ":" + HTTP_Port.Text : ""));
}
if (HTTPS_IP.Text != ""){
ret.Add("https=" + HTTPS_IP.Text + (HTTPS_Port.Text != "" ? ":" + HTTPS_Port.Text : ""));
}
if (FTP_IP.Text != ""){
ret.Add("ftp=" + FTP_IP.Text + (FTP_Port.Text != "" ? ":" + FTP_Port.Text : ""));
}
if (SOCKS_IP.Text != ""){
ret.Add("socks=" + SOCKS_IP.Text + (SOCKS_Port.Text != "" ? ":" + SOCKS_Port.Text : ""));
}
return string.Join(";", (string[])ret.ToArray(typeof(string)));
}
private void ConfigList_SelectedIndexChanged(object sender, EventArgs e){
SettingItem st = (SettingItem)ConfigList.SelectedItem;
Hashtable ret = Xml.getSettingById(st.Id);
if (ret == null) return;
if ((string)ret["ProxyEnable"] == "1"){
EnableProxy.Checked = true;
HTTP_IP.Text = HTTP_Port.Text = HTTPS_IP.Text = HTTPS_Port.Text = FTP_IP.Text = FTP_Port.Text = SOCKS_IP.Text = SOCKS_Port.Text = "";
if (((string)ret["ProxyServer"]).IndexOf("=") == -1){
UseAllSameSetting.Checked = true;
string[] proxy = ((string)ret["ProxyServer"]).Split(':');
HTTP_IP.Text = proxy[0];
HTTP_Port.Text = proxy[1];
}else{
UseAllSameSetting.Checked = false;
string[] proxys = ((string)ret["ProxyServer"]).Split(';');
foreach (string p in proxys){
string[] proxy = p.Split('=');
string[] sett = proxy[1].Split(':');
switch (proxy[0].ToUpper()){
case "HTTP":
HTTP_IP.Text = sett[0];
HTTP_Port.Text = sett[1];
break;
case "HTTPS":
HTTPS_IP.Text = sett[0];
HTTPS_Port.Text = sett[1];
break;
case "FTP":
FTP_IP.Text = sett[0];
FTP_Port.Text = sett[1];
break;
case "SOCKS":
SOCKS_IP.Text = sett[0];
SOCKS_Port.Text = sett[1];
break;
}
}
}
SkipLocalProxy.Checked = (((string)ret["ProxyOverride"]).IndexOf("<local>") != -1);
}else{
EnableProxy.Checked = false;
}
ProxyOverride.Text = ((string)ret["ProxyOverride"]).Replace("<local>", "");
HostContent.Text = (string)ret["Hosts"];
SaveSetting.Enabled = true;
ApplySetting.Enabled = true;
ShareConfig.Enabled = true;
}
private void UseAllSameSetting_CheckedChanged(object sender, EventArgs e){
HTTPS_IP.Enabled = HTTPS_Port.Enabled = FTP_IP.Enabled = FTP_Port.Enabled = SOCKS_IP.Enabled = SOCKS_Port.Enabled = !UseAllSameSetting.Checked;
}
private void SaveSetting_Click(object sender, EventArgs e){
SaveConfig();
ShowTips("配置保存成功!");
}
private void SaveConfig(){
SettingItem st = (SettingItem)ConfigList.SelectedItem;
Xml.saveSetting(st.Id, new string[]{
this.EnableProxy.Checked?"1":"0", getProxyServer(), getProxyOverride(), HostContent.Text
});
}
private void ApplySetting_Click(object sender, EventArgs e){
SaveConfig();
SettingItem st = (SettingItem)ConfigList.SelectedItem;
applySetting(st.Id, st.Name);
}
private void applySetting(string id, string name){
Hashtable ret = Xml.getSettingById(id);
if (ret == null) return;
foreach (ToolStripMenuItem item in this.ProxyList.DropDownItems){
item.Checked = item.Name == id;
}
Proxies.SetProxy((string)ret["ProxyServer"], (string)ret["ProxyOverride"]);
string file = Environment.GetEnvironmentVariable("SystemRoot") + @"\system32\drivers\etc\hosts";
string txt = File.ReadAllText(file, Encoding.Default);
txt = Regex.Replace(txt, @"#WhatEver Start[\s\S]+?#WhatEver End\r\n", "");
string host = ((string)ret["Hosts"]);
//把Hosts放在 hosts文件的最上面,优先级最高
File.WriteAllText(file, string.Format("#WhatEver Start\r\n{0}\r\n#WhatEver End\r\n{1}", host, txt), Encoding.Default);
ShowTips("配置 [" + name + "] 应用成功!");
}
private void ShareConfig_Click(object sender, EventArgs e){
SaveConfig();
SettingItem st = (SettingItem)ConfigList.SelectedItem;
Hashtable ret = Xml.getSettingById(st.Id);
if (ret == null) return;
ret.Add("Name", st.Name);
//Clipboard.SetDataObject(Convert.ToBase64String(Encoding.UTF8.GetBytes(JSON.Encode(ret))));
Clipboard.SetDataObject(JSON.Encode(ret));
MessageBox.Show("当前的配置信息已经保存到剪贴板了,发给好友分享吧!");
}
private void getShareConfig_Click(object sender, EventArgs e){
IDataObject iData = Clipboard.GetDataObject();
if (iData.GetDataPresent(DataFormats.Text)){
try{
string txt = (String)iData.GetData(DataFormats.Text);
if(txt == "")return;
Hashtable config = (Hashtable)JSON.Decode(txt);
string guid = string.Format("{{{0}}}", Guid.NewGuid().ToString());
Xml.addNewSetting((string)config["Name"], guid, new string[]{
(string)config["ProxyEnable"], (string)config["ProxyServer"], (string)config["ProxyOverride"], (string)config["Hosts"]
});
LoadConfigToOption();
Clipboard.SetDataObject("");
MessageBox.Show("配置信息导入成功!");
}catch{
MessageBox.Show("配置信息导入失败!\n剪贴板中的内容不是本程序所需要的配置信息!");
}
}
}
private void WhatEver_SizeChanged(object sender, EventArgs e){
if (this.WindowState == FormWindowState.Minimized){
this.Hide();
}
}
private void IT_MouseDoubleClick(object sender, MouseEventArgs e){
if (this.WindowState == FormWindowState.Minimized){
this.Show();
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
return;
}
this.Hide();
this.WindowState = FormWindowState.Minimized;
}
private void 退出EToolStripMenuItem_Click(object sender, EventArgs e){
this.Close();
}
private void 关闭代理CToolStripMenuItem_Click(object sender, EventArgs e){
Proxies.UnsetProxy();
ShowTips("代理关闭成功!");
}
private void ShowTips(string tips){
IT.ShowBalloonTip(3000, "提示", tips, ToolTipIcon.Info);
}
private Form prompt;
private TextBox textBox;
private string ShowDialog(string text, string caption){
prompt = new Form();
prompt.Width = 450;
prompt.Height = 165;
prompt.Text = caption;
prompt.MaximumSize = new Size(450, 165);
prompt.MinimumSize = new Size(450, 165);
prompt.MaximizeBox = false;
prompt.MinimizeBox = false;
prompt.TopMost = true;
prompt.StartPosition = FormStartPosition.CenterScreen;
textBox = new TextBox();
textBox.Left = 50;
textBox.Top = 50;
textBox.Width = 350;
textBox.TabIndex = 0;
Button confirmation = new Button();
confirmation.Text = "Ok";
confirmation.Left = 300;
confirmation.Width = 100;
confirmation.Top = 80;
confirmation.Click += new System.EventHandler(ConfrimPrompt);
Button CloseBtn = new Button();
CloseBtn.Click += new System.EventHandler(ClosePrompt);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textBox);
prompt.ShowIcon = false;
prompt.AcceptButton = confirmation;
prompt.CancelButton = CloseBtn;
prompt.ActiveControl = textBox;
prompt.Activate();
textBox.Focus();
prompt.ShowDialog();
return textBox.Text;
}
private void ConfrimPrompt(object sender, EventArgs e){
prompt.Close();
}
private void ClosePrompt(object sender, EventArgs e){
textBox.Text = "";
prompt.Close();
}
[STAThread]
static void Main(){
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new WhatEver());
}
}
class SettingItem{
private string _Id, _Name;
public string Id { get{return this._Id;} set{this._Id = value;} }
public string Name { get{return this._Name;} set{this._Name = value;} }
public SettingItem(string[] Setting){
Id = Setting[0];
Name = Setting[1];
}
public override string ToString(){
return Name;
}
}
class XmlManager{
private XmlDocument XmlDoc;
private XmlNode Root;
private string XmlFile;
private string[] Nodes = new string[]{
"ProxyEnable",
"ProxyServer",
"ProxyOverride",
"Hosts"
};
public XmlManager(string file){
try{
XmlFile = file;
XmlDoc = new XmlDocument();
XmlDoc.Load(XmlFile);
Root = XmlDoc.SelectSingleNode("Config");
if (Root.Equals(null)){
throw new Exception("Xml文件格式不正确!");
}
} catch(Exception ex){
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(0);
}
}
public ArrayList getAllSettingList(){
XmlNodeList list = Root.SelectNodes("Settings");
ArrayList ret = new ArrayList();
foreach (XmlNode node in list){
try{
ret.Add(new string[]{node.Attributes["Id"].Value, node.Attributes["Name"].Value});
}catch{ }
}
return ret;
}
public Hashtable getSettingById(string id){
XmlNode node = Root.SelectSingleNode(string.Format("//Settings[@Id='{0}']", id));
if (node == null) return null;
Hashtable ret = new Hashtable();
foreach (string key in Nodes){
ret.Add(key, getNodeValue(node, key));
}
return ret;
}
private string getNodeValue(XmlNode node, string p){
XmlNode nd = node.SelectSingleNode(p);
if (nd == null) return "";
return nd.InnerText;
}
public void saveSetting(string id, string[] config){
XmlNode node = Root.SelectSingleNode(string.Format("//Settings[@Id='{0}']", id)), n;
if (node == null) return;
int i = 0;
foreach (string key in Nodes){
n = node.SelectSingleNode(key);
if (n == null){
i++;
continue;
}
n.InnerXml = string.Format("<![CDATA[{0}]]>", config[i++]);
}
XmlDoc.Save(XmlFile);
}
public void addNewSetting(string name, string guid, string[] config){
XmlElement st = XmlDoc.CreateElement("Settings"), n;
st.SetAttribute("Name", name);
st.SetAttribute("Id", guid);
int i = 0;
foreach (string key in Nodes){
n = XmlDoc.CreateElement(key);
n.AppendChild(XmlDoc.CreateCDataSection(config[i++]));
st.AppendChild(n);
}
Root.AppendChild(st);
XmlDoc.Save(XmlFile);
}
}
}