pinggy 0.6.11 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -795,7 +795,18 @@ function autostartFilePath(platform, home) {
795
795
  }
796
796
 
797
797
  // src/daemon/lifecycle/serviceInstaller.ts
798
+ var AUTOSTART_ON_COMMAND = "pinggy autostart on";
799
+ var AUTOSTART_OFF_COMMAND = "pinggy autostart off";
798
800
  var SERVICE_ANY_LOCATION_ENV = "PINGGY_SERVICE_ANY_LOCATION";
801
+ function machineServiceInstallContext() {
802
+ return {
803
+ platform: os3.platform(),
804
+ isPkg: "pkg" in process,
805
+ execPath: process.execPath,
806
+ home: os3.homedir(),
807
+ env: process.env
808
+ };
809
+ }
799
810
  function realPath(p) {
800
811
  try {
801
812
  return fs6.realpathSync(p);
@@ -825,25 +836,25 @@ function isInstallerBinary(ctx) {
825
836
  }
826
837
  function installerFix(platform) {
827
838
  return [
828
- "pinggy can only run as a service when it is installed with the Pinggy installer.",
839
+ "Autostart works only for pinggy installed with the Pinggy installer.",
829
840
  "Install it, then run this command again:",
830
841
  ` ${installCommandFor(platform)}`,
831
- " pinggy daemon service-install"
842
+ ` ${AUTOSTART_ON_COMMAND}`
832
843
  ].join("\n");
833
844
  }
834
845
  function npmInstallerFix(platform) {
835
846
  return [
836
- "The npm version of pinggy cannot run as a service.",
847
+ "The npm version of pinggy cannot turn on autostart.",
837
848
  "Remove it, install pinggy with the Pinggy installer, then run this command again:",
838
849
  ` ${NPM_UNINSTALL_COMMAND}`,
839
850
  ` ${installCommandFor(platform)}`,
840
- " pinggy daemon service-install"
851
+ ` ${AUTOSTART_ON_COMMAND}`
841
852
  ].join("\n");
842
853
  }
843
854
  function installedCopyFix(installedPath) {
844
855
  return [
845
856
  "This pinggy is not the installed one. Run this command with the installed copy:",
846
- ` ${installedPath} daemon service-install`
857
+ ` ${installedPath} autostart on`
847
858
  ].join("\n");
848
859
  }
849
860
  function serviceInstallRefusal(ctx, fileExists = fs6.existsSync) {
@@ -853,20 +864,48 @@ function serviceInstallRefusal(ctx, fileExists = fs6.existsSync) {
853
864
  if (fileExists(installedPath)) return installedCopyFix(installedPath);
854
865
  return installerFix(ctx.platform);
855
866
  }
856
- function printInstalled() {
857
- printer_default.success("pinggy now starts automatically when this computer starts.");
858
- }
859
- function printUninstalled() {
860
- printer_default.success("pinggy no longer starts automatically when this computer starts.");
861
- }
862
- function printNotInstalled() {
863
- printer_default.print("pinggy is not set to start automatically.");
864
- }
865
867
  var QUIET = { stdio: ["ignore", "ignore", "pipe"] };
866
868
  function toolError(err) {
867
869
  const stderr = err.stderr?.toString().trim();
868
870
  return stderr ? stderr.replace(/^ERROR:\s*/i, "") : errorMessage(err);
869
871
  }
872
+ var NO_BOOT_ENTRY = { present: false, program: null };
873
+ function unescapeXml(value) {
874
+ return value.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"').replace(/&amp;/g, "&");
875
+ }
876
+ function bootEntryProgram(platform, text) {
877
+ let match = null;
878
+ if (platform === "linux") {
879
+ match = /^ExecStart=(.+) --_daemon-child\s*$/m.exec(text);
880
+ } else if (platform === "darwin") {
881
+ match = /<key>ProgramArguments<\/key>\s*<array>\s*<string>([^<]+)<\/string>/.exec(text);
882
+ } else if (platform === "win32") {
883
+ match = /<Command>([^<]+)<\/Command>/.exec(text);
884
+ }
885
+ return match ? unescapeXml(match[1].trim()) : null;
886
+ }
887
+ function sameProgram(platform, left, right) {
888
+ return platform === "win32" ? left.toLowerCase() === right.toLowerCase() : left === right;
889
+ }
890
+ function readBootEntry(platform = os3.platform()) {
891
+ if (platform === "win32") {
892
+ return readWindowsTask();
893
+ }
894
+ const entryPath = platform === "linux" ? getSystemdServicePath() : platform === "darwin" ? getLaunchdPlistPath() : null;
895
+ if (!entryPath || !fs6.existsSync(entryPath)) {
896
+ return NO_BOOT_ENTRY;
897
+ }
898
+ let text = "";
899
+ try {
900
+ text = fs6.readFileSync(entryPath, "utf-8");
901
+ } catch (err) {
902
+ logger.debug("Could not read the boot entry", { entryPath, error: errorMessage(err) });
903
+ }
904
+ return { present: true, program: bootEntryProgram(platform, text) };
905
+ }
906
+ function startsBeforeSignIn(platform = os3.platform()) {
907
+ return platform === "linux" && lingerEnabled(os3.userInfo().username);
908
+ }
870
909
  function getSystemdServicePath() {
871
910
  return systemdServicePath(os3.homedir());
872
911
  }
@@ -896,7 +935,6 @@ function installSystemd(program) {
896
935
  execSync(`systemctl --user enable ${SYSTEMD_SERVICE_NAME}`, QUIET);
897
936
  execSync(`systemctl --user start ${SYSTEMD_SERVICE_NAME}`, QUIET);
898
937
  logger.info("systemd user service installed", { servicePath });
899
- printInstalled();
900
938
  enableLinger(os3.userInfo().username);
901
939
  }
902
940
  function lingerEnabled(username) {
@@ -917,24 +955,22 @@ function enableLinger(username) {
917
955
  logger.info("systemd lingering enabled", { username });
918
956
  } catch (err) {
919
957
  logger.warn("loginctl enable-linger failed", { error: errorMessage(err) });
920
- printer_default.print(`pinggy starts when ${username} signs in. To start it before anyone signs in, run: loginctl enable-linger ${username}`);
921
958
  }
922
959
  }
923
- function uninstallSystemd() {
960
+ function removeSystemd() {
924
961
  const servicePath = getSystemdServicePath();
925
962
  try {
926
- execSync(`systemctl --user stop ${SYSTEMD_SERVICE_NAME} 2>/dev/null || true`);
927
- execSync(`systemctl --user disable ${SYSTEMD_SERVICE_NAME} 2>/dev/null || true`);
928
- } catch {
963
+ execSync(`systemctl --user disable ${SYSTEMD_SERVICE_NAME}`, QUIET);
964
+ } catch (err) {
965
+ logger.debug("systemctl disable failed", { error: toolError(err) });
929
966
  }
930
- if (fs6.existsSync(servicePath)) {
931
- fs6.unlinkSync(servicePath);
967
+ fs6.rmSync(servicePath, { force: true });
968
+ try {
932
969
  execSync("systemctl --user daemon-reload", QUIET);
933
- logger.info("systemd user service removed", { servicePath });
934
- printUninstalled();
935
- } else {
936
- printNotInstalled();
970
+ } catch (err) {
971
+ logger.debug("systemctl daemon-reload failed", { error: toolError(err) });
937
972
  }
973
+ logger.info("systemd user service removed", { servicePath });
938
974
  }
939
975
  function getLaunchdPlistPath() {
940
976
  return launchdPlistPath(os3.homedir());
@@ -961,48 +997,45 @@ ${programArgs}
961
997
  </plist>
962
998
  `;
963
999
  }
1000
+ function launchdUid() {
1001
+ return process.getuid?.() ?? 501;
1002
+ }
1003
+ function launchdJobLoaded(uid) {
1004
+ try {
1005
+ execFileSync2("launchctl", ["print", `gui/${uid}/${SERVICE_LABEL}`], QUIET);
1006
+ return true;
1007
+ } catch {
1008
+ return false;
1009
+ }
1010
+ }
964
1011
  function installLaunchd(program) {
965
1012
  const plistPath = getLaunchdPlistPath();
966
1013
  const dir = path6.dirname(plistPath);
967
1014
  fs6.mkdirSync(dir, { recursive: true });
968
1015
  fs6.writeFileSync(plistPath, generateLaunchdPlist(program), "utf-8");
969
- const uid = process.getuid?.() ?? 501;
970
- try {
971
- execSync(`launchctl bootout gui/${uid}/${SERVICE_LABEL} 2>/dev/null || true`);
972
- } catch {
1016
+ const uid = launchdUid();
1017
+ if (launchdJobLoaded(uid)) {
1018
+ try {
1019
+ execFileSync2("launchctl", ["kickstart", `gui/${uid}/${SERVICE_LABEL}`], QUIET);
1020
+ } catch (err) {
1021
+ logger.debug("launchctl kickstart failed", { error: toolError(err) });
1022
+ }
1023
+ } else {
1024
+ loadLaunchdPlist(uid, plistPath);
973
1025
  }
974
- loadLaunchdPlist(uid, plistPath);
975
1026
  logger.info("launchd agent installed", { plistPath });
976
- printInstalled();
977
1027
  }
978
1028
  function loadLaunchdPlist(uid, plistPath) {
979
1029
  try {
980
- execSync(`launchctl bootstrap gui/${uid} ${plistPath}`);
1030
+ execFileSync2("launchctl", ["bootstrap", `gui/${uid}`, plistPath], QUIET);
981
1031
  } catch {
982
- try {
983
- execSync(`launchctl load -w ${plistPath}`);
984
- } catch {
985
- }
1032
+ execFileSync2("launchctl", ["load", "-w", plistPath], QUIET);
986
1033
  }
987
1034
  }
988
- function uninstallLaunchd() {
1035
+ function removeLaunchd() {
989
1036
  const plistPath = getLaunchdPlistPath();
990
- const uid = process.getuid?.() ?? 501;
991
- try {
992
- execSync(`launchctl bootout gui/${uid}/${SERVICE_LABEL} 2>/dev/null || true`);
993
- } catch {
994
- try {
995
- execSync(`launchctl unload ${plistPath} 2>/dev/null || true`);
996
- } catch {
997
- }
998
- }
999
- if (fs6.existsSync(plistPath)) {
1000
- fs6.unlinkSync(plistPath);
1001
- logger.info("launchd agent removed", { plistPath });
1002
- printUninstalled();
1003
- } else {
1004
- printNotInstalled();
1005
- }
1037
+ fs6.rmSync(plistPath, { force: true });
1038
+ logger.info("launchd agent removed", { plistPath });
1006
1039
  }
1007
1040
  var WINDOWS_TASK_NAME = "PinggyDaemon";
1008
1041
  function escapeXml(value) {
@@ -1070,6 +1103,18 @@ function windowsTaskExists() {
1070
1103
  return false;
1071
1104
  }
1072
1105
  }
1106
+ function readWindowsTask() {
1107
+ try {
1108
+ const xml = execFileSync2("schtasks", ["/Query", "/TN", WINDOWS_TASK_NAME, "/XML"], {
1109
+ encoding: "utf-8",
1110
+ stdio: ["ignore", "pipe", "ignore"],
1111
+ windowsHide: true
1112
+ });
1113
+ return { present: true, program: bootEntryProgram("win32", xml) };
1114
+ } catch {
1115
+ return NO_BOOT_ENTRY;
1116
+ }
1117
+ }
1073
1118
  function isElevated() {
1074
1119
  try {
1075
1120
  return /S-1-16-(12288|16384)\b/.test(execFileSync2("whoami", ["/groups"], { encoding: "utf-8" }));
@@ -1088,29 +1133,17 @@ function installWindows(program) {
1088
1133
  createWindowsTask(program);
1089
1134
  execFileSync2("schtasks", ["/Run", "/TN", WINDOWS_TASK_NAME], QUIET);
1090
1135
  } catch (err) {
1091
- printer_default.error(windowsTaskError("set pinggy to start automatically", "change it", err));
1092
- process.exit(1);
1136
+ throw new Error(windowsTaskError("turn on autostart", "change it", err));
1093
1137
  }
1094
1138
  logger.info("Scheduled task installed", { task: WINDOWS_TASK_NAME });
1095
- printInstalled();
1096
1139
  }
1097
- async function uninstallWindows() {
1098
- if (!windowsTaskExists()) {
1099
- printNotInstalled();
1100
- return;
1101
- }
1140
+ function removeWindows() {
1102
1141
  try {
1103
1142
  execFileSync2("schtasks", ["/Delete", "/TN", WINDOWS_TASK_NAME, "/F"], QUIET);
1104
1143
  } catch (err) {
1105
- printer_default.error(windowsTaskError("stop pinggy from starting automatically", "turn it off", err));
1106
- process.exit(1);
1144
+ throw new Error(windowsTaskError("turn off autostart", "turn it off", err));
1107
1145
  }
1108
1146
  logger.info("Scheduled task removed", { task: WINDOWS_TASK_NAME });
1109
- if (isDaemonRunning()) {
1110
- const result = await stopDaemon();
1111
- if (!result.ok) console.log(`Could not stop the daemon: ${result.error}`);
1112
- }
1113
- printUninstalled();
1114
1147
  }
1115
1148
  function readServiceEntryText() {
1116
1149
  const platform = os3.platform();
@@ -1141,51 +1174,34 @@ function serviceRestartInstruction(info, platform = os3.platform(), query = runS
1141
1174
  }
1142
1175
  return null;
1143
1176
  }
1144
- function installService() {
1145
- const platform = os3.platform();
1146
- const context = {
1147
- platform,
1148
- isPkg: "pkg" in process,
1149
- execPath: process.execPath,
1150
- home: os3.homedir(),
1151
- env: process.env
1152
- };
1153
- const refusal = serviceInstallRefusal(context);
1154
- if (refusal) {
1155
- console.error(refusal);
1156
- process.exit(1);
1157
- }
1158
- const program = serviceProgramPath(context);
1159
- switch (platform) {
1160
- case "linux":
1161
- installSystemd(program);
1162
- break;
1163
- case "darwin":
1164
- installLaunchd(program);
1165
- break;
1166
- case "win32":
1167
- installWindows(program);
1168
- break;
1169
- default:
1170
- console.error(`Unsupported platform: ${platform}`);
1171
- process.exit(1);
1177
+ function installBootEntry(program, platform = os3.platform()) {
1178
+ if (platform === "win32") {
1179
+ installWindows(program);
1180
+ return;
1181
+ }
1182
+ if (platform !== "linux" && platform !== "darwin") {
1183
+ throw new Error(`Autostart is not supported on ${platform}.`);
1184
+ }
1185
+ try {
1186
+ if (platform === "linux") installSystemd(program);
1187
+ else installLaunchd(program);
1188
+ } catch (err) {
1189
+ throw new Error(`Could not turn on autostart: ${toolError(err)}`);
1172
1190
  }
1173
1191
  }
1174
- async function uninstallService() {
1175
- const platform = os3.platform();
1176
- switch (platform) {
1177
- case "linux":
1178
- uninstallSystemd();
1179
- break;
1180
- case "darwin":
1181
- uninstallLaunchd();
1182
- break;
1183
- case "win32":
1184
- await uninstallWindows();
1185
- break;
1186
- default:
1187
- console.error(`Unsupported platform: ${platform}`);
1188
- process.exit(1);
1192
+ function removeBootEntry(platform = os3.platform()) {
1193
+ if (platform === "win32") {
1194
+ removeWindows();
1195
+ return;
1196
+ }
1197
+ if (platform !== "linux" && platform !== "darwin") {
1198
+ throw new Error(`Autostart is not supported on ${platform}.`);
1199
+ }
1200
+ try {
1201
+ if (platform === "linux") removeSystemd();
1202
+ else removeLaunchd();
1203
+ } catch (err) {
1204
+ throw new Error(`Could not turn off autostart: ${toolError(err)}`);
1189
1205
  }
1190
1206
  }
1191
1207
 
@@ -1324,7 +1340,7 @@ async function ensureDaemonRunning() {
1324
1340
  return info;
1325
1341
  }
1326
1342
  if (process.versions.electron) {
1327
- const { runDaemonChild } = await import("./daemonChild-GGG34AOT.js");
1343
+ const { runDaemonChild } = await import("./daemonChild-5HZFQLQR.js");
1328
1344
  const handle = await runDaemonChild({
1329
1345
  installSignalHandlers: false,
1330
1346
  exitOnFailure: false,
@@ -1901,8 +1917,16 @@ export {
1901
1917
  createInstallProbe,
1902
1918
  installerBinaryPath,
1903
1919
  autostartFilePath,
1904
- installService,
1905
- uninstallService,
1920
+ AUTOSTART_ON_COMMAND,
1921
+ AUTOSTART_OFF_COMMAND,
1922
+ machineServiceInstallContext,
1923
+ serviceProgramPath,
1924
+ serviceInstallRefusal,
1925
+ sameProgram,
1926
+ readBootEntry,
1927
+ startsBeforeSignIn,
1928
+ installBootEntry,
1929
+ removeBootEntry,
1906
1930
  currentBootId,
1907
1931
  startedInEarlierBoot,
1908
1932
  resolveLatestTag,
@@ -37,6 +37,7 @@ var Subcommand = {
37
37
  Restart: "restart",
38
38
  Device: "device",
39
39
  Update: "update",
40
+ Autostart: "autostart",
40
41
  // The old spelling, refused with a hint. As a subcommand it never reaches tunnel mode, where `--token` is the
41
42
  // tunnel token and a device token would go to the tunnel server.
42
43
  Devices: "devices"