Skip to content

Commit

Permalink
改了很多东西
Browse files Browse the repository at this point in the history
  • Loading branch information
snltty committed Oct 22, 2024
1 parent 47404bf commit d3fead6
Show file tree
Hide file tree
Showing 45 changed files with 293 additions and 1,255 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
release_name: v1.5.0.${{ steps.date.outputs.today }}
draft: false
prerelease: false
body: "1. 新增多分组管理,方便分组切换\r\n2. 增加分组密码,提高分组隔离安全性\r\n3. 优化配置同步,可自由选择同步哪些信息\r\n4. 自动分配虚拟网卡IP\r\n5. 线程池优化\r\n6. 一些UI优化\r\n7. 测试中,不要更新,不要更新,不要更新,请使用v1.4.9"
body: "1. 新增多分组管理,方便分组切换\r\n2. 增加分组密码,提高分组隔离安全性\r\n3. 优化配置同步,可自由选择同步哪些信息\r\n4. 自动分配虚拟网卡IP\r\n5. 线程池优化\r\n6. 一些UI优化\r\n7. 减少很多耦合,更方便自由组合各个组件\r\n8. 测试中,不要更新,不要更新,不要更新,请使用v1.4.9"
- name: upload-win-x86-oss
id: upload-win-x86-oss
uses: tvrcgo/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion linker.libs/api/ApiServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public async Task<ApiControllerResponseInfo> OnMessage(ApiControllerRequestInfo
{
return new ApiControllerResponseInfo
{
Content = "not exists this path",
Content = $"{model.Path} not exists",
RequestId = model.RequestId,
Path = model.Path,
Code = ApiControllerResponseCodes.NotFound
Expand Down
15 changes: 6 additions & 9 deletions linker.tunnel/TunnelTransfer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ public async Task<ITunnelConnection> ConnectAsync(string remoteMachineId, string
continue;
}

foreach (var wanPortProtocol in tunnelAdapter.GetTunnelWanPortProtocols().Where(c => c.Disabled == false && string.IsNullOrWhiteSpace(c.Host) == false))
foreach (var wanPortProtocol in compactTransfer.Protocols)
{

//这个打洞协议不支持这个外网端口协议
if ((transport.AllowWanPortProtocolType & wanPortProtocol.ProtocolType) != wanPortProtocol.ProtocolType)
if ((transport.AllowWanPortProtocolType & wanPortProtocol) != wanPortProtocol)
{
continue;
}
Expand All @@ -172,8 +172,7 @@ public async Task<ITunnelConnection> ConnectAsync(string remoteMachineId, string
Task<TunnelTransportWanPortInfo> remoteInfo = tunnelAdapter.GetRemoteWanPort(new TunnelWanPortProtocolInfo
{
MachineId = remoteMachineId,
ProtocolType = wanPortProtocol.ProtocolType,
Type = wanPortProtocol.Type,
ProtocolType = wanPortProtocol
});
await Task.WhenAll(localInfo, remoteInfo).ConfigureAwait(false);

Expand Down Expand Up @@ -305,18 +304,16 @@ public void OnSuccess(TunnelTransportInfo tunnelTransportInfo)
/// <returns></returns>
public async Task<TunnelTransportWanPortInfo> GetWanPort(TunnelWanPortProtocolInfo _info)
{
TunnelWanPortInfo info = tunnelAdapter.GetTunnelWanPortProtocols().FirstOrDefault(c => c.Type == _info.Type && c.ProtocolType == _info.ProtocolType);
if (info == null) return null;
return await GetLocalInfo(info).ConfigureAwait(false);
return await GetLocalInfo(_info.ProtocolType).ConfigureAwait(false);
}

/// <summary>
/// 获取自己的外网IP
/// </summary>
/// <returns></returns>
private async Task<TunnelTransportWanPortInfo> GetLocalInfo(TunnelWanPortInfo info)
private async Task<TunnelTransportWanPortInfo> GetLocalInfo(TunnelWanPortProtocolType tunnelWanPortProtocolType)
{
TunnelWanPortEndPoint ip = await compactTransfer.GetWanPortAsync(tunnelAdapter.LocalIP, info).ConfigureAwait(false);
TunnelWanPortEndPoint ip = await compactTransfer.GetWanPortAsync(tunnelAdapter.LocalIP, tunnelWanPortProtocolType).ConfigureAwait(false);
if (ip != null)
{
MapInfo portMapInfo = TunnelUpnpTransfer.PortMap ?? new MapInfo { PrivatePort = 0, PublicPort = 0 };
Expand Down
19 changes: 5 additions & 14 deletions linker.tunnel/adapter/ITunnelAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,16 @@ public interface ITunnelAdapter
public IPAddress LocalIP { get; }

/// <summary>
/// ssl加密证书,没有证书则无法加密通信
/// 服务器地址
/// </summary>
public X509Certificate2 Certificate { get; }
public string ServerHost { get; }

/// <summary>
/// 获取外网端口协议列表
/// </summary>
/// <returns></returns>
public List<TunnelWanPortInfo> GetTunnelWanPortProtocols();
/// <summary>
/// 保存外网端口协议列表
/// ssl加密证书,没有证书则无法加密通信
/// </summary>
/// <param name="compacts"></param>
public void SetTunnelWanPortProtocols(List<TunnelWanPortInfo> protocols, bool updateVersion);
public X509Certificate2 Certificate { get; }


/// <summary>
/// 获取打洞协议列表
/// </summary>
Expand Down Expand Up @@ -96,10 +91,6 @@ public sealed class PortMapInfo

public sealed class TunnelWanPortProtocolInfo
{
/// <summary>
/// 类别
/// </summary>
public TunnelWanPortType Type { get; set; }
/// <summary>
/// 协议
/// </summary>
Expand Down
56 changes: 0 additions & 56 deletions linker.tunnel/wanport/ITunnelWanPortProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace linker.tunnel.wanport
public interface ITunnelWanPortProtocol
{
public string Name { get; }
public TunnelWanPortType Type { get; }
public TunnelWanPortProtocolType ProtocolType { get; }
/// <summary>
/// 获取外网端口
Expand All @@ -31,65 +30,10 @@ public sealed class TunnelWanPortEndPoint
public IPEndPoint Remote { get; set; }
}

public sealed partial class TunnelWanPortInfo
{
public TunnelWanPortInfo() { }
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; } = string.Empty;
/// <summary>
/// 协议类别
/// </summary>
public TunnelWanPortType Type { get; set; }
/// <summary>
/// 协议
/// </summary>
public TunnelWanPortProtocolType ProtocolType { get; set; } = TunnelWanPortProtocolType.Udp;

/// <summary>
/// 地址
/// </summary>
public string Host { get; set; } = string.Empty;
/// <summary>
/// 是否禁用
/// </summary>
public bool Disabled { get; set; }
}

public enum TunnelWanPortType : byte
{
Linker = 0,
Stun = 1,
Other = 255,
}

[Flags]
public enum TunnelWanPortProtocolType : byte
{
Tcp = 1,
Udp = 2,
}

public sealed class TunnelWanPortTypeInfo
{
public TunnelWanPortType Value { get; set; }
public string Name { get; set; }
public Dictionary<int, string> Protocols { get; set; } = new Dictionary<int, string>();
}

public sealed class TunnelWanPortTypeInfoEqualityComparer : IEqualityComparer<TunnelWanPortTypeInfo>
{
public bool Equals(TunnelWanPortTypeInfo x, TunnelWanPortTypeInfo y)
{
return x.Value == y.Value;
}

public int GetHashCode(TunnelWanPortTypeInfo obj)
{
return obj.Value.GetHashCode();
}
}


}
2 changes: 0 additions & 2 deletions linker.tunnel/wanport/TunnelWanPortProtocolLinker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace linker.tunnel.wanport
public sealed class TunnelWanPortProtocolLinkerUdp : ITunnelWanPortProtocol
{
public string Name => "Linker Udp";
public TunnelWanPortType Type => TunnelWanPortType.Linker;

public TunnelWanPortProtocolType ProtocolType => TunnelWanPortProtocolType.Udp;

Expand Down Expand Up @@ -73,7 +72,6 @@ public async Task<TunnelWanPortEndPoint> GetAsync(IPAddress localIP, IPEndPoint
public sealed class TunnelWanPortProtocolLinkerTcp : ITunnelWanPortProtocol
{
public string Name => "Linker Tcp";
public TunnelWanPortType Type => TunnelWanPortType.Linker;

public TunnelWanPortProtocolType ProtocolType => TunnelWanPortProtocolType.Tcp;

Expand Down
1 change: 0 additions & 1 deletion linker.tunnel/wanport/TunnelWanPortProtocolStun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace linker.tunnel.wanport
public sealed class TunnelWanPortProtocolStun : ITunnelWanPortProtocol
{
public string Name => "Stun Udp";
public TunnelWanPortType Type => TunnelWanPortType.Stun;

public TunnelWanPortProtocolType ProtocolType => TunnelWanPortProtocolType.Udp;

Expand Down
34 changes: 14 additions & 20 deletions linker.tunnel/wanport/TunnelWanPortTransfer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using linker.libs;
using linker.tunnel.adapter;
using System.Net;

namespace linker.tunnel.wanport
Expand All @@ -8,13 +9,18 @@ namespace linker.tunnel.wanport
/// </summary>
public sealed class TunnelWanPortTransfer
{
private List<ITunnelWanPortProtocol> tunnelWanPorts;
private List<ITunnelWanPortProtocol> tunnelWanPorts = new List<ITunnelWanPortProtocol>();

public TunnelWanPortTransfer()
{
public List<TunnelWanPortProtocolType> Protocols => tunnelWanPorts.Select(p => p.ProtocolType).ToList();

private readonly ITunnelAdapter tunnelAdapter;
public TunnelWanPortTransfer(ITunnelAdapter tunnelAdapter)
{
this.tunnelAdapter = tunnelAdapter;
}



/// <summary>
/// 加载所有外网端口协议
/// </summary>
Expand All @@ -25,35 +31,23 @@ public void LoadTransports(List<ITunnelWanPortProtocol> tunnelWanPorts)
LoggerHelper.Instance.Info($"load tunnel wanport compacts:{string.Join(",", tunnelWanPorts.Select(c => c.Name))}");
}

public List<TunnelWanPortTypeInfo> GetTypes()
{
List<TunnelWanPortTypeInfo> res = tunnelWanPorts.Select(c => new TunnelWanPortTypeInfo
{
Value = c.Type,
Name = c.Type.ToString(),
Protocols = tunnelWanPorts
.Where(d => d.Type == c.Type).ToList().Select(d => d.ProtocolType).Distinct().ToDictionary(c => (int)c, d => d.ToString()),
}).Distinct(new TunnelWanPortTypeInfoEqualityComparer()).ToList();
return res;
}

/// <summary>
/// 获取外网端口
/// </summary>
/// <param name="localIP">你的局域网IP</param>
/// <returns></returns>
public async Task<TunnelWanPortEndPoint> GetWanPortAsync(IPAddress localIP, TunnelWanPortInfo info)
public async Task<TunnelWanPortEndPoint> GetWanPortAsync(IPAddress localIP, TunnelWanPortProtocolType protocolType)
{
if (info == null) return null;
var tunnelWanPort = tunnelWanPorts.FirstOrDefault(c => c.Type == info.Type && c.ProtocolType == info.ProtocolType);
var tunnelWanPort = tunnelWanPorts.FirstOrDefault(c => c.ProtocolType == protocolType);
if (tunnelWanPort == null) return null;
try
{
if (LoggerHelper.Instance.LoggerLevel <= LoggerTypes.DEBUG)
LoggerHelper.Instance.Debug($"get domain ip {info.Host}");
IPEndPoint server = NetworkHelper.GetEndPoint(info.Host, 3478);
LoggerHelper.Instance.Debug($"get domain ip {tunnelAdapter.ServerHost}");
IPEndPoint server = NetworkHelper.GetEndPoint(tunnelAdapter.ServerHost, 3478);
if (LoggerHelper.Instance.LoggerLevel <= LoggerTypes.DEBUG)
LoggerHelper.Instance.Debug($"got domain ip {info.Host}->{server}");
LoggerHelper.Instance.Debug($"got domain ip {tunnelAdapter.ServerHost}->{server}");
if (server == null) return null;
TunnelWanPortEndPoint wanPort = await tunnelWanPort.GetAsync(localIP, server).ConfigureAwait(false);
if (wanPort != null)
Expand Down
3 changes: 0 additions & 3 deletions linker.web/src/apis/relay.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { sendWebsocketMsg } from './request'

export const getRelayTypes = () => {
return sendWebsocketMsg('relay/gettypes');
}
export const setRelayServers = (servers) => {
return sendWebsocketMsg('relay/SetServers', servers);
}
Expand Down
20 changes: 0 additions & 20 deletions linker.web/src/apis/tunnel.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { sendWebsocketMsg } from './request'

export const getTunnelTypes = () => {
return sendWebsocketMsg('tunnel/gettypes');
}
export const setTunnelServers = (servers) => {
return sendWebsocketMsg('tunnel/SetServers', servers);
}

export const getTunnelInfo = (hashcode = '0') => {
return sendWebsocketMsg('tunnel/get', hashcode);
}
Expand All @@ -25,19 +18,6 @@ export const setTunnelTransports = (data) => {
return sendWebsocketMsg('tunnel/SetTransports', data);
}

export const getTunnelExcludeIPs = () => {
return sendWebsocketMsg('tunnel/GetExcludeIPs');
}
export const setTunnelExcludeIPs = (data) => {
return sendWebsocketMsg('tunnel/SetExcludeIPs', data);
}

export const getInterfaces = () => {
return sendWebsocketMsg('tunnel/GeInterfaces');
}
export const setInterface = (ip) => {
return sendWebsocketMsg('tunnel/SetInterface', ip);
}
export const getTunnelRecords = () => {
return sendWebsocketMsg('tunnel/Records');
}
Loading

0 comments on commit d3fead6

Please sign in to comment.