arblant
firefox-vpn-client / app / lib / core / core_controller.dart
core_controller.dart450 lines · 14.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
import 'dart:async';

import 'package:flutter/foundation.dart';

import 'core_api.dart';
import 'remote_flags.dart';
import 'models.dart';
import 'ports.dart';
import 'tun_service.dart';

/// Connects to the core, polls its status and accounts, and runs the
/// user's actions. It holds no VPN logic: every decision is the core's.
class CoreController extends ChangeNotifier {
  CoreController(
    this._connect, {
    this.pollInterval = const Duration(seconds: 2),
    this.moveStoppedCore,
    this.retryStoppedCore,
    this.turnOn,
    this.tunService,
  });

  /// Installs and removes the TUN mode's service (Windows); null elsewhere.
  final TunService? tunService;

  /// The TUN service is being installed or removed: Windows may be showing
  /// its administrator prompt.
  bool installingTun = false;
  bool removingTun = false;

  /// Turns the proxy on or off where the app has a switch for the whole
  /// proxy (Android, which has no system proxy); null elsewhere.
  final Future<void> Function(bool on)? turnOn;

  /// Moves a core that could not start to another API port (it edits the
  /// core's config.json, since the core is not there to ask); null where
  /// the app does not start the core.
  final Future<void> Function(int port)? moveStoppedCore;

  /// Lets a core that stopped right after starting start again.
  final Future<void> Function()? retryStoppedCore;

  /// Opens a client; it throws [CoreUnavailable] while there is no core.
  final Future<CoreApi> Function() _connect;
  final Duration pollInterval;

  CoreApi? _api;
  Timer? _timer;

  /// A quick retry while the core is starting, so the app shows it as soon
  /// as its API answers rather than at the next poll.
  Timer? _quick;
  bool _polling = false;
  int _tick = 0;

  /// Failed reads in a row while the core answered before; the first one
  /// is retried at once instead of showing the core as lost.
  int _failures = 0;
  bool _disposed = false;

  CoreStatus? status;
  AccountList? accounts;

  /// Why the core cannot be reached; null while it answers.
  CoreUnavailable? unavailable;

  /// The system proxy mode, or the proxy's power, is being changed.
  bool switchingProxy = false;
  bool testing = false;

  /// The latest connection test, ours or the core's automatic one.
  ConnectionTest? get lastTest => _test ?? status?.lastTest;
  ConnectionTest? _test;

  /// When the phone may test the connection again (_retestUnconfirmed).
  DateTime? _retestAt;

  /// The wait before the next unconfirmed test: it doubles from 5 seconds
  /// to a minute while tests keep failing, so an unreachable Mozilla is not
  /// asked for a pass every few seconds.
  Duration _retestDelay = _retestFirst;
  static const _retestFirst = Duration(seconds: 5);
  static const _retestMax = Duration(minutes: 1);

  /// What the last poll showed of the connection: a new one (the proxy
  /// started again, the network changed, the phase came back to running)
  /// is tested soon again, not after an old backoff.
  String? _seenConnection;

  /// The last log entry the logs page had read when it was cleared, and
  /// when: the page starts after that entry and leaves out anything logged
  /// before the moment, for as long as the app runs.
  int logsClearedAfter = 0;
  DateTime? logsClearedAt;

  /// Accounts with an action in progress.
  final Set<String> busyAccounts = {};

  CoreApi? get api => _api;
  bool get connected => _api != null && unavailable == null;

  void start() {
    _timer ??= Timer.periodic(pollInterval, (_) => refresh());
    refresh();
  }

  /// Reads the status, and the accounts every third tick (the core reads
  /// them from disk). [accountsToo] reads them now.
  Future<void> refresh({bool accountsToo = false}) async {
    if (_polling || _disposed) return;
    _polling = true;
    try {
      final api = _api ??= await _connect();
      // Flags the app does not carry come from this core.
      RemoteFlags.use(api);
      final next = await api.status();
      final readAccounts = accountsToo || accounts == null || _tick % 3 == 0;
      _tick++;
      status = next;
      // The phone's VPN while on; the computer's proxy, which always runs,
      // in either mode.
      if (turnOn != null || next.systemProxySupported) {
        _retestUnconfirmed(next);
      }
      if (_test != null &&
          next.lastTest?.at != null &&
          _test!.at != null &&
          next.lastTest!.at!.isAfter(_test!.at!)) {
        _test = null;
      }
      if (readAccounts) accounts = await api.accounts();
      unavailable = null;
      _failures = 0;
    } on CoreUnavailable catch (e) {
      // One failed read while the core answered is tried again at once:
      // Android closes the app's open connections, the one to the core
      // included, when the phone's network goes away.
      if (unavailable == null && status != null && _failures == 0) {
        _failures++;
      } else {
        _lost(e);
      }
    } on CoreApiException catch (e) {
      // 401/403: the token changed (another core took the address); find
      // the core again. Other errors are passing.
      if (e.statusCode == 401 || e.statusCode == 403) {
        _lost(CoreUnavailable(CoreProblem.refused, e.message));
      }
    } finally {
      _polling = false;
      if (!_disposed) {
        notifyListeners();
        if ((unavailable?.problem == CoreProblem.starting || _failures > 0) &&
            _timer != null) {
          _quick?.cancel();
          _quick = Timer(const Duration(milliseconds: 250), refresh);
        }
      }
    }
  }

  void _lost(CoreUnavailable problem) {
    _failures = 0;
    _api?.close();
    _api = null;
    unavailable = problem;
  }

  Future<void> runTest() => _act(() async {
    testing = true;
    notifyListeners();
    try {
      _test = await _require().test();
    } finally {
      testing = false;
      // Any test, the user's too, puts the next unconfirmed one off by the
      // current step, counted from its end: a slow failing test is not
      // followed by another at once.
      _retestAt = DateTime.now().add(
        _retestAt == null ? _retestFirst : _retestDelay ~/ 2,
      );
    }
  });

  Future<void> restartProxy() => _act(() => _require().restartProxy());

  /// Sets the system proxy mode: set (Windows uses the VPN) or clear.
  Future<void> setSystemProxy(String mode) => _act(() async {
    switchingProxy = true;
    notifyListeners();
    try {
      await _require().setSystemProxy(mode);
    } finally {
      switchingProxy = false;
    }
  });

  /// Installs the TUN service, with the system's administrator prompt;
  /// [prompt] is its reason on macOS.
  Future<TunServiceResult> installTunService({String? prompt}) =>
      _tunService(() => tunService!.install(prompt: prompt), installing: true);

  /// Removes the TUN service; [prompt] is the reason macOS' administrator
  /// prompt shows when the service does not run.
  Future<TunServiceResult> removeTunService({String? prompt}) => _tunService(
    () => tunService!.uninstall(prompt: prompt),
    installing: false,
  );

  Future<TunServiceResult> _tunService(
    Future<TunServiceResult> Function() run, {
    required bool installing,
  }) async {
    if (tunService == null) {
      return const TunServiceFailed('the TUN mode is not available here');
    }
    if (installing) {
      installingTun = true;
    } else {
      removingTun = true;
    }
    notifyListeners();
    try {
      return await run();
    } finally {
      installingTun = removingTun = false;
      if (!_disposed) {
        notifyListeners();
        unawaited(refresh());
      }
    }
  }

  /// Turns the proxy on or off ([turnOn] on the phone, the core's API on the
  /// computer) and waits, briefly, until the core reports it.
  Future<void> setOn(bool on) => _act(() async {
    switchingProxy = true;
    notifyListeners();
    try {
      await (turnOn ?? _require().setPower)(on);
      final api = _require();
      for (var i = 0; i < 20; i++) {
        final next = await api.status();
        status = next;
        if ((next.state.phase == Phase.off) != on) break;
        await Future<void>.delayed(const Duration(milliseconds: 150));
      }
    } finally {
      switchingProxy = false;
    }
  });

  /// The user is looking (the app came to the front): the core tries the
  /// next connection at once instead of waiting after a failure. At most
  /// every few seconds.
  Future<void> retryNow() async {
    final now = DateTime.now();
    if (_retriedAt != null && now.difference(_retriedAt!) < _retryGap) return;
    _retriedAt = now;
    try {
      await _api?.retry();
    } on Exception {
      // Not answering: the next poll says so.
    }
  }

  DateTime? _retriedAt;
  static const _retryGap = Duration(seconds: 5);

  /// Turns the proxy on by itself, for an app started with the computer.
  Future<void> autoConnect() => _act(() => _require().autoConnect());

  /// Starts downloading [version] of the app (the Windows app's core).
  Future<void> downloadUpdate(String version) =>
      _act(() => _require().downloadUpdate(version));

  /// Installs the downloaded [version]; the app ([pid]) quits once this
  /// returns, and starts again with [args].
  Future<void> installUpdate(String version, int pid, List<String> args) =>
      _act(() => _require().installUpdate(version, pid, args));

  /// The core's config.json as raw JSON.
  Future<Map<String, Object?>> settings() => _require().settings();

  /// Changes the core's config: [edit] changes the raw JSON in place.
  Future<void> editSettings(void Function(Map<String, Object?> config) edit) =>
      _act(() async {
        final api = _require();
        final config = await api.settings();
        edit(config);
        await api.saveSettings(config);
      });

  Future<void> openFolder() => _act(() => _require().openFolder());

  /// A fresh sign-in link to the web dashboard.
  Future<Uri> dashboardLink() => _require().loginLink();

  /// Tries the core again at once, also one that stopped right after
  /// starting.
  Future<void> retry() async {
    await retryStoppedCore?.call();
    await refresh(accountsToo: true);
  }

  /// Moves the core's API to [port]: through the core while it runs, or in
  /// its config.json when it could not start.
  Future<void> setAppPort(int port) async {
    if (_api != null) {
      await editSettings((config) {
        final dashboard = config['dashboard'];
        if (dashboard is Map<String, Object?>) {
          dashboard['listen'] = withPort(
            dashboard['listen'] as String? ?? '',
            port,
          );
        }
      });
      return;
    }
    await moveStoppedCore?.call(port);
    await refresh(accountsToo: true);
  }

  /// Moves the proxy (SOCKS5 and HTTP) to [port]; the core restarts it.
  Future<void> setProxyPort(int port) => editSettings((config) {
    final proxy = config['proxy'];
    if (proxy is Map<String, Object?>) {
      proxy['listen'] = withPort(proxy['listen'] as String? ?? '', port);
    }
  });

  /// Runs a request for a page (servers, logs, routing tools) with the
  /// connection's error handling; errors reach the caller.
  Future<T> call<T>(Future<T> Function(CoreApi api) request) async {
    try {
      return await request(_require());
    } on CoreUnavailable catch (e) {
      _lost(e);
      if (!_disposed) notifyListeners();
      rethrow;
    }
  }

  /// Chooses the location (a country code, `fastest`, or empty for the
  /// recommended one); the proxy restarts on it.
  Future<void> setLocation(String country) =>
      _act(() => _require().setLocation(country));

  /// Stops polling, before the app quits.
  void stopPolling() {
    _timer?.cancel();
    _timer = null;
  }

  Future<void> activate(Account a) =>
      _account(a.id, () => _require().activateAccount(a.id));

  Future<void> setEnabled(Account a, bool enabled) =>
      _account(a.id, () => _require().setAccountEnabled(a.id, enabled));

  Future<void> refreshQuota(Account a) =>
      _account(a.id, () => _require().refreshAccount(a.id));

  Future<void> remove(Account a) =>
      _account(a.id, () => _require().removeAccount(a.id));

  /// Moves [a] by [delta] places in the priority order.
  Future<void> move(Account a, int delta) {
    final list = [...?accounts?.accounts];
    final from = list.indexWhere((x) => x.id == a.id);
    final to = from + delta;
    if (from < 0 || to < 0 || to >= list.length) return Future.value();
    list.insert(to, list.removeAt(from));
    return _act(() => _require().setAccountOrder([for (final x in list) x.id]));
  }

  Future<void> _account(String id, Future<void> Function() action) async {
    busyAccounts.add(id);
    notifyListeners();
    try {
      await _act(action);
    } finally {
      busyAccounts.remove(id);
      if (!_disposed) notifyListeners();
    }
  }

  /// Runs [action], then reads the status and accounts again. Errors reach
  /// the caller, which shows them.
  Future<void> _act(Future<void> Function() action) async {
    try {
      await action();
    } on CoreUnavailable catch (e) {
      _lost(e);
      rethrow;
    } finally {
      if (!_disposed) {
        notifyListeners();
        unawaited(refresh(accountsToo: true));
      }
    }
  }

  /// The home page shows "Connecting" until a test confirms the connection
  /// (CoreStatus.confirmedBy); until then it tests again after 5 seconds,
  /// then less often (up to once a minute) while tests keep failing.
  void _retestUnconfirmed(CoreStatus s) {
    final connection = s.state.phase == Phase.running
        ? '${s.proxy?.startedAt}|${s.networkChangedAt}'
        : null;
    if (connection != _seenConnection) {
      _seenConnection = connection;
      _retestAt = null;
      _retestDelay = _retestFirst;
    }
    if (s.state.phase != Phase.running || testing || switchingProxy) return;
    if (s.noNetwork) return;
    // The core's auto-connect tests by itself.
    if (s.autoConnecting) return;
    if (s.confirmedBy(lastTest)) {
      _retestAt = null;
      _retestDelay = _retestFirst;
      return;
    }
    final now = DateTime.now();
    if (_retestAt != null && now.isBefore(_retestAt!)) return;
    _retestAt = now.add(_retestDelay);
    final doubled = _retestDelay * 2;
    _retestDelay = doubled > _retestMax ? _retestMax : doubled;
    unawaited(runTest().catchError((Object _) {}));
  }

  CoreApi _require() {
    final api = _api;
    if (api == null) {
      throw unavailable ?? const CoreUnavailable(CoreProblem.notAnswering);
    }
    return api;
  }

  @override
  void dispose() {
    _disposed = true;
    _timer?.cancel();
    _quick?.cancel();
    _api?.close();
    super.dispose();
  }
}