mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-11-24 17:51:51 +08:00
fix: make HTTP user agents configurable and remove tool identifiers
This commit is contained in:
1 parent
40746901db
commit
17c2fc5e08
30 files changed
+1204
-26
No files matched your search
@@ -18,6 +18,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/Autumn-27/artex/db"
|
||||
"github.com/Autumn-27/artex/internal/httpidentity"
|
||||
"github.com/Autumn-27/norma/permission"
|
||||
actool "github.com/Autumn-27/norma/tool"
|
||||
)
|
||||
@@ -438,6 +439,7 @@ func (s *Server) runHTTPTool(ctx context.Context, execRaw json.RawMessage, param
|
||||
if err != nil {
|
||||
return actool.Errorf(maskSecrets(err.Error())), nil
|
||||
}
|
||||
httpidentity.Apply(req)
|
||||
for k, v := range spec.Headers {
|
||||
req.Header.Set(k, renderTemplate(v, params, identity))
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/Autumn-27/artex/internal/httpidentity"
|
||||
"github.com/Autumn-27/artex/traffic"
|
||||
)
|
||||
|
||||
@@ -305,7 +306,7 @@ func probeOne(ctx context.Context, client *http.Client, target string) (string,
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("User-Agent", "artex-egress-probe/1.0")
|
||||
httpidentity.Apply(req)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
|
||||
@@ -27,6 +27,28 @@ func fakeIPServer(ip string) *httptest.Server {
|
||||
}))
|
||||
}
|
||||
|
||||
func TestProbeOneUserAgent(t *testing.T) {
|
||||
for _, ua := range []string{"", "Captured-UA/1.0"} {
|
||||
t.Run(ua, func(t *testing.T) {
|
||||
t.Setenv("ARTEX_HTTP_USER_AGENT", ua)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if got := r.UserAgent(); got != ua {
|
||||
t.Errorf("User-Agent = %q, want %q", got, ua)
|
||||
}
|
||||
if _, present := r.Header["User-Agent"]; ua == "" && present {
|
||||
t.Error("empty User-Agent present on wire")
|
||||
}
|
||||
fmt.Fprint(w, `{"ip":"203.0.113.7"}`)
|
||||
}))
|
||||
defer server.Close()
|
||||
ip, err := probeOne(context.Background(), server.Client(), server.URL)
|
||||
if err != nil || ip != "203.0.113.7" {
|
||||
t.Fatalf("ip = %q, err = %v", ip, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestProbeEgress 覆盖 probeEgress 的输入/响应形态:成功、非法协议、死代理、坏响应、回落。
|
||||
// 每条用例自带服务端与断言,失败时 t.Run 的名字直接指出是哪一条。
|
||||
func TestProbeEgress(t *testing.T) {
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTargetHTTPIdentityOverrides(t *testing.T) {
|
||||
t.Setenv(repeaterAllowLocalEnv, "1")
|
||||
for _, route := range []string{"custom", "repeater"} {
|
||||
for _, tc := range []struct {
|
||||
name, env, want string
|
||||
headers map[string]string
|
||||
}{
|
||||
{name: "default"},
|
||||
{name: "environment", env: "Captured-UA/1.0", want: "Captured-UA/1.0"},
|
||||
{name: "override", env: "Environment-UA/1.0", want: "Explicit-UA/1.0", headers: map[string]string{"user-agent": "Explicit-UA/1.0"}},
|
||||
{name: "explicit-empty", env: "Environment-UA/1.0", headers: map[string]string{"User-Agent": ""}},
|
||||
} {
|
||||
t.Run(route+"/"+tc.name, func(t *testing.T) {
|
||||
t.Setenv("ARTEX_HTTP_USER_AGENT", tc.env)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if got := r.UserAgent(); got != tc.want {
|
||||
t.Errorf("User-Agent = %q, want %q", got, tc.want)
|
||||
}
|
||||
if _, present := r.Header["User-Agent"]; tc.want == "" && present {
|
||||
t.Error("empty User-Agent present on wire")
|
||||
}
|
||||
w.Write([]byte("ok"))
|
||||
}))
|
||||
defer server.Close()
|
||||
payload := map[string]any{"url": server.URL, "headers": tc.headers, "use_recording_proxy": false}
|
||||
if route == "custom" {
|
||||
raw, _ := json.Marshal(payload)
|
||||
result, err := (&Server{}).runHTTPTool(context.Background(), raw, nil, nil)
|
||||
if err != nil || result.IsError {
|
||||
t.Fatalf("custom tool: %v %+v", err, result)
|
||||
}
|
||||
} else {
|
||||
status, result, body := repeaterTestSend(t, &Server{}, payload)
|
||||
if status != http.StatusOK || result.Status != http.StatusOK {
|
||||
t.Fatalf("repeater: %d %s", status, body)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,8 @@ import (
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/Autumn-27/artex/internal/httpidentity"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -141,6 +143,7 @@ func (s *Server) sendRepeater(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, http.StatusBadRequest, "构造请求失败")
|
||||
return
|
||||
}
|
||||
httpidentity.Apply(req)
|
||||
for key, value := range in.Headers {
|
||||
name := strings.TrimSpace(key)
|
||||
if name == "" {
|
||||
|
||||
Reference in new issue
Block a user