mirror of
https://github.com/wanghongenpin/proxypin.git
synced 2026-06-03 17:25:48 +08:00
引导安装根证书 排除websocket 启动失败提示
This commit is contained in:
@@ -146,12 +146,11 @@ class ChannelPipeline extends ChannelHandler<Uint8List> {
|
||||
data.hostAndPort?.host = data.headers.host()!;
|
||||
}
|
||||
|
||||
data.remoteDomain = data.hostAndPort?.domain;
|
||||
data.requestUrl = data.uri.startsWith("/") ? '${data.remoteDomain}${data.uri}' : data.uri;
|
||||
try {
|
||||
data.path = data.hostAndPort?.isSsl() == true ? data.uri : Uri.parse(data.requestUrl).path;
|
||||
} catch (e) {
|
||||
logger.e("data.requestUrl ${data.requestUrl}", e, StackTrace.current);
|
||||
//websocket协议
|
||||
if (data.headers.get("Upgrade") == 'websocket' && channel.getAttribute(channel.id) != null) {
|
||||
relay(channel, channel.getAttribute(channel.id));
|
||||
channel.pipeline.channelRead(channel, msg);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -374,7 +373,8 @@ class Client extends Network {
|
||||
return Socket.connect(host, hostAndPort.port, timeout: const Duration(seconds: 3)).then((socket) => listen(socket));
|
||||
}
|
||||
|
||||
Future<Channel> sllConnect(HostAndPort hostAndPort) async {
|
||||
/// ssl连接
|
||||
Future<Channel> secureConnect(HostAndPort hostAndPort) async {
|
||||
return SecureSocket.connect(hostAndPort.host, hostAndPort.port,
|
||||
timeout: const Duration(seconds: 3), onBadCertificate: (certificate) => true).then((socket) => listen(socket));
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ class HttpChannelHandler extends ChannelHandler<HttpRequest> {
|
||||
//请求本服务
|
||||
localRequest(HttpRequest msg, Channel channel) async {
|
||||
//获取配置
|
||||
if (msg.path == '/config') {
|
||||
if (msg.path() == '/config') {
|
||||
var response = HttpResponse(msg.protocolVersion, HttpStatus.ok);
|
||||
var body = {
|
||||
"requestRewrites": requestRewrites?.toJson(),
|
||||
@@ -92,13 +92,14 @@ class HttpChannelHandler extends ChannelHandler<HttpRequest> {
|
||||
|
||||
/// 转发请求
|
||||
Future<void> forward(Channel channel, HttpRequest httpRequest) async {
|
||||
|
||||
var remoteChannel = await _getRemoteChannel(channel, httpRequest);
|
||||
|
||||
//实现抓包代理转发
|
||||
if (httpRequest.method != HttpMethod.connect) {
|
||||
// log.i("[${channel.id}] ${httpRequest.requestUrl}");
|
||||
|
||||
var replaceBody = requestRewrites?.findRequestReplaceWith(httpRequest.path);
|
||||
var replaceBody = requestRewrites?.findRequestReplaceWith(httpRequest.path());
|
||||
if (replaceBody?.isNotEmpty == true) {
|
||||
httpRequest.body = utf8.encode(replaceBody!);
|
||||
}
|
||||
@@ -146,13 +147,13 @@ class HttpChannelHandler extends ChannelHandler<HttpRequest> {
|
||||
//远程代理
|
||||
HostAndPort? remote = clientChannel.getAttribute(AttributeKeys.remote);
|
||||
if (remote != null) {
|
||||
var proxyChannel = await HttpClients.connect(remote, proxyHandler);
|
||||
var proxyChannel = await HttpClients.rawConnect(remote, proxyHandler);
|
||||
clientChannel.putAttribute(clientId, proxyChannel);
|
||||
proxyChannel.write(httpRequest);
|
||||
return proxyChannel;
|
||||
}
|
||||
|
||||
var proxyChannel = await HttpClients.connect(hostAndPort, proxyHandler);
|
||||
var proxyChannel = await HttpClients.rawConnect(hostAndPort, proxyHandler);
|
||||
clientChannel.putAttribute(clientId, proxyChannel);
|
||||
|
||||
//https代理新建连接请求
|
||||
@@ -179,7 +180,7 @@ class HttpResponseProxyHandler extends ChannelHandler<HttpResponse> {
|
||||
msg.request?.response = msg;
|
||||
// log.i("[${clientChannel.id}] Response ${msg.bodyAsString}");
|
||||
|
||||
var replaceBody = requestRewrites?.findResponseReplaceWith(msg.request?.path);
|
||||
var replaceBody = requestRewrites?.findResponseReplaceWith(msg.request?.path());
|
||||
if (replaceBody?.isNotEmpty == true) {
|
||||
msg.body = utf8.encode(replaceBody!);
|
||||
}
|
||||
|
||||
@@ -51,16 +51,25 @@ abstract class HttpMessage {
|
||||
class HttpRequest extends HttpMessage {
|
||||
final String uri;
|
||||
late HttpMethod method;
|
||||
late String requestUrl;
|
||||
|
||||
String? path;
|
||||
HostAndPort? hostAndPort;
|
||||
final DateTime requestTime = DateTime.now();
|
||||
String? remoteDomain;
|
||||
HttpResponse? response;
|
||||
|
||||
HttpRequest(this.method, this.uri, {String protocolVersion = "HTTP/1.1"}) : super(protocolVersion);
|
||||
|
||||
String? remoteDomain() => hostAndPort?.domain;
|
||||
|
||||
String get requestUrl => uri.startsWith("/") ? '${remoteDomain()}$uri' : uri;
|
||||
|
||||
String? path() {
|
||||
try {
|
||||
return hostAndPort?.isSsl() == true ? uri : Uri.parse(requestUrl).path;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
///复制请求
|
||||
HttpRequest copy({String? uri}) {
|
||||
var request = HttpRequest(method, uri ?? this.uri, protocolVersion: protocolVersion);
|
||||
|
||||
@@ -8,13 +8,24 @@ import 'http/codec.dart';
|
||||
|
||||
class HttpClients {
|
||||
/// 建立连接
|
||||
static Future<Channel> connect(HostAndPort hostAndPort, ChannelHandler handler) async {
|
||||
static Future<Channel> rawConnect(HostAndPort hostAndPort, ChannelHandler handler) async {
|
||||
var client = Client()
|
||||
..initChannel((channel) => channel.pipeline.handle(HttpResponseCodec(), HttpRequestCodec(), handler));
|
||||
|
||||
return client.connect(hostAndPort);
|
||||
}
|
||||
|
||||
/// 建立连接
|
||||
static Future<Channel> connect(Uri uri, ChannelHandler handler) async {
|
||||
Client client = Client()
|
||||
..initChannel((channel) => channel.pipeline.handle(HttpResponseCodec(), HttpRequestCodec(), handler));
|
||||
if (uri.scheme == "https" || uri.scheme == "wss") {
|
||||
return client.secureConnect(HostAndPort.of(uri.toString()));
|
||||
}
|
||||
|
||||
return client.connect(HostAndPort.of(uri.toString()));
|
||||
}
|
||||
|
||||
/// 发送get请求
|
||||
static Future<HttpResponse> get(String url, {Duration duration = const Duration(seconds: 3)}) async {
|
||||
HttpRequest msg = HttpRequest(HttpMethod.get, url);
|
||||
|
||||
@@ -75,5 +75,6 @@ class Blacks extends HostList {
|
||||
list.add(RegExp("github.com"));
|
||||
list.add(RegExp(".*.google.com"));
|
||||
list.add(RegExp(".*.apple.com"));
|
||||
list.add(RegExp(".*.icloud.com"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user