Skip to content

Commit

Permalink
feat: ✨ 新增代理服务器时,检测是否可以连接。
Browse files Browse the repository at this point in the history
feat: ✨ When adding a proxy server, detect whether it can be connected.。
  • Loading branch information
ys1231 committed Aug 24, 2024
1 parent b813944 commit a014076
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class MyApp extends StatelessWidget {
error: Colors.transparent, // 可选:设置错误颜色为透明,避免产生额外的颜色
// 其他颜色也可以根据需要设置为透明或自定义颜色
),
appBarTheme: const AppBarTheme(centerTitle: true)
),
// 设置底部导航菜单作为应用的起始页面
home: const iyueMainPage(),
Expand Down
52 changes: 49 additions & 3 deletions lib/ui/addproxy.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -69,9 +71,7 @@ class _AddProxyWidgetState extends State<AddProxyWidget> {
proxyConfig['proxyHost'] = _controller_proxyHost.text;
proxyConfig['proxyPort'] = _controller_proxyPort.text;
if (isNullOrEmpty(proxyConfig)) {
if (kDebugMode) {
print("proxyConfig:$proxyConfig");
}
debugPrint("proxyConfig:$proxyConfig");
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: const Text('参数请填写完整!'),
backgroundColor: Colors.purple.withOpacity(0.4)));
Expand Down Expand Up @@ -130,6 +130,7 @@ class _AddProxyWidgetState extends State<AddProxyWidget> {
if (value.isNotEmpty && !RegExp(r'^[0-9.]+$').hasMatch(value)) {
_controller_proxyHost.text = value.substring(0, value.length - 1);
}
checkConnect(context);
},
),
const SizedBox(height: 20.0),
Expand All @@ -144,6 +145,7 @@ class _AddProxyWidgetState extends State<AddProxyWidget> {
if (value.isNotEmpty && !RegExp(r'^[0-9]+$').hasMatch(value)) {
_controller_proxyPort.text = value.substring(0, value.length - 1);
}
checkConnect(context);
},
),
const SizedBox(height: 20.0),
Expand Down Expand Up @@ -174,6 +176,50 @@ class _AddProxyWidgetState extends State<AddProxyWidget> {
),
)));
}

void checkConnect(context) async {
final ip = _controller_proxyHost.text;
final port = _controller_proxyPort.text;
final proxyType = _controller_proxyType.text;
if (ip.isEmpty || port.isEmpty){
return ;
}
// 1. 判断代理类型 http 或者 socks5
if ("http"== proxyType) {
// http代理
try{
final response = await Dio().get('http://$ip:$port', options: Options(
sendTimeout: const Duration(seconds: 1),
receiveTimeout: const Duration(seconds: 1)
));
debugPrint("connect status_code:${response.statusCode}");
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(
content: Text('connect success'),
backgroundColor: Colors.greenAccent)
);
}catch(e){
debugPrint(e.toString());
}
} else if ("socks5" == proxyType){
// socks5代理
try{
final socket = await Socket.connect(ip, int.parse(port), timeout: const Duration(seconds: 1));
socket.close();
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(
content: Text('connect success'),
backgroundColor: Colors.greenAccent)
);
}catch(e){
debugPrint(e.toString());
}
}else{

}

}

}

class ProxyType extends StatefulWidget {
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/app_config_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class AppConfigState extends State<AppConfigList> {
*/
return Scaffold(
appBar: AppBar(
title: const Text('AppConfigList'),
title: const Text('APP 配置列表'),
backgroundColor: Theme.of(context).primaryColor,
actions: <Widget>[
AnimatedCrossFade(
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/proxy_config_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class _ProxyListHomeState extends State<ProxyListHome> {
// debugPrint("---- ProxyListHome build call: $_dataLists");
return Scaffold(
appBar: AppBar(
title: const Text('ProxyConfig'),
title: const Text('Server 配置列表'),
backgroundColor: Theme.of(context).primaryColor,
),
body: ListView.separated(
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class _AppSettingsState extends State<AppSettings> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Settings'),
title: const Text('设置'),
backgroundColor: Theme.of(context).primaryColor,
),
body: Column(
Expand Down

0 comments on commit a014076

Please sign in to comment.