@wendongfly/myhi 1.3.22 → 1.3.23

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.
Files changed (2) hide show
  1. package/bin/myhi.js +105 -1
  2. package/package.json +1 -1
package/bin/myhi.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import { spawn } from 'child_process';
3
3
  import { fileURLToPath } from 'url';
4
4
  import { dirname, join } from 'path';
5
- import { mkdirSync, openSync, readFileSync, writeFileSync, unlinkSync } from 'fs';
5
+ import { mkdirSync, openSync, readFileSync, writeFileSync, unlinkSync, existsSync } from 'fs';
6
6
  import { homedir } from 'os';
7
7
 
8
8
  const __filename = fileURLToPath(import.meta.url);
@@ -570,6 +570,9 @@ myhi — 基于 Web 的终端共享工具
570
570
  myhi restart 重启后台服务器
571
571
  myhi status 查看后台运行状态
572
572
  myhi log [N] 查看后台日志(最后 N 行,默认 50)
573
+ myhi service install 安装为 systemd 服务(Linux,需 sudo)
574
+ myhi service uninstall 卸载 systemd 服务
575
+ myhi service status 查看 systemd 服务状态
573
576
 
574
577
  用户管理:
575
578
  myhi user list 列出所有用户
@@ -621,6 +624,107 @@ SSH 开发:
621
624
  myhi user add 1234 张三 D:\\project # 添加用户
622
625
  `);
623
626
 
627
+ } else if (cmd === 'service') {
628
+ // systemd service 管理
629
+ const subCmd = args[1] || 'install';
630
+ const { execSync: ex } = await import('child_process');
631
+
632
+ if (process.platform !== 'linux') {
633
+ console.log('[myhi] service 命令仅支持 Linux (systemd)');
634
+ process.exit(1);
635
+ }
636
+
637
+ // 找到 myhi 可执行文件路径
638
+ let myhiBin;
639
+ try { myhiBin = ex('which myhi', { encoding: 'utf8' }).trim(); } catch { myhiBin = '/usr/local/bin/myhi'; }
640
+
641
+ const user = process.env.USER || 'root';
642
+ const home = homedir();
643
+ const cwd = process.env.MYHI_CWD || process.cwd();
644
+ const port = process.env.PORT || '12300';
645
+ const serviceFile = '/etc/systemd/system/myhi.service';
646
+
647
+ const unitContent = `[Unit]
648
+ Description=myhi - Web Terminal Sharing
649
+ After=network.target
650
+
651
+ [Service]
652
+ Type=forking
653
+ PIDFile=${home}/.myhi/daemon.pid
654
+ ExecStart=${myhiBin} -d
655
+ ExecStop=${myhiBin} stop
656
+ WorkingDirectory=${cwd}
657
+ Restart=on-failure
658
+ RestartSec=10
659
+ User=${user}
660
+ Environment=PORT=${port}
661
+ Environment=HOME=${home}
662
+
663
+ [Install]
664
+ WantedBy=multi-user.target
665
+ `;
666
+
667
+ if (subCmd === 'install') {
668
+ try {
669
+ writeFileSync(serviceFile, unitContent);
670
+ } catch (e) {
671
+ if (e.code === 'EACCES') {
672
+ console.log('[myhi] 需要 root 权限,请使用: sudo myhi service install');
673
+ process.exit(1);
674
+ }
675
+ throw e;
676
+ }
677
+ console.log(`[myhi] 已创建 ${serviceFile}`);
678
+ console.log(` User: ${user}`);
679
+ console.log(` Port: ${port}`);
680
+ console.log(` WorkingDirectory: ${cwd}`);
681
+ console.log(` ExecStart: ${myhiBin} -d`);
682
+ console.log('');
683
+ try {
684
+ ex('systemctl daemon-reload', { stdio: 'inherit' });
685
+ ex('systemctl enable myhi.service', { stdio: 'inherit' });
686
+ ex('systemctl start myhi.service', { stdio: 'inherit' });
687
+ console.log('');
688
+ console.log('[myhi] 服务已启动并设为开机自启');
689
+ console.log(' 查看状态: systemctl status myhi.service');
690
+ console.log(' 查看日志: journalctl -u myhi -f');
691
+ console.log(' 停止服务: sudo systemctl stop myhi.service');
692
+ } catch (e) {
693
+ console.log('[myhi] systemctl 执行失败,请手动运行:');
694
+ console.log(' sudo systemctl daemon-reload');
695
+ console.log(' sudo systemctl enable myhi.service');
696
+ console.log(' sudo systemctl start myhi.service');
697
+ }
698
+
699
+ } else if (subCmd === 'uninstall' || subCmd === 'remove') {
700
+ try {
701
+ ex('systemctl stop myhi.service', { stdio: 'pipe' });
702
+ ex('systemctl disable myhi.service', { stdio: 'pipe' });
703
+ } catch {}
704
+ try {
705
+ unlinkSync(serviceFile);
706
+ ex('systemctl daemon-reload', { stdio: 'pipe' });
707
+ console.log('[myhi] 已卸载 systemd 服务');
708
+ } catch (e) {
709
+ if (e.code === 'EACCES') {
710
+ console.log('[myhi] 需要 root 权限,请使用: sudo myhi service uninstall');
711
+ } else if (e.code === 'ENOENT') {
712
+ console.log('[myhi] 服务文件不存在');
713
+ } else {
714
+ console.log('[myhi] 卸载失败:', e.message);
715
+ }
716
+ }
717
+
718
+ } else if (subCmd === 'status') {
719
+ try {
720
+ ex('systemctl status myhi.service', { stdio: 'inherit' });
721
+ } catch {}
722
+
723
+ } else {
724
+ console.log('用法: myhi service [install|uninstall|status]');
725
+ }
726
+ process.exit(0);
727
+
624
728
  } else if (cmd === '-d' || cmd === '--daemon' || cmd === 'daemon') {
625
729
  startDaemon();
626
730
  process.exit(0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wendongfly/myhi",
3
- "version": "1.3.22",
3
+ "version": "1.3.23",
4
4
  "description": "Web-based terminal sharing with chat UI — control your terminal from phone via LAN/Tailscale",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",