forked from tmoonlight/NSmartProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSmartProxyClient.cs
More file actions
135 lines (119 loc) · 4.22 KB
/
NSmartProxyClient.cs
File metadata and controls
135 lines (119 loc) · 4.22 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
using log4net;
using Microsoft.Extensions.Configuration;
using NSmartProxy.Client;
using NSmartProxy.Data;
using NSmartProxy.Interfaces;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using log4net.Config;
using NSmartProxy.Data.Models;
using Exception = System.Exception;
using NSmartProxy.Shared;
namespace NSmartProxy
{
class NSmartProxyClient
{
#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"));
NSmartProxyClient.Logger = LogManager.GetLogger(loggerRepository.Name, "NSmartServerClient");
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($"*** {Global.NSmartProxyClientName} ***");
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 async Task StartClient()
{
Router clientRouter = new Router(new Log4netLogger());
//read config from config file.
SetConfig(clientRouter);// clientRouter.SetConifiguration();
if (_currentLoginInfo != null)
{
clientRouter.SetLoginInfo(_currentLoginInfo);
}
Task tsk = clientRouter.Start(true);
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.ProviderPort = int.Parse(Configuration.GetSection("ProviderPort").Value);
config.ProviderConfigPort = int.Parse(Configuration.GetSection("ProviderConfigPort").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
});
}
// Configuration.GetSection("1").
clientRouter.SetConfiguration(config);
}
}
}