Skip to content

Commit 2ef97d5

Browse files
authored
Log config output path in generate command (#4)
1 parent 5beeb19 commit 2ef97d5

4 files changed

Lines changed: 65 additions & 0 deletions

File tree

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,30 @@ Install a specific version:
7979
curl -fsSL https://raw.githubusercontent.com/rainbend/sing-box-subscribe-cli/main/install.sh | VERSION=v1.0.0 bash
8080
```
8181

82+
Use `ExecStartPre` in a systemd unit when you want to regenerate the sing-box config before starting sing-box:
83+
84+
```ini
85+
[Unit]
86+
Description=sing-box service
87+
Documentation=https://sing-box.sagernet.org
88+
After=network.target nss-lookup.target network-online.target
89+
90+
[Service]
91+
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_SYS_PTRACE CAP_DAC_READ_SEARCH
92+
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_SYS_PTRACE CAP_DAC_READ_SEARCH
93+
ExecStartPre=/usr/local/bin/sing-box-sub --out /etc/sing-box/config.json 'https://example.com/api/v1/client/subscribe?token=REDACTED'
94+
ExecStart=/usr/bin/sing-box -D /var/lib/sing-box -C /etc/sing-box run
95+
ExecReload=/bin/kill -HUP $MAINPID
96+
Restart=on-failure
97+
RestartSec=10s
98+
LimitNOFILE=infinity
99+
100+
[Install]
101+
WantedBy=multi-user.target
102+
```
103+
104+
If `ExecStartPre` fails, systemd stops the startup before launching sing-box. Make sure `/usr/local/bin/sing-box-sub` exists, the service can write `/etc/sing-box/config.json`, and the subscription URL is reachable when the service starts.
105+
82106
### Windows
83107

84108
Download the matching `.exe` from the [GitHub Releases page](https://github.com/rainbend/sing-box-subscribe-cli/releases), rename it to `sing-box-sub.exe` if you like, and place it in a directory listed in `PATH`.

README.zh-CN.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,30 @@ curl -fsSL https://raw.githubusercontent.com/rainbend/sing-box-subscribe-cli/mai
7979
curl -fsSL https://raw.githubusercontent.com/rainbend/sing-box-subscribe-cli/main/install.sh | VERSION=v1.0.0 bash
8080
```
8181

82+
如果希望在 sing-box 启动前自动重新生成配置,可以在 systemd unit 中使用 `ExecStartPre`
83+
84+
```ini
85+
[Unit]
86+
Description=sing-box service
87+
Documentation=https://sing-box.sagernet.org
88+
After=network.target nss-lookup.target network-online.target
89+
90+
[Service]
91+
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_SYS_PTRACE CAP_DAC_READ_SEARCH
92+
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_SYS_PTRACE CAP_DAC_READ_SEARCH
93+
ExecStartPre=/usr/local/bin/sing-box-sub --out /etc/sing-box/config.json 'https://example.com/api/v1/client/subscribe?token=REDACTED'
94+
ExecStart=/usr/bin/sing-box -D /var/lib/sing-box -C /etc/sing-box run
95+
ExecReload=/bin/kill -HUP $MAINPID
96+
Restart=on-failure
97+
RestartSec=10s
98+
LimitNOFILE=infinity
99+
100+
[Install]
101+
WantedBy=multi-user.target
102+
```
103+
104+
如果 `ExecStartPre` 执行失败,systemd 会在启动 sing-box 前停止本次启动流程。请确认 `/usr/local/bin/sing-box-sub` 已存在,服务有权限写入 `/etc/sing-box/config.json`,并且服务启动时可以访问订阅 URL。
105+
82106
### Windows
83107

84108
[GitHub Releases 页面](https://github.com/rainbend/sing-box-subscribe-cli/releases) 下载匹配的 `.exe` 文件,可以重命名为 `sing-box-sub.exe`,并放到 `PATH` 包含的目录。

cmd/sing-box-subscribe-cli/main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"os"
9+
"path/filepath"
910
"time"
1011

1112
"github.com/rainbend/sing-box-subscribe-cli/internal/subconv"
@@ -112,12 +113,25 @@ func runGenerate(cmd *cobra.Command, opts subconv.Options, timeout time.Duration
112113
fmt.Fprintf(stderr, " with %d warnings", len(result.Warnings))
113114
}
114115
fmt.Fprintln(stderr)
116+
if opts.Output == "-" {
117+
fmt.Fprintln(stderr, "config output: stdout")
118+
} else {
119+
fmt.Fprintf(stderr, "config path: %s\n", displayPath(opts.Output))
120+
}
115121
for _, warning := range result.Warnings {
116122
fmt.Fprintf(stderr, "warning: %s\n", warning)
117123
}
118124
return nil
119125
}
120126

127+
func displayPath(path string) string {
128+
absolute, err := filepath.Abs(path)
129+
if err != nil {
130+
return path
131+
}
132+
return absolute
133+
}
134+
121135
func writeJSON(path string, value any) error {
122136
var out *os.File
123137
if path == "-" {

cmd/sing-box-subscribe-cli/main_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ proxies:
7676
if !strings.Contains(stderr, "generated 1 nodes") {
7777
t.Fatalf("stderr missing generated count:\n%s", stderr)
7878
}
79+
if !strings.Contains(stderr, "config path: "+outputPath) {
80+
t.Fatalf("stderr missing output path:\n%s", stderr)
81+
}
7982
output, err := os.ReadFile(outputPath)
8083
if err != nil {
8184
t.Fatal(err)

0 commit comments

Comments
 (0)