OCT 1. 启动ICS前移除所有现有的ICS配置

This commit is contained in:
黄昕 2023-09-25 16:28:02 +08:00
parent 61ba441f3e
commit 4af5e154c6
4 changed files with 100 additions and 18 deletions

View File

@ -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);
@ -174,7 +166,7 @@ int LocalWireGuardControl(bool isStart, bool setPrivateMode) {
return -ERR_NET_WIREGUARD_ICS;
}
// 检查 WireGuard 网络共享状态
// 检查 WireGuard 网络共享状态
if ((ret = GetNetIntelnetConnectionSharing(ifInetlnetIndex, &chkStatus)) != ERR_SUCCESS) {
SPDLOG_ERROR(TEXT("Call GetNetIntelnetConnectionSharing error: {0}"), ret);
return ret;
@ -188,14 +180,18 @@ 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);
// 重设隧道 IP 地址
if ((ret = SetInterfaceIpAddressWMI(GetGlobalCfgInfo()->userCfg.userName, ipInfo.ip, ipInfo.netmask)) != ERR_SUCCESS) {
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) {
SPDLOG_ERROR(TEXT("Call SetInterfaceIpAddressWMI error: {0}"), ret);
return ret;
}
}
}
SPDLOG_INFO(TEXT("Net Share Service Work now on ICS mode: {0}"), GetGlobalCfgInfo()->userCfg.userName);
} else if (GetCurrentNetShareMode() == NAT_SHARE_MODE) {

View File

@ -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

View File

@ -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;

View File

@ -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);