alroyso 4 months ago
parent
commit
5738b686e5

+ 15 - 0
lib/wl_base_help.dart

@@ -37,4 +37,19 @@ class WlBaseHelp {
   Future<void> killProcess(String processName)  async {
     await  WlBaseHelpPlatform.instance.killProcess(processName);
   }
+
+  // 新增:启用代理
+  Future<void> startProxy(int port) async {
+    await _channel.invokeMethod('startProxy', {'port': port});
+  }
+
+  // 新增:禁用代理
+  Future<void> stopProxy() async {
+    await _channel.invokeMethod('stopProxy');
+  }
+
+  // 新增:检测代理是否启用
+  Future<bool> isProxyEnabled() async {
+    return await _channel.invokeMethod('isProxyEnabled');
+  }
 }

+ 16 - 0
lib/wl_base_help_method_channel.dart

@@ -59,5 +59,21 @@ class MethodChannelWlBaseHelp extends WlBaseHelpPlatform {
     final isRunning = await methodChannel.invokeMethod('isProcessRunning');
     return isRunning;
   }
+  // 新增:启用代理
+  @override
+  Future<void> startProxy(int port) async {
+    await _channel.invokeMethod('startProxy', {'port': port});
+  }
 
+  // 新增:禁用代理
+  @override
+  Future<void> stopProxy() async {
+    await _channel.invokeMethod('stopProxy');
+  }
+
+  // 新增:检测代理是否启用
+  @override
+  Future<bool> isProxyEnabled() async {
+    return await _channel.invokeMethod('isProxyEnabled');
+  }
 }

+ 13 - 0
lib/wl_base_help_platform_interface.dart

@@ -59,5 +59,18 @@ abstract class WlBaseHelpPlatform extends PlatformInterface {
     throw UnimplementedError('isProcessRunning() has not been implemented.');
   }
 
+  // 新增:启用代理
+  Future<void> startProxy(int port) async {
+    throw UnimplementedError('startProxy() has not been implemented.');
+  }
+
+  // 新增:禁用代理
+  Future<void> stopProxy() async {
+    throw UnimplementedError('stopProxy() has not been implemented.');
+  }
 
+  // 新增:检测代理是否启用
+  Future<bool> isProxyEnabled() async {
+    throw UnimplementedError('isProxyEnabled() has not been implemented.');
+  }
 }

+ 142 - 1
windows/wl_base_help_plugin.cpp

@@ -14,11 +14,139 @@
 #include <memory>
 #include <sstream>
 
+
+#include <Windows.h>
+#include <Wininet.h>
+#include <Ras.h>
+#include <string>
+#include <vector>
+#include <iostream>
+
+#pragma comment(lib, "wininet.lib")
+#pragma comment(lib, "rasapi32.lib")
 typedef LONG NTSTATUS, * PNTSTATUS;
 #define STATUS_SUCCESS (0x00000000)
 
 typedef NTSTATUS(WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
 
+
+void startProxy(const int port)
+{
+  INTERNET_PER_CONN_OPTION_LIST list;
+  DWORD dwBufSize = sizeof(list);
+  list.dwSize = sizeof(list);
+  list.pszConnection = nullptr;
+  auto url = "127.0.0.1:" + std::to_string(port);
+  auto wUrl = std::wstring(url.begin(), url.end());
+  auto fullAddr = new WCHAR[url.length() + 1];
+  wcscpy_s(fullAddr, url.length() + 1, wUrl.c_str());
+  list.dwOptionCount = 2;
+  list.pOptions = new INTERNET_PER_CONN_OPTION[2];
+
+  if (!list.pOptions)
+  {
+    return;
+  }
+
+  list.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS;
+  list.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT | PROXY_TYPE_PROXY;
+
+  list.pOptions[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER;
+  list.pOptions[1].Value.pszValue = fullAddr;
+
+  InternetSetOption(nullptr, INTERNET_OPTION_PER_CONNECTION_OPTION, &list, dwBufSize);
+
+  RASENTRYNAME entry;
+  entry.dwSize = sizeof(entry);
+  std::vector<RASENTRYNAME> entries;
+  DWORD size = sizeof(entry), count;
+  LPRASENTRYNAME entryAddr = &entry;
+  auto ret = RasEnumEntries(nullptr, nullptr, entryAddr, &size, &count);
+  if (ret == ERROR_BUFFER_TOO_SMALL)
+  {
+    entries.resize(count);
+    entries[0].dwSize = sizeof(RASENTRYNAME);
+    entryAddr = entries.data();
+    ret = RasEnumEntries(nullptr, nullptr, entryAddr, &size, &count);
+  }
+  if (ret != ERROR_SUCCESS)
+  {
+    return;
+  }
+  for (DWORD i = 0; i < count; i++)
+  {
+    list.pszConnection = entryAddr[i].szEntryName;
+    InternetSetOption(nullptr, INTERNET_OPTION_PER_CONNECTION_OPTION, &list, dwBufSize);
+  }
+  delete[] list.pOptions;
+  InternetSetOption(nullptr, INTERNET_OPTION_SETTINGS_CHANGED, nullptr, 0);
+  InternetSetOption(nullptr, INTERNET_OPTION_REFRESH, nullptr, 0);
+}
+
+void stopProxy()
+{
+  INTERNET_PER_CONN_OPTION_LIST list;
+  DWORD dwBufSize = sizeof(list);
+
+  list.dwSize = sizeof(list);
+  list.pszConnection = nullptr;
+  list.dwOptionCount = 1;
+  list.pOptions = new INTERNET_PER_CONN_OPTION[1];
+  if (nullptr == list.pOptions)
+  {
+    return;
+  }
+  list.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS;
+  list.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT;
+
+  InternetSetOption(nullptr, INTERNET_OPTION_PER_CONNECTION_OPTION, &list, dwBufSize);
+
+  RASENTRYNAME entry;
+  entry.dwSize = sizeof(entry);
+  std::vector<RASENTRYNAME> entries;
+  DWORD size = sizeof(entry), count;
+  LPRASENTRYNAME entryAddr = &entry;
+  auto ret = RasEnumEntries(nullptr, nullptr, entryAddr, &size, &count);
+  if (ret == ERROR_BUFFER_TOO_SMALL)
+  {
+    entries.resize(count);
+    entries[0].dwSize = sizeof(RASENTRYNAME);
+    entryAddr = entries.data();
+    ret = RasEnumEntries(nullptr, nullptr, entryAddr, &size, &count);
+  }
+  if (ret != ERROR_SUCCESS)
+  {
+    return;
+  }
+  for (DWORD i = 0; i < count; i++)
+  {
+    list.pszConnection = entryAddr[i].szEntryName;
+    InternetSetOption(nullptr, INTERNET_OPTION_PER_CONNECTION_OPTION, &list, dwBufSize);
+  }
+  delete[] list.pOptions;
+  InternetSetOption(nullptr, INTERNET_OPTION_SETTINGS_CHANGED, nullptr, 0);
+  InternetSetOption(nullptr, INTERNET_OPTION_REFRESH, nullptr, 0);
+}
+
+// 检测代理是否启用的函数
+bool isProxyEnabled() {
+    INTERNET_PER_CONN_OPTION_LIST list;
+    DWORD dwBufSize = sizeof(list);
+    list.dwSize = sizeof(list);
+    list.pszConnection = nullptr;
+    list.dwOptionCount = 1;
+    list.pOptions = new INTERNET_PER_CONN_OPTION[1];
+    list.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS;
+
+    bool proxyEnabled = false;
+    if (InternetQueryOption(nullptr, INTERNET_OPTION_PER_CONNECTION_OPTION, &list, &dwBufSize)) {
+        proxyEnabled = (list.pOptions[0].Value.dwValue & PROXY_TYPE_PROXY) != 0;
+    }
+
+    delete[] list.pOptions;
+    return proxyEnabled;
+}
+
 RTL_OSVERSIONINFOW GetRealOSVersion() {
     HMODULE hMod = ::GetModuleHandleW(L"ntdll.dll");
     if (hMod) {
@@ -174,7 +302,20 @@ void WlBaseHelpPlugin::HandleMethodCall(
       version_stream << "7";
     }
     result->Success(flutter::EncodableValue(version_stream.str()));
-  } else {
+  } else if (method_call.method_name().compare("startProxy") == 0) {
+       const auto* arguments = std::get_if<flutter::EncodableMap>(method_call.arguments());
+       int port = std::get<int>(arguments->at(flutter::EncodableValue("port")));
+       startProxy(port);
+       result->Success();
+     } else if (method_call.method_name().compare("stopProxy") == 0) {
+       stopProxy();
+       result->Success();
+     } else if (method_call.method_name().compare("isProxyEnabled") == 0) {
+       bool enabled = isProxyEnabled();
+       result->Success(flutter::EncodableValue(enabled));
+     } else {
+       result->NotImplemented();
+     } else {
     result->NotImplemented();
   }
 }