feat: update

This commit is contained in:
2021-05-09 00:53:54 +08:00
parent 07be685de9
commit 14b9cd1176

View File

@@ -2,6 +2,12 @@ import 'dart:convert';
import 'dart:io';
import 'config.dart';
int createdConnectionCount = 0;
int runningConnectionCount = 0;
int upstreamBytes = 0;
int downstreamBytes = 0;
Map<int, bool> createdConnectionMap = <int, bool>{};
void main(List<String> arguments) {
final listenAddress = "127.0.0.1";
final listenPort = 8801;
@@ -34,9 +40,54 @@ void main(List<String> arguments) {
void startListen(HostAndPort listen, HostAndPort target) {
ServerSocket.bind(listen.host, listen.port).then((serverSocket) {
Socket.connect(target.host, target.port).then((clientSocket) {
// TODO ...
serverSocket.listen((Socket socket) {
Socket.connect(target.host, target.port).then((clientSocket) {
final createdConnectionIndex = createdConnectionCount;
createdConnectionCount++;
runningConnectionCount++;
createdConnectionMap[createdConnectionIndex] = true;
socket.listen(
(event) {
upstreamBytes += event.lengthInBytes;
clientSocket.write(event);
},
onError: (error, StackTrace stackTrace) {
if (createdConnectionMap.remove(createdConnectionIndex) != null) {
runningConnectionCount--;
}
print(
'Error in connection: ' + error + " " + stackTrace.toString());
},
onDone: () {
if (createdConnectionMap.remove(createdConnectionIndex) != null) {
runningConnectionCount--;
}
clientSocket.destroy();
},
cancelOnError: true,
);
clientSocket.listen(
(event) {
downstreamBytes += event.lengthInBytes;
socket.write(event);
},
onError: (error, StackTrace stackTrace) {
if (createdConnectionMap.remove(createdConnectionIndex) != null) {
runningConnectionCount--;
}
print(
'Error in connection: ' + error + " " + stackTrace.toString());
},
onDone: () {
if (createdConnectionMap.remove(createdConnectionIndex) != null) {
runningConnectionCount--;
}
socket.destroy();
},
cancelOnError: true,
);
});
});
// serverSocket.listen((event) { })
});
}