OCT 1. 服务端获取信息接口修改为 vmid

2. 增加隧道建立后,强制设置隧道IP地址为隧道配置地址
This commit is contained in:
黄昕 2023-09-21 17:54:55 +08:00
parent 7f2f686228
commit f0ee6909e0
9 changed files with 347 additions and 15 deletions

View File

@ -141,6 +141,8 @@ int LocalWireGuardControl(bool isStart, bool setPrivateMode) {
}
if (GetCurrentNetShareMode() == ICS_SHARE_MODE) {
IP_INFO ipInfo;
// 获取 WireGuard 隧道网络网卡 Index
if ((ret = GetInterfaceIfIndexByName(GetGlobalCfgInfo()->userCfg.userName, &ifWireGuardIndex)) !=
ERR_SUCCESS) {
@ -183,6 +185,13 @@ int LocalWireGuardControl(bool isStart, bool setPrivateMode) {
return -ERR_NET_WIREGUARD_ICS;
}
// 重设隧道 IP 地址
GetIpV4InfoFromCIDR(GetGlobalCfgInfo()->userCfg.cliConfig.cliAddress, &ipInfo);
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) {
IP_INFO ipInfo;
@ -408,6 +417,7 @@ int GetUserServerConfigure(const TCHAR *pUserName, const TCHAR *pToken, PUSER_SE
int ret;
PUSER_CONFIG pUser = &GetGlobalCfgInfo()->userCfg;
PUSER_SERVER_CONFIG pUserCfg = &pUser->svrConfig;
TCHAR *pConvRet;
#if USER_REAL_PLATFORM
ProtocolRequest<ReqGetUserCfgParams> req;
@ -451,16 +461,16 @@ int GetUserServerConfigure(const TCHAR *pUserName, const TCHAR *pToken, PUSER_SE
StringCbCopy(pUserCfg->svrAddress, MAX_IP_LEN, rsp.msgContent.svrAddress.c_str());
#else
req.vmIp = pToken;
ret = PlatformProtolPostMessage(GET_SERVERCFG_PATH, &req, &rsp);
req.extVmId = pToken;
ret = PlatformProtolPostMessage(GET_SERVERCFG_PATH, &req, &rsp);
if (ret != ERR_SUCCESS) {
return ret;
}
ret = strtol(rsp.code.c_str(), nullptr, 10);
ret = strtol(rsp.code.c_str(), &pConvRet, 10);
if (ret != 0) {
if (ret != 0 || lstrlen(pConvRet) > 0) {
SPDLOG_ERROR(TEXT("Server response error code: {0}"), ret);
return -ERR_HTTP_SERVER_RSP;
}

View File

@ -5,8 +5,8 @@
#if !USER_REAL_PLATFORM
class PlatformReqServerCfgParms {
public:
std::string vmIp;
AIGC_JSON_HELPER(vmIp)
std::string extVmId;
AIGC_JSON_HELPER(extVmId)
};
class PlatformReqClientCfgParms {

View File

@ -145,6 +145,27 @@ SCCSDK_API void __cdecl SetCurrentNetShareMode(NET_SHARE_MODE shareMode);
*/
SCCSDK_API int __cdecl GetAllNICInfo(PNIC_CONTENT *pInfo, int *pItemCounts);
/**
* @brief IP地址
* @param pInterfaceName
* @param pIpaddr IP
* @param pNetmask
* @return 0 @see USER_ERRNO
* - -ERR_INPUT_PARAMS
* - -ERR_COM_INITIALIZE COM失败
* - -ERR_CREATE_COMMOBJECT COM
* - -ERR_WMI_CONN_SVR WMI
* - -ERR_WMI_SET_PROXY WMI
* - -ERR_WMI_QUERY WMI SQL
* - -ERR_WMI_EXEC_METHOD WMI
* - -ERR_ITEM_UNEXISTS
* - -ERR_MEMORY_STR
* - ERR_SUCCESS
*/
SCCSDK_API int __cdecl SetInterfaceIpAddressWMI(const TCHAR *pInterfaceName,
const TCHAR *pIpaddr,
const TCHAR *pNetmask);
/**
* @brief Internet
* @param[out] pIfIndex

View File

@ -60,10 +60,16 @@ enum USER_ERRNO {
ERR_GET_IPFOWARDTBL = 300, ///< 获取系统 IP 转发表失败
ERR_CREATE_COMMOBJECT = 400, ///< 创建 COM 对象失败
ERR_CALL_COMMOBJECT, ///< 调用 COM 对象失败
ERR_WMI_GET_COMMOBJECT, ///< 获取 WMI COM 对象失败
ERR_WMI_CONN_SVR, ///< 连接到 WMI 失败
ERR_WMI_SET_PROXY, ///< 设置 WMI 代理服务失败
ERR_WMI_QUERY, ///< 执行 WMI SQL查询失败
ERR_WMI_EXEC_METHOD, ///< 执行 WMI 函数失败
ERR_COM_INITIALIZE, ///< 初始化COM失败
ERR_JSON_CREATE = 500, ///< 创建 JSON 对象失败
ERR_JSON_DECODE, ///< 从 JSON 反序列化对象失败
ERR_HTTP_SERVER_RSP = 600, ///< HTTP 服务端返回错误
ERR_HTTP_POST_DATA, ///< 发送 POST 请求失败
ERR_NET_ADD_ROUTE, ///< 添加路由失败
ERR_NET_REMOVE_ROUTE, ///< 删除路由失败
ERR_HTTP_SERVER_RSP = 600, ///< HTTP 服务端返回错误
ERR_HTTP_POST_DATA, ///< 发送 POST 请求失败
ERR_NET_ADD_ROUTE, ///< 添加路由失败
ERR_NET_REMOVE_ROUTE, ///< 删除路由失败
};

View File

@ -15,9 +15,14 @@
#include <netcon.h>
#include <shlwapi.h>
#include <wbemcli.h>
#include <comutil.h>
#include <atlbase.h>
#pragma comment(lib, "Iphlpapi.lib")
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "comsuppwd.lib")
#pragma comment(lib, "wbemuuid.lib")
static NIC_CONTENT g_NetAdapterInfo[NET_CARD_MAX];
@ -1267,6 +1272,264 @@ int RemoveNATRule(const TCHAR *pInterfaceName) {
return ERR_SUCCESS;
}
/**
* @brief IP地址
* @param pInterfaceName
* @param pIpaddr IP
* @param pNetmask
* @return 0 @see USER_ERRNO
* - -ERR_INPUT_PARAMS
* - -ERR_COM_INITIALIZE COM失败
* - -ERR_CREATE_COMMOBJECT COM
* - -ERR_WMI_CONN_SVR WMI
* - -ERR_WMI_SET_PROXY WMI
* - -ERR_WMI_QUERY WMI SQL
* - -ERR_WMI_EXEC_METHOD WMI
* - -ERR_ITEM_UNEXISTS
* - -ERR_MEMORY_STR
* - ERR_SUCCESS
*/
int SetInterfaceIpAddressWMI(const TCHAR *pInterfaceName, const TCHAR *pIpaddr, const TCHAR *pNetmask) {
int ret;
GUID guid;
IWbemLocator *pLoc = nullptr;
IWbemServices *pSvc = nullptr;
IEnumWbemClassObject *pEnum = nullptr;
IWbemClassObject *pObj = nullptr;
IWbemClassObject *pIfCfg = nullptr;
IWbemClassObject *params = nullptr;
IWbemClassObject *paramsInst = nullptr;
IWbemClassObject *pResult = nullptr;
VARIANT vPath;
VARIANT vtRet;
ULONG num = 0;
VARIANT paramVt;
SAFEARRAY *psa = nullptr;
long idx[] = {0};
BSTR ip;
BSTR mask;
TCHAR wmiCmd[1024];
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName);
return -ERR_INPUT_PARAMS;
}
if (pIpaddr == nullptr || lstrlen(pIpaddr) == 0) {
SPDLOG_ERROR("Input pIpaddr params error: {0}", pIpaddr);
return -ERR_INPUT_PARAMS;
}
if (pNetmask == nullptr || lstrlen(pNetmask) == 0) {
SPDLOG_WARN("Input pNetmask params error: {0}", pNetmask);
return -ERR_INPUT_PARAMS;
}
if ((ret = GetInterfaceGUIDByName(pInterfaceName, &guid)) != ERR_SUCCESS) {
SPDLOG_ERROR(TEXT("Get NetCard [{0}] GUID error: {1}"), pInterfaceName, ret);
return ret;
}
// Step 1: Initialize COM.
HRESULT hr = CoInitialize(nullptr);
if (FAILED(hr)) {
SPDLOG_ERROR(TEXT("CoInitializeEx failed: {0}."), hr);
return -ERR_COM_INITIALIZE;
}
// Step 2: Obtain the initial locator to WMI
hr = CoCreateInstance(CLSID_WbemLocator,
nullptr,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
reinterpret_cast<void **>(&pLoc));
if (FAILED(hr)) {
SPDLOG_ERROR(TEXT("CoCreateInstance WbemLocator failed: {0}."), hr);
return -ERR_CREATE_COMMOBJECT;
}
// Step 3: Connect to the local root\cimv2 namespace and obtain pointer pSvc to make IWbemServices calls.
hr = pLoc->ConnectServer(_bstr_t("ROOT\\CIMV2"), nullptr, nullptr, nullptr, 0, nullptr, nullptr, &pSvc);
if (FAILED(hr)) {
SPDLOG_ERROR(TEXT("WMI ConnectServer failed: {0}."), hr);
pLoc->Release();
return -ERR_WMI_CONN_SVR;
}
// Step 4: Set security levels for the proxy
hr = CoSetProxyBlanket(pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
nullptr, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
nullptr, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hr)) {
SPDLOG_ERROR(TEXT("WMI CoSetProxyBlanket failed: {0}."), hr);
pSvc->Release();
pLoc->Release();
return -ERR_WMI_SET_PROXY;
}
memset(wmiCmd, 0, 1024);
StringCbPrintf(wmiCmd,
1024,
TEXT("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE SettingID = "
"\"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}\""),
guid.Data1,
guid.Data2,
guid.Data3,
guid.Data4[0],
guid.Data4[1],
guid.Data4[2],
guid.Data4[3],
guid.Data4[4],
guid.Data4[5],
guid.Data4[6],
guid.Data4[7]);
hr = pSvc->ExecQuery(bstr_t("WQL"),
bstr_t(wmiCmd),
WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
nullptr,
&pEnum);
if (FAILED(hr)) {
SPDLOG_ERROR(TEXT("WMI ExecQuery failed: {0}."), hr);
pSvc->Release();
pLoc->Release();
return -ERR_WMI_QUERY;
}
// Get the adapter object.
hr = pEnum->Next(WBEM_INFINITE, 1, &pObj, &num);
if (FAILED(hr)) {
SPDLOG_ERROR(TEXT("Not found WBEM_INFINITE of {1}: {0}."), hr, pInterfaceName);
pEnum->Release();
pSvc->Release();
pLoc->Release();
return -ERR_ITEM_UNEXISTS;
}
if (num < 1) {
SPDLOG_ERROR(TEXT("WBEM_INFINITE of {1} is {2}: {0}."), hr, pInterfaceName, num);
pEnum->Release();
pSvc->Release();
pLoc->Release();
pObj->Release();
return -ERR_ITEM_UNEXISTS;
}
VariantInit(&vPath);
hr = pObj->Get(L"__PATH", 0, &vPath, nullptr, nullptr);
if (FAILED(hr)) {
SPDLOG_ERROR(TEXT("WMI GetObject Win32_NetworkAdapterConfiguration error: {0}."), hr);
pEnum->Release();
pSvc->Release();
pLoc->Release();
pObj->Release();
return -ERR_WMI_GET_COMMOBJECT;
}
hr = pSvc->GetObject(_bstr_t(TEXT("Win32_NetworkAdapterConfiguration")), 0, nullptr, &pIfCfg, nullptr);
if (FAILED(hr)) {
SPDLOG_ERROR(TEXT("WMI GetObject Win32_NetworkAdapterConfiguration error: {0}."), hr);
VariantClear(&vPath);
pEnum->Release();
pSvc->Release();
pLoc->Release();
pObj->Release();
return -ERR_WMI_GET_COMMOBJECT;
}
pIfCfg->GetMethod(_bstr_t("EnableStatic"), 0, &params, nullptr);
params->SpawnInstance(0, &paramsInst);
psa = SafeArrayCreateVector(VT_BSTR, 0, 1);
ip = SysAllocString(_bstr_t(pIpaddr));
idx[0] = 0;
if (FAILED(SafeArrayPutElement(psa, idx, ip))) {
VariantClear(&vPath);
pEnum->Release();
pSvc->Release();
pLoc->Release();
pObj->Release();
paramsInst->Release();
return -ERR_MEMORY_STR;
}
SysFreeString(ip);
paramVt.vt = VT_ARRAY | VT_BSTR;
paramVt.parray = psa;
hr = paramsInst->Put(L"IPAddress", 0, &paramVt, 0);
SafeArrayDestroy(psa);
psa = SafeArrayCreateVector(VT_BSTR, 0, 1);
mask = SysAllocString(_bstr_t(pNetmask));
idx[0] = 0;
if (FAILED(SafeArrayPutElement(psa, idx, mask))) {
VariantClear(&vPath);
pEnum->Release();
pSvc->Release();
pLoc->Release();
pObj->Release();
paramsInst->Release();
return -ERR_MEMORY_STR;
}
SysFreeString(mask);
paramVt.parray = psa;
hr = paramsInst->Put(L"SubnetMask", 0, &paramVt, 0);
SafeArrayDestroy(psa);
hr = pSvc->ExecMethod(vPath.bstrVal, _bstr_t(TEXT("EnableStatic")), 0, nullptr, paramsInst, &pResult, nullptr);
if (FAILED(hr)) {
SPDLOG_ERROR(TEXT("WMI ExecMethod EnableStatic error: {0}."), hr);
VariantClear(&vPath);
pEnum->Release();
pSvc->Release();
pLoc->Release();
pObj->Release();
paramsInst->Release();
return -ERR_WMI_EXEC_METHOD;
}
VariantInit(&vtRet);
hr = pResult->Get(L"ReturnValue", 0, &vtRet, nullptr, nullptr);
// 清理资源
VariantClear(&vPath);
pResult->Release();
pEnum->Release();
pSvc->Release();
pLoc->Release();
pObj->Release();
paramsInst->Release();
if (SUCCEEDED(hr)) {
if (vtRet.uintVal == 0 || vtRet.uintVal == 1) {
VariantClear(&vtRet);
return ERR_SUCCESS;
} else {
SPDLOG_ERROR(TEXT("WMI ExecMethod EnableStatic ReturnValue error: {0}."), vtRet.uintVal);
VariantClear(&vtRet);
return -ERR_WMI_EXEC_METHOD;
}
} else {
SPDLOG_ERROR(TEXT("WMI ExecMethod EnableStatic error: {0}."), hr);
VariantClear(&vtRet);
return -ERR_WMI_EXEC_METHOD;
}
}
#if 0
int SetInterfaceIpAddress(const TCHAR *pInterfaceName, const TCHAR *pIpaddr, const TCHAR *pNetmask) {
int ret;

View File

@ -145,6 +145,27 @@ SCCSDK_API void __cdecl SetCurrentNetShareMode(NET_SHARE_MODE shareMode);
*/
SCCSDK_API int __cdecl GetAllNICInfo(PNIC_CONTENT *pInfo, int *pItemCounts);
/**
* @brief IP地址
* @param pInterfaceName
* @param pIpaddr IP
* @param pNetmask
* @return 0 @see USER_ERRNO
* - -ERR_INPUT_PARAMS
* - -ERR_COM_INITIALIZE COM失败
* - -ERR_CREATE_COMMOBJECT COM
* - -ERR_WMI_CONN_SVR WMI
* - -ERR_WMI_SET_PROXY WMI
* - -ERR_WMI_QUERY WMI SQL
* - -ERR_WMI_EXEC_METHOD WMI
* - -ERR_ITEM_UNEXISTS
* - -ERR_MEMORY_STR
* - ERR_SUCCESS
*/
SCCSDK_API int __cdecl SetInterfaceIpAddressWMI(const CHAR *pInterfaceName,
const CHAR *pIpaddr,
const CHAR *pNetmask);
/**
* @brief Internet
* @param[out] pIfIndex

View File

@ -60,10 +60,16 @@ enum USER_ERRNO {
ERR_GET_IPFOWARDTBL = 300, ///< 获取系统 IP 转发表失败
ERR_CREATE_COMMOBJECT = 400, ///< 创建 COM 对象失败
ERR_CALL_COMMOBJECT, ///< 调用 COM 对象失败
ERR_WMI_GET_COMMOBJECT, ///< 获取 WMI COM 对象失败
ERR_WMI_CONN_SVR, ///< 连接到 WMI 失败
ERR_WMI_SET_PROXY, ///< 设置 WMI 代理服务失败
ERR_WMI_QUERY, ///< 执行 WMI SQL查询失败
ERR_WMI_EXEC_METHOD, ///< 执行 WMI 函数失败
ERR_COM_INITIALIZE, ///< 初始化COM失败
ERR_JSON_CREATE = 500, ///< 创建 JSON 对象失败
ERR_JSON_DECODE, ///< 从 JSON 反序列化对象失败
ERR_HTTP_SERVER_RSP = 600, ///< HTTP 服务端返回错误
ERR_HTTP_POST_DATA, ///< 发送 POST 请求失败
ERR_NET_ADD_ROUTE, ///< 添加路由失败
ERR_NET_REMOVE_ROUTE, ///< 删除路由失败
ERR_HTTP_SERVER_RSP = 600, ///< HTTP 服务端返回错误
ERR_HTTP_POST_DATA, ///< 发送 POST 请求失败
ERR_NET_ADD_ROUTE, ///< 添加路由失败
ERR_NET_REMOVE_ROUTE, ///< 删除路由失败
};

View File

@ -17,7 +17,7 @@ int main() {
EnableVerifySignature("sc-winvdisdk-efa9v12xwtz5eppr", "lh5r8sw6m9m416nm");
if (ERR_SUCCESS != (ret = GetUserServerConfigure("tunnel_svr", "172.21.97.100", &pSvrCfg))) {
if (ERR_SUCCESS != (ret = GetUserServerConfigure("tunnel_svr", "7E9769CF-A5E6-41F3-8719-B81E8507C299", &pSvrCfg))) {
wprintf(L"GetUserServerConfigure Error: %d\n", ret);
return -2;
}

View File

@ -58,6 +58,11 @@ public:
free(pInfo);
}
TEST_METHOD(TestSetInterfaceIpAddressWMI) {
const int ret = SetInterfaceIpAddressWMI(TEXT("huangxin"), TEXT("192.168.100.100"), TEXT("255.255.0.0"));
Assert::AreEqual(RET_OK, ret);
}
#if 0
TEST_METHOD(TestSetInterfaceIpAddressFromCIDR) {
Assert::AreEqual(RET_OK, SetInterfaceIpAddressFromCIDR(TEXT("wg_server"), TEXT("192.168.100.250/24")));