Support get global IP address
This commit is contained in:
@@ -449,6 +449,102 @@ extern "C" __declspec(dllexport) int GetSystemHealthParameters(
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) int GetPublicIPAddress(LStrHandle ipv4) {
|
||||
try {
|
||||
if (!ipv4) return 0;
|
||||
const std::string s = ANSCENTER::ANSLicenseHelper::GetPublicIPAddress();
|
||||
const int size = static_cast<int>(s.length());
|
||||
const MgErr err = DSSetHandleSize(ipv4, sizeof(int32) + size * sizeof(uChar));
|
||||
if (err != noErr) return 0;
|
||||
(*ipv4)->cnt = size;
|
||||
if (size > 0) memcpy((*ipv4)->str, s.c_str(), size);
|
||||
return s.empty() ? 0 : 1;
|
||||
} catch (...) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
inline void WriteLStr(LStrHandle h, const std::string& s) {
|
||||
if (!h) return;
|
||||
const int size = static_cast<int>(s.length());
|
||||
const MgErr err = DSSetHandleSize(h, sizeof(int32) + size * sizeof(uChar));
|
||||
if (err == noErr) {
|
||||
(*h)->cnt = size;
|
||||
if (size > 0) memcpy((*h)->str, s.c_str(), size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) int GetPublicNetworkParameters(
|
||||
LStrHandle publicIP,
|
||||
LStrHandle country,
|
||||
LStrHandle countryCode,
|
||||
LStrHandle region,
|
||||
LStrHandle city,
|
||||
LStrHandle isp,
|
||||
LStrHandle timeZone,
|
||||
double* latitude,
|
||||
double* longitude,
|
||||
LStrHandle errorMsg)
|
||||
{
|
||||
try {
|
||||
const ANSCENTER::PublicNetworkInfo info =
|
||||
ANSCENTER::ANSLicenseHelper::GetPublicNetworkInformation();
|
||||
|
||||
WriteLStr(publicIP, info.publicIP);
|
||||
WriteLStr(country, info.country);
|
||||
WriteLStr(countryCode, info.countryCode);
|
||||
WriteLStr(region, info.region);
|
||||
WriteLStr(city, info.city);
|
||||
WriteLStr(isp, info.isp);
|
||||
WriteLStr(timeZone, info.timezone);
|
||||
WriteLStr(errorMsg, info.error);
|
||||
if (latitude) *latitude = info.latitude;
|
||||
if (longitude) *longitude = info.longitude;
|
||||
|
||||
return info.ok ? 1 : 0;
|
||||
} catch (...) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) int GetPublicNetworkParametersString(LStrHandle outString) {
|
||||
try {
|
||||
if (!outString) return 0;
|
||||
const ANSCENTER::PublicNetworkInfo info =
|
||||
ANSCENTER::ANSLicenseHelper::GetPublicNetworkInformation();
|
||||
|
||||
auto sanitize = [](std::string s) {
|
||||
std::replace(s.begin(), s.end(), ';', ',');
|
||||
return s;
|
||||
};
|
||||
|
||||
std::ostringstream os;
|
||||
os.imbue(std::locale::classic());
|
||||
os << std::fixed << std::setprecision(6);
|
||||
os << sanitize(info.publicIP) << ';'
|
||||
<< sanitize(info.country) << ';'
|
||||
<< sanitize(info.countryCode) << ';'
|
||||
<< sanitize(info.region) << ';'
|
||||
<< sanitize(info.city) << ';'
|
||||
<< sanitize(info.isp) << ';'
|
||||
<< sanitize(info.timezone) << ';'
|
||||
<< info.latitude << ';'
|
||||
<< info.longitude;
|
||||
|
||||
const std::string s = os.str();
|
||||
const int size = static_cast<int>(s.length());
|
||||
const MgErr err = DSSetHandleSize(outString, sizeof(int32) + size * sizeof(uChar));
|
||||
if (err != noErr) return 0;
|
||||
(*outString)->cnt = size;
|
||||
if (size > 0) memcpy((*outString)->str, s.c_str(), size);
|
||||
return info.ok ? 1 : 0;
|
||||
} catch (...) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) int GetSystemHealthParametersString(LStrHandle outString) {
|
||||
try {
|
||||
if (!outString) return 0;
|
||||
|
||||
Reference in New Issue
Block a user