OCT 1. 启动ICS前移除所有现有的ICS配置
This commit is contained in:
parent
61ba441f3e
commit
4af5e154c6
|
@ -117,20 +117,12 @@ int LocalWireGuardControl(bool isStart, bool setPrivateMode) {
|
|||
}
|
||||
}
|
||||
|
||||
// 检查 Internet 网络共享状态
|
||||
if ((ret = GetNetIntelnetConnectionSharing(ifInetlnetIndex, &chkStatus)) != ERR_SUCCESS) {
|
||||
SPDLOG_ERROR(TEXT("Call GetNetIntelnetConnectionSharing error: {0}"), ret);
|
||||
// 移除系统所有先前ICS共享
|
||||
if ((ret = RemoveAllIntelnetConnectSharing()) != ERR_SUCCESS) {
|
||||
SPDLOG_ERROR(TEXT("Call RemoveAllIntelnetConnectSharing error: {0}"), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 关闭 Intelnet 网络连接共享
|
||||
if (chkStatus) {
|
||||
if ((ret = SetNetIntelnetConnectionSharing(ifInetlnetIndex, false, false)) != ERR_SUCCESS) {
|
||||
SPDLOG_ERROR(TEXT("Call SetNetIntelnetConnectionSharing error: {0}"), ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
if (isStart) {
|
||||
// 启动服务
|
||||
ret = WireGuardInstallDefaultServerService(true);
|
||||
|
@ -188,10 +180,14 @@ int LocalWireGuardControl(bool isStart, bool setPrivateMode) {
|
|||
GetIpV4InfoFromCIDR(GetGlobalCfgInfo()->userCfg.cliConfig.cliAddress, &ipInfo);
|
||||
|
||||
// 检查 ICS 是否自动添加了其它非法 IP 地址
|
||||
if((ret = VerifyInterfaceIpAddr(ifWireGuardIndex, ipInfo.ip, ipInfo.netmask, 1)) != ERR_SUCCESS) {
|
||||
SPDLOG_WARN(TEXT("Check Windows automatic set another ip to wireguard network interface, force set to {0}/{1}"), ipInfo.ip, ipInfo.netmask);
|
||||
if ((ret = VerifyInterfaceIpAddr(ifWireGuardIndex, ipInfo.ip, ipInfo.netmask, 1)) != ERR_SUCCESS) {
|
||||
SPDLOG_WARN(
|
||||
TEXT("Check Windows automatic set another ip to wireguard network interface, force set to {0}/{1}"),
|
||||
ipInfo.ip,
|
||||
ipInfo.netmask);
|
||||
// 重设隧道 IP 地址
|
||||
if ((ret = SetInterfaceIpAddressWMI(GetGlobalCfgInfo()->userCfg.userName, ipInfo.ip, ipInfo.netmask)) != ERR_SUCCESS) {
|
||||
if ((ret = SetInterfaceIpAddressWMI(GetGlobalCfgInfo()->userCfg.userName, ipInfo.ip, ipInfo.netmask)) !=
|
||||
ERR_SUCCESS) {
|
||||
SPDLOG_ERROR(TEXT("Call SetInterfaceIpAddressWMI error: {0}"), ret);
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -142,6 +142,18 @@ int SetNetIntelnetConnectionSharing(int ifIndex, bool isEnable, bool isSetPrivat
|
|||
* - ERR_SUCCESS 成功
|
||||
*/
|
||||
int GetNetIntelnetConnectionSharing(int ifIndex, bool *pIsEnable);
|
||||
|
||||
/**
|
||||
* brief 删除系统所有ICS共享
|
||||
* return 函数执行结果 0: 成功, 小于0 失败 @see USER_ERRNO
|
||||
* - -ERR_INPUT_PARAMS 输入参数错误
|
||||
* - -ERR_MEMORY_STR 字符串处理
|
||||
* - -ERR_CALL_SHELL 调用系统命令行失败
|
||||
* - -ERR_PROCESS_RETURN 系统调用执行结束返回失败
|
||||
* - ERR_SUCCESS 成功
|
||||
*/
|
||||
int RemoveAllIntelnetConnectSharing();
|
||||
|
||||
/**
|
||||
* @brief 设置网卡为 Private/Public 模式
|
||||
* @param[in] pInterfaceName pInterfaceName 网卡名称
|
||||
|
|
|
@ -1008,7 +1008,7 @@ int SetNetIntelnetConnectionSharing(int ifIndex, bool isEnable, bool isSetPrivat
|
|||
}
|
||||
|
||||
// 未连接的网卡不处理
|
||||
if (pNP->Status != NCS_CONNECTED) {
|
||||
if (pNP->Status != NCS_CONNECTED && isEnable) {
|
||||
return -ERR_NET_UNCONNECT;
|
||||
}
|
||||
|
||||
|
@ -1055,6 +1055,74 @@ int SetNetIntelnetConnectionSharing(int ifIndex, bool isEnable, bool isSetPrivat
|
|||
return ERR_ITEM_UNEXISTS;
|
||||
}
|
||||
|
||||
/**
|
||||
* brief 删除系统所有ICS共享
|
||||
* return 函数执行结果 0: 成功, 小于0 失败 @see USER_ERRNO
|
||||
* - -ERR_INPUT_PARAMS 输入参数错误
|
||||
* - -ERR_MEMORY_STR 字符串处理
|
||||
* - -ERR_CALL_SHELL 调用系统命令行失败
|
||||
* - -ERR_PROCESS_RETURN 系统调用执行结束返回失败
|
||||
* - ERR_SUCCESS 成功
|
||||
*/
|
||||
int RemoveAllIntelnetConnectSharing() {
|
||||
bool chkStatus = false;
|
||||
PIP_ADAPTER_INFO pAdapterInfo;
|
||||
DWORD dwRetVal;
|
||||
|
||||
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
|
||||
pAdapterInfo = static_cast<IP_ADAPTER_INFO *>(HeapAlloc(GetProcessHeap(), 0, sizeof(IP_ADAPTER_INFO)));
|
||||
|
||||
if (pAdapterInfo == nullptr) {
|
||||
SPDLOG_ERROR(TEXT("Error allocating memory needed to call GetAdaptersinfo"));
|
||||
return -ERR_MALLOC_MEMORY;
|
||||
}
|
||||
|
||||
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
|
||||
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
|
||||
pAdapterInfo = static_cast<IP_ADAPTER_INFO *>(HeapAlloc(GetProcessHeap(), 0, ulOutBufLen));
|
||||
if (pAdapterInfo == nullptr) {
|
||||
SPDLOG_ERROR(TEXT("Error allocating memory needed to call GetAdaptersinfo\n"));
|
||||
return -ERR_MALLOC_MEMORY;
|
||||
}
|
||||
}
|
||||
|
||||
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
|
||||
int ret;
|
||||
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
|
||||
while (pAdapter) {
|
||||
// 网卡索引
|
||||
int ifIndex = static_cast<int>(pAdapter->Index);
|
||||
|
||||
// 检查 Internet 网络共享状态
|
||||
if ((ret = GetNetIntelnetConnectionSharing(ifIndex, &chkStatus)) == ERR_SUCCESS) {
|
||||
if (chkStatus) {
|
||||
// 关闭 Intelnet 网络连接共享
|
||||
if ((ret = SetNetIntelnetConnectionSharing(ifIndex, false, false)) != ERR_SUCCESS) {
|
||||
SPDLOG_ERROR(TEXT("Remove {0}-{1} ICS error: {2}"), ifIndex, pAdapter->AdapterName, ret);
|
||||
} else {
|
||||
SPDLOG_DEBUG(TEXT("Remove {0}-{1} ICS"), ifIndex, pAdapter->AdapterName);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
SPDLOG_ERROR(TEXT("Get {0}-{1} ICS status error: {2}"), ifIndex, pAdapter->AdapterName, ret);
|
||||
}
|
||||
|
||||
pAdapter = pAdapter->Next;
|
||||
}
|
||||
} else {
|
||||
SPDLOG_ERROR(TEXT("GetAdaptersInfo failed with error: {0}\n"), dwRetVal);
|
||||
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
|
||||
return -ERR_SYS_CALL;
|
||||
}
|
||||
|
||||
if (pAdapterInfo) {
|
||||
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
|
||||
}
|
||||
|
||||
return ERR_SUCCESS;
|
||||
}
|
||||
|
||||
int GetNetIntelnetConnectionSharing(int ifIndex, bool *pIsEnable) {
|
||||
VARIANT v;
|
||||
INetConnection *pNC = nullptr;
|
||||
|
|
|
@ -64,6 +64,12 @@ public:
|
|||
}
|
||||
|
||||
#if 0
|
||||
|
||||
TEST_METHOD(TestRemoveAllConnectSHaring) {
|
||||
const int ret = RemoveAllConnectSHaring();
|
||||
Assert::AreEqual(RET_OK, ret);
|
||||
}
|
||||
|
||||
TEST_METHOD(TestVerifyInterfaceIpAddr) {
|
||||
const int ret = VerifyInterfaceIpAddr(11, TEXT("10.10.10.251"), TEXT("255.255.255.0"), 2);
|
||||
Assert::AreEqual(RET_OK, ret);
|
||||
|
|
Loading…
Reference in New Issue