fix: working HTTP and SOCKS5 proxies; save only working servers

- SOCKS5 had been fed to HttpClient.findProxy, which supports only HTTP
  proxies ("Invalid proxy configuration"), and socks5h was treated as an
  HTTP proxy ("Invalid request method"). HTTP/HTTPS proxies stay on
  findProxy; socks5/socks5h tunnel through socks5_proxy, which resolves
  names at the proxy; user:pass in the proxy URL is passed along.
- Saving moved to the successful path: a failed attempt stays in the form
  with a short reason, and the server list keeps no duplicates.
- package net.blumia.openchamber.flutter (namespace, applicationId,
  MainActivity, iOS bundle id), version 0.3.1 (code 301).
- show the proxy in the instance and server lists; clearer connect errors.
- tool/proxy_probe.dart checks direct, socks5, socks5h and HTTP proxies
  (requests and SSE) against a live server.
This commit is contained in:
blumia committed 2026-11-18 22:18:45 +08:00
1 parent 782ac2bd4d
commit 55dac37baf
12 files changed
+227 -46

No files matched your search

+86 -13
View File
@@ -6,6 +6,8 @@ import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:socks5_proxy/socks_client.dart';
import 'models.dart';
class ApiError implements Exception {
@@ -16,6 +18,48 @@ class ApiError implements Exception {
String toString() => message.isEmpty ? 'HTTP $status' : message;
}
/// A proxy as configured: an HTTP CONNECT proxy, or a SOCKS5 one.
class _ProxyConfig {
final String scheme; // http | https | socks5 | socks5h
final String host;
final int port;
final String? username;
final String? password;
_ProxyConfig(this.scheme, this.host, this.port, [this.username, this.password]);
bool get socks => scheme.startsWith('socks');
}
_ProxyConfig? _parseProxy(String? raw) {
final s = raw?.trim() ?? '';
if (s.isEmpty) return null;
var normalized = s;
if (!normalized.contains('://')) normalized = 'http://$normalized';
final uri = Uri.tryParse(normalized);
if (uri == null || uri.host.isEmpty) {
throw ArgumentError('代理地址无效:$raw');
}
final scheme = uri.scheme.toLowerCase();
final socks = scheme == 'socks' || scheme == 'socks5' || scheme == 'socks5h';
final http = scheme == 'http' || scheme == 'https';
if (!socks && !http) {
throw ArgumentError('不支持的代理协议:$scheme(支持 http、https、socks5、socks5h)');
}
String? user;
String? pass;
if (uri.userInfo.isNotEmpty) {
final i = uri.userInfo.indexOf(':');
if (i >= 0) {
user = Uri.decodeComponent(uri.userInfo.substring(0, i));
pass = Uri.decodeComponent(uri.userInfo.substring(i + 1));
} else {
user = Uri.decodeComponent(uri.userInfo);
}
}
final port = uri.hasPort ? uri.port : (socks ? 1080 : 8080);
return _ProxyConfig(scheme, uri.host, port, user, pass);
}
class Client {
final Uri base;
final String? proxy; // http://host:port or socks5://host:port
@@ -23,6 +67,8 @@ class Client {
final String? fingerprint;
String? token;
late HttpClient _http;
_ProxyConfig? _proxyConfig;
InternetAddress? _proxyAddress;
Client(
this.base, {
@@ -32,23 +78,50 @@ class Client {
}) {
_http = HttpClient();
_http.connectionTimeout = const Duration(seconds: 20);
if (proxy != null && proxy!.isNotEmpty) {
_http.findProxy = (uri) {
final u = Uri.parse(proxy!);
final scheme = u.scheme.toLowerCase();
if (scheme == 'socks5' || scheme == 'socks') {
return 'SOCKS ${u.host}:${u.port}';
_proxyConfig = _parseProxy(proxy);
if (_proxyConfig != null && _proxyConfig!.socks) {
// Dart's HttpClient has no SOCKS support in findProxy; the socks5_proxy
// package tunnels the socket and hands a Socket back to the client.
_http.connectionFactory = (uri, _, __) async {
final address = await _resolveProxyAddress();
final settings = ProxySettings(address, _proxyConfig!.port,
username: _proxyConfig!.username, password: _proxyConfig!.password);
final tunnel = SocksTCPClient.connect(
[settings],
// A unix-type address carries the host name, so the proxy resolves
// it (socks5h semantics) instead of resolving it here.
InternetAddress(uri.host, type: InternetAddressType.unix),
uri.port,
);
if (uri.scheme == 'https') {
final secure = tunnel.then(
(s) => s.secure(uri.host, onBadCertificate: _badCertificate));
return ConnectionTask.fromSocket(secure, () {});
}
return 'PROXY ${u.host}:${u.port}';
return ConnectionTask.fromSocket(tunnel, () {});
};
} else if (_proxyConfig != null) {
_http.findProxy = (_) => 'PROXY ${_proxyConfig!.host}:${_proxyConfig!.port}';
}
if (insecure || (fingerprint != null && fingerprint!.isNotEmpty)) {
_http.badCertificateCallback = (cert, host, port) {
if (insecure) return true;
final der = _sha256(cert.der);
return der == fingerprint!.replaceAll(':', '');
};
_http.badCertificateCallback = (cert, host, port) => _badCertificate(cert);
}
Future<InternetAddress> _resolveProxyAddress() async {
if (_proxyAddress != null) return _proxyAddress!;
final addresses = await InternetAddress.lookup(_proxyConfig!.host);
if (addresses.isEmpty) {
throw const SocketException('无法解析代理主机');
}
return _proxyAddress = addresses.first;
}
/// True for a certificate the app was told to accept: the user trusts any
/// (insecure), or its SHA-256 matches the pinned fingerprint.
bool _badCertificate(X509Certificate cert) {
if (insecure) return true;
final pinned = fingerprint?.replaceAll(':', '').toLowerCase();
if (pinned == null || pinned.isEmpty) return false;
return _sha256(cert.der) == pinned;
}
String get baseUrl => base.toString();