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
117 lines (104 loc) · 3.46 KB
/
NSmartProxyClient.cs
File metadata and controls
117 lines (104 loc) · 3.46 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
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;
using Protocol = NSmartProxy.Data.Protocol;
using NSmartProxy.Infrastructure;
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;
private static readonly string ConfigFilePath = ConfigHelper.AppSettingFullPath;
public void Start(string[] args)
{
//appSettingFilePath = Directory.GetCurrentDirectory() + "/appsettings.json";
//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;
//用户登录 e.g.: ./NSmartProxyClient -u admin -p admin
if (args.Length == 4)
{
_currentLoginInfo = new LoginInfo();
_currentLoginInfo.UserName = args[1];
_currentLoginInfo.UserPwd = args[3];
}
Logger.Info($"*** {NSPVersion.NSmartProxyClientName} ***");
//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.
clientRouter.SetConfiguration(ConfigHelper.ReadAllConfig<NSPClientConfig>(ConfigFilePath));
if (_currentLoginInfo != null)
{
clientRouter.SetLoginInfo(_currentLoginInfo);
}
Task tsk = clientRouter.Start(true);
try
{
await tsk;
}
catch (Exception e)
{
Logger.Error(e);
throw;
}
}
public void Stop()
{
//
Console.WriteLine(NSPVersion.NSmartProxyServerName + " STOPPED.");
Environment.Exit(0);
}
}
}