增加搜索功能,socket写入中延迟关闭,完善证书安装引导

This commit is contained in:
wanghongen
2023-07-20 14:02:34 +08:00
parent 1646405e76
commit 3d980fb879
16 changed files with 538 additions and 151 deletions

View File

@@ -19,11 +19,19 @@ Future<void> main() async {
/// 代理服务器
class ProxyServer {
//是否初始化
bool init = false;
int port = 9099;
bool _enableSsl = false;
bool enableDesktop = true;
//是否引导
bool guide = false;
//是否启动
bool get isRunning => server?.isRunning ?? false;
Server? server;
EventListener? listener;
RequestRewrites requestRewrites = RequestRewrites();
@@ -32,8 +40,6 @@ class ProxyServer {
ProxyServer({this.listener});
bool get enableSsl => _enableSsl;
//初始化
Future<void> initializedListener(Function action) async {
_initializedListeners.add(action);
@@ -58,7 +64,9 @@ class ProxyServer {
return File("${home.path}${separator}config.cnf");
}
/// 是否启用ssl
///是否启用https抓包
bool get enableSsl => _enableSsl;
set enableSsl(bool enableSsl) {
_enableSsl = enableSsl;
server?.enableSsl = enableSsl;
@@ -136,13 +144,16 @@ class ProxyServer {
var file = await configFile();
var exits = await file.exists();
if (!exits) {
guide = true;
return;
}
Map<String, dynamic> config = jsonDecode(await file.readAsString());
logger.i('加载配置文件 [$file]');
port = config['port'] ?? port;
enableSsl = config['enableSsl'] == true;
enableDesktop = config['enableDesktop'] ?? true;
guide = config['guide'] ?? false;
HostFilter.whitelist.load(config['whitelist']);
HostFilter.blacklist.load(config['blacklist']);
@@ -179,6 +190,7 @@ class ProxyServer {
Map<String, dynamic> toJson() {
return {
'guide': guide,
'port': port,
'enableSsl': enableSsl,
'enableDesktop': enableDesktop,

View File

@@ -41,6 +41,9 @@ class Channel {
final InternetAddress remoteAddress;
final int remotePort;
//是否写入中
bool isWriting = false;
Channel(this._socket)
: _id = DateTime.now().millisecondsSinceEpoch + Random().nextInt(999999),
remoteAddress = _socket.remoteAddress,
@@ -58,10 +61,14 @@ class Channel {
logger.w("channel is closed $obj");
return;
}
var data = pipeline._encoder.encode(obj);
_socket.add(data);
await _socket.flush();
isWriting = true;
try {
var data = pipeline._encoder.encode(obj);
_socket.add(data);
await _socket.flush();
} finally {
isWriting = false;
}
}
Future<void> writeAndClose(Object obj) async {
@@ -69,10 +76,16 @@ class Channel {
close();
}
void close() {
void close() async {
if (isClosed) {
return;
}
//写入中,延迟关闭
int retry = 0;
while (isWriting && retry++ < 10) {
await Future.delayed(const Duration(milliseconds: 150));
}
_socket.destroy();
isOpen = false;
}

View File

@@ -38,6 +38,11 @@ class CertificateManager {
static X509CertificateData get caCert => _caCert;
/// 清除缓存
static void cleanCache() {
_certificateMap.clear();
}
/// 获取域名自签名证书
static Future<SecurityContext> getCertificateContext(String host) async {
var cer = _certificateMap[host];