Skip to content

Commit 26cd987

Browse files
ldm0206claude
andcommitted
Expose the core's connection-idle timeout in the panel
proxy.timeout drives Xray's connIdle policy, but only PATCH /api/settings could set it (as timeout_sec) and GET /api/settings never returned it, so the 5-minute idle limit that drops quiet SS tunnels was invisible and uneditable from the panel. The proxy card now carries an 空闲超时 field, and the API takes and returns proxy.timeout as a duration string, the same shape the health durations already use. Drop the http inbound's own "timeout" setting: this core version removed the field, so it was dead config claiming an idle limit that never applied. Correct the README and config.example wording too - handshake is pinned at 4s and is not configurable. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
1 parent c35b773 commit 26cd987

7 files changed

Lines changed: 99 additions & 11 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ v2h core config # 看内核实际拿到的配置(排
322322
| `proxy.fallback` | `reject` | 全局兜底:`reject` 拒绝连接,`direct` 直连 |
323323
| `proxy.dns_servers` | 空 | 内核使用的 DNS 服务器,留空用系统 DNS |
324324
| `proxy.custom_rules` | 空 | 原样插入 Xray 路由规则(排在按账号分流之前),只能手改文件 |
325-
| `proxy.timeout` | `300s` | 连接超时,Go duration 写法 |
325+
| `proxy.timeout` | `300s` | 内核的连接空闲上限(Xray 的 connIdle):一条连接连续 5 分钟没有任何上下行流量就被断开;面板可改 |
326326

327327
### `logs` / `health`
328328

‎config.example.yaml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ proxy:
120120
# 目前只能手工编辑文件(面板不提供编辑器)。面板不可改。
121121
custom_rules: []
122122

123-
# 连接空闲/握手超时,写法为 Go 的 duration:"300s"、"5m"。面板可改。
123+
# 连接空闲上限(Xray 的 connIdle):一条连接连续 5 分钟没有任何上下行流量,
124+
# 内核就会断开它。写法为 Go 的 duration:"300s"、"5m"。面板可改。
124125
timeout: 300s
125126

126127
# ---------------------------------------------------------------------------

‎internal/api/api_test.go‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"path/filepath"
1010
"strings"
1111
"testing"
12+
"time"
1213

1314
"github.com/ldm0206/vless-to-http/internal/auth"
1415
"github.com/ldm0206/vless-to-http/internal/config"
@@ -535,6 +536,42 @@ func TestSettingsPatchAndSecrecy(t *testing.T) {
535536
}
536537
}
537538

539+
// The panel edits the core's connection-idle timeout as a duration string, so
540+
// the settings view and the patch have to agree on that format.
541+
func TestSettingsPatchProxyTimeout(t *testing.T) {
542+
ts := newTestServer(t)
543+
cookie, csrf := ts.login(t, adminPassword)
544+
headers := [][2]string{{"X-CSRF-Token", csrf}}
545+
546+
resp := ts.do(t, http.MethodGet, "/api/settings", cookie, nil)
547+
var settings map[string]any
548+
decodeBody(t, resp, &settings)
549+
proxy, _ := settings["proxy"].(map[string]any)
550+
if proxy["timeout"] != "5m0s" {
551+
t.Fatalf("settings proxy.timeout = %v, want 5m0s", proxy["timeout"])
552+
}
553+
554+
resp = ts.do(t, http.MethodPatch, "/api/settings", cookie,
555+
map[string]any{"proxy": map[string]any{"timeout": "15m"}}, headers...)
556+
resp.Body.Close()
557+
if resp.StatusCode != http.StatusOK {
558+
t.Fatalf("patch = %d", resp.StatusCode)
559+
}
560+
if got := ts.store.Get().Proxy.Timeout.D(); got != 15*time.Minute {
561+
t.Fatalf("proxy timeout = %v, want 15m", got)
562+
}
563+
564+
bad := ts.do(t, http.MethodPatch, "/api/settings", cookie,
565+
map[string]any{"proxy": map[string]any{"timeout": "soon"}}, headers...)
566+
bad.Body.Close()
567+
if bad.StatusCode != http.StatusBadRequest {
568+
t.Fatalf("invalid timeout = %d, want 400", bad.StatusCode)
569+
}
570+
if got := ts.store.Get().Proxy.Timeout.D(); got != 15*time.Minute {
571+
t.Fatalf("a rejected patch changed the timeout to %v", got)
572+
}
573+
}
574+
538575
func TestStaticPanelIsServed(t *testing.T) {
539576
ts := newTestServer(t)
540577
resp := ts.do(t, http.MethodGet, "/", "", nil)

‎internal/api/assets/app.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2755,6 +2755,7 @@
27552755
return el('option', { value: f, selected: proxy.fallback === f ? true : null, text: fallbackLabel(f) + '(' + f + ')' });
27562756
}));
27572757
var dnsInput = el('input', { type: 'text', value: (proxy.dns_servers || []).join(', '), placeholder: '1.1.1.1, 8.8.8.8' });
2758+
var timeoutInput = el('input', { type: 'text', value: proxy.timeout || '5m0s', placeholder: '5m0s' });
27582759

27592760
var save = saveButton(function () {
27602761
var dns = dnsInput.value.split(',').map(function (x) { return x.trim(); }).filter(function (x) { return !!x; });
@@ -2765,7 +2766,8 @@
27652766
socks: { enabled: !!socksEnabled.checked, listen: socksListen.value.trim(), udp: !!socksUDP.checked },
27662767
sniffing: !!sniffing.checked,
27672768
fallback: fallbackSelect.value,
2768-
dns_servers: dns
2769+
dns_servers: dns,
2770+
timeout: timeoutInput.value.trim()
27692771
}
27702772
}).then(applySaved);
27712773
});
@@ -2785,7 +2787,9 @@
27852787
]),
27862788
toggleRow('允许 UDP(SOCKS5)', socksUDP),
27872789
el('div', { class: 'divider' }),
2790+
sectionHead('连接', '内核对每条代理连接的限制,改完会重启内核。'),
27882791
el('div', { class: 'form-grid' }, [
2792+
field('空闲超时', timeoutInput, '一条连接连续这么久没有任何上下行流量,内核就断开它(Xray 的 connIdle)。写法如 300s、5m。'),
27892793
field('兜底策略', fallbackSelect, '账号没有自己的兜底设置时使用这里的策略。'),
27902794
field('DNS 服务器', dnsInput, '逗号分隔,例如 1.1.1.1, 8.8.8.8;用于内核解析域名。')
27912795
]),

‎internal/api/handlers.go‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ type proxyInput struct {
914914
Sniffing *bool `json:"sniffing"`
915915
Fallback *string `json:"fallback"`
916916
DNSServers *[]string `json:"dns_servers"`
917-
TimeoutSec *int `json:"timeout_sec"`
917+
Timeout *string `json:"timeout"`
918918
}
919919

920920
type logsInput struct {
@@ -999,8 +999,14 @@ func (s *Server) handleSettingsPatch(w http.ResponseWriter, r *http.Request) {
999999
if in.Proxy.DNSServers != nil {
10001000
c.Proxy.DNSServers = *in.Proxy.DNSServers
10011001
}
1002-
if in.Proxy.TimeoutSec != nil && *in.Proxy.TimeoutSec > 0 {
1003-
c.Proxy.Timeout = config.Duration(time.Duration(*in.Proxy.TimeoutSec) * time.Second)
1002+
if in.Proxy.Timeout != nil && *in.Proxy.Timeout != "" {
1003+
d, err := time.ParseDuration(*in.Proxy.Timeout)
1004+
if err != nil {
1005+
return fmt.Errorf("时间格式不正确:%q", *in.Proxy.Timeout)
1006+
}
1007+
if d > 0 {
1008+
c.Proxy.Timeout = config.Duration(d)
1009+
}
10041010
}
10051011
}
10061012

‎internal/xraycore/build.go‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ type Observatory struct {
5353

5454
// Plan is everything the config builder needs; the engine fills it in.
5555
type Plan struct {
56-
HTTP ListenSpec
57-
SOCKS ListenSpec
58-
SocksUDP bool
59-
Sniffing bool
56+
HTTP ListenSpec
57+
SOCKS ListenSpec
58+
SocksUDP bool
59+
Sniffing bool
60+
// Timeout is the core's connection-idle policy, in seconds. The inbounds
61+
// have no timeout of their own, so this is the only place it can land.
6062
Timeout int
6163
DNSServers []string
6264
CustomRules []map[string]any
@@ -158,7 +160,6 @@ func (p Plan) inbounds() []any {
158160
"protocol": "http",
159161
"settings": map[string]any{
160162
"accounts": accounts,
161-
"timeout": p.Timeout,
162163
"userLevel": 0,
163164
},
164165
}

‎internal/xraycore/build_test.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,42 @@ func TestSkipSniffingRulePrecedesCustomRules(t *testing.T) {
7878
t.Fatalf("normal user rule order changed: %+v", rules)
7979
}
8080
}
81+
82+
// The core dropped the http inbound's own "timeout" setting, so the policy
83+
// block is the only place a value can still take effect. Emitting the dead
84+
// field would make the generated config claim an idle limit it does not have.
85+
func TestHTTPInboundCarriesNoTimeoutOfItsOwn(t *testing.T) {
86+
plan := xraycore.Plan{
87+
HTTP: xraycore.ListenSpec{Enabled: true, Listen: "127.0.0.1:8080"},
88+
Timeout: 900,
89+
Users: []xraycore.PlanUser{{Name: "alice", Password: "pw", OutboundTag: xraycore.DirectTag}},
90+
}
91+
92+
raw, err := xraycore.BuildJSON(plan)
93+
if err != nil {
94+
t.Fatalf("build: %v", err)
95+
}
96+
var doc struct {
97+
Policy struct {
98+
Levels map[string]map[string]any `json:"levels"`
99+
} `json:"policy"`
100+
Inbounds []struct {
101+
Tag string `json:"tag"`
102+
Settings map[string]any `json:"settings"`
103+
} `json:"inbounds"`
104+
}
105+
if err := json.Unmarshal(raw, &doc); err != nil {
106+
t.Fatalf("decode: %v", err)
107+
}
108+
if got, _ := doc.Policy.Levels["0"]["connIdle"].(float64); int(got) != 900 {
109+
t.Fatalf("connIdle = %v, want 900", doc.Policy.Levels["0"]["connIdle"])
110+
}
111+
for _, in := range doc.Inbounds {
112+
if in.Tag != xraycore.HTTPInTag {
113+
continue
114+
}
115+
if _, dead := in.Settings["timeout"]; dead {
116+
t.Fatalf("http inbound still carries a timeout setting: %v", in.Settings)
117+
}
118+
}
119+
}

0 commit comments

Comments
 (0)