Improve: getPublicIP may fail and block mater program
This commit is contained in:
@@ -183,22 +183,28 @@ public:
|
|||||||
|
|
||||||
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>IP, <20><>ȡʧ<C8A1>ܷ<EFBFBD><DCB7>ؿ<EFBFBD>
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>IP, <20><>ȡʧ<C8A1>ܷ<EFBFBD><DCB7>ؿ<EFBFBD>
|
||||||
std::string getPublicIP() {
|
std::string getPublicIP() {
|
||||||
|
clock_t t = clock();
|
||||||
HINTERNET hInternet, hConnect;
|
HINTERNET hInternet, hConnect;
|
||||||
DWORD bytesRead;
|
DWORD bytesRead;
|
||||||
char buffer[1024] = { 0 };
|
char buffer[1024] = { 0 };
|
||||||
|
|
||||||
hInternet = InternetOpen("Mozilla/5.0", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
|
hInternet = InternetOpen("Mozilla/5.0", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
|
||||||
if (!hInternet) return "";
|
if (!hInternet) {
|
||||||
|
Mprintf("getPublicIP failed cost %d ms.\n", clock() - t);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
hConnect = InternetOpenUrl(hInternet, "https://api.ipify.org", NULL, 0, INTERNET_FLAG_RELOAD | INTERNET_FLAG_SECURE, 0);
|
hConnect = InternetOpenUrl(hInternet, "https://api.ipify.org", NULL, 0, INTERNET_FLAG_RELOAD | INTERNET_FLAG_SECURE, 0);
|
||||||
if (!hConnect) {
|
if (!hConnect) {
|
||||||
InternetCloseHandle(hInternet);
|
InternetCloseHandle(hInternet);
|
||||||
|
Mprintf("getPublicIP failed cost %d ms.\n", clock() - t);
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
InternetReadFile(hConnect, buffer, sizeof(buffer) - 1, &bytesRead);
|
InternetReadFile(hConnect, buffer, sizeof(buffer) - 1, &bytesRead);
|
||||||
InternetCloseHandle(hConnect);
|
InternetCloseHandle(hConnect);
|
||||||
InternetCloseHandle(hInternet);
|
InternetCloseHandle(hInternet);
|
||||||
|
Mprintf("getPublicIP succeed cost %d ms.\n", clock() - t);
|
||||||
|
|
||||||
return std::string(buffer);
|
return std::string(buffer);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,7 +89,9 @@ public:
|
|||||||
// 智能计时器,计算函数的耗时
|
// 智能计时器,计算函数的耗时
|
||||||
class auto_tick {
|
class auto_tick {
|
||||||
private:
|
private:
|
||||||
|
const char* file;
|
||||||
const char* func;
|
const char* func;
|
||||||
|
int line;
|
||||||
int span;
|
int span;
|
||||||
clock_t tick;
|
clock_t tick;
|
||||||
__inline clock_t now() const {
|
__inline clock_t now() const {
|
||||||
@@ -100,7 +102,8 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
auto_tick(const char* func_name, int th = 5) : func(func_name), span(th), tick(now()) { }
|
auto_tick(const char* file_name, const char* func_name, int line_no, int th = 5) :
|
||||||
|
file(file_name), func(func_name), line(line_no), span(th), tick(now()) { }
|
||||||
~auto_tick() {
|
~auto_tick() {
|
||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
@@ -108,7 +111,11 @@ public:
|
|||||||
__inline void stop() {
|
__inline void stop() {
|
||||||
if (span != 0) {
|
if (span != 0) {
|
||||||
int s(this->time());
|
int s(this->time());
|
||||||
if (s > span)Mprintf("[%s] cost: [%d]ms.\n", func, s);
|
if (s > span) {
|
||||||
|
char buf[1024];
|
||||||
|
sprintf_s(buf, "%s(%d) : [%s] cost [%d]ms.\n", file, line, func, s);
|
||||||
|
OutputDebugStringA(buf);
|
||||||
|
}
|
||||||
span = 0;
|
span = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -116,7 +123,7 @@ public:
|
|||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
// 智能计算当前函数的耗时,超时会打印
|
// 智能计算当前函数的耗时,超时会打印
|
||||||
#define AUTO_TICK(thresh) auto_tick TICK(__FUNCTION__, thresh)
|
#define AUTO_TICK(thresh) auto_tick TICK(__FILE__, __FUNCTION__, __LINE__, thresh)
|
||||||
#define STOP_TICK TICK.stop()
|
#define STOP_TICK TICK.stop()
|
||||||
#else
|
#else
|
||||||
#define AUTO_TICK(thresh)
|
#define AUTO_TICK(thresh)
|
||||||
|
|||||||
@@ -674,6 +674,12 @@ VOID CMy2015RemoteDlg::AddList(CString strIP, CString strAddr, CString strPCName
|
|||||||
}
|
}
|
||||||
|
|
||||||
LRESULT CMy2015RemoteDlg::OnShowMessage(WPARAM wParam, LPARAM lParam) {
|
LRESULT CMy2015RemoteDlg::OnShowMessage(WPARAM wParam, LPARAM lParam) {
|
||||||
|
if (wParam && !lParam) {
|
||||||
|
CString* text = (CString*)wParam;
|
||||||
|
ShowMessage("提示信息", *text);
|
||||||
|
delete text;
|
||||||
|
return S_OK;
|
||||||
|
}
|
||||||
std::string pwd = THIS_CFG.GetStr("settings", "Password");
|
std::string pwd = THIS_CFG.GetStr("settings", "Password");
|
||||||
if (pwd.empty())
|
if (pwd.empty())
|
||||||
ShowMessage("授权提醒", "程序可能有使用限制,请联系管理员请求授权");
|
ShowMessage("授权提醒", "程序可能有使用限制,请联系管理员请求授权");
|
||||||
@@ -691,6 +697,7 @@ LRESULT CMy2015RemoteDlg::OnShowMessage(WPARAM wParam, LPARAM lParam) {
|
|||||||
|
|
||||||
VOID CMy2015RemoteDlg::ShowMessage(CString strType, CString strMsg)
|
VOID CMy2015RemoteDlg::ShowMessage(CString strType, CString strMsg)
|
||||||
{
|
{
|
||||||
|
AUTO_TICK(200);
|
||||||
CTime Timer = CTime::GetCurrentTime();
|
CTime Timer = CTime::GetCurrentTime();
|
||||||
CString strTime= Timer.Format("%H:%M:%S");
|
CString strTime= Timer.Format("%H:%M:%S");
|
||||||
|
|
||||||
@@ -899,6 +906,7 @@ bool IsFunctionReallyHooked(const char* dllName, const char* funcName)
|
|||||||
|
|
||||||
BOOL CMy2015RemoteDlg::OnInitDialog()
|
BOOL CMy2015RemoteDlg::OnInitDialog()
|
||||||
{
|
{
|
||||||
|
AUTO_TICK(500);
|
||||||
CDialogEx::OnInitDialog();
|
CDialogEx::OnInitDialog();
|
||||||
|
|
||||||
if (!IsPwdHashValid()) {
|
if (!IsPwdHashValid()) {
|
||||||
@@ -1003,10 +1011,6 @@ BOOL CMy2015RemoteDlg::OnInitDialog()
|
|||||||
m_nMaxConnection = nMaxConnection <= 0 ? 10000 : nMaxConnection;
|
m_nMaxConnection = nMaxConnection <= 0 ? 10000 : nMaxConnection;
|
||||||
}
|
}
|
||||||
const std::string method = THIS_CFG.GetStr("settings", "UDPOption", "0");
|
const std::string method = THIS_CFG.GetStr("settings", "UDPOption", "0");
|
||||||
if (!Activate(nPort, m_nMaxConnection, method)){
|
|
||||||
OnCancel();
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
int m = atoi(THIS_CFG.GetStr("settings", "ReportInterval", "5").c_str());
|
int m = atoi(THIS_CFG.GetStr("settings", "ReportInterval", "5").c_str());
|
||||||
int n = THIS_CFG.GetInt("settings", "SoftwareDetect");
|
int n = THIS_CFG.GetInt("settings", "SoftwareDetect");
|
||||||
int usingFRP = master.empty() ? 0 : THIS_CFG.GetInt("frp", "UseFrp");
|
int usingFRP = master.empty() ? 0 : THIS_CFG.GetInt("frp", "UseFrp");
|
||||||
@@ -1031,23 +1035,36 @@ BOOL CMy2015RemoteDlg::OnInitDialog()
|
|||||||
#else
|
#else
|
||||||
SetTimer(TIMER_CHECK, max(1, tm) * 60 * 1000, NULL);
|
SetTimer(TIMER_CHECK, max(1, tm) * 60 * 1000, NULL);
|
||||||
#endif
|
#endif
|
||||||
IPConverter cvt;
|
|
||||||
CString tip = !ip.empty() && ip != cvt.getPublicIP() ?
|
|
||||||
CString(ip.c_str()) + " 必须是\"公网IP\"或反向代理服务器IP":
|
|
||||||
"请设置\"公网IP\",或使用反向代理服务器的IP";
|
|
||||||
ShowMessage("使用提示", tip);
|
|
||||||
|
|
||||||
#ifdef _WIN64
|
m_hFRPThread = CreateThread(NULL, 0, StartFrpClient, this, NULL, NULL);
|
||||||
if (usingFRP) {
|
|
||||||
m_hFRPThread = CreateThread(NULL, 0, StartFrpClient, this, NULL, NULL);
|
// 最后启动SOCKET
|
||||||
|
if (!Activate(nPort, m_nMaxConnection, method)) {
|
||||||
|
OnCancel();
|
||||||
|
return FALSE;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
|
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
|
||||||
}
|
}
|
||||||
|
|
||||||
DWORD WINAPI CMy2015RemoteDlg::StartFrpClient(LPVOID param){
|
DWORD WINAPI CMy2015RemoteDlg::StartFrpClient(LPVOID param){
|
||||||
CMy2015RemoteDlg* This = (CMy2015RemoteDlg*)param;
|
CMy2015RemoteDlg* This = (CMy2015RemoteDlg*)param;
|
||||||
|
IPConverter cvt;
|
||||||
|
std::string ip = THIS_CFG.GetStr("settings", "master", "");
|
||||||
|
CString tip = !ip.empty() && ip != cvt.getPublicIP() ?
|
||||||
|
CString(ip.c_str()) + " 必须是\"公网IP\"或反向代理服务器IP" :
|
||||||
|
"请设置\"公网IP\",或使用反向代理服务器的IP";
|
||||||
|
This->PostMessageA(WM_SHOWMESSAGE, (WPARAM)new CString(tip), NULL);
|
||||||
|
int usingFRP = 0;
|
||||||
|
#ifdef _WIN64
|
||||||
|
usingFRP = ip.empty() ? 0 : THIS_CFG.GetInt("frp", "UseFrp");
|
||||||
|
#endif
|
||||||
|
if (!usingFRP) {
|
||||||
|
CloseHandle(This->m_hFRPThread);
|
||||||
|
This->m_hFRPThread = NULL;
|
||||||
|
return 0x20250820;
|
||||||
|
}
|
||||||
|
|
||||||
Mprintf("[FRP] Proxy thread start running\n");
|
Mprintf("[FRP] Proxy thread start running\n");
|
||||||
|
|
||||||
do
|
do
|
||||||
@@ -1830,6 +1847,7 @@ std::vector<std::string> splitByNewline(const std::string& input) {
|
|||||||
|
|
||||||
BOOL CMy2015RemoteDlg::Activate(const std::string& nPort,int nMaxConnection, const std::string& method)
|
BOOL CMy2015RemoteDlg::Activate(const std::string& nPort,int nMaxConnection, const std::string& method)
|
||||||
{
|
{
|
||||||
|
AUTO_TICK(200);
|
||||||
UINT ret = 0;
|
UINT ret = 0;
|
||||||
if ( (ret = THIS_APP->StartServer(NotifyProc, OfflineProc, nPort, nMaxConnection, method)) !=0 )
|
if ( (ret = THIS_APP->StartServer(NotifyProc, OfflineProc, nPort, nMaxConnection, method)) !=0 )
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user