mirror of
https://github.com/2dust/v2rayN.git
synced 2025-05-03 05:38:51 +00:00
Tun mode force start sing-box core
This commit is contained in:
parent
a01b934fdb
commit
758d0d82d4
2 changed files with 47 additions and 51 deletions
|
@ -19,15 +19,16 @@ namespace v2rayN.Handler
|
||||||
msg = ResUI.CheckServerSettings;
|
msg = ResUI.CheckServerSettings;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
var config = LazyConfig.Instance.GetConfig();
|
||||||
|
|
||||||
msg = ResUI.InitialConfiguration;
|
msg = ResUI.InitialConfiguration;
|
||||||
if (node.configType == EConfigType.Custom)
|
if (node.configType == EConfigType.Custom)
|
||||||
{
|
{
|
||||||
return GenerateClientCustomConfig(node, fileName, out msg);
|
return GenerateClientCustomConfig(node, fileName, out msg);
|
||||||
}
|
}
|
||||||
else if (LazyConfig.Instance.GetCoreType(node, node.configType) == ECoreType.sing_box)
|
else if (config.tunModeItem.enableTun || LazyConfig.Instance.GetCoreType(node, node.configType) == ECoreType.sing_box)
|
||||||
{
|
{
|
||||||
var configGenSingbox = new CoreConfigSingbox(LazyConfig.Instance.GetConfig());
|
var configGenSingbox = new CoreConfigSingbox(config);
|
||||||
if (configGenSingbox.GenerateClientConfigContent(node, out SingboxConfig? singboxConfig, out msg) != 0)
|
if (configGenSingbox.GenerateClientConfigContent(node, out SingboxConfig? singboxConfig, out msg) != 0)
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -43,7 +44,7 @@ namespace v2rayN.Handler
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var coreConfigV2ray = new CoreConfigV2ray(LazyConfig.Instance.GetConfig());
|
var coreConfigV2ray = new CoreConfigV2ray(config);
|
||||||
if (coreConfigV2ray.GenerateClientConfigContent(node, out V2rayConfig? v2rayConfig, out msg) != 0)
|
if (coreConfigV2ray.GenerateClientConfigContent(node, out V2rayConfig? v2rayConfig, out msg) != 0)
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
|
|
|
@ -12,7 +12,6 @@ namespace v2rayN.Handler
|
||||||
internal class CoreHandler
|
internal class CoreHandler
|
||||||
{
|
{
|
||||||
private Config _config;
|
private Config _config;
|
||||||
private CoreInfo? _coreInfo;
|
|
||||||
private Process? _process;
|
private Process? _process;
|
||||||
private Process? _processPre;
|
private Process? _processPre;
|
||||||
private Action<bool, string> _updateFunc;
|
private Action<bool, string> _updateFunc;
|
||||||
|
@ -35,11 +34,6 @@ namespace v2rayN.Handler
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SetCore(node) != 0)
|
|
||||||
{
|
|
||||||
ShowMsg(false, ResUI.CheckServerSettings);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
string fileName = Utils.GetConfigPath(Global.coreConfigFileName);
|
string fileName = Utils.GetConfigPath(Global.coreConfigFileName);
|
||||||
if (CoreConfigHandler.GenerateClientConfig(node, fileName, out string msg, out string content) != 0)
|
if (CoreConfigHandler.GenerateClientConfig(node, fileName, out string msg, out string content) != 0)
|
||||||
{
|
{
|
||||||
|
@ -74,30 +68,13 @@ namespace v2rayN.Handler
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
bool hasProc = false;
|
||||||
if (_process != null)
|
if (_process != null)
|
||||||
{
|
{
|
||||||
KillProcess(_process);
|
KillProcess(_process);
|
||||||
_process.Dispose();
|
_process.Dispose();
|
||||||
_process = null;
|
_process = null;
|
||||||
}
|
hasProc =true;
|
||||||
else
|
|
||||||
{
|
|
||||||
if (_coreInfo == null || _coreInfo.coreExes == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
foreach (string vName in _coreInfo.coreExes)
|
|
||||||
{
|
|
||||||
Process[] existing = Process.GetProcessesByName(vName);
|
|
||||||
foreach (Process p in existing)
|
|
||||||
{
|
|
||||||
string? path = p.MainModule?.FileName;
|
|
||||||
if (path == $"{Utils.GetBinPath(vName, _coreInfo.coreType)}.exe")
|
|
||||||
{
|
|
||||||
KillProcess(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_processPre != null)
|
if (_processPre != null)
|
||||||
|
@ -105,6 +82,31 @@ namespace v2rayN.Handler
|
||||||
KillProcess(_processPre);
|
KillProcess(_processPre);
|
||||||
_processPre.Dispose();
|
_processPre.Dispose();
|
||||||
_processPre = null;
|
_processPre = null;
|
||||||
|
hasProc = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasProc)
|
||||||
|
{
|
||||||
|
var coreInfos = LazyConfig.Instance.GetCoreInfos();
|
||||||
|
foreach (var it in coreInfos)
|
||||||
|
{
|
||||||
|
if (it.coreType == ECoreType.v2rayN)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
foreach (string vName in it.coreExes)
|
||||||
|
{
|
||||||
|
Process[] existing = Process.GetProcessesByName(vName);
|
||||||
|
foreach (Process p in existing)
|
||||||
|
{
|
||||||
|
string? path = p.MainModule?.FileName;
|
||||||
|
if (path == $"{Utils.GetBinPath(vName, it.coreType)}.exe")
|
||||||
|
{
|
||||||
|
KillProcess(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
@ -152,7 +154,18 @@ namespace v2rayN.Handler
|
||||||
{
|
{
|
||||||
ShowMsg(false, string.Format(ResUI.StartService, DateTime.Now.ToString()));
|
ShowMsg(false, string.Format(ResUI.StartService, DateTime.Now.ToString()));
|
||||||
|
|
||||||
var proc = RunProcess(node, _coreInfo, "", ShowMsg);
|
ECoreType coreType;
|
||||||
|
if (node.configType != EConfigType.Custom && _config.tunModeItem.enableTun)
|
||||||
|
{
|
||||||
|
coreType = ECoreType.sing_box;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
coreType = LazyConfig.Instance.GetCoreType(node, node.configType);
|
||||||
|
}
|
||||||
|
var coreInfo = LazyConfig.Instance.GetCoreInfo(coreType);
|
||||||
|
|
||||||
|
var proc = RunProcess(node, coreInfo, "", ShowMsg);
|
||||||
if (proc is null)
|
if (proc is null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
|
@ -162,21 +175,20 @@ namespace v2rayN.Handler
|
||||||
//start a socks service
|
//start a socks service
|
||||||
if (_process != null && !_process.HasExited)
|
if (_process != null && !_process.HasExited)
|
||||||
{
|
{
|
||||||
if ((node.configType == EConfigType.Custom && node.preSocksPort > 0)
|
if ((node.configType == EConfigType.Custom && node.preSocksPort > 0))
|
||||||
|| (node.configType != EConfigType.Custom && _coreInfo.coreType != ECoreType.sing_box && _config.tunModeItem.enableTun))
|
|
||||||
{
|
{
|
||||||
var itemSocks = new ProfileItem()
|
var itemSocks = new ProfileItem()
|
||||||
{
|
{
|
||||||
coreType = ECoreType.sing_box,
|
coreType = ECoreType.sing_box,
|
||||||
configType = EConfigType.Socks,
|
configType = EConfigType.Socks,
|
||||||
address = Global.Loopback,
|
address = Global.Loopback,
|
||||||
port = node.preSocksPort > 0 ? node.preSocksPort : LazyConfig.Instance.GetLocalPort(Global.InboundSocks)
|
port = node.preSocksPort
|
||||||
};
|
};
|
||||||
string fileName2 = Utils.GetConfigPath(Global.corePreConfigFileName);
|
string fileName2 = Utils.GetConfigPath(Global.corePreConfigFileName);
|
||||||
if (CoreConfigHandler.GenerateClientConfig(itemSocks, fileName2, out string msg2, out string configStr) == 0)
|
if (CoreConfigHandler.GenerateClientConfig(itemSocks, fileName2, out string msg2, out string configStr) == 0)
|
||||||
{
|
{
|
||||||
var coreInfo = LazyConfig.Instance.GetCoreInfo(ECoreType.sing_box);
|
var coreInfo2 = LazyConfig.Instance.GetCoreInfo(ECoreType.sing_box);
|
||||||
var proc2 = RunProcess(node, coreInfo, $" -c {Global.corePreConfigFileName}", ShowMsg);
|
var proc2 = RunProcess(node, coreInfo2, $" -c {Global.corePreConfigFileName}", ShowMsg);
|
||||||
if (proc2 is not null)
|
if (proc2 is not null)
|
||||||
{
|
{
|
||||||
_processPre = proc2;
|
_processPre = proc2;
|
||||||
|
@ -257,23 +269,6 @@ namespace v2rayN.Handler
|
||||||
_updateFunc(updateToTrayTooltip, msg);
|
_updateFunc(updateToTrayTooltip, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int SetCore(ProfileItem node)
|
|
||||||
{
|
|
||||||
if (node == null)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
var coreType = LazyConfig.Instance.GetCoreType(node, node.configType);
|
|
||||||
|
|
||||||
_coreInfo = LazyConfig.Instance.GetCoreInfo(coreType);
|
|
||||||
|
|
||||||
if (_coreInfo == null)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#region Process
|
#region Process
|
||||||
|
|
||||||
private Process? RunProcess(ProfileItem node, CoreInfo coreInfo, string configPath, Action<bool, string> update)
|
private Process? RunProcess(ProfileItem node, CoreInfo coreInfo, string configPath, Action<bool, string> update)
|
||||||
|
|
Loading…
Reference in a new issue