Skip to content

Commit

Permalink
增加工具箱,提供常用编解码码,增加HTTP请求,可粘贴cURL格式解析发起请求
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghongenpin committed Aug 27, 2023
1 parent 4154bda commit b748aa6
Show file tree
Hide file tree
Showing 22 changed files with 667 additions and 148 deletions.
9 changes: 8 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:desktop_multi_window/desktop_multi_window.dart';
import 'package:flutter/material.dart';
import 'package:network_proxy/network/bin/configuration.dart';
import 'package:network_proxy/ui/component/chinese_font.dart';
import 'package:network_proxy/ui/component/encoder.dart';
import 'package:network_proxy/ui/content/body.dart';
import 'package:network_proxy/ui/desktop/desktop.dart';
import 'package:network_proxy/ui/desktop/left/request_editor.dart';
Expand Down Expand Up @@ -54,7 +55,8 @@ void main(List<String> args) async {
Widget multiWindow(int windowId, Map<dynamic, dynamic> argument) {
if (argument['name'] == 'RequestEditor') {
return RequestEditor(
windowController: WindowController.fromWindowId(windowId), request: HttpRequest.fromJson(argument['request']));
windowController: WindowController.fromWindowId(windowId),
request: argument['request'] == null ? null : HttpRequest.fromJson(argument['request']));
}

if (argument['name'] == 'HttpBodyWidget') {
Expand All @@ -64,6 +66,11 @@ Widget multiWindow(int windowId, Map<dynamic, dynamic> argument) {
inNewWindow: true);
}

if (argument['name'] == 'EncoderWidget') {
return EncoderWidget(
type: EncoderType.nameOf(argument['type']), windowController: WindowController.fromWindowId(windowId));
}

return const SizedBox();
}

Expand Down
4 changes: 2 additions & 2 deletions lib/network/channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ class ChannelPipeline extends ChannelHandler<Uint8List> {

if (data is HttpRequest) {
data.hostAndPort = channel.getAttribute(AttributeKeys.host) ?? getHostAndPort(data);
if (data.headers.host() != null && data.headers.host()?.contains(":") == false) {
data.hostAndPort?.host = data.headers.host()!;
if (data.headers.host != null && data.headers.host?.contains(":") == false) {
data.hostAndPort?.host = data.headers.host!;
}

//websocket协议
Expand Down
11 changes: 10 additions & 1 deletion lib/network/http/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,16 @@ class HttpRequest extends HttpMessage {

HttpRequest(this.method, this.uri, {String protocolVersion = "HTTP/1.1"}) : super(protocolVersion);

String? remoteDomain() => hostAndPort?.domain;
String? remoteDomain() {
if (hostAndPort == null) {
try {
return Uri.parse(uri).host;
} catch (e) {
return null;
}
}
return hostAndPort?.domain;
}

String get requestUrl => uri.startsWith("/") ? '${remoteDomain()}$uri' : uri;

Expand Down
8 changes: 7 additions & 1 deletion lib/network/http/http_headers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@ class HttpHeaders {

String get contentType => get(CONTENT_TYPE) ?? "";

String? host() => get(HOST);
String? get host => get(HOST);

set host(String? host) {
if (host != null) {
set(HOST, host);
}
}

String headerLines() {
StringBuffer sb = StringBuffer();
Expand Down
7 changes: 7 additions & 0 deletions lib/network/http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ class HttpClients {
var proxyTypes = request.uri.startsWith("https://") ? ProxyTypes.https : ProxyTypes.http;
proxyInfo = await SystemProxy.getSystemProxy(proxyTypes);
}

if (request.headers.host == null || request.headers.host?.trim().isEmpty == true) {
try {
request.headers.host = Uri.parse(request.uri).host;
} catch (_) {}
}

var httpResponseHandler = HttpResponseHandler();

HostAndPort hostPort = HostAndPort.of(request.uri);
Expand Down
89 changes: 43 additions & 46 deletions lib/network/util/system_proxy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,14 @@ class SystemProxy {
_hardwarePort = await hardwarePort();
var result = await Process.run('bash', [
'-c',
'networksetup ${proxyTypes == ProxyTypes.http ? '-getwebproxy' : '-getsecurewebproxy'} $_hardwarePort',
'networksetup ${proxyTypes == ProxyTypes.http ? '-getwebproxy' : '-getsecurewebproxy'} $_hardwarePort'
]).then((results) => results.stdout.toString().split('\n'));

var proxyEnable = result.firstWhere((item) => item.contains('Enabled')).trim().split(": ")[1];
if (proxyEnable == 'No') {
return null;
}

var proxyServer = result.firstWhere((item) => item.contains('Server')).trim().split(": ")[1];
var proxyPort = result.firstWhere((item) => item.contains('Port')).trim().split(": ")[1];
if (proxyEnable == 'Yes' && proxyServer.isNotEmpty) {
Expand All @@ -107,7 +111,7 @@ class SystemProxy {
'-c',
_concatCommands([
'networksetup -setwebproxystate $_hardwarePort $proxyMode',
sslSetting ? 'networksetup -setsecurewebproxystate $_hardwarePort $proxyMode' : '',
sslSetting ? 'networksetup -setsecurewebproxystate $_hardwarePort $proxyMode' : ''
])
]);
return results.exitCode == 0;
Expand All @@ -120,7 +124,7 @@ class SystemProxy {
'-c',
proxyEnable
? 'networksetup -setsecurewebproxy $name 127.0.0.1 $port'
: 'networksetup -setsecurewebproxystate $name off',
: 'networksetup -setsecurewebproxystate $name off'
]);
return results.exitCode == 0;
}
Expand All @@ -129,76 +133,69 @@ class SystemProxy {
var name = await networkName();
var results = await Process.run('bash', [
'-c',
_concatCommands([
'networksetup -listnetworkserviceorder |grep "Device: $name" -A 1 |grep "Hardware Port" |awk -F ": " \'{print \$2}\'',
])
'networksetup -listnetworkserviceorder |grep "Device: $name" -A 1 |grep "Hardware Port" |awk -F ": " \'{print \$2}\''
]);
return results.stdout.toString().split(", ")[0];
}

static Future<bool> _setProxyServerWindows(int proxyPort) async {
print("setSystemProxy $proxyPort");

static Future<void> _setProxyServerWindows(int proxyPort) async {
ProxyManager manager = ProxyManager();
await manager.setAsSystemProxy(ProxyTypes.https, "127.0.0.1", proxyPort);
print("setSystemProxy end");
var results = await Process.run('reg', [
'add',
'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings',
'/v',
var results = await _internetSettings('add', [
'ProxyOverride',
'/t',
'REG_SZ',
'/d',
'192.168.0.*;10.0.0.*;172.16.0.*;127.0.0.1;localhost;*.local;<local>',
'/f',
'/f'
]);

print('set proxyServer $proxyPort, exitCode: ${results.exitCode}, stdout: ${results.stderr}');
return results.exitCode == 0;
print('set proxyServer $proxyPort, result: $results');
}

static Future<bool> setProxyEnableWindows(bool proxyEnable) async {
var results = await Process.run('reg', [
'add',
'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings',
'/v',
'ProxyEnable',
'/t',
'REG_DWORD',
'/f',
'/d',
proxyEnable ? '1' : '0',
]);
return results.exitCode == 0;
static Future<void> setProxyEnableWindows(bool proxyEnable) async {
await _internetSettings('add', ['ProxyEnable', '/t', 'REG_DWORD', '/f', '/d', proxyEnable ? '1' : '0']);
}

/// 获取系统代理
static Future<ProxyInfo?> _getSystemProxyWindows() async {
var results = await Process.run('reg', [
'query',
'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings',
'/v',
'ProxyEnable',
]).then((it) => it.stdout.toString());
var results = await _internetSettings('query', ['ProxyEnable']);

var proxyEnableLine = results.split('\r\n').where((item) => item.contains('ProxyEnable')).first;
var proxyEnableLine = results.split('\r\n').where((item) => item.contains('ProxyEnable')).first.trim();
if (proxyEnableLine.substring(proxyEnableLine.length - 1) != '1') {
return null;
}

return _internetSettings('query', ['ProxyServer']).then((results) {
var proxyServerLine = results.split('\r\n').where((item) => item.contains('ProxyServer')).firstOrNull;
var proxyServerLineSplits = proxyServerLine?.split(RegExp(r"\s+"));

if (proxyServerLineSplits == null || proxyServerLineSplits.length < 2) {
return null;
}

var proxyLine = proxyServerLineSplits[proxyServerLineSplits.length - 1];
if (proxyLine.startsWith("http://") || proxyLine.startsWith("https:///")) {
proxyLine = proxyLine.replaceFirst("http://", "").replaceFirst("https:///", "");
}

var proxyServer = proxyLine.split(":")[0];
var proxyPort = proxyLine.split(":")[1];
print("$proxyServer:$proxyPort");
return ProxyInfo.of(proxyServer, int.parse(proxyPort));
}).catchError((e) {
print(e);
return null;
});
}

static Future<String> _internetSettings(String cmd, List<String> args) async {
return Process.run('reg', [
'query',
cmd,
'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings',
'/v',
'ProxyServer',
]).then((results) {
var proxyServerLine =
(results.stdout as String).split('\r\n').where((item) => item.contains('ProxyServer')).first;
var proxyServerLineSplits = proxyServerLine.split(RegExp(r"\s+"));
proxyServerLineSplits[proxyServerLineSplits.length - 1];
return null;
});
...args,
]).then((results) => results.stdout.toString());
}

static _concatCommands(List<String> commands) {
Expand Down
Loading

0 comments on commit b748aa6

Please sign in to comment.