Skip to content

Commit 1ed57eb

Browse files
committed
Add dashboard support for API service
1 parent 326615d commit 1ed57eb

13 files changed

Lines changed: 533 additions & 7 deletions

File tree

docs/configuration/rule-set/index.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,16 @@ HTTP Client for downloading rule-set.
119119

120120
See [HTTP Client Fields](/configuration/shared/http-client/) for details.
121121

122-
Default transport will be used if empty.
122+
When empty, the default HTTP client is used: the one named by
123+
[`default_http_client`](/configuration/route/#default_http_client), or the first top-level
124+
`http_clients` entry when `default_http_client` is empty.
125+
126+
!!! failure "Implicit default deprecated in sing-box 1.14.0"
127+
128+
When neither `http_clients` nor `default_http_client` is configured, an implicit HTTP
129+
client connecting through the default outbound is used. This implicit default is
130+
deprecated in sing-box 1.14.0 and will be removed in sing-box 1.16.0; define
131+
`http_clients` instead.
123132

124133
#### update_interval
125134

docs/configuration/rule-set/index.zh.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,13 @@
119119

120120
参阅 [HTTP 客户端字段](/zh/configuration/shared/http-client/) 了解详情。
121121

122-
如果为空,将使用默认传输。
122+
留空时使用默认 HTTP 客户端:即由 [`default_http_client`](/zh/configuration/route/#default_http_client)
123+
指定的客户端,或当 `default_http_client` 为空时使用顶级 `http_clients` 的第一项。
124+
125+
!!! failure "隐式默认已在 sing-box 1.14.0 废弃"
126+
127+
当 `http_clients` 与 `default_http_client` 均未配置时,将使用通过默认出站连接的隐式 HTTP 客户端。
128+
该隐式默认已在 sing-box 1.14.0 废弃,并将在 sing-box 1.16.0 移除;请改为定义 `http_clients`。
123129

124130
#### update_interval
125131

docs/configuration/service/api.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ for bidirectional streaming methods.
2323
"secret": "",
2424
"access_control_allow_origin": [],
2525
"access_control_allow_private_network": false,
26+
"dashboard": {
27+
"enabled": true,
28+
"path": "",
29+
"download_url": "",
30+
"http_client": "", // or {}
31+
"update_interval": ""
32+
},
2633
"tls": {}
2734
}
2835
```
@@ -49,6 +56,59 @@ CORS allowed origins, `*` will be used if empty.
4956

5057
Allow access from private network.
5158

59+
#### dashboard
60+
61+
Web dashboard downloaded and served over the API listener at `/dashboard/`; other browser
62+
requests are redirected to it.
63+
64+
!!! info ""
65+
66+
The object can be replaced with a boolean value (equivalent to `{ "enabled": <bool> }`),
67+
or with a string path (equivalent to `{ "enabled": true, "path": "<string>" }`).
68+
69+
##### enabled
70+
71+
Enable the dashboard.
72+
73+
##### path
74+
75+
Directory the dashboard files are stored in.
76+
77+
`dashboard` in the working directory will be used by default.
78+
79+
If the directory is empty, the dashboard is downloaded and an `.etag` file is stored inside
80+
it to skip unchanged updates. A non-empty directory without an `.etag` file is served as-is
81+
and never updated automatically.
82+
83+
##### download_url
84+
85+
Download URL of the dashboard archive (zip).
86+
87+
`https://github.com/SagerNet/sing-box-dashboard/archive/refs/heads/gh-pages.zip` will be used by default.
88+
89+
##### http_client
90+
91+
HTTP client used to download the dashboard, with the same behavior as remote rule-sets.
92+
93+
See [HTTP Client Fields](/configuration/shared/http-client/) for details.
94+
95+
When empty, the default HTTP client is used: the one named by
96+
[`default_http_client`](/configuration/route/#default_http_client), or the first top-level
97+
`http_clients` entry when `default_http_client` is empty.
98+
99+
!!! failure "Implicit default deprecated in sing-box 1.14.0"
100+
101+
When neither `http_clients` nor `default_http_client` is configured, an implicit HTTP
102+
client connecting through the default outbound is used. This implicit default is
103+
deprecated in sing-box 1.14.0 and will be removed in sing-box 1.16.0; define
104+
`http_clients` instead.
105+
106+
##### update_interval
107+
108+
Update interval of the dashboard.
109+
110+
`1d` will be used by default.
111+
52112
#### tls
53113

54114
TLS configuration, see [TLS](/configuration/shared/tls/#inbound).

docs/configuration/service/api.zh.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ sing-box API 服务是用于观察与控制正在运行的 sing-box 实例的 gR
2222
"secret": "",
2323
"access_control_allow_origin": [],
2424
"access_control_allow_private_network": false,
25+
"dashboard": {
26+
"enabled": true,
27+
"path": "",
28+
"download_url": "",
29+
"http_client": "", // 或 {}
30+
"update_interval": ""
31+
},
2532
"tls": {}
2633
}
2734
```
@@ -38,7 +45,7 @@ API 密钥。
3845

3946
客户端通过标准的 `authorization: Bearer <secret>` gRPC metadata 头认证。
4047

41-
留空则禁用认证
48+
默认无需认证
4249

4350
#### access_control_allow_origin
4451

@@ -48,6 +55,54 @@ API 密钥。
4855

4956
允许从私有网络访问。
5057

58+
#### dashboard
59+
60+
下载并通过 API 监听器在 `/dashboard/` 提供的 Web 仪表板;其他浏览器请求将被重定向到该路径。
61+
62+
!!! info ""
63+
64+
该对象可以替换为布尔值(等同于 `{ "enabled": <bool> }`),
65+
或字符串路径(等同于 `{ "enabled": true, "path": "<string>" }`)。
66+
67+
##### enabled
68+
69+
启用仪表板。
70+
71+
##### path
72+
73+
存放仪表板文件的目录。
74+
75+
默认使用工作目录下的 `dashboard`
76+
77+
如果目录为空,将下载仪表板,并在其中存放 `.etag` 文件以跳过未变更的更新。
78+
非空且不含 `.etag` 文件的目录将按原样提供,且不会自动更新。
79+
80+
##### download_url
81+
82+
仪表板压缩包(zip)的下载 URL。
83+
84+
默认使用 `https://github.com/SagerNet/sing-box-dashboard/archive/refs/heads/gh-pages.zip`
85+
86+
##### http_client
87+
88+
用于下载仪表板的 HTTP 客户端,行为与远程规则集相同。
89+
90+
参阅 [HTTP 客户端字段](/zh/configuration/shared/http-client/)
91+
92+
留空时使用默认 HTTP 客户端:即由 [`default_http_client`](/zh/configuration/route/#default_http_client)
93+
指定的客户端,或当 `default_http_client` 为空时使用顶级 `http_clients` 的第一项。
94+
95+
!!! failure "隐式默认已在 sing-box 1.14.0 废弃"
96+
97+
当 `http_clients` 与 `default_http_client` 均未配置时,将使用通过默认出站连接的隐式 HTTP 客户端。
98+
该隐式默认已在 sing-box 1.14.0 废弃,并将在 sing-box 1.16.0 移除;请改为定义 `http_clients`。
99+
100+
##### update_interval
101+
102+
仪表板的更新间隔。
103+
104+
默认使用 `1d`
105+
51106
#### tls
52107

53108
TLS 配置,参阅 [TLS](/zh/configuration/shared/tls/#inbound)

option/api.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,50 @@
11
package option
22

3-
import "github.com/sagernet/sing/common/json/badoption"
3+
import (
4+
"github.com/sagernet/sing/common/json"
5+
"github.com/sagernet/sing/common/json/badoption"
6+
)
47

58
type APIServiceOptions struct {
69
ListenOptions
710
Secret string `json:"secret,omitempty"`
811
AccessControlAllowOrigin badoption.Listable[string] `json:"access_control_allow_origin,omitempty"`
912
AccessControlAllowPrivateNetwork bool `json:"access_control_allow_private_network,omitempty"`
13+
Dashboard *APIDashboardOptions `json:"dashboard,omitempty"`
1014
InboundTLSOptionsContainer
1115
}
16+
17+
type _APIDashboardOptions struct {
18+
Enabled bool `json:"enabled,omitempty"`
19+
Path string `json:"path,omitempty"`
20+
DownloadURL string `json:"download_url,omitempty"`
21+
HTTPClient *HTTPClientOptions `json:"http_client,omitempty"`
22+
UpdateInterval badoption.Duration `json:"update_interval,omitempty"`
23+
}
24+
25+
type APIDashboardOptions _APIDashboardOptions
26+
27+
func (o APIDashboardOptions) MarshalJSON() ([]byte, error) {
28+
if o.DownloadURL == "" && o.HTTPClient == nil && o.UpdateInterval == 0 {
29+
if o.Path == "" {
30+
return json.Marshal(o.Enabled)
31+
}
32+
if o.Enabled {
33+
return json.Marshal(o.Path)
34+
}
35+
}
36+
return json.Marshal(_APIDashboardOptions(o))
37+
}
38+
39+
func (o *APIDashboardOptions) UnmarshalJSON(bytes []byte) error {
40+
err := json.Unmarshal(bytes, &o.Enabled)
41+
if err == nil {
42+
return nil
43+
}
44+
err = json.Unmarshal(bytes, &o.Path)
45+
if err == nil {
46+
o.Enabled = true
47+
return nil
48+
}
49+
return json.UnmarshalDisallowUnknownFields(bytes, (*_APIDashboardOptions)(o))
50+
}

0 commit comments

Comments
 (0)