start method
Starts the proxy server. Binds to the configured IP and port, and listens for incoming connections. If the port is already in use, it will try the next port.
Implementation
Future<void> start() async {
try {
final InternetAddress internetAddress = InternetAddress(Config.ip);
server = await ServerSocket.bind(internetAddress, Config.port);
logD('Proxy server started ${server?.address.address}:${server?.port}');
if (server == null) {
retry();
} else {
startHealthCheck();
server?.listen(_handleConnection);
}
} on SocketException catch (e) {
logW('Proxy server Socket close: $e');
// If the port is occupied (EADDRINUSE), increment port and retry.
// Error code 48 on macOS/iOS, 98 on Linux.
final code = e.osError?.errorCode;
if (code == 48 || code == 98) {
Config.port = Config.port + 1;
await start();
}
}
}