Support Unicode encode

This commit is contained in:
wanghongenpin
2024-09-22 01:44:56 +08:00
parent 248576b103
commit e152f9b9e6
7 changed files with 36 additions and 21 deletions

View File

@@ -48,8 +48,6 @@ class ConnectionHandler(
*/
@Throws(IOException::class)
fun handlePacket(stream: ByteBuffer) {
val rawPacket = ByteArray(stream.limit())
stream[rawPacket, 0, stream.limit()]
stream.rewind()
val ipHeader = IPPacketFactory.createIP4Header(stream)
@@ -105,7 +103,7 @@ class ConnectionHandler(
* 是否支持协议
*/
private val methods: List<String> =
mutableListOf("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "TRACE", "CONNECT")
mutableListOf("GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS", "TRACE", "CONNECT", "PROPFIND", "REPORT")
private fun supperProtocol(packetData: ByteBuffer): Boolean {
val position = packetData.position()

View File

@@ -63,11 +63,7 @@ class ProxyVpnThread(
if (length > 0) {
try {
readBuffer.flip()
val byteArray = ByteArray(length)
readBuffer.get(byteArray)
val packet = ByteBuffer.wrap(byteArray)
handler.handlePacket(packet)
handler.handlePacket(readBuffer)
} catch (e: Exception) {
val errorMessage = (e.message ?: e.toString())
Log.e(TAG, errorMessage, e)

View File

@@ -9,9 +9,6 @@ import java.nio.ByteOrder
/**
* Helper class to perform various useful task
*
* @author Borey Sao
* Date: May 8, 2014
*/
object PacketUtil {
@get:Synchronized

View File

@@ -12,6 +12,7 @@ import 'package:network_proxy/network/util/logger.dart';
enum EncoderType {
url,
base64,
unicode,
md5;
static EncoderType nameOf(String name) {
@@ -39,6 +40,7 @@ class _EncoderState extends State<EncoderWidget> with SingleTickerProviderStateM
var tabs = const [
Tab(text: 'URL'),
Tab(text: 'Base64'),
Tab(text: 'Unicode'),
Tab(text: 'MD5'),
];
@@ -154,6 +156,8 @@ class _EncoderState extends State<EncoderWidget> with SingleTickerProviderStateM
result = base64.encode(utf8.encode(inputText));
case EncoderType.md5:
result = md5.convert(inputText.codeUnits).toString();
case EncoderType.unicode:
result = encodeToUnicode(inputText);
}
} catch (e) {
FlutterToastr.show(localizations.encodeFail, context);
@@ -181,6 +185,8 @@ class _EncoderState extends State<EncoderWidget> with SingleTickerProviderStateM
result = String.fromCharCodes(compressed);
}
case EncoderType.md5:
case EncoderType.unicode:
result = decodeFromUnicode(inputText);
}
} catch (e, t) {
logger.e("$e", error: e, stackTrace: t);
@@ -188,4 +194,14 @@ class _EncoderState extends State<EncoderWidget> with SingleTickerProviderStateM
}
outputTextController.text = result;
}
String encodeToUnicode(String input) {
return input.runes.map((rune) => '\\u${rune.toRadixString(16).padLeft(4, '0')}').join();
}
String decodeFromUnicode(String input) {
return input.replaceAllMapped(RegExp(r'\\u([0-9a-fA-F]{4})'), (match) {
return String.fromCharCode(int.parse(match.group(1)!, radix: 16));
});
}
}

View File

@@ -91,6 +91,13 @@ class _ToolboxState extends State<Toolbox> {
child: const Column(children: [Icon(Icons.currency_bitcoin), Text('Base64')]),
)),
const SizedBox(width: 15),
InkWell(
onTap: () => encodeWindow(EncoderType.unicode, context),
child: Container(
padding: const EdgeInsets.all(10),
child: const Column(children: [Icon(Icons.format_underline), Text('Unicode')]),
)),
const SizedBox(width: 15),
InkWell(
onTap: () => encodeWindow(EncoderType.md5, context),
child: Container(

View File

@@ -116,6 +116,7 @@ class HttpBodyState extends State<HttpBodyWidget> {
SizedBox(
height: 36,
child: TabBar(
labelStyle: const TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
labelPadding: const EdgeInsets.only(left: 3, right: 5),
tabs: tabs.tabList(),
onTap: (index) {
@@ -184,7 +185,7 @@ class HttpBodyState extends State<HttpBodyWidget> {
list.add(const SizedBox(width: 3));
list.add(IconButton(
icon: const Icon(Icons.abc, size: 20),
icon: const Icon(Icons.text_format, size: 21),
tooltip: localizations.encode,
onPressed: () {
encodeWindow(EncoderType.base64, context, bodyKey.currentState?.body);
@@ -415,12 +416,12 @@ class Tabs {
tabs.list.add(ViewType.of(contentType) ?? ViewType.text);
//text 为json时增加json格式化
if (contentType == ContentType.text) {
//为json时增加json格式化
if (isJsonText && !tabs.list.contains(ViewType.jsonText)) {
tabs.list.add(ViewType.jsonText);
if (isJsonText) tabs.list.add(ViewType.json);
tabs.list.add(ViewType.json);
}
if (contentType == ContentType.formUrl || contentType == ContentType.json) {
tabs.list.add(ViewType.text);
}
@@ -430,7 +431,7 @@ class Tabs {
}
List<Tab> tabList() {
return list.map((e) => Tab(child: Text(e.title, style: const TextStyle(fontSize: 14)))).toList();
return list.map((e) => Tab(text: e.title)).toList();
}
}

View File

@@ -26,7 +26,7 @@ class Har {
static int maxBodyLength = 1024 * 1024 * 4;
static List<Map> _entries(List<HttpRequest> list) {
return list.map((e) => toHar(e)).toList().reversed.toList();
return list.map((e) => toHar(e)).toList();
}
static Map toHar(HttpRequest request) {
@@ -54,7 +54,7 @@ class Har {
'wait': request.response?.responseTime.difference(request.requestTime).inMilliseconds,
'receive': 0,
},
'serverIPAddress': request.response?.remoteHost ?? "", // 服务器IP地址
'serverIPAddress': request.response?.remoteHost ?? '', // 服务器IP地址
};
har['response'] = {
@@ -172,8 +172,8 @@ class Har {
static List<Map<String, String>> _getQueryString(HttpRequest request) {
final queryStringList = <Map<String, String>>[];
final queries = Uri.parse(request.uri).queryParametersAll;
queries.forEach((key, valueList) {
final queries = request.requestUri?.queryParametersAll;
queries?.forEach((key, valueList) {
for (var value in valueList) {
queryStringList.add({"name": key, "value": value});
}