mirror of
https://github.com/FastTunnel/FastTunnel.git
synced 2025-02-08 02:39:29 +08:00
新增基于FastTunnel的SuiDao程序
This commit is contained in:
parent
acdfe23cb3
commit
f54de4bda7
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -7,3 +7,6 @@ obj
|
|||
/FastTunnel.Server/Properties/PublishProfiles/*.user
|
||||
/FastTunnel.Client/Properties/PublishProfiles/*.pubxml
|
||||
publish
|
||||
/SuiDao.Client/Properties/PublishProfiles/*.user
|
||||
/SuiDao.Client/*.user
|
||||
/SuiDao.Client/Properties/PublishProfiles/FolderProfile.pubxml
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using FastTunnel.Core.Client;
|
||||
using FastTunnel.Core.Config;
|
||||
using FastTunnel.Core.Config;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
fileName="${basedir}/Logs/${shortdate}.${level}.log" />
|
||||
|
||||
<target xsi:type="Console" name="console"
|
||||
layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" />
|
||||
layout="${date}|${level:uppercase=true}|${message} ${exception} ${all-event-properties}" />
|
||||
</targets>
|
||||
|
||||
<rules>
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using FastTunnel.Core;
|
||||
using FastTunnel.Core.Client;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
|
@ -12,6 +11,8 @@ using System.Threading.Tasks;
|
|||
using NLog;
|
||||
using FastTunnel.Core.Host;
|
||||
using FastTunnel.Core.Config;
|
||||
using FastTunnel.Core.Core;
|
||||
using FastTunnel.Core.Models;
|
||||
|
||||
namespace FastTunnel.Client
|
||||
{
|
||||
|
@ -52,7 +53,36 @@ namespace FastTunnel.Client
|
|||
var client = servicesProvider.GetRequiredService<FastTunnelClient>();
|
||||
var config = servicesProvider.GetRequiredService<ClientConfig>();
|
||||
client.SetConfig(config);
|
||||
client.Login();
|
||||
|
||||
client.Login(() =>
|
||||
{
|
||||
Connecter _client;
|
||||
|
||||
try
|
||||
{
|
||||
// 连接到的目标IP
|
||||
_client = new Connecter(config.Common.ServerAddr, config.Common.ServerPort);
|
||||
_client.Connect();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Thread.Sleep(5000);
|
||||
throw;
|
||||
}
|
||||
|
||||
// 登录
|
||||
_client.Send(new Message<LogInRequest>
|
||||
{
|
||||
MessageType = MessageType.C_LogIn,
|
||||
Content = new LogInRequest
|
||||
{
|
||||
Webs = config.Webs,
|
||||
SSH = config.SSH
|
||||
}
|
||||
});
|
||||
|
||||
return _client;
|
||||
});
|
||||
|
||||
while (true)
|
||||
{
|
||||
|
|
|
@ -9,9 +9,9 @@
|
|||
"ClientSettings": {
|
||||
"Common": {
|
||||
// 服务端公网ip, 对应服务端配置文件的 BindAddr
|
||||
"ServerAddr": "45.132.12.57",
|
||||
//"ServerAddr": "45.132.12.57",
|
||||
|
||||
//"ServerAddr": "127.0.0.1",
|
||||
"ServerAddr": "127.0.0.1",
|
||||
|
||||
// 服务端通信端口,对应服务端配置文件的 BindPort
|
||||
"ServerPort": 1271
|
||||
|
|
|
@ -7,10 +7,17 @@ namespace FastTunnel.Core.Config
|
|||
{
|
||||
public class ClientConfig
|
||||
{
|
||||
public FastTunnelServer Common { get; set; }
|
||||
public SuiDaoServer Common { get; set; }
|
||||
|
||||
public IEnumerable<WebConfig> Webs { get; set; }
|
||||
|
||||
public IEnumerable<SSHConfig> SSH { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
public class SuiDaoServer
|
||||
{
|
||||
public string ServerAddr { get; set; }
|
||||
|
||||
public int ServerPort { get; set; }
|
||||
}
|
||||
}
|
|
@ -13,7 +13,7 @@ using System.Timers;
|
|||
using System.Threading;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FastTunnel.Core.Client
|
||||
namespace FastTunnel.Core.Core
|
||||
{
|
||||
public class FastTunnelClient
|
||||
{
|
||||
|
@ -26,6 +26,7 @@ namespace FastTunnel.Core.Client
|
|||
System.Timers.Timer timer_timeout;
|
||||
System.Timers.Timer timer_heart;
|
||||
|
||||
Func<Connecter> login;
|
||||
double heartInterval = 5000;
|
||||
DateTime lastHeart;
|
||||
Thread th;
|
||||
|
@ -69,7 +70,8 @@ namespace FastTunnel.Core.Client
|
|||
private void reConnect()
|
||||
{
|
||||
Close();
|
||||
Login();
|
||||
_client = login.Invoke();
|
||||
LogSuccess(_client.Socket);
|
||||
}
|
||||
|
||||
private void HeartElapsed(object sender, ElapsedEventArgs e)
|
||||
|
@ -84,6 +86,13 @@ namespace FastTunnel.Core.Client
|
|||
}
|
||||
}
|
||||
|
||||
public void Login(Func<Connecter> fun)
|
||||
{
|
||||
login = fun;
|
||||
_client = login.Invoke();
|
||||
LogSuccess(_client.Socket);
|
||||
}
|
||||
|
||||
void Close()
|
||||
{
|
||||
timer_heart.Stop();
|
||||
|
@ -112,37 +121,16 @@ namespace FastTunnel.Core.Client
|
|||
_clientConfig = config;
|
||||
}
|
||||
|
||||
public void Login()
|
||||
private void LogSuccess(Socket socket)
|
||||
{
|
||||
_logger.LogDebug("FastTunnel Client Start");
|
||||
_logger.LogDebug("登录中...");
|
||||
|
||||
//连接到的目标IP
|
||||
try
|
||||
{
|
||||
_client = new Connecter(_clientConfig.Common.ip, _clientConfig.Common.bind_port);
|
||||
_client.Connect();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
_client.Socket.Close();
|
||||
|
||||
Thread.Sleep(5000);
|
||||
Login();
|
||||
return;
|
||||
}
|
||||
|
||||
// 登录
|
||||
_client.Send(new Message<LogInRequest> { MessageType = MessageType.C_LogIn, Content = new LogInRequest { ClientConfig = _clientConfig } });
|
||||
_logger.LogDebug("登录成功");
|
||||
_logger.LogDebug("通信已建立");
|
||||
|
||||
// 心跳开始
|
||||
timer_heart.Start();
|
||||
timer_timeout.Start();
|
||||
|
||||
th = new Thread(ReceiveServer);
|
||||
th.Start(_client.Socket);
|
||||
th.Start(socket);
|
||||
}
|
||||
|
||||
private void ReceiveServer(object obj)
|
||||
|
@ -217,7 +205,7 @@ namespace FastTunnel.Core.Client
|
|||
break;
|
||||
case MessageType.S_NewCustomer:
|
||||
var request = (Msg.Content as JObject).ToObject<NewCustomerRequest>();
|
||||
var connecter = new Connecter(_clientConfig.Common.ip, _clientConfig.Common.bind_port);
|
||||
var connecter = new Connecter(_clientConfig.Common.ServerAddr, _clientConfig.Common.ServerPort);
|
||||
connecter.Connect();
|
||||
connecter.Send(new Message<string> { MessageType = MessageType.C_SwapMsg, Content = request.MsgId });
|
||||
|
||||
|
@ -228,7 +216,7 @@ namespace FastTunnel.Core.Client
|
|||
break;
|
||||
case MessageType.S_NewSSH:
|
||||
var request_ssh = (Msg.Content as JObject).ToObject<NewSSHRequest>();
|
||||
var connecter_ssh = new Connecter(_clientConfig.Common.ip, _clientConfig.Common.bind_port);
|
||||
var connecter_ssh = new Connecter(_clientConfig.Common.ServerAddr, _clientConfig.Common.ServerPort);
|
||||
connecter_ssh.Connect();
|
||||
connecter_ssh.Send(new Message<string> { MessageType = MessageType.C_SwapMsg, Content = request_ssh.MsgId });
|
||||
|
||||
|
@ -239,15 +227,15 @@ namespace FastTunnel.Core.Client
|
|||
break;
|
||||
case MessageType.Info:
|
||||
var info = Msg.Content.ToJson();
|
||||
_logger.LogInformation(info);
|
||||
_logger.LogInformation("From Server:" + info);
|
||||
break;
|
||||
case MessageType.LogDebug:
|
||||
var LogDebug = Msg.Content.ToJson();
|
||||
_logger.LogDebug(LogDebug);
|
||||
_logger.LogDebug("From Server:" + LogDebug);
|
||||
break;
|
||||
case MessageType.Error:
|
||||
var err = Msg.Content.ToJson();
|
||||
_logger.LogError(err);
|
||||
_logger.LogError("From Server:" + err);
|
||||
break;
|
||||
case MessageType.C_SwapMsg:
|
||||
case MessageType.C_LogIn:
|
|
@ -12,8 +12,9 @@ using System.Text;
|
|||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using FastTunnel.Core.Handlers;
|
||||
|
||||
namespace FastTunnel.Core.Server
|
||||
namespace FastTunnel.Core.Core
|
||||
{
|
||||
public class FastTunnelServer
|
||||
{
|
||||
|
@ -21,13 +22,15 @@ namespace FastTunnel.Core.Server
|
|||
Dictionary<int, SSHInfo<SSHHandlerArg>> SSHList = new Dictionary<int, SSHInfo<SSHHandlerArg>>();
|
||||
Dictionary<string, NewRequest> newRequest = new Dictionary<string, NewRequest>();
|
||||
|
||||
private ServerConfig serverSettings;
|
||||
private ServerConfig _serverSettings;
|
||||
ILogger<FastTunnelServer> _logger;
|
||||
ILoginHandler _loginHandler;
|
||||
|
||||
public FastTunnelServer(ServerConfig serverSettings, ILogger<FastTunnelServer> logger)
|
||||
public FastTunnelServer(ServerConfig settings, ILogger<FastTunnelServer> logger, ILoginHandler loginHandler)
|
||||
{
|
||||
_serverSettings = settings;
|
||||
_logger = logger;
|
||||
this.serverSettings = serverSettings;
|
||||
_loginHandler = loginHandler;
|
||||
}
|
||||
|
||||
public void Run()
|
||||
|
@ -39,17 +42,17 @@ namespace FastTunnel.Core.Server
|
|||
|
||||
private void ListenFastTunnelClient()
|
||||
{
|
||||
var listener = new Listener<object>(serverSettings.BindAddr, serverSettings.BindPort, _logger, ReceiveClient, null);
|
||||
var listener = new Listener<object>(_serverSettings.BindAddr, _serverSettings.BindPort, _logger, ReceiveClient, null);
|
||||
listener.Listen();
|
||||
_logger.LogDebug($"监听客户端 -> {serverSettings.BindAddr}:{serverSettings.BindPort}");
|
||||
_logger.LogDebug($"监听客户端 -> {_serverSettings.BindAddr}:{_serverSettings.BindPort}");
|
||||
}
|
||||
|
||||
private void ListenCustomer()
|
||||
{
|
||||
var listener = new Listener<object>(serverSettings.BindAddr, serverSettings.ProxyPort_HTTP, _logger, ReceiveCustomer, null);
|
||||
var listener = new Listener<object>(_serverSettings.BindAddr, _serverSettings.ProxyPort_HTTP, _logger, ReceiveCustomer, null);
|
||||
listener.Listen();
|
||||
|
||||
_logger.LogDebug($"监听HTTP -> {serverSettings.BindAddr}:{serverSettings.ProxyPort_HTTP}");
|
||||
_logger.LogDebug($"监听HTTP -> {_serverSettings.BindAddr}:{_serverSettings.ProxyPort_HTTP}");
|
||||
}
|
||||
|
||||
//接收消息
|
||||
|
@ -185,7 +188,9 @@ namespace FastTunnel.Core.Server
|
|||
{
|
||||
_logger.LogError(ex);
|
||||
_logger.LogError($"收到客户端 words:{words}");
|
||||
throw;
|
||||
|
||||
// throw;
|
||||
client.Send(new Message<string>() { MessageType = MessageType.Error, Content = ex.Message });
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -209,17 +214,17 @@ namespace FastTunnel.Core.Server
|
|||
|
||||
private void handle(string words, Socket client)
|
||||
{
|
||||
Message<object> msg = JsonConvert.DeserializeObject<Message<object>>(words);
|
||||
Message<JObject> msg = JsonConvert.DeserializeObject<Message<JObject>>(words);
|
||||
HandleMsg(client, msg);
|
||||
}
|
||||
|
||||
private void HandleMsg(Socket client, Message<object> msg)
|
||||
private void HandleMsg(Socket client, Message<JObject> msg)
|
||||
{
|
||||
_logger.LogDebug($"收到客户端指令:{msg.MessageType}");
|
||||
switch (msg.MessageType)
|
||||
{
|
||||
case MessageType.C_LogIn:
|
||||
HandleLogin(client, msg);
|
||||
HandleLogin(client, _loginHandler.GetConfig(msg.Content));
|
||||
|
||||
// 递归调用
|
||||
ReceiveClient(client, null);
|
||||
|
@ -231,7 +236,7 @@ namespace FastTunnel.Core.Server
|
|||
ReceiveClient(client, null);
|
||||
break;
|
||||
case MessageType.C_SwapMsg:
|
||||
var msgId = (msg.Content as string);
|
||||
var msgId = msg.Content.ToObject<string>();
|
||||
NewRequest request;
|
||||
|
||||
if (!string.IsNullOrEmpty(msgId) && newRequest.TryGetValue(msgId, out request))
|
||||
|
@ -258,14 +263,13 @@ namespace FastTunnel.Core.Server
|
|||
}
|
||||
}
|
||||
|
||||
private void HandleLogin(Socket client, Message<object> msg)
|
||||
private void HandleLogin(Socket client, LogInRequest requet)
|
||||
{
|
||||
var requet = (msg.Content as JObject).ToObject<LogInRequest>();
|
||||
if (requet.ClientConfig.Webs != null && requet.ClientConfig.Webs.Count() > 0)
|
||||
if (requet.Webs != null && requet.Webs.Count() > 0)
|
||||
{
|
||||
foreach (var item in requet.ClientConfig.Webs)
|
||||
foreach (var item in requet.Webs)
|
||||
{
|
||||
var hostName = $"{item.SubDomain}.{serverSettings.Domain}".Trim();
|
||||
var hostName = $"{item.SubDomain}.{_serverSettings.Domain}".Trim();
|
||||
if (WebList.ContainsKey(hostName))
|
||||
{
|
||||
_logger.LogDebug($"renew domain '{hostName}'");
|
||||
|
@ -279,23 +283,25 @@ namespace FastTunnel.Core.Server
|
|||
WebList.Add(hostName, new WebInfo { Socket = client, WebConfig = item });
|
||||
}
|
||||
|
||||
client.Send(new Message<string> { MessageType = MessageType.Info, Content = $"TunnelForWeb is OK: you can visit {item.LocalIp}:{item.LocalPort} from http://{hostName}:{serverSettings.ProxyPort_HTTP}" });
|
||||
client.Send(new Message<string> { MessageType = MessageType.Info, Content = $"TunnelForWeb is OK: you can visit {item.LocalIp}:{item.LocalPort} from http://{hostName}:{_serverSettings.ProxyPort_HTTP}" });
|
||||
}
|
||||
|
||||
client.Send(new Message<string> { MessageType = MessageType.Info, Content = "web隧道已建立完毕" });
|
||||
}
|
||||
|
||||
if (requet.ClientConfig.SSH != null && requet.ClientConfig.SSH.Count() > 0)
|
||||
if (requet.SSH != null && requet.SSH.Count() > 0)
|
||||
{
|
||||
foreach (var item in requet.ClientConfig.SSH)
|
||||
foreach (var item in requet.SSH)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (item.RemotePort.Equals(serverSettings.BindPort))
|
||||
if (item.RemotePort.Equals(_serverSettings.BindPort))
|
||||
{
|
||||
_logger.LogError($"RemotePort can not be same with BindPort: {item.RemotePort}");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (item.RemotePort.Equals(serverSettings.ProxyPort_HTTP))
|
||||
if (item.RemotePort.Equals(_serverSettings.ProxyPort_HTTP))
|
||||
{
|
||||
_logger.LogError($"RemotePort can not be same with ProxyPort_HTTP: {item.RemotePort}");
|
||||
continue;
|
||||
|
@ -316,7 +322,7 @@ namespace FastTunnel.Core.Server
|
|||
SSHList.Add(item.RemotePort, new SSHInfo<SSHHandlerArg> { Listener = ls, Socket = client, SSHConfig = item });
|
||||
_logger.LogDebug($"SSH proxy success: {item.RemotePort} -> {item.LocalIp}:{item.LocalPort}");
|
||||
|
||||
client.Send(new Message<string> { MessageType = MessageType.Info, Content = $"TunnelForSSH is OK: {requet.ClientConfig.Common.ip}:{item.RemotePort}->{item.LocalIp}:{item.LocalPort}" });
|
||||
client.Send(new Message<string> { MessageType = MessageType.Info, Content = $"TunnelForSSH is OK: [ServerAddr]:{item.RemotePort}->{item.LocalIp}:{item.LocalPort}" });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -326,6 +332,8 @@ namespace FastTunnel.Core.Server
|
|||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
client.Send(new Message<string> { MessageType = MessageType.Info, Content = "远程桌面隧道已建立完毕" });
|
||||
}
|
||||
}
|
||||
|
14
FastTunnel.Core/Exceptions/APIErrorException.cs
Normal file
14
FastTunnel.Core/Exceptions/APIErrorException.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace FastTunnel.Core.Exceptions
|
||||
{
|
||||
public class APIErrorException : Exception
|
||||
{
|
||||
public APIErrorException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
13
FastTunnel.Core/Handlers/ILoginHandler.cs
Normal file
13
FastTunnel.Core/Handlers/ILoginHandler.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using FastTunnel.Core.Models;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace FastTunnel.Core.Handlers
|
||||
{
|
||||
public interface ILoginHandler
|
||||
{
|
||||
LogInRequest GetConfig(JObject content);
|
||||
}
|
||||
}
|
16
FastTunnel.Core/Handlers/LoginHandler.cs
Normal file
16
FastTunnel.Core/Handlers/LoginHandler.cs
Normal file
|
@ -0,0 +1,16 @@
|
|||
using FastTunnel.Core.Models;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace FastTunnel.Core.Handlers
|
||||
{
|
||||
public class LoginHandler : ILoginHandler
|
||||
{
|
||||
public LogInRequest GetConfig(JObject content)
|
||||
{
|
||||
return content.ToObject<LogInRequest>();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
using FastTunnel.Core.Config;
|
||||
using FastTunnel.Core.Server;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
|
11
FastTunnel.Core/Models/LogInByKeyRequest.cs
Normal file
11
FastTunnel.Core/Models/LogInByKeyRequest.cs
Normal file
|
@ -0,0 +1,11 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace FastTunnel.Core.Models
|
||||
{
|
||||
public class LogInByKeyRequest
|
||||
{
|
||||
public string key { get; set; }
|
||||
}
|
||||
}
|
|
@ -7,6 +7,8 @@ namespace FastTunnel.Core.Models
|
|||
{
|
||||
public class LogInRequest
|
||||
{
|
||||
public ClientConfig ClientConfig { get; set; }
|
||||
public IEnumerable<WebConfig> Webs { get; set; }
|
||||
|
||||
public IEnumerable<SSHConfig> SSH { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,6 +27,6 @@ namespace FastTunnel.Core.Models
|
|||
|
||||
LogDebug,
|
||||
|
||||
Error
|
||||
Error,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
using FastTunnel.Core.Client;
|
||||
using FastTunnel.Core.Config;
|
||||
using FastTunnel.Core.Server;
|
||||
using FastTunnel.Core.Config;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
fileName="${basedir}/Logs/${shortdate}.${level}.log" />
|
||||
|
||||
<target xsi:type="Console" name="console"
|
||||
layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" />
|
||||
layout="${date}|${level:uppercase=true}|${message} ${exception}| ${all-event-properties}" />
|
||||
</targets>
|
||||
|
||||
<rules>
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using FastTunnel.Core;
|
||||
using FastTunnel.Core.Server;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
@ -16,6 +15,7 @@ using Microsoft.Extensions.Logging;
|
|||
using NLog;
|
||||
using FastTunnel.Core.Config;
|
||||
using FastTunnel.Core.Host;
|
||||
using FastTunnel.Core.Core;
|
||||
|
||||
namespace FastTunnel.Server
|
||||
{
|
||||
|
|
|
@ -9,7 +9,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastTunnel.Core", "FastTunn
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastTunnel.Server", "FastTunnel.Server\FastTunnel.Server.csproj", "{DEF2E322-9075-4C3F-9967-7EAF0EE28CEB}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SuiDao.Client", "SuiDao.Client\SuiDao.Client.csproj", "{23E6C767-3927-4281-9C1D-C0A724D869A9}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SuiDao.Client", "SuiDao.Client\SuiDao.Client.csproj", "{23E6C767-3927-4281-9C1D-C0A724D869A9}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SuiDao.Server", "SuiDao.Server\SuiDao.Server.csproj", "{9DA66955-1497-42BA-B345-3C4D2F3E37A9}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "suidao.io", "suidao.io", "{024D84DC-BA2E-4CBC-8D9B-0187C0C09AE1}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FastTunnel", "FastTunnel", "{051B4BB3-621D-4386-BA67-BB8896FDB29E}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
|
@ -33,10 +39,21 @@ Global
|
|||
{23E6C767-3927-4281-9C1D-C0A724D869A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{23E6C767-3927-4281-9C1D-C0A724D869A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{23E6C767-3927-4281-9C1D-C0A724D869A9}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{9DA66955-1497-42BA-B345-3C4D2F3E37A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{9DA66955-1497-42BA-B345-3C4D2F3E37A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9DA66955-1497-42BA-B345-3C4D2F3E37A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9DA66955-1497-42BA-B345-3C4D2F3E37A9}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{29DA74AE-FFBD-4A24-82B9-B5675593B63A} = {051B4BB3-621D-4386-BA67-BB8896FDB29E}
|
||||
{C8ADFEB1-59DB-4CE3-8D04-5B547107BCCB} = {051B4BB3-621D-4386-BA67-BB8896FDB29E}
|
||||
{DEF2E322-9075-4C3F-9967-7EAF0EE28CEB} = {051B4BB3-621D-4386-BA67-BB8896FDB29E}
|
||||
{23E6C767-3927-4281-9C1D-C0A724D869A9} = {024D84DC-BA2E-4CBC-8D9B-0187C0C09AE1}
|
||||
{9DA66955-1497-42BA-B345-3C4D2F3E37A9} = {024D84DC-BA2E-4CBC-8D9B-0187C0C09AE1}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {3D9C6B44-6706-4EE8-9043-802BBE474A2E}
|
||||
EndGlobalSection
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace FastTunnel.Core.Models
|
||||
namespace SuiDao.Client.Models
|
||||
{
|
||||
public class FastTunnelServer
|
||||
public class SuiDaoServerConfig
|
||||
{
|
||||
public string ip { get; set; }
|
||||
|
|
@ -8,7 +8,7 @@
|
|||
fileName="${basedir}/Logs/${shortdate}.${level}.log" />
|
||||
|
||||
<target xsi:type="Console" name="console"
|
||||
layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" />
|
||||
layout="${date}|${level:uppercase=true}|${message} ${exception} ${all-event-properties}" />
|
||||
</targets>
|
||||
|
||||
<rules>
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
using FastTunnel.Core;
|
||||
using FastTunnel.Core.Client;
|
||||
using FastTunnel.Core.Config;
|
||||
using FastTunnel.Core.Core;
|
||||
using FastTunnel.Core.Host;
|
||||
using FastTunnel.Core.Models;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NLog;
|
||||
using SuiDao.Client.Models;
|
||||
using System;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
|
@ -14,6 +16,10 @@ namespace SuiDao.Client
|
|||
{
|
||||
class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// suidao.io 客户端
|
||||
/// </summary>
|
||||
/// <param name="args"></param>
|
||||
static void Main(string[] args)
|
||||
{
|
||||
LogManager.LoadConfiguration("Nlog.config");
|
||||
|
@ -65,45 +71,42 @@ namespace SuiDao.Client
|
|||
|
||||
private static void Run(IServiceProvider servicesProvider, ILogger _logger, string key)
|
||||
{
|
||||
var client = servicesProvider.GetRequiredService<FastTunnelClient>();
|
||||
|
||||
// https://localhost:5002
|
||||
var res = HttpHelper.PostAsJson("https://api1.suidao.io/api/Client/GetServerByKey", $"{{ \"key\":\"{key}\"}}").Result;
|
||||
var jobj = JObject.Parse(res);
|
||||
if ((bool)jobj["success"] == true)
|
||||
{
|
||||
var server = jobj["data"].ToObject<FastTunnelServer>();
|
||||
var client = servicesProvider.GetRequiredService<FastTunnelClient>();
|
||||
client.Login(() =>
|
||||
{
|
||||
var server = jobj["data"].ToObject<SuiDaoServerConfig>();
|
||||
Connecter _client = null;
|
||||
|
||||
try
|
||||
{
|
||||
_client = new Connecter(server.ip, server.bind_port);
|
||||
|
||||
_client.Connect();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error(ex.Message);
|
||||
_client.Socket.Close();
|
||||
throw;
|
||||
}
|
||||
|
||||
// 登录
|
||||
_client.Send(new Message<LogInByKeyRequest> { MessageType = MessageType.C_LogIn, Content = new LogInByKeyRequest { key = key } });
|
||||
|
||||
return _client;
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Console.WriteLine(jobj["errorMsg"].ToString());
|
||||
}
|
||||
|
||||
//client.Login(() =>
|
||||
//{
|
||||
// //连接到的目标IP
|
||||
// Connecter _client = null;
|
||||
|
||||
// try
|
||||
// {
|
||||
// _client = new Connecter(config.Common.ServerAddr, config.Common.ServerPort);
|
||||
// _client.Connect();
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// _logger.Error(ex.Message);
|
||||
// _client.Socket.Close();
|
||||
|
||||
// Thread.Sleep(5000);
|
||||
// Login();
|
||||
// return;
|
||||
// }
|
||||
|
||||
// // 登录
|
||||
// _client.Send(new Message<LogInRequest> { MessageType = MessageType.LOGIN_ERVERYONE, Content = new LogInRequest { ClientConfig = config } });
|
||||
|
||||
//});
|
||||
}
|
||||
|
||||
private static void Config(ServiceCollection service)
|
||||
|
|
59
SuiDao.Server/Handlers/SuiDaoLoginHandler.cs
Normal file
59
SuiDao.Server/Handlers/SuiDaoLoginHandler.cs
Normal file
|
@ -0,0 +1,59 @@
|
|||
using FastTunnel.Core.Exceptions;
|
||||
using FastTunnel.Core.Handlers;
|
||||
using FastTunnel.Core.Models;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SuiDao.Client;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SuiDao.Server
|
||||
{
|
||||
public class SuiDaoLoginHandler : ILoginHandler
|
||||
{
|
||||
public LogInRequest GetConfig(JObject content)
|
||||
{
|
||||
var key = content["key"].ToString();
|
||||
var res = HttpHelper.PostAsJson("https://api1.suidao.io/api/Client/GetTunnelByKey", $"{{ \"key\":\"{key}\"}}").Result;
|
||||
var jobj = JObject.Parse(res);
|
||||
if ((bool)jobj["success"] == true)
|
||||
{
|
||||
var tunnels = jobj["data"].ToObject<IEnumerable<Tunnel>>();
|
||||
var Webs = new List<WebConfig>();
|
||||
var SSH = new List<SSHConfig>();
|
||||
|
||||
foreach (var tunnel in tunnels)
|
||||
{
|
||||
if (tunnel.app_type == 1) // web
|
||||
{
|
||||
Webs.Add(new WebConfig
|
||||
{
|
||||
LocalIp = tunnel.local_ip,
|
||||
LocalPort = tunnel.local_port,
|
||||
SubDomain = tunnel.sub_domain
|
||||
});
|
||||
}
|
||||
else if (tunnel.app_type == 2)
|
||||
{
|
||||
SSH.Add(new SSHConfig
|
||||
{
|
||||
LocalIp = tunnel.local_ip,
|
||||
LocalPort = tunnel.local_port,
|
||||
RemotePort = tunnel.remote_port,
|
||||
Protocol = Protocol.TCP
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return new LogInRequest
|
||||
{
|
||||
SSH = SSH,
|
||||
Webs = Webs,
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new APIErrorException(jobj["errorMsg"].ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
13
SuiDao.Server/Model/Appsettings.cs
Normal file
13
SuiDao.Server/Model/Appsettings.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using FastTunnel.Core.Config;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SuiDao.Server
|
||||
{
|
||||
public class Appsettings
|
||||
{
|
||||
public ServerConfig ServerSettings { get; set; }
|
||||
}
|
||||
}
|
24
SuiDao.Server/Model/Tunnel.cs
Normal file
24
SuiDao.Server/Model/Tunnel.cs
Normal file
|
@ -0,0 +1,24 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SuiDao.Server
|
||||
{
|
||||
public class Tunnel
|
||||
{
|
||||
public int app_type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 用户别名
|
||||
/// </summary>
|
||||
public string name { get; set; }
|
||||
|
||||
public string sub_domain { get; set; }
|
||||
|
||||
public string local_ip { get; set; }
|
||||
|
||||
public int local_port { get; set; }
|
||||
|
||||
public int remote_port { get; set; }
|
||||
}
|
||||
}
|
17
SuiDao.Server/Nlog.config
Normal file
17
SuiDao.Server/Nlog.config
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" ?>
|
||||
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
|
||||
<targets>
|
||||
<target xsi:type="File" name="file"
|
||||
layout="${longdate} ${logger} ${message}${exception:format=ToString}"
|
||||
fileName="${basedir}/Logs/${shortdate}.${level}.log" />
|
||||
|
||||
<target xsi:type="Console" name="console"
|
||||
layout="${date}|${level:uppercase=true}|${message} ${exception} ${all-event-properties}" />
|
||||
</targets>
|
||||
|
||||
<rules>
|
||||
<logger name="*" minlevel="Debug" writeTo="file,console" />
|
||||
</rules>
|
||||
</nlog>
|
75
SuiDao.Server/Program.cs
Normal file
75
SuiDao.Server/Program.cs
Normal file
|
@ -0,0 +1,75 @@
|
|||
using FastTunnel.Core.Config;
|
||||
using FastTunnel.Core.Core;
|
||||
using FastTunnel.Core.Handlers;
|
||||
using FastTunnel.Core.Host;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NLog;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
|
||||
namespace SuiDao.Server
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
static Appsettings appsettings;
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
LogManager.LoadConfiguration("Nlog.config");
|
||||
var logger = LogManager.GetCurrentClassLogger();
|
||||
logger.Debug("===== FastTunnel Server Start =====");
|
||||
|
||||
try
|
||||
{
|
||||
var servicesProvider = new Host().Config(Config).Build();
|
||||
Run(servicesProvider);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// NLog: catch any exception and log it.
|
||||
logger.Error(ex);
|
||||
Console.WriteLine(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
|
||||
LogManager.Shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
private static ServerConfig implementationFactory(IServiceProvider arg)
|
||||
{
|
||||
if (appsettings == null)
|
||||
{
|
||||
var conf = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", true, true)
|
||||
.Build();
|
||||
|
||||
appsettings = conf.Get<Appsettings>();
|
||||
}
|
||||
|
||||
return appsettings.ServerSettings;
|
||||
}
|
||||
|
||||
private static void Config(ServiceCollection service)
|
||||
{
|
||||
service.AddTransient<FastTunnelServer>()
|
||||
.AddSingleton<ServerConfig>(implementationFactory)
|
||||
.AddTransient<ILoginHandler, SuiDaoLoginHandler>();
|
||||
}
|
||||
|
||||
private static void Run(IServiceProvider servicesProvider)
|
||||
{
|
||||
var server = servicesProvider.GetRequiredService<FastTunnelServer>();
|
||||
server.Run();
|
||||
|
||||
while (true)
|
||||
{
|
||||
Thread.Sleep(10000 * 60);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
https://go.microsoft.com/fwlink/?LinkID=208121.
|
||||
-->
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<PublishProtocol>FileSystem</PublishProtocol>
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Any CPU</Platform>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<PublishDir>bin\Release\netcoreapp3.1\publish\</PublishDir>
|
||||
</PropertyGroup>
|
||||
</Project>
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
https://go.microsoft.com/fwlink/?LinkID=208121.
|
||||
-->
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
</Project>
|
25
SuiDao.Server/SuiDao.Server.csproj
Normal file
25
SuiDao.Server/SuiDao.Server.csproj
Normal file
|
@ -0,0 +1,25 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="..\SuiDao.Client\HttpHelper.cs" Link="HttpHelper.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\FastTunnel.Core\FastTunnel.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Nlog.config">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
6
SuiDao.Server/SuiDao.Server.csproj.user
Normal file
6
SuiDao.Server/SuiDao.Server.csproj.user
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<_LastSelectedProfileId>G:\GitHub\FastTunnel\SuiDao.Server\Properties\PublishProfiles\FolderProfile.pubxml</_LastSelectedProfileId>
|
||||
</PropertyGroup>
|
||||
</Project>
|
19
SuiDao.Server/appsettings.json
Normal file
19
SuiDao.Server/appsettings.json
Normal file
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"ServerSettings": {
|
||||
"BindAddr": "0.0.0.0",
|
||||
"BindPort": 2271,
|
||||
|
||||
// 自定义域名
|
||||
"Domain": "sd.suidao.io",
|
||||
|
||||
// 服务监听的端口号, 访问自定义域名站点时url为 http://{SubDomain}.{Domain}:{ProxyPort_HTTP}/
|
||||
"ProxyPort_HTTP": 2270
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user