setSystemProxy function

Future<void> setSystemProxy(
  1. int port
)

设置Windows系统代理

Implementation

Future<void> setSystemProxy(int port) async {
  if (Platform.isWindows) {
    try {
      // 1. 设置代理地址 (ProxyServer = 127.0.0.1:2080)
      await Process.run('reg', [
        'add',
        'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings',
        '/v',
        'ProxyServer',
        '/t',
        'REG_SZ',
        '/d',
        '127.0.0.1:$port',
        '/f',
      ]);
      // 2. 开启代理开关 (ProxyEnable = 1)
      await Process.run('reg', [
        'add',
        'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings',
        '/v',
        'ProxyEnable',
        '/t',
        'REG_DWORD',
        '/d',
        '1',
        '/f',
      ]);
      // 3. 设置跳过列表,防止本地流量死循环
      await Process.run('reg', [
        'add',
        'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings',
        '/v',
        'ProxyOverride',
        '/t',
        'REG_SZ',
        '/d',
        'localhost;127.*;192.168.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;<local>',
        '/f',
      ]);
      devlog('[setSystemProxy] ✅ 系统代理指向: 127.0.0.1:$port');
    } catch (e) {
      devlog('[setSystemProxy] ❌ 设置失败: $e');
    }
  }
}