94 lines
2.6 KiB
Dart
94 lines
2.6 KiB
Dart
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;
|
|
final targetHost = "";
|
|
final targetPort = 443;
|
|
|
|
parseProxyConfig('''{
|
|
"managementConfig": { "listen": "127.0.0.1:8888" },
|
|
"tcpListens": [
|
|
{
|
|
"listen": ":8443",
|
|
"backend": "101.132.122.240:443",
|
|
"allowIps": ["127.0.0.1"]
|
|
}
|
|
]
|
|
}''');
|
|
parseProxyConfig('''{
|
|
"tcpListens": [
|
|
{
|
|
"listen": ":8443",
|
|
"backend": "101.132.122.240:443",
|
|
"allowIps": ["127.0.0.1"]
|
|
}
|
|
]
|
|
}''');
|
|
|
|
// TODO ...
|
|
print('Hello world!');
|
|
}
|
|
|
|
void startListen(HostAndPort listen, HostAndPort target) {
|
|
ServerSocket.bind(listen.host, listen.port).then((serverSocket) {
|
|
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,
|
|
);
|
|
});
|
|
});
|
|
});
|
|
}
|