Repository navigation
Expand file tree
/
Copy pathJSONIO.cs
More file actions
123 lines (114 loc) · 3.5 KB
/
Copy pathJSONIO.cs
File metadata and controls
123 lines (114 loc) · 3.5 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Data;
using System.Threading;
namespace ShadowsockslibevGUI
{
[Serializable]
class ServerInfo
{
public ServerInfo()
{
server_port = 1088;
server = "example.com";
password = "0";
method = "aes-128-gcm";
plugin_opts = "obfs=http;obfs-host=img.alicdn.com";
}
public string server {
get;
set;
}
public int server_port {
get;
set;
}
public int local_port {
get;
set;
}
public string password {
get;
set;
}
public string method {
get;
set;
}
public string plugin {
get;
set;
}
public string plugin_opts {
get;
set;
}
}
static class JSONIO
{
public static IReadOnlyList<string> method = new string[6] { "aes-128-cfb", "rc4-md5", "chacha20", "aes-128-ctr", "aes-128-gcm", "chacha20-ietf-poly1305" };
private static ThreadLocal<Regex> rx0 = new ThreadLocal<Regex>(() => new Regex("obfs=http;.*", RegexOptions.Compiled));
private static ThreadLocal<Regex> rx1 = new ThreadLocal<Regex>(() => new Regex("obfs=tls;.*", RegexOptions.Compiled));
public static ServerInfo LoadFromFile()
{
ServerInfo serverFromFile = null;
try {
var configtxt = File.ReadAllText("config.json");
serverFromFile = JsonConvert.DeserializeObject<ServerInfo>(configtxt);
return serverFromFile;
} catch(Exception) { return null; }
}
public static void SaveToFile(ServerInfo s)
{
var configstr = JsonConvert.SerializeObject(s);
File.WriteAllText("config.json", configstr, Encoding.UTF8);
}
public static void checkConfigExit()
{
if(!File.Exists(Application.StartupPath + "config.json")) {
File.WriteAllText("config.json", "", Encoding.UTF8);
}
}
public static int AEAD_MethodNum(ServerInfo s)
{
if(s.method == null) {
return 0;
} else {
for(var i = 0; i < method.Count; i++) {
if(s.method == method[i]) {
return i;
}
}
return 0;
}
}
public static int GetObfsNum(ServerInfo s)
{
if(rx0.Value.IsMatch(s.plugin_opts)) {
return 0;
} else if(rx1.Value.IsMatch(s.plugin_opts)) {
return 1;
} else {
return -1;
}
}
public static string GetPara(ServerInfo s)
{
switch(GetObfsNum(s)) {
case 0:
return s.plugin_opts.Replace("obfs=http;obfs-host=", string.Empty);
case 1:
return s.plugin_opts.Replace("obfs=tls;obfs-host=", string.Empty);
default:
return "";
}
}
}
}