mirror of
https://github.com/snltty/linker.git
synced 2025-10-16 14:11:18 +08:00
网卡自动启动
This commit is contained in:
@@ -11,7 +11,7 @@ namespace linker.tun.test
|
|||||||
{
|
{
|
||||||
linkerTunDeviceAdapter = new LinkerTunDeviceAdapter();
|
linkerTunDeviceAdapter = new LinkerTunDeviceAdapter();
|
||||||
linkerTunDeviceAdapter.SetReadCallback(new LinkerTunDeviceCallback());
|
linkerTunDeviceAdapter.SetReadCallback(new LinkerTunDeviceCallback());
|
||||||
linkerTunDeviceAdapter.SetUp("linker111"
|
linkerTunDeviceAdapter.Setup("linker111"
|
||||||
, IPAddress.Parse("192.168.55.2"), 24);
|
, IPAddress.Parse("192.168.55.2"), 24);
|
||||||
linkerTunDeviceAdapter.SetMtu(1420);
|
linkerTunDeviceAdapter.SetMtu(1420);
|
||||||
|
|
||||||
|
@@ -29,7 +29,7 @@ namespace linker.tun
|
|||||||
/// <param name="prefixLength"></param>
|
/// <param name="prefixLength"></param>
|
||||||
/// <param name="error"></param>
|
/// <param name="error"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public bool SetUp(IPAddress address, IPAddress gateway, byte prefixLength, out string error);
|
public bool Setup(IPAddress address, IPAddress gateway, byte prefixLength, out string error);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 关闭
|
/// 关闭
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@@ -24,7 +24,7 @@ namespace linker.tun
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SetUp(IPAddress address, IPAddress gateway, byte prefixLength, out string error)
|
public bool Setup(IPAddress address, IPAddress gateway, byte prefixLength, out string error)
|
||||||
{
|
{
|
||||||
error = string.Empty;
|
error = string.Empty;
|
||||||
this.address = address;
|
this.address = address;
|
||||||
|
@@ -23,7 +23,7 @@ namespace linker.tun
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SetUp(IPAddress address, IPAddress gateway, byte prefixLength, out string error)
|
public bool Setup(IPAddress address, IPAddress gateway, byte prefixLength, out string error)
|
||||||
{
|
{
|
||||||
error = string.Empty;
|
error = string.Empty;
|
||||||
|
|
||||||
|
@@ -57,7 +57,7 @@ namespace linker.tun
|
|||||||
/// <param name="name">网卡名,如果是osx,需要utunX的命名,X是一个数字</param>
|
/// <param name="name">网卡名,如果是osx,需要utunX的命名,X是一个数字</param>
|
||||||
/// <param name="address">网卡IP</param>
|
/// <param name="address">网卡IP</param>
|
||||||
/// <param name="prefixLength">掩码。一般24即可</param>
|
/// <param name="prefixLength">掩码。一般24即可</param>
|
||||||
public bool SetUp(string name, IPAddress address, byte prefixLength)
|
public bool Setup(string name, IPAddress address, byte prefixLength)
|
||||||
{
|
{
|
||||||
if (Interlocked.CompareExchange(ref operating, 1, 0) == 1)
|
if (Interlocked.CompareExchange(ref operating, 1, 0) == 1)
|
||||||
{
|
{
|
||||||
@@ -72,7 +72,7 @@ namespace linker.tun
|
|||||||
error = $"{System.Runtime.InteropServices.RuntimeInformation.OSDescription} not support";
|
error = $"{System.Runtime.InteropServices.RuntimeInformation.OSDescription} not support";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
linkerTunDevice.SetUp(address, NetworkHelper.ToGatewayIP(address, prefixLength), prefixLength, out error);
|
linkerTunDevice.Setup(address, NetworkHelper.ToGatewayIP(address, prefixLength), prefixLength, out error);
|
||||||
if (string.IsNullOrWhiteSpace(error) == false)
|
if (string.IsNullOrWhiteSpace(error) == false)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@@ -114,7 +114,7 @@ namespace linker.tun
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 关闭网卡
|
/// 关闭网卡
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool Shutdown(int index)
|
public bool Shutdown()
|
||||||
{
|
{
|
||||||
if (Interlocked.CompareExchange(ref operating, 1, 0) == 1)
|
if (Interlocked.CompareExchange(ref operating, 1, 0) == 1)
|
||||||
{
|
{
|
||||||
@@ -187,7 +187,7 @@ namespace linker.tun
|
|||||||
ReadOnlyMemory<byte> buffer = linkerTunDevice.Read();
|
ReadOnlyMemory<byte> buffer = linkerTunDevice.Read();
|
||||||
if (buffer.Length == 0)
|
if (buffer.Length == 0)
|
||||||
{
|
{
|
||||||
Shutdown(4);
|
Shutdown();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,7 +213,7 @@ namespace linker.tun
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
error = ex.Message;
|
error = ex.Message;
|
||||||
Shutdown(5);
|
Shutdown();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -28,7 +28,7 @@ namespace linker.tun
|
|||||||
this.guid = guid;
|
this.guid = guid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SetUp(IPAddress address, IPAddress gateway, byte prefixLength, out string error)
|
public bool Setup(IPAddress address, IPAddress gateway, byte prefixLength, out string error)
|
||||||
{
|
{
|
||||||
this.address = address;
|
this.address = address;
|
||||||
this.prefixLength = prefixLength;
|
this.prefixLength = prefixLength;
|
||||||
|
@@ -64,7 +64,7 @@ namespace linker.plugins.tuntap
|
|||||||
/// <param name="param"></param>
|
/// <param name="param"></param>
|
||||||
public void Refresh(ApiControllerParamsInfo param)
|
public void Refresh(ApiControllerParamsInfo param)
|
||||||
{
|
{
|
||||||
tuntapTransfer.Refresh();
|
tuntapTransfer.RefreshConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -77,8 +77,8 @@ namespace linker.plugins.tuntap
|
|||||||
//运行自己的
|
//运行自己的
|
||||||
if (param.Content == config.Data.Client.Id)
|
if (param.Content == config.Data.Client.Id)
|
||||||
{
|
{
|
||||||
tuntapTransfer.Stop();
|
tuntapTransfer.Shutdown();
|
||||||
tuntapTransfer.Run();
|
tuntapTransfer.Setup();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -102,7 +102,7 @@ namespace linker.plugins.tuntap
|
|||||||
//停止自己的
|
//停止自己的
|
||||||
if (param.Content == config.Data.Client.Id)
|
if (param.Content == config.Data.Client.Id)
|
||||||
{
|
{
|
||||||
tuntapTransfer.Stop();
|
tuntapTransfer.Shutdown();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -124,7 +124,7 @@ namespace linker.plugins.tuntap
|
|||||||
//更新自己的
|
//更新自己的
|
||||||
if (info.MachineId == config.Data.Client.Id)
|
if (info.MachineId == config.Data.Client.Id)
|
||||||
{
|
{
|
||||||
tuntapTransfer.OnUpdate(info);
|
tuntapTransfer.UpdateConfig(info);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@@ -44,45 +44,32 @@ namespace linker.plugins.tuntap
|
|||||||
this.tuntapProxy = tuntapProxy;
|
this.tuntapProxy = tuntapProxy;
|
||||||
this.runningConfig = runningConfig;
|
this.runningConfig = runningConfig;
|
||||||
|
|
||||||
Shutdown();
|
linkerTunDeviceAdapter.Shutdown();
|
||||||
linkerTunDeviceAdapter.Clear();
|
linkerTunDeviceAdapter.Clear();
|
||||||
linkerTunDeviceAdapter.SetReadCallback(tuntapProxy);
|
linkerTunDeviceAdapter.SetReadCallback(tuntapProxy);
|
||||||
|
AppDomain.CurrentDomain.ProcessExit += (s, e) => linkerTunDeviceAdapter.Shutdown();
|
||||||
|
Console.CancelKeyPress += (s, e) => linkerTunDeviceAdapter.Shutdown();
|
||||||
|
|
||||||
clientSignInState.NetworkEnabledHandle += (times) =>
|
clientSignInState.NetworkEnabledHandle += (times) =>
|
||||||
{
|
{
|
||||||
OnChange();
|
NotifyConfig();
|
||||||
};
|
};
|
||||||
clientSignInState.NetworkFirstEnabledHandle += () =>
|
clientSignInState.NetworkFirstEnabledHandle += () =>
|
||||||
{
|
{
|
||||||
OnChange();
|
NotifyConfig();
|
||||||
|
CheckTuntapStatusTask();
|
||||||
if (runningConfig.Data.Tuntap.Running)
|
if (runningConfig.Data.Tuntap.Running)
|
||||||
{
|
{
|
||||||
Stop(); Run();
|
Setup();
|
||||||
_ = CheckVeaStatusTask();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
AppDomain.CurrentDomain.ProcessExit += (s, e) => Shutdown();
|
|
||||||
Console.CancelKeyPress += (s, e) => Shutdown();
|
|
||||||
|
|
||||||
GetRouteIps();
|
GetRouteIps();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 程序关闭
|
|
||||||
/// </summary>
|
|
||||||
private void Shutdown()
|
|
||||||
{
|
|
||||||
bool running = runningConfig.Data.Tuntap.Running;
|
|
||||||
Stop();
|
|
||||||
runningConfig.Data.Tuntap.Running = running;
|
|
||||||
runningConfig.Data.Update();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 运行网卡
|
/// 运行网卡
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Run()
|
public void Setup()
|
||||||
{
|
{
|
||||||
if (Interlocked.CompareExchange(ref operating, 1, 0) == 1)
|
if (Interlocked.CompareExchange(ref operating, 1, 0) == 1)
|
||||||
{
|
{
|
||||||
@@ -90,14 +77,14 @@ namespace linker.plugins.tuntap
|
|||||||
}
|
}
|
||||||
Task.Run(() =>
|
Task.Run(() =>
|
||||||
{
|
{
|
||||||
OnChange();
|
NotifyConfig();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (runningConfig.Data.Tuntap.IP.Equals(IPAddress.Any))
|
if (runningConfig.Data.Tuntap.IP.Equals(IPAddress.Any))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
linkerTunDeviceAdapter.SetUp(interfaceName, runningConfig.Data.Tuntap.IP, 24);
|
linkerTunDeviceAdapter.Setup(interfaceName, runningConfig.Data.Tuntap.IP, 24);
|
||||||
if (string.IsNullOrWhiteSpace(linkerTunDeviceAdapter.Error))
|
if (string.IsNullOrWhiteSpace(linkerTunDeviceAdapter.Error))
|
||||||
{
|
{
|
||||||
linkerTunDeviceAdapter.SetMtu(1416);
|
linkerTunDeviceAdapter.SetMtu(1416);
|
||||||
@@ -117,14 +104,14 @@ namespace linker.plugins.tuntap
|
|||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
Interlocked.Exchange(ref operating, 0);
|
Interlocked.Exchange(ref operating, 0);
|
||||||
OnChange();
|
NotifyConfig();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 停止网卡
|
/// 停止网卡
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Stop()
|
public void Shutdown()
|
||||||
{
|
{
|
||||||
if (Interlocked.CompareExchange(ref operating, 1, 0) == 1)
|
if (Interlocked.CompareExchange(ref operating, 1, 0) == 1)
|
||||||
{
|
{
|
||||||
@@ -132,8 +119,8 @@ namespace linker.plugins.tuntap
|
|||||||
}
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
OnChange();
|
NotifyConfig();
|
||||||
linkerTunDeviceAdapter.Shutdown(3);
|
linkerTunDeviceAdapter.Shutdown();
|
||||||
|
|
||||||
runningConfig.Data.Tuntap.Running = Status == TuntapStatus.Running;
|
runningConfig.Data.Tuntap.Running = Status == TuntapStatus.Running;
|
||||||
runningConfig.Data.Update();
|
runningConfig.Data.Update();
|
||||||
@@ -148,23 +135,22 @@ namespace linker.plugins.tuntap
|
|||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
Interlocked.Exchange(ref operating, 0);
|
Interlocked.Exchange(ref operating, 0);
|
||||||
OnChange();
|
NotifyConfig();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 刷新信息,把自己的网卡配置发给别人,顺便把别人的网卡信息带回来
|
/// 刷新信息,把自己的网卡配置发给别人,顺便把别人的网卡信息带回来
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Refresh()
|
public void RefreshConfig()
|
||||||
{
|
{
|
||||||
OnChange();
|
NotifyConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 更新本机网卡信息
|
/// 更新本机网卡信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="info"></param>
|
/// <param name="info"></param>
|
||||||
public void OnUpdate(TuntapInfo info)
|
public void UpdateConfig(TuntapInfo info)
|
||||||
{
|
{
|
||||||
Task.Run(() =>
|
Task.Run(() =>
|
||||||
{
|
{
|
||||||
@@ -175,12 +161,12 @@ namespace linker.plugins.tuntap
|
|||||||
runningConfig.Data.Update();
|
runningConfig.Data.Update();
|
||||||
if (Status == TuntapStatus.Running)
|
if (Status == TuntapStatus.Running)
|
||||||
{
|
{
|
||||||
Stop();
|
Shutdown();
|
||||||
Run();
|
Setup();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
OnChange();
|
NotifyConfig();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -201,17 +187,16 @@ namespace linker.plugins.tuntap
|
|||||||
|
|
||||||
return GetLocalInfo();
|
return GetLocalInfo();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 信息有变化,刷新信息,把自己的网卡配置发给别人,顺便把别人的网卡信息带回来
|
/// 信息有变化,刷新信息,把自己的网卡配置发给别人,顺便把别人的网卡信息带回来
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void OnChange()
|
private void NotifyConfig()
|
||||||
{
|
{
|
||||||
GetRemoteInfo().ContinueWith((result) =>
|
GetRemoteInfo().ContinueWith((result) =>
|
||||||
{
|
{
|
||||||
if (result.Result == null)
|
if (result.Result == null)
|
||||||
{
|
{
|
||||||
OnChange();
|
NotifyConfig();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -303,6 +288,8 @@ namespace linker.plugins.tuntap
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
List<IPAddress> routeIps = new List<IPAddress>();
|
||||||
private List<TuntapVeaLanIPAddressList> ParseIPs(List<TuntapInfo> infos)
|
private List<TuntapVeaLanIPAddressList> ParseIPs(List<TuntapInfo> infos)
|
||||||
{
|
{
|
||||||
uint[] localIps = NetworkHelper.GetIPV4()
|
uint[] localIps = NetworkHelper.GetIPV4()
|
||||||
@@ -349,32 +336,31 @@ namespace linker.plugins.tuntap
|
|||||||
Broadcast = ipInt | (~maskValue),
|
Broadcast = ipInt | (~maskValue),
|
||||||
OriginIPAddress = ip,
|
OriginIPAddress = ip,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
List<IPAddress> routeIps = new List<IPAddress>();
|
|
||||||
private void GetRouteIps()
|
private void GetRouteIps()
|
||||||
{
|
{
|
||||||
NetworkHelper.GetRouteLevel(out routeIps);
|
NetworkHelper.GetRouteLevel(out routeIps);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task CheckVeaStatusTask()
|
private void CheckTuntapStatusTask()
|
||||||
{
|
{
|
||||||
while (true)
|
Task.Run(async () =>
|
||||||
{
|
{
|
||||||
try
|
while (true)
|
||||||
{
|
{
|
||||||
if (runningConfig.Data.Tuntap.Running && OperatingSystem.IsWindows())
|
await Task.Delay(15000).ConfigureAwait(false);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (runningConfig.Data.Tuntap.Running && OperatingSystem.IsWindows())
|
||||||
|
{
|
||||||
|
await CheckInterface().ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
{
|
{
|
||||||
await CheckInterface().ConfigureAwait(false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception)
|
});
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
await Task.Delay(15000).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
private async Task CheckInterface()
|
private async Task CheckInterface()
|
||||||
{
|
{
|
||||||
@@ -383,13 +369,13 @@ namespace linker.plugins.tuntap
|
|||||||
if (networkInterface == null || networkInterface.OperationalStatus != OperationalStatus.Up)
|
if (networkInterface == null || networkInterface.OperationalStatus != OperationalStatus.Up)
|
||||||
{
|
{
|
||||||
LoggerHelper.Instance.Error($"tuntap inerface {interfaceName} is {networkInterface?.OperationalStatus ?? OperationalStatus.Unknown}, restarting");
|
LoggerHelper.Instance.Error($"tuntap inerface {interfaceName} is {networkInterface?.OperationalStatus ?? OperationalStatus.Unknown}, restarting");
|
||||||
Stop();
|
Shutdown();
|
||||||
await Task.Delay(5000).ConfigureAwait(false);
|
await Task.Delay(5000).ConfigureAwait(false);
|
||||||
|
|
||||||
networkInterface = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(c => c.Name == interfaceName);
|
networkInterface = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(c => c.Name == interfaceName);
|
||||||
if (networkInterface == null || networkInterface.OperationalStatus != OperationalStatus.Up)
|
if (networkInterface == null || networkInterface.OperationalStatus != OperationalStatus.Up)
|
||||||
{
|
{
|
||||||
Run();
|
Setup();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -20,8 +20,8 @@ namespace linker.plugins.tuntap.messenger
|
|||||||
[MessengerId((ushort)TuntapMessengerIds.Run)]
|
[MessengerId((ushort)TuntapMessengerIds.Run)]
|
||||||
public void Run(IConnection connection)
|
public void Run(IConnection connection)
|
||||||
{
|
{
|
||||||
tuntapTransfer.Stop();
|
tuntapTransfer.Shutdown();
|
||||||
tuntapTransfer.Run();
|
tuntapTransfer.Setup();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -31,7 +31,7 @@ namespace linker.plugins.tuntap.messenger
|
|||||||
[MessengerId((ushort)TuntapMessengerIds.Stop)]
|
[MessengerId((ushort)TuntapMessengerIds.Stop)]
|
||||||
public void Stop(IConnection connection)
|
public void Stop(IConnection connection)
|
||||||
{
|
{
|
||||||
tuntapTransfer.Stop();
|
tuntapTransfer.Shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -42,7 +42,7 @@ namespace linker.plugins.tuntap.messenger
|
|||||||
public void Update(IConnection connection)
|
public void Update(IConnection connection)
|
||||||
{
|
{
|
||||||
TuntapInfo info = MemoryPackSerializer.Deserialize<TuntapInfo>(connection.ReceiveRequestWrap.Payload.Span);
|
TuntapInfo info = MemoryPackSerializer.Deserialize<TuntapInfo>(connection.ReceiveRequestWrap.Payload.Span);
|
||||||
tuntapTransfer.OnUpdate(info);
|
tuntapTransfer.UpdateConfig(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
Reference in New Issue
Block a user