Skip to content

PasteFerry (隔空贴): clipboard and file sync across macOS, Windows and Linux. Direct-first, end-to-end encrypted. Optional official access: no server to host. Independent self-hosting remains available.

5.5 KB
Raw Blame
import 'dart:io';

import 'background_service.dart';
import 'i18n.dart';

/// Windows:后台服务 `pasteferryd.exe` 与 `PasteFerry.exe` 放在同一个目录。
///
/// 开机自启登记在 `HKCU\...\Run`,只影响当前用户,不需要管理员权限。登记的命令是
/// `pasteferryd.exe -supervise`:守护进程拉起真正干活的子进程,子进程崩溃就隔 10 秒
/// 再拉起,相当于 Mac 上 launchd 的 KeepAlive。
///
/// 不做成 Windows 服务:服务运行在会话 0,与用户桌面隔离,访问不到用户的剪贴板。
class WindowsBackgroundService implements BackgroundService {
  WindowsBackgroundService({
    String? executable,
    Map<String, String>? environment,
  }) : _executable = executable ?? Platform.resolvedExecutable,
       _env = environment ?? Platform.environment;

  static const _runKey = r'HKCU\Software\Microsoft\Windows\CurrentVersion\Run';
  static const _valueName = 'PasteFerry';
  static const _image = 'pasteferryd.exe';

  final String _executable;
  final Map<String, String> _env;

  String get daemonPath => '${File(_executable).parent.path}\\$_image';

  /// 登记到自启项里的命令行
  String get _command => '"$daemonPath" -supervise';

  String get _localAppData =>
      _env['LOCALAPPDATA'] ?? '${_env['USERPROFILE']}\\AppData\\Local';

  String get logPath => '$_localAppData\\PasteFerry\\logs\\daemon.log';

  @override
  String get displayLogPath =>
      logPath.replaceFirst(_localAppData, '%LOCALAPPDATA%');

  @override
  bool get available => Platform.isWindows && File(daemonPath).existsSync();

  /// 在资源管理器里直接双击压缩包里的程序时,它其实运行在临时目录里,
  /// 关掉之后整个目录就被删了
  bool get _runningFromTemporaryLocation {
    final temp = (_env['TEMP'] ?? '').toLowerCase();
    final exe = _executable.toLowerCase();
    return temp.isNotEmpty && exe.startsWith(temp);
  }

  @override
  Future<ServiceState> state() async {
    if (!available) return ServiceState.unavailable;
    if (_runningFromTemporaryLocation) return ServiceState.mustMove;
    final registered = await _registeredCommand();
    if (registered == null) {
      return await _running() ? ServiceState.running : ServiceState.stopped;
    }
    if (!registered.toLowerCase().contains(daemonPath.toLowerCase())) {
      return ServiceState.stale;
    }
    return await _running() ? ServiceState.running : ServiceState.stopped;
  }

  @override
  Future<bool> launchAtLogin() async => await _registeredCommand() != null;

  @override
  Future<void> setLaunchAtLogin(bool enabled) async {
    if (!enabled) {
      await Process.run('reg', ['delete', _runKey, '/v', _valueName, '/f']);
      if (await launchAtLogin()) {
        throw ServiceException('Could not remove the login entry.');
      }
      return;
    }
    final r = await Process.run('reg', [
      'add',
      _runKey,
      '/v',
      _valueName,
      '/t',
      'REG_SZ',
      '/d',
      _command,
      '/f',
    ]);
    if (r.exitCode != 0) {
      throw ServiceException(appL10n.errAutostart(_output(r)));
    }
  }

  @override
  Future<void> stop() => _stop();

  @override
  Future<void> install() async {
    // Preserve existing startup choices, including entries from older versions.
    if (await launchAtLogin()) await setLaunchAtLogin(true);
    await restart();
  }

  @override
  Future<void> restart() async {
    await _stop();
    // 脱离界面进程运行:关掉窗口后照常同步
    await Process.start(daemonPath, [
      '-supervise',
    ], mode: ProcessStartMode.detached);
  }

  @override
  Future<void> uninstall() async {
    await setLaunchAtLogin(false);
    await stop();
  }

  @override
  Future<bool> removeLegacy() async {
    const legacyValue = 'CopySync', legacyImage = 'copysyncd.exe';
    final enabled =
        (await Process.run('reg', [
          'query',
          _runKey,
          '/v',
          legacyValue,
        ])).exitCode ==
        0;
    if (enabled) {
      await Process.run('reg', ['delete', _runKey, '/v', legacyValue, '/f']);
    }
    if (await _running(legacyImage)) await _stop(legacyImage);
    return enabled;
  }

  /// 守护进程与后台服务一起结束:只结束后台服务的话,守护进程会把它重新拉起来
  Future<void> _stop([String image = _image]) async {
    await Process.run('taskkill', ['/F', '/T', '/IM', image]);
    for (var i = 0; i < 20 && await _running(image); i++) {
      await Future<void>.delayed(const Duration(milliseconds: 250));
    }
    if (await _running(image)) {
      throw ServiceException('The background service did not stop.');
    }
  }

  Future<bool> _running([String image = _image]) async {
    final r = await Process.run('tasklist', [
      '/FI',
      'IMAGENAME eq $image',
      '/NH',
    ]);
    return '${r.stdout}'.toLowerCase().contains(image);
  }

  /// 自启项里登记的命令;没有登记时为 null。
  ///
  /// `reg query` 的输出形如:
  ///
  ///     HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
  ///         PasteFerry    REG_SZ    "C:\...\pasteferryd.exe" -supervise
  Future<String?> _registeredCommand() async {
    final r = await Process.run('reg', ['query', _runKey, '/v', _valueName]);
    if (r.exitCode != 0) return null;
    for (final line in '${r.stdout}'.split('\n')) {
      final i = line.indexOf('REG_SZ');
      if (line.trimLeft().startsWith(_valueName) && i >= 0) {
        return line.substring(i + 'REG_SZ'.length).trim();
      }
    }
    return null;
  }

  static String _output(ProcessResult r) => '${r.stderr}${r.stdout}'.trim();
}