Files
simple-dart-tcp-proxy/bin/config.dart
2021-05-22 00:50:41 +08:00

75 lines
1.6 KiB
Dart

import 'dart:convert';
import 'dart:io';
class HostAndPort {
String host;
int port;
HostAndPort({this.host, this.port});
}
class ManageConfig {
String listen;
ManageConfig({this.listen});
}
class ProxyItemConfig {
String listen;
String backend;
List<String> allowIps;
ProxyItemConfig({this.listen, this.backend, this.allowIps});
}
class ProxyConfig {
ManageConfig managementConfig;
List<ProxyItemConfig> tcpListens;
ProxyConfig({this.managementConfig, this.tcpListens});
}
ProxyConfig parseProxyConfig(String config) {
final jsonConfig = json.decode(config);
ManageConfig manageConfig = null;
final managementConfig = jsonConfig['managementConfig'];
if (managementConfig != null) {
final managementListen = managementConfig['listen'];
}
List<ProxyItemConfig> outTcpListens = [];
final tcpListens = jsonConfig['tcpListens'];
final tcpListenList = List.from(tcpListens);
for (final tl in tcpListenList) {
print(tl);
}
print(managementConfig);
// print(jsonConfig);
return null;
}
HostAndPort parseHostAndPort(String hnp) {
final indexOfC = hnp.indexOf(":");
if (indexOfC < 0) {
throw 'Missing port: ' + hnp;
}
var host = hnp.substring(0, indexOfC);
final port = int.parse(hnp.substring(indexOfC + 1));
if (host.isEmpty) {
host = '0.0.0.0';
}
return HostAndPort(host: host, port: port);
}
Future<ProxyConfig> loadProxyConfig(String configFile
/*, {List<String> files} ?? */) async {
final configFn = File(configFile);
if (!await configFn.exists()) {
throw 'Config file not found: ' + configFile;
}
return parseProxyConfig(await configFn.readAsString());
}