forked from tmoonlight/NSmartProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
157 lines (139 loc) · 5.3 KB
/
Program.cs
File metadata and controls
157 lines (139 loc) · 5.3 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
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
using log4net;
using log4net.Config;
using Microsoft.Extensions.Configuration;
using NSmartProxy.Client;
using NSmartProxy.Data;
using NSmartProxy.Data.Models;
using NSmartProxy.Interfaces;
using NSmartProxy.Shared;
using System;
using System.IO;
using System.Threading.Tasks;
using FtpServer;
using System.Net;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace NSmartProxyFTP
{
class Program
{
#region logger
public class Log4netLogger : INSmartLogger
{
public void Debug(object message)
{
//Logger.Debug(message);
Logger.Debug(message);
}
public void Error(object message, Exception ex)
{
//Logger.Debug(message);
Logger.Error(message, ex);
}
public void Info(object message)
{
Logger.Info(message);
}
}
#endregion
public static ILog Logger;
public static IConfigurationRoot Configuration { get; set; }
private static LoginInfo _currentLoginInfo;
static void Main(string[] args)
{
//log
var loggerRepository = LogManager.CreateRepository("NSmartClientRouterRepository");
XmlConfigurator.Configure(loggerRepository, new FileInfo("log4net.config"));
Logger = LogManager.GetLogger(loggerRepository.Name, "NSmartProxyFTP");
if (!loggerRepository.Configured) throw new Exception("log config failed.");
Console.ForegroundColor = ConsoleColor.Yellow;
//用户登录
if (args.Length == 4)
{
_currentLoginInfo = new LoginInfo();
_currentLoginInfo.UserName = args[1];
_currentLoginInfo.UserPwd = args[3];
}
Logger.Info($"*** {NSPVersion.NSmartProxyServerName} ***");
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
//start clientrouter.
try
{
StartClient().Wait();
}
catch (Exception e)
{
Logger.Error(e.Message);
}
Console.Read();
Logger.Info("Client terminated,press any key to continue.");
}
private static void StartFTP(ClientModel clientModel)
{
var ip = GetIP(Configuration.GetSection("ProviderAddress").Value);
Console.WriteLine("外网IP:" + ip.ToString());
var users = new List<UserElement>();
foreach (var u in Configuration.GetSection("FtpUsers").GetChildren())
{
users.Add(new UserElement() { username = u["username"], password = u["password"], rootDir = u["rootDir"] });
}
var server = new FtpServer.FtpServer(int.Parse(Configuration.GetSection("FtpPort").Value), int.Parse(Configuration.GetSection("PasvPort").Value), int.Parse(Configuration.GetSection("FtpMaxConnect").Value), users);
server.Start(ip, clientModel.AppList[1].Port);
}
private static IPAddress GetIP(string server)
{
Regex rx = new Regex(@"((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))");
if (!rx.IsMatch(server))
{
IPAddress[] ips = Dns.GetHostAddresses(server);
return ips[0];
}
return IPAddress.Parse(server);
}
private static async Task StartClient()
{
Router clientRouter = new Router(new Log4netLogger());
//read config from config file.
SetConfig(clientRouter);
if (_currentLoginInfo != null)
{
clientRouter.SetLoginInfo(_currentLoginInfo);
}
Task tsk = clientRouter.Start(true, StartFTP);
try
{
await tsk;
}
catch (Exception e)
{
Logger.Error(e);
throw;
}
}
private static void SetConfig(Router clientRouter)
{
NSPClientConfig config = new NSPClientConfig();
config.ProviderAddress = Configuration.GetSection("ProviderAddress").Value;
config.ProviderWebPort = int.Parse(Configuration.GetSection("ProviderWebPort").Value);
var configClients = Configuration.GetSection("Clients").GetChildren();
foreach (var cli in configClients)
{
int confConsumerPort = 0;
if (cli["ConsumerPort"] != null) confConsumerPort = int.Parse(cli["ConsumerPort"]);
config.Clients.Add(new ClientApp
{
IP = cli["IP"],
TargetServicePort = int.Parse(cli["TargetServicePort"]),
ConsumerPort = confConsumerPort,
Host = cli["Host"],
Protocol = Enum.Parse<Protocol>((cli["Protocol"] ?? "TCP").ToUpper()),
Description = cli["Description"]
});
}
clientRouter.SetConfiguration(config);
}
}
}