|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "runtime" |
| 9 | + "text/template" |
| 10 | + |
| 11 | + "github.com/qingchencloud/cftunnel/internal/config" |
| 12 | + "github.com/qingchencloud/cftunnel/internal/relay" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +func init() { |
| 17 | + relayCmd.AddCommand(relayInstallCmd) |
| 18 | + relayCmd.AddCommand(relayUninstallCmd) |
| 19 | +} |
| 20 | + |
| 21 | +var relayInstallCmd = &cobra.Command{ |
| 22 | + Use: "install", |
| 23 | + Short: "注册中继客户端为系统服务(开机自启)", |
| 24 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 25 | + cfg, err := config.Load() |
| 26 | + if err != nil { |
| 27 | + return err |
| 28 | + } |
| 29 | + if cfg.Relay.Server == "" { |
| 30 | + return fmt.Errorf("未配置中继服务器,请先执行 cftunnel relay init") |
| 31 | + } |
| 32 | + if len(cfg.Relay.Rules) == 0 { |
| 33 | + return fmt.Errorf("暂无中继规则,请先执行 cftunnel relay add") |
| 34 | + } |
| 35 | + binPath, err := relay.EnsureFrpc() |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + if err := relay.GenerateFrpcConfig(&cfg.Relay); err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + switch runtime.GOOS { |
| 43 | + case "darwin": |
| 44 | + return installRelayLaunchd(binPath) |
| 45 | + case "linux": |
| 46 | + return installRelaySystemd(binPath) |
| 47 | + case "windows": |
| 48 | + return installRelayWindows(binPath) |
| 49 | + default: |
| 50 | + return fmt.Errorf("不支持的平台: %s", runtime.GOOS) |
| 51 | + } |
| 52 | + }, |
| 53 | +} |
| 54 | + |
| 55 | +// ==================== macOS launchd ==================== |
| 56 | + |
| 57 | +const relayPlistName = "com.cftunnel.frpc" |
| 58 | + |
| 59 | +const relayPlistTmpl = `<?xml version="1.0" encoding="UTF-8"?> |
| 60 | +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 61 | +<plist version="1.0"> |
| 62 | +<dict> |
| 63 | + <key>Label</key> |
| 64 | + <string>{{.Label}}</string> |
| 65 | + <key>ProgramArguments</key> |
| 66 | + <array> |
| 67 | + <string>{{.BinPath}}</string> |
| 68 | + <string>-c</string> |
| 69 | + <string>{{.ConfigPath}}</string> |
| 70 | + </array> |
| 71 | + <key>KeepAlive</key> |
| 72 | + <true/> |
| 73 | + <key>RunAtLoad</key> |
| 74 | + <true/> |
| 75 | + <key>StandardOutPath</key> |
| 76 | + <string>{{.LogPath}}</string> |
| 77 | + <key>StandardErrorPath</key> |
| 78 | + <string>{{.LogPath}}</string> |
| 79 | +</dict> |
| 80 | +</plist> |
| 81 | +` |
| 82 | + |
| 83 | +func relayPlistPath() string { |
| 84 | + home, _ := os.UserHomeDir() |
| 85 | + return filepath.Join(home, "Library/LaunchAgents", relayPlistName+".plist") |
| 86 | +} |
| 87 | + |
| 88 | +func installRelayLaunchd(binPath string) error { |
| 89 | + home, _ := os.UserHomeDir() |
| 90 | + data := map[string]string{ |
| 91 | + "Label": relayPlistName, |
| 92 | + "BinPath": binPath, |
| 93 | + "ConfigPath": relay.FrpcConfigPath(), |
| 94 | + "LogPath": filepath.Join(home, "Library/Logs/cftunnel-relay.log"), |
| 95 | + } |
| 96 | + f, err := os.Create(relayPlistPath()) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + defer f.Close() |
| 101 | + if err := template.Must(template.New("").Parse(relayPlistTmpl)).Execute(f, data); err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + if err := exec.Command("launchctl", "load", relayPlistPath()).Run(); err != nil { |
| 105 | + return fmt.Errorf("launchctl load 失败: %w", err) |
| 106 | + } |
| 107 | + fmt.Printf("✓ 已注册 launchd 服务: %s\n", relayPlistName) |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +func uninstallRelayLaunchd() error { |
| 112 | + exec.Command("launchctl", "unload", relayPlistPath()).Run() |
| 113 | + os.Remove(relayPlistPath()) |
| 114 | + fmt.Printf("✓ 已卸载 launchd 服务: %s\n", relayPlistName) |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +// ==================== Linux systemd ==================== |
| 119 | + |
| 120 | +const relayUnitName = "cftunnel-relay" |
| 121 | + |
| 122 | +func relayUnitPath() string { |
| 123 | + return "/etc/systemd/system/" + relayUnitName + ".service" |
| 124 | +} |
| 125 | + |
| 126 | +func installRelaySystemd(binPath string) error { |
| 127 | + unit := fmt.Sprintf(`[Unit] |
| 128 | +Description=cftunnel relay (frpc) |
| 129 | +After=network.target |
| 130 | +
|
| 131 | +[Service] |
| 132 | +ExecStart=%s -c %s |
| 133 | +Restart=always |
| 134 | +RestartSec=5 |
| 135 | +
|
| 136 | +[Install] |
| 137 | +WantedBy=multi-user.target |
| 138 | +`, binPath, relay.FrpcConfigPath()) |
| 139 | + |
| 140 | + if err := os.WriteFile(relayUnitPath(), []byte(unit), 0644); err != nil { |
| 141 | + return fmt.Errorf("写入 systemd unit 失败: %w", err) |
| 142 | + } |
| 143 | + if err := exec.Command("systemctl", "daemon-reload").Run(); err != nil { |
| 144 | + return fmt.Errorf("systemctl daemon-reload 失败: %w", err) |
| 145 | + } |
| 146 | + if err := exec.Command("systemctl", "enable", "--now", relayUnitName).Run(); err != nil { |
| 147 | + return fmt.Errorf("systemctl enable 失败: %w", err) |
| 148 | + } |
| 149 | + fmt.Printf("✓ 已注册 systemd 服务: %s\n", relayUnitName) |
| 150 | + return nil |
| 151 | +} |
| 152 | + |
| 153 | +func uninstallRelaySystemd() error { |
| 154 | + exec.Command("systemctl", "disable", "--now", relayUnitName).Run() |
| 155 | + os.Remove(relayUnitPath()) |
| 156 | + fmt.Printf("✓ 已卸载 systemd 服务: %s\n", relayUnitName) |
| 157 | + return nil |
| 158 | +} |
| 159 | + |
| 160 | +// ==================== Windows sc ==================== |
| 161 | + |
| 162 | +const relaySvcName = "cftunnel-relay" |
| 163 | + |
| 164 | +func installRelayWindows(binPath string) error { |
| 165 | + binArg := fmt.Sprintf(`%s -c %s`, binPath, relay.FrpcConfigPath()) |
| 166 | + if err := exec.Command("sc", "create", relaySvcName, "binPath=", binArg, "start=", "auto").Run(); err != nil { |
| 167 | + return fmt.Errorf("创建 Windows 服务失败: %w", err) |
| 168 | + } |
| 169 | + if err := exec.Command("sc", "start", relaySvcName).Run(); err != nil { |
| 170 | + return fmt.Errorf("启动服务失败: %w", err) |
| 171 | + } |
| 172 | + fmt.Printf("✓ 已注册 Windows 服务: %s\n", relaySvcName) |
| 173 | + return nil |
| 174 | +} |
| 175 | + |
| 176 | +func uninstallRelayWindows() error { |
| 177 | + exec.Command("sc", "stop", relaySvcName).Run() |
| 178 | + exec.Command("sc", "delete", relaySvcName).Run() |
| 179 | + fmt.Printf("✓ 已卸载 Windows 服务: %s\n", relaySvcName) |
| 180 | + return nil |
| 181 | +} |
| 182 | + |
| 183 | +var relayUninstallCmd = &cobra.Command{ |
| 184 | + Use: "uninstall", |
| 185 | + Short: "卸载中继客户端系统服务", |
| 186 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 187 | + switch runtime.GOOS { |
| 188 | + case "darwin": |
| 189 | + return uninstallRelayLaunchd() |
| 190 | + case "linux": |
| 191 | + return uninstallRelaySystemd() |
| 192 | + case "windows": |
| 193 | + return uninstallRelayWindows() |
| 194 | + default: |
| 195 | + return fmt.Errorf("不支持的平台: %s", runtime.GOOS) |
| 196 | + } |
| 197 | + }, |
| 198 | +} |
0 commit comments