-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
178 lines (160 loc) · 6.27 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
using System;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Reflection;
using System.Text;
using System.Linq;
namespace FTPow
{
class Program
{
// Entrance
static void Main(string[] args)
{
if (args.Length == 0) return;
else
{
// Encoding Registration
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// Get Binary Path
string binaryPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
// Parse User Configuration
UConfig uconfig = LoadConfig(Path.Combine(binaryPath, "config.json"));
// Get File URL
string URL = args[0];
// Parse Username and Password
Match m = Regex.Match(URL, @"^ftp://(?<fore>(?<username>.*?)?(?:\:(?<password>.*?)?)?@)?(?<back>(?<domain>.+?)(?:/|$).*$)");
if (m.Success)
{
string username = m.Groups["username"].Value;
string domain = m.Groups["domain"].Value;
string back = m.Groups["back"].Value;
foreach (ServerItem server in uconfig.servers)
{
// Reconstruct URL
if(Uri.UnescapeDataString(username) == server.username && Uri.UnescapeDataString(domain) == server.address)
{
if (server.username == "")
{
URL = string.Format(@"ftp://{0}", back);
}
else if (server.password == "")
{
URL = string.Format(@"ftp://{0}@{1}", username, back);
}
else
{
URL = string.Format(@"ftp://{0}:{1}@{2}", username, Uri.EscapeDataString(server.password), back);
}
break;
}
}
// Get File Name Extension
string ext = Regex.Replace(Path.GetExtension(URL), @"^\.([^\?]+)", "$1");
// Filter and Execute
foreach (AppItem item in uconfig.apps)
{
if (item.extList.Any(s => s.Equals(ext, StringComparison.OrdinalIgnoreCase)))
{
// Decode URL
URL = DecodeURL(item, URL);
// Append Query String
URL += "?" + item.queryString;
// Execute
ExecuteApplication(item.programPath, string.Join(URL, item.command));
// Return after Execution
return;
}
}
}
// Fallback Execution
URL = DecodeURL(uconfig.fallback, URL);
URL = Uri.EscapeUriString(URL);
ExecuteApplication(uconfig.fallback.programPath, string.Join(URL, uconfig.fallback.command));
return;
}
}
// Decode URL
static string DecodeURL(AppItem item, string URL)
{
// Get Encoding Type
Encoding e = EncodingType[item.decode.ToUpper()];
// "+" Decode
if (!item.decodePlus)
{
string oldPathAndQuery = new Uri(URL).PathAndQuery;
string newPathAndQuery = oldPathAndQuery.Replace("+", "%2B");
URL = URL.Replace(oldPathAndQuery, newPathAndQuery);
}
// Decode
if (e != null)
{
URL = HttpUtility.UrlDecode(URL, e);
}
return URL;
}
// Load Json Configuration
static UConfig LoadConfig(string fileName)
{
using (StreamReader r = new StreamReader(fileName))
{
string json = r.ReadToEnd();
UConfig uconfig = JsonConvert.DeserializeObject<UConfig>(json);
return uconfig;
}
}
// Execute Application
static void ExecuteApplication(string path, string command)
{
using (System.Diagnostics.Process pProcess = new System.Diagnostics.Process())
{
pProcess.StartInfo.FileName = path;
pProcess.StartInfo.Arguments = command;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true;
pProcess.Start();
pProcess.WaitForExit();
}
}
// User Configuration Class Definition
public class UConfig
{
public List<ServerItem> servers;
public List<AppItem> apps;
public AppItem fallback;
}
// FTP Server Item Class Definition
public class ServerItem
{
public string address;
public string username;
public string password;
}
// Application Item Class Definition
public class AppItem
{
public string programPath;
public List<string> command;
public List<string> extList;
public string queryString;
public bool decodePlus;
public string decode;
}
public static Dictionary<string, Encoding> EncodingType = new Dictionary<string, Encoding>
{
{"", null },
{"NONE", null },
{"GB2312", Encoding.GetEncoding("GB2312") },
{"UTF8", Encoding.UTF8 },
{"UTF32", Encoding.UTF32 },
{"ASCII", Encoding.ASCII },
{"AUTO", Encoding.Default },
{"DEFAULT", Encoding.Default }
};
}
}