|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:kazumi/bean/dialog/dialog_helper.dart'; |
| 3 | +import 'package:hive/hive.dart'; |
| 4 | +import 'package:kazumi/bean/appbar/sys_app_bar.dart'; |
| 5 | +import 'package:kazumi/utils/storage.dart'; |
| 6 | +import 'package:kazumi/utils/proxy_utils.dart'; |
| 7 | +import 'package:kazumi/utils/proxy_manager.dart'; |
| 8 | +import 'package:kazumi/request/request.dart'; |
| 9 | + |
| 10 | +class ProxyEditorPage extends StatefulWidget { |
| 11 | + const ProxyEditorPage({super.key}); |
| 12 | + |
| 13 | + @override |
| 14 | + State<ProxyEditorPage> createState() => _ProxyEditorPageState(); |
| 15 | +} |
| 16 | + |
| 17 | +class _ProxyEditorPageState extends State<ProxyEditorPage> { |
| 18 | + Box setting = GStorage.setting; |
| 19 | + final _formKey = GlobalKey<FormState>(); |
| 20 | + final TextEditingController urlController = TextEditingController(); |
| 21 | + final TextEditingController usernameController = TextEditingController(); |
| 22 | + final TextEditingController passwordController = TextEditingController(); |
| 23 | + bool passwordVisible = false; |
| 24 | + |
| 25 | + @override |
| 26 | + void initState() { |
| 27 | + super.initState(); |
| 28 | + urlController.text = |
| 29 | + setting.get(SettingBoxKey.proxyUrl, defaultValue: ''); |
| 30 | + usernameController.text = |
| 31 | + setting.get(SettingBoxKey.proxyUsername, defaultValue: ''); |
| 32 | + passwordController.text = |
| 33 | + setting.get(SettingBoxKey.proxyPassword, defaultValue: ''); |
| 34 | + } |
| 35 | + |
| 36 | + @override |
| 37 | + void dispose() { |
| 38 | + urlController.dispose(); |
| 39 | + usernameController.dispose(); |
| 40 | + passwordController.dispose(); |
| 41 | + super.dispose(); |
| 42 | + } |
| 43 | + |
| 44 | + Future<void> saveAndTest() async { |
| 45 | + if (!_formKey.currentState!.validate()) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + final url = urlController.text.trim(); |
| 50 | + if (url.isEmpty) { |
| 51 | + KazumiDialog.showToast(message: '请输入代理地址'); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + await setting.put(SettingBoxKey.proxyUrl, url); |
| 56 | + await setting.put(SettingBoxKey.proxyUsername, usernameController.text); |
| 57 | + await setting.put(SettingBoxKey.proxyPassword, passwordController.text); |
| 58 | + |
| 59 | + KazumiDialog.showToast(message: '配置已保存,开始测试'); |
| 60 | + |
| 61 | + final proxyEnable = |
| 62 | + setting.get(SettingBoxKey.proxyEnable, defaultValue: false); |
| 63 | + if (!proxyEnable) { |
| 64 | + await setting.put(SettingBoxKey.proxyEnable, true); |
| 65 | + } |
| 66 | + ProxyManager.applyProxy(); |
| 67 | + |
| 68 | + try { |
| 69 | + final response = await Request().get( |
| 70 | + 'https://www.google.com', |
| 71 | + extra: {'customError': true}, |
| 72 | + shouldRethrow: true, |
| 73 | + ); |
| 74 | + if (response.statusCode == 200) { |
| 75 | + KazumiDialog.showToast(message: '测试成功'); |
| 76 | + } else { |
| 77 | + KazumiDialog.showToast(message: '测试失败: HTTP ${response.statusCode}'); |
| 78 | + if (!proxyEnable) { |
| 79 | + await setting.put(SettingBoxKey.proxyEnable, false); |
| 80 | + ProxyManager.clearProxy(); |
| 81 | + } |
| 82 | + } |
| 83 | + } catch (e) { |
| 84 | + KazumiDialog.showToast(message: '测试失败: $e'); |
| 85 | + if (!proxyEnable) { |
| 86 | + await setting.put(SettingBoxKey.proxyEnable, false); |
| 87 | + ProxyManager.clearProxy(); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @override |
| 93 | + Widget build(BuildContext context) { |
| 94 | + return Scaffold( |
| 95 | + appBar: const SysAppBar(title: Text('代理配置')), |
| 96 | + body: SingleChildScrollView( |
| 97 | + padding: const EdgeInsets.all(16.0), |
| 98 | + child: Center( |
| 99 | + child: SizedBox( |
| 100 | + width: (MediaQuery.of(context).size.width > 800) ? 800 : null, |
| 101 | + child: Form( |
| 102 | + key: _formKey, |
| 103 | + child: Column( |
| 104 | + children: [ |
| 105 | + TextFormField( |
| 106 | + controller: urlController, |
| 107 | + decoration: const InputDecoration( |
| 108 | + labelText: '代理地址', |
| 109 | + hintText: 'http://127.0.0.1:7890', |
| 110 | + border: OutlineInputBorder(), |
| 111 | + ), |
| 112 | + validator: (value) { |
| 113 | + if (value == null || value.isEmpty) { |
| 114 | + return '请输入代理地址'; |
| 115 | + } |
| 116 | + if (!ProxyUtils.isValidProxyUrl(value)) { |
| 117 | + return '格式错误,请使用 http://host:port 格式'; |
| 118 | + } |
| 119 | + return null; |
| 120 | + }, |
| 121 | + ), |
| 122 | + const SizedBox(height: 20), |
| 123 | + TextFormField( |
| 124 | + controller: usernameController, |
| 125 | + decoration: const InputDecoration( |
| 126 | + labelText: '用户名(可选)', |
| 127 | + border: OutlineInputBorder(), |
| 128 | + ), |
| 129 | + ), |
| 130 | + const SizedBox(height: 20), |
| 131 | + TextFormField( |
| 132 | + controller: passwordController, |
| 133 | + obscureText: !passwordVisible, |
| 134 | + decoration: InputDecoration( |
| 135 | + labelText: '密码(可选)', |
| 136 | + border: const OutlineInputBorder(), |
| 137 | + suffixIcon: IconButton( |
| 138 | + onPressed: () { |
| 139 | + setState(() { |
| 140 | + passwordVisible = !passwordVisible; |
| 141 | + }); |
| 142 | + }, |
| 143 | + icon: Icon(passwordVisible |
| 144 | + ? Icons.visibility_rounded |
| 145 | + : Icons.visibility_off_rounded), |
| 146 | + ), |
| 147 | + ), |
| 148 | + ), |
| 149 | + ], |
| 150 | + ), |
| 151 | + ), |
| 152 | + ), |
| 153 | + ), |
| 154 | + ), |
| 155 | + floatingActionButton: FloatingActionButton.extended( |
| 156 | + onPressed: saveAndTest, |
| 157 | + icon: const Icon(Icons.save), |
| 158 | + label: const Text('保存并测试'), |
| 159 | + ), |
| 160 | + ); |
| 161 | + } |
| 162 | +} |
0 commit comments