fix(traffic): 流量录制代理默认只监听 127.0.0.1,避免默认开放代理暴露 (#129) (#130)

This commit is contained in:
dingpotian authored and GitHub committed 2026-09-16 09:36:32 +08:00
1 parent 7f71f77d16
commit 80a085d157
5 files changed
+32 -5

No files matched your search

+1 -1
View File
@@ -54,7 +54,7 @@ func run() int {
var ( var (
addr = flag.String("addr", ":8787", "HTTP listen address") addr = flag.String("addr", ":8787", "HTTP listen address")
dataDir = flag.String("data", filepath.Join(config.BaseDir(), "data"), "data directory for SQLite stores (default: data/ next to the executable)") dataDir = flag.String("data", filepath.Join(config.BaseDir(), "data"), "data directory for SQLite stores (default: data/ next to the executable)")
proxy = flag.String("proxy", ":8788", "traffic recording proxy address (empty to disable)") proxy = flag.String("proxy", "127.0.0.1:8788", "traffic recording proxy address (empty to disable)")
) )
flag.Parse() flag.Parse()
+1 -1
View File
@@ -11,7 +11,7 @@ cleanup() { kill 0 2>/dev/null || true; }
trap cleanup EXIT INT TERM trap cleanup EXIT INT TERM
# 后端(普通 go run,不内嵌前端);并发 work agent 数在「系统设置」里配置。 # 后端(普通 go run,不内嵌前端);并发 work agent 数在「系统设置」里配置。
go run ./cmd/artex -addr :8787 -proxy :8788 & go run ./cmd/artex -addr :8787 -proxy 127.0.0.1:8788 &
# 前端热更新(Vite/Next dev server,/api 反代到 :8787)。 # 前端热更新(Vite/Next dev server,/api 反代到 :8787)。
( cd web && npm run dev ) & ( cd web && npm run dev ) &
+1 -1
View File
@@ -29,7 +29,7 @@ services:
ARTEX_LLM_PROXY: ${ARTEX_LLM_PROXY:-} ARTEX_LLM_PROXY: ${ARTEX_LLM_PROXY:-}
ports: ports:
- "8787:8787" - "8787:8787"
- "8788:8788" - "127.0.0.1:8788:8788"
volumes: volumes:
# 把数据目录(含 jwt.key、SQLite 等)绑定到项目根目录的 data/, # 把数据目录(含 jwt.key、SQLite 等)绑定到项目根目录的 data/,
# 方便在宿主机直接查看/备份,容器重建也不丢。 # 方便在宿主机直接查看/备份,容器重建也不丢。
+21
View File
@@ -57,3 +57,24 @@ func TestSetUpstreamProxyStoreClear(t *testing.T) {
t.Fatalf("invalid set mutated upstream to %v, want nil", got) t.Fatalf("invalid set mutated upstream to %v, want nil", got)
} }
} }
func TestProxyAddr(t *testing.T) {
cases := []struct {
addr string
want string
}{
// Bare :port means "bind all interfaces" — the legacy default. The URL
// agents consume must still point at loopback so they reach the local proxy.
{":8788", "http://127.0.0.1:8788"},
// Explicit loopback — the current default since #129 (open proxy exposure).
{"127.0.0.1:8788", "http://127.0.0.1:8788"},
// Explicit all-interface bind is still supported (remote capture via SSH).
{"0.0.0.0:8788", "http://0.0.0.0:8788"},
}
for _, c := range cases {
got := (&Traffic{addr: c.addr}).ProxyAddr()
if got != c.want {
t.Errorf("ProxyAddr(%q) = %q, want %q", c.addr, got, c.want)
}
}
}
+8 -2
View File
@@ -200,8 +200,14 @@ func hostOnly(hostport string) string {
return hostport return hostport
} }
// ProxyAddr returns the address workers should set as HTTP(S)_PROXY. // ProxyAddr returns the address workers should set as HTTP(S)_PROXY. A bare
func (t *Traffic) ProxyAddr() string { return "http://127.0.0.1" + t.addr } // ":port" means "bind all interfaces" (legacy default), so map it to loopback.
func (t *Traffic) ProxyAddr() string {
if strings.HasPrefix(t.addr, ":") {
return "http://127.0.0.1" + t.addr
}
return "http://" + t.addr
}
// SetUpstreamProxy points every captured request at a global egress proxy // SetUpstreamProxy points every captured request at a global egress proxy
// (http/https/socks5, optional user:pass in the URL). An empty raw string clears // (http/https/socks5, optional user:pass in the URL). An empty raw string clears