forked from simpleway2016/JMS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
74 lines (63 loc) · 2.57 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
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace JMS.Proxy
{
public class Program
{
static Socks5Server _socks5Server;
public static void Main(string[] args)
{
ThreadPool.GetMaxThreads(out int w, out int c);
ThreadPool.SetMinThreads(w, c);
var builder = new ConfigurationBuilder();
string appsettingFileName = "appsettings.json";
IConfiguration configuration;
int port;
if (args.Length > 0)
{
port = int.Parse(args[0]);
if (args.Length > 1)
{
appsettingFileName = args[1];
}
builder.AddJsonFile(appsettingFileName, optional: true, reloadOnChange: true);
configuration = builder.Build();
}
else
{
builder.AddJsonFile(appsettingFileName, optional: true, reloadOnChange: true);
configuration = builder.Build();
port = configuration.GetValue<int>("Port");
}
ServiceCollection services = new ServiceCollection();
services.AddLogging(loggingBuilder =>
{
loggingBuilder.AddConfiguration(configuration.GetSection("Logging"));
loggingBuilder.AddConsole(); // 将日志输出到控制台
});
services.AddSingleton<IConfiguration>(configuration);
services.AddSingleton<Socks5Server>();
services.AddSingleton<RequestHandler>();
var serviceProvider = services.BuildServiceProvider();
_socks5Server = serviceProvider.GetService<Socks5Server>();
ConfigurationChangeCallback(configuration);
_socks5Server.Run(port);
}
static IDisposable CallbackRegistration;
static void ConfigurationChangeCallback(object p)
{
CallbackRegistration?.Dispose();
CallbackRegistration = null;
IConfiguration configuration = (IConfiguration)p;
Task.Run(() => {
Thread.Sleep(1000);//延迟注册,否则可能每次都回调两次
CallbackRegistration = configuration.GetReloadToken().RegisterChangeCallback(ConfigurationChangeCallback, configuration);
});
_socks5Server.WhiteList = configuration.GetSection("WhiteList").Get<NetAddress[]>();
}
}
}