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