#pragma once #include #include #include #include #include #pragma comment(lib,"crypt32.lib") #pragma comment(lib,"advapi32.lib") struct ProxySettings { int type=0; std::wstring host,port=L"1080",user,password; bool operator==(const ProxySettings&) const=default; }; inline bool ValidateProxy(const ProxySettings& value,std::wstring& error) { if(value.type<0 || value.type>2) { error=L"代理配置无效或无法解密,请重新保存"; return false; } for(const auto* field:{&value.host,&value.port,&value.user,&value.password}) { if(field->size()>1024) { error=L"代理字段过长"; return false; } for(auto c:*field) if(c<32 || c==127) { error=L"代理字段不能包含控制字符"; return false; } } if(value.type==0) return true; if(value.host.empty() || value.host.size()>253 || value.host.find_first_of(L" /\\@?#\r\n\t")!=std::wstring::npos) { error=L"请输入主机名或 IP,不含协议和端口"; return false; } if(value.host.find(L':')!=std::wstring::npos && value.host.find(L':')==value.host.rfind(L':')) { error=L"端口请填在独立端口框中"; return false; } unsigned port=0; if(value.port.empty() || value.port.size()>5) { error=L"代理端口无效"; return false; } for(auto c:value.port) { if(cL'9') { error=L"代理端口无效"; return false; } port=port*10+c-L'0'; } if(!port || port>65535) { error=L"代理端口超出范围"; return false; } for(const auto* text:{&value.user,&value.password}) { if(text->size()>255) { error=L"代理账号或密码过长"; return false; } for(auto c:*text) if(c<32 || c==127) { error=L"代理账号密码不能包含控制字符"; return false; } } return true; } inline ProxySettings LoadProxy() { HKEY key=nullptr; auto opened=RegOpenKeyExW(HKEY_CURRENT_USER,L"Software\\TTDownloader",0,KEY_QUERY_VALUE,&key); if(opened==ERROR_FILE_NOT_FOUND || opened==ERROR_PATH_NOT_FOUND) return {}; if(opened!=ERROR_SUCCESS) { ProxySettings invalid; invalid.type=-1; return invalid; } DWORD type=0,size=0; auto status=RegQueryValueExW(key,L"ProxyDPAPI",nullptr,&type,nullptr,&size); if(status==ERROR_FILE_NOT_FOUND) { RegCloseKey(key); return {}; } ProxySettings invalid; invalid.type=-1; if(status!=ERROR_SUCCESS || type!=REG_BINARY || size>16384 || !size) { RegCloseKey(key); return invalid; } std::vector bytes(size); status=RegQueryValueExW(key,L"ProxyDPAPI",nullptr,&type,bytes.data(),&size); RegCloseKey(key); if(status!=ERROR_SUCCESS) return invalid; DATA_BLOB encrypted{size,bytes.data()},plain{}; if(!CryptUnprotectData(&encrypted,nullptr,nullptr,nullptr,nullptr,CRYPTPROTECT_UI_FORBIDDEN,&plain)) return invalid; std::wstring text; if(plain.cbData%2==0) text.assign(reinterpret_cast(plain.pbData),plain.cbData/2); SecureZeroMemory(plain.pbData,plain.cbData); LocalFree(plain.pbData); std::vector parts; size_t begin=0; for(int i=0;i<4;++i) { auto end=text.find(L'\n',begin); if(end==std::wstring::npos) return invalid; parts.push_back(text.substr(begin,end-begin)); begin=end+1; } parts.push_back(text.substr(begin)); ProxySettings result; result.type=parts[0]==L"0"?0:parts[0]==L"1"?1:parts[0]==L"2"?2:-1; result.host=parts[1]; result.port=parts[2]; result.user=parts[3]; result.password=parts[4]; std::wstring error; return ValidateProxy(result,error)?result:invalid; } inline bool SaveProxy(const ProxySettings& value) { std::wstring error; if(!ValidateProxy(value,error)) return false; auto text=std::to_wstring(value.type)+L"\n"+value.host+L"\n"+value.port+L"\n"+value.user+L"\n"+value.password; DATA_BLOB plain{static_cast(text.size()*2),reinterpret_cast(text.data())},encrypted{}; if(!CryptProtectData(&plain,L"TTDownloader proxy",nullptr,nullptr,nullptr,CRYPTPROTECT_UI_FORBIDDEN,&encrypted)) return false; HKEY key=nullptr; bool saved=false; if(RegCreateKeyExW(HKEY_CURRENT_USER,L"Software\\TTDownloader",0,nullptr,0,KEY_SET_VALUE,nullptr,&key,nullptr)==ERROR_SUCCESS) { saved=RegSetValueExW(key,L"ProxyDPAPI",0,REG_BINARY,encrypted.pbData,encrypted.cbData)==ERROR_SUCCESS; RegCloseKey(key); } LocalFree(encrypted.pbData); return saved; } struct ProxyState { std::mutex mutex; ProxySettings value=LoadProxy(); }; inline ProxyState& SharedProxyState() { static ProxyState state; return state; } inline ProxySettings CurrentProxy() { auto& state=SharedProxyState(); std::lock_guard lock(state.mutex); return state.value; } inline void SetCurrentProxy(ProxySettings value) { auto& state=SharedProxyState(); std::lock_guard lock(state.mutex); state.value=std::move(value); }