HTTP 探测改为填写网址,缺本机引擎时立刻失败
HTTP 模式不再单独填端口。完整网址沿用其中的协议、端口和路径;只写主机名或 IP 时按 http 和 80 端口。超时默认改为 5000 毫秒。本机找不到或启动不了 netquality-reflector 时直接说明原因,并建议改用远程节点,不再记成一次全部失败。 Co-authored-by: yanjiu0721 <yanjiu0721@users.noreply.github.com>
This commit is contained in:
1 parent
846e40c023
commit
918ec8f190
20 files changed
+807
-131
No files matched your search
+20
-10
@@ -173,9 +173,9 @@ func probeHTTP(ctx context.Context, in HTTPInput) Outcome {
|
||||
}
|
||||
|
||||
type httpHop struct {
|
||||
dns time.Duration
|
||||
connect time.Duration
|
||||
tls time.Duration
|
||||
dns time.Duration
|
||||
connect time.Duration
|
||||
tls time.Duration
|
||||
ttfb time.Duration
|
||||
addr net.IP
|
||||
version string
|
||||
@@ -460,10 +460,7 @@ func buildHTTPURL(target string, port int) (*url.URL, error) {
|
||||
}
|
||||
raw := target
|
||||
if !strings.Contains(raw, "://") {
|
||||
scheme := "https"
|
||||
if port == 80 {
|
||||
scheme = "http"
|
||||
}
|
||||
// A bare host or IP is HTTP on port 80 unless the text itself has a port.
|
||||
hostpart := raw
|
||||
path := ""
|
||||
if i := strings.Index(raw, "/"); i >= 0 {
|
||||
@@ -472,17 +469,30 @@ func buildHTTPURL(target string, port int) (*url.URL, error) {
|
||||
if strings.Count(hostpart, ":") >= 2 && !strings.HasPrefix(hostpart, "[") {
|
||||
hostpart = "[" + hostpart + "]"
|
||||
}
|
||||
raw = scheme + "://" + hostpart + path
|
||||
raw = "http://" + hostpart + path
|
||||
}
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil || u.Hostname() == "" || (u.Scheme != "http" && u.Scheme != "https") {
|
||||
return nil, errors.New("HTTP 目标需要是主机名或 http/https 网址")
|
||||
}
|
||||
if u.Port() == "" {
|
||||
if port < 1 || port > 65535 {
|
||||
use := port
|
||||
if use == 0 {
|
||||
if u.Scheme == "https" {
|
||||
use = 443
|
||||
} else {
|
||||
use = 80
|
||||
}
|
||||
}
|
||||
if use < 1 || use > 65535 {
|
||||
return nil, errors.New("端口需要在 1 到 65535 之间")
|
||||
}
|
||||
u.Host = net.JoinHostPort(u.Hostname(), strconv.Itoa(use))
|
||||
} else {
|
||||
parsed, err := strconv.Atoi(u.Port())
|
||||
if err != nil || parsed < 1 || parsed > 65535 {
|
||||
return nil, errors.New("端口需要在 1 到 65535 之间")
|
||||
}
|
||||
u.Host = net.JoinHostPort(u.Hostname(), strconv.Itoa(port))
|
||||
}
|
||||
if u.Path == "" {
|
||||
u.Path = "/"
|
||||
|
||||
@@ -332,6 +332,112 @@ func TestHelperSpeaksHTTPEngine(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildHTTPURLDefaults(t *testing.T) {
|
||||
cases := []struct {
|
||||
target string
|
||||
wire int
|
||||
scheme string
|
||||
host string
|
||||
port string
|
||||
path string
|
||||
query string
|
||||
}{
|
||||
{"example.com", 0, "http", "example.com", "80", "/", ""},
|
||||
{"example.com", 80, "http", "example.com", "80", "/", ""},
|
||||
{"example.com", 443, "http", "example.com", "443", "/", ""},
|
||||
{"rxsend.cn", 0, "http", "rxsend.cn", "80", "/", ""},
|
||||
{"https://www.cause-by-siky.top:2025/", 80, "https", "www.cause-by-siky.top", "2025", "/", ""},
|
||||
{"https://www.cause-by-siky.top:2025/a?q=1", 9, "https", "www.cause-by-siky.top", "2025", "/a", "q=1"},
|
||||
{"http://example.com/path", 0, "http", "example.com", "80", "/path", ""},
|
||||
{"https://example.com/path", 0, "https", "example.com", "443", "/path", ""},
|
||||
{"example.com:8080/a", 443, "http", "example.com", "8080", "/a", ""},
|
||||
{"example.com/path?q=1", 0, "http", "example.com", "80", "/path", "q=1"},
|
||||
{"[2001:db8::1]", 0, "http", "2001:db8::1", "80", "/", ""},
|
||||
{"[2001:db8::1]:8080/a", 80, "http", "2001:db8::1", "8080", "/a", ""},
|
||||
{"2001:db8::1", 0, "http", "2001:db8::1", "80", "/", ""},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
u, err := buildHTTPURL(tc.target, tc.wire)
|
||||
if err != nil {
|
||||
t.Fatalf("%s port %d: %v", tc.target, tc.wire, err)
|
||||
}
|
||||
if u.Scheme != tc.scheme || u.Hostname() != tc.host || u.Port() != tc.port || u.Path != tc.path || u.RawQuery != tc.query {
|
||||
t.Fatalf("%s port %d -> %s", tc.target, tc.wire, u.String())
|
||||
}
|
||||
}
|
||||
if _, err := buildHTTPURL("", 80); err == nil || !strings.Contains(err.Error(), "目标地址") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := buildHTTPURL("ftp://example.com", 80); err == nil || !strings.Contains(err.Error(), "http/https") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := buildHTTPURL("https://example.com:99999/", 80); err == nil || !strings.Contains(err.Error(), "端口") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := buildHTTPURL("example.com", 70000); err == nil || !strings.Contains(err.Error(), "端口") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPExplicitURLPortIgnoresField(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/health" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}))
|
||||
defer srv.Close()
|
||||
out := probeHTTP(context.Background(), HTTPInput{
|
||||
Target: srv.URL + "/health", Port: 1, Version: "1.1",
|
||||
Allow4: true, Allow6: true, Timeout: 3 * time.Second,
|
||||
})
|
||||
if out.Status != statusOK || out.Detail["statusCode"] != "204" {
|
||||
t.Fatalf("url port should win: %s %s %+v", out.Status, out.Note, out.Detail)
|
||||
}
|
||||
omitted := probeHTTP(context.Background(), HTTPInput{
|
||||
Target: srv.URL + "/health", Port: 0, Version: "1.1",
|
||||
Allow4: true, Allow6: true, Timeout: 3 * time.Second,
|
||||
})
|
||||
if omitted.Status != statusOK || omitted.Detail["statusCode"] != "204" {
|
||||
t.Fatalf("omitted port: %s %s %+v", omitted.Status, omitted.Note, omitted.Detail)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPJobAcceptsURLWithoutPortField(t *testing.T) {
|
||||
page := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
}))
|
||||
defer page.Close()
|
||||
node := newNode("", true, nil, testLimits())
|
||||
ts := httptest.NewServer(node.Handler())
|
||||
defer ts.Close()
|
||||
created := postJSON(t, ts.URL+"/v1/jobs", `{"target":"`+page.URL+`/","port":0,"profile":"http","intervalMs":200,"timeoutMs":1500,"durationSec":2,"http":{"version":"1.1","method":"GET"}}`)
|
||||
defer created.Body.Close()
|
||||
if created.StatusCode != http.StatusCreated {
|
||||
raw, _ := io.ReadAll(created.Body)
|
||||
t.Fatalf("http without port field %d %s", created.StatusCode, raw)
|
||||
}
|
||||
var job map[string]any
|
||||
if err := json.NewDecoder(created.Body).Decode(&job); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
id, _ := job["jobId"].(string)
|
||||
sample := readSampleURL(t, ts.URL+"/v1/jobs/"+id+"/stream")
|
||||
if sample["status"] != statusOK {
|
||||
t.Fatalf("sample %+v", sample)
|
||||
}
|
||||
detail, _ := sample["detail"].(map[string]any)
|
||||
if detail["statusCode"] != "202" {
|
||||
t.Fatalf("detail %+v", detail)
|
||||
}
|
||||
tcp := postJSON(t, ts.URL+"/v1/jobs", `{"target":"127.0.0.1","port":0,"profile":"tcp-connect","intervalMs":200,"timeoutMs":300,"durationSec":1}`)
|
||||
defer tcp.Body.Close()
|
||||
if tcp.StatusCode != http.StatusBadRequest {
|
||||
t.Fatalf("tcp still requires a port, got %d", tcp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func tlsInput(target string, port int, version string, pool *x509.CertPool) HTTPInput {
|
||||
return HTTPInput{
|
||||
Target: target, Port: port, Version: version, Resolve: "127.0.0.1",
|
||||
|
||||
+16
-9
@@ -11,6 +11,7 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -155,13 +156,13 @@ func (n *Node) auth(next http.HandlerFunc) http.HandlerFunc {
|
||||
}
|
||||
|
||||
type createReq struct {
|
||||
Target string `json:"target"`
|
||||
Port int `json:"port"`
|
||||
Profile string `json:"profile"`
|
||||
IntervalMs int `json:"intervalMs"`
|
||||
TimeoutMs int `json:"timeoutMs"`
|
||||
DurationSec int `json:"durationSec"`
|
||||
PacketSize int `json:"packetSize"`
|
||||
Target string `json:"target"`
|
||||
Port int `json:"port"`
|
||||
Profile string `json:"profile"`
|
||||
IntervalMs int `json:"intervalMs"`
|
||||
TimeoutMs int `json:"timeoutMs"`
|
||||
DurationSec int `json:"durationSec"`
|
||||
PacketSize int `json:"packetSize"`
|
||||
EchoToken string `json:"echoToken"`
|
||||
DNSName string `json:"dnsName"`
|
||||
PayloadHex string `json:"payloadHex"`
|
||||
@@ -224,7 +225,7 @@ func (n *Node) prepare(ctx context.Context, req createReq) (jobSpec, error) {
|
||||
}
|
||||
allow4 := flagOrDefault(req.AllowIPv4, true)
|
||||
allow6 := flagOrDefault(req.AllowIPv6, true)
|
||||
if req.Port < 1 || req.Port > 65535 {
|
||||
if req.Profile != "http" && (req.Port < 1 || req.Port > 65535) {
|
||||
return jobSpec{}, &apiError{http.StatusBadRequest, "bad_port", "端口需要在 1 到 65535 之间"}
|
||||
}
|
||||
interval := time.Duration(req.IntervalMs) * time.Millisecond
|
||||
@@ -279,11 +280,17 @@ func (n *Node) prepare(ctx context.Context, req createReq) (jobSpec, error) {
|
||||
}
|
||||
applyHTTPOptions(&httpIn, req.HTTP)
|
||||
lookupHost := req.Target
|
||||
resolvedPort := req.Port
|
||||
if req.Profile == "http" {
|
||||
page, err := buildHTTPURL(req.Target, req.Port)
|
||||
if err != nil {
|
||||
return jobSpec{}, &apiError{http.StatusBadRequest, "bad_target", err.Error()}
|
||||
}
|
||||
if p, convErr := strconv.Atoi(page.Port()); convErr == nil {
|
||||
resolvedPort = p
|
||||
httpIn.Port = p
|
||||
}
|
||||
httpIn.Target = page.String()
|
||||
if _, err := normalizeHTTPVersion(httpIn.Version); err != nil {
|
||||
return jobSpec{}, &apiError{http.StatusBadRequest, "bad_profile", err.Error()}
|
||||
}
|
||||
@@ -315,7 +322,7 @@ func (n *Node) prepare(ctx context.Context, req createReq) (jobSpec, error) {
|
||||
return jobSpec{}, &apiError{http.StatusBadRequest, "bad_family", "地址族不被允许"}
|
||||
}
|
||||
return jobSpec{
|
||||
Host: req.Target, Port: req.Port, Profile: req.Profile, IP: ips[0],
|
||||
Host: req.Target, Port: resolvedPort, Profile: req.Profile, IP: ips[0],
|
||||
state: &dialState{picker: newEndpointPicker(ips)},
|
||||
Interval: interval, Timeout: timeout, Duration: duration, PacketSize: size,
|
||||
Echo: echo, DNSName: req.DNSName, Payload: payload, Expect: expect,
|
||||
|
||||
Reference in new issue
Block a user