Skip to content

Commit

Permalink
Script headers support multiple values #259
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghongenpin committed Jul 19, 2024
1 parent fc66f57 commit ac39b41
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
9 changes: 9 additions & 0 deletions lib/network/components/script_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ async function onResponse(context, request, response) {
request.uri = Uri.parse('${request.remoteDomain()}${map['path']}?$query').toString();

map['headers'].forEach((key, value) {
if (value is List) {
request.headers.addValues(key, value.map((e) => e.toString()).toList());
return;
}
request.headers.add(key, value);
});
request.body = map['body'] == null ? null : utf8.encode(map['body'].toString());
Expand All @@ -324,6 +328,11 @@ async function onResponse(context, request, response) {
response.headers.clear();
response.status = HttpStatus.valueOf(map['statusCode']);
map['headers'].forEach((key, value) {
if (value is List) {
response.headers.addValues(key, value.map((e) => e.toString()).toList());
return;
}

response.headers.add(key, value);
});
response.headers.remove(HttpHeaders.CONTENT_ENCODING);
Expand Down
4 changes: 2 additions & 2 deletions lib/network/handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import 'package:network_proxy/network/http/http.dart';
import 'package:network_proxy/network/http/websocket.dart';
import 'package:network_proxy/network/proxy_helper.dart';
import 'package:network_proxy/network/util/attribute_keys.dart';
import 'package:network_proxy/network/util/localizations.dart';
import 'package:network_proxy/network/util/logger.dart';
import 'package:network_proxy/network/util/uri.dart';
import 'package:network_proxy/utils/ip.dart';
Expand All @@ -51,7 +52,6 @@ class HttpProxyChannelHandler extends ChannelHandler<HttpRequest> {

@override
void channelRead(ChannelContext channelContext, Channel channel, HttpRequest msg) async {

//下载证书
if (msg.uri == 'http://proxy.pin/ssl' || msg.requestUrl == 'http://127.0.0.1:${channel.socket.port}/ssl') {
ProxyHelper.crtDownload(channel, msg);
Expand Down Expand Up @@ -253,7 +253,7 @@ class HttpResponseProxyHandler extends ChannelHandler<HttpResponse> {
}
msg = response;
} catch (e, t) {
msg.status = HttpStatus(-1, '执行脚本异常');
msg.status = HttpStatus(-1, Localizations.isEN ? 'Script exec error' : '执行脚本异常');
msg.body = "$e\n${msg.bodyAsString}".codeUnits;
log.e('[${clientChannel.id}] 执行脚本异常 ', error: e, stackTrace: t);
}
Expand Down
4 changes: 3 additions & 1 deletion lib/network/proxy_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:network_proxy/network/http/codec.dart';
import 'package:network_proxy/network/http/http.dart';
import 'package:network_proxy/network/http/http_headers.dart';
import 'package:network_proxy/network/util/file_read.dart';
import 'package:network_proxy/network/util/localizations.dart';

import 'components/host_filter.dart';

Expand Down Expand Up @@ -71,7 +72,8 @@ class ProxyHelper {
String message = error.toString();
HttpStatus status = HttpStatus(-1, message);
if (error is HandshakeException) {
status = HttpStatus(-2, 'SSL握手失败');
status = HttpStatus(
-2, Localizations.isEN ? 'SSL handshake failed, please check the certificate' : 'SSL握手失败,请检查证书安装是否正确');
} else if (error is ParserException) {
status = HttpStatus(-3, error.message);
} else if (error is SocketException) {
Expand Down
8 changes: 8 additions & 0 deletions lib/network/util/localizations.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:network_proxy/ui/configuration.dart';

class Localizations {

static bool get isEN {
return AppConfiguration.current?.language?.languageCode == 'en';
}
}

0 comments on commit ac39b41

Please sign in to comment.