Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions lib/tor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'dart:async';
import 'dart:ffi';
import 'dart:io';
import 'dart:isolate';
import 'dart:math';

import 'package:ffi/ffi.dart';
import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -127,16 +126,15 @@ class Tor {
}

Future<int> _getRandomUnusedPort({List<int> excluded = const []}) async {
var random = Random.secure();
int potentialPort = 0;
int port = 0;

retry:
while (potentialPort <= 0 || excluded.contains(potentialPort)) {
potentialPort = random.nextInt(65535);
while (port == 0 || excluded.contains(port)) {
try {
var socket = await ServerSocket.bind("0.0.0.0", potentialPort);
var socket = await ServerSocket.bind("0.0.0.0", 0);
port = socket.port;
socket.close();
return potentialPort;
return port;
} catch (_) {
continue retry;
}
Expand Down
Loading