update
This commit is contained in:
@@ -4,11 +4,13 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/jmoiron/sqlx"
|
||||
@@ -105,6 +107,7 @@ func registerHandlers(app *fiber.App, deps *Deps) {
|
||||
app.Post("/api/rule/parse", deps.HandleRuleParse)
|
||||
app.Post("/api/utils/proxy-uri", deps.HandleProxyURI)
|
||||
app.Post("/api/utils/node-info", deps.HandleNodeInfo)
|
||||
app.Post("/api/utils/egress-info", deps.HandleEgressInfo)
|
||||
|
||||
app.Get("/sources/:name/:token", deps.HandleDownloadSource)
|
||||
app.Get("/collections/:name/:token", deps.HandleDownloadCollection)
|
||||
@@ -2065,6 +2068,78 @@ func TestHandleNodeInfoServerWithBrackets(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildEgressProbeConfig(t *testing.T) {
|
||||
data, err := buildEgressProbeConfig(model.ProxyNode{
|
||||
"type": "ss",
|
||||
"name": "demo",
|
||||
"server": "127.0.0.1",
|
||||
"port": 8388,
|
||||
"cipher": "aes-256-gcm",
|
||||
"password": "pass",
|
||||
}, 19090)
|
||||
if err != nil {
|
||||
t.Fatalf("buildEgressProbeConfig returned error: %v", err)
|
||||
}
|
||||
var doc map[string]any
|
||||
if err := json.Unmarshal(data, &doc); err != nil {
|
||||
t.Fatalf("invalid config JSON: %v", err)
|
||||
}
|
||||
route := doc["route"].(map[string]any)
|
||||
if route["final"] != "PROXY" {
|
||||
t.Fatalf("route.final = %v, want PROXY", route["final"])
|
||||
}
|
||||
inbound := doc["inbounds"].([]any)[0].(map[string]any)
|
||||
if inbound["listen"] != "127.0.0.1" || inbound["listen_port"].(float64) != 19090 {
|
||||
t.Fatalf("unexpected inbound: %v", inbound)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleEgressInfoUnsupported(t *testing.T) {
|
||||
deps := newTestDeps(t)
|
||||
app := newApp(deps)
|
||||
code, _ := doRequest(t, app, "POST", "/api/utils/egress-info", `{"type":"unknown","server":"1.2.3.4","port":443}`, nil)
|
||||
assertStatus(t, "EgressInfo unsupported", code, 400)
|
||||
}
|
||||
|
||||
func TestProbeServerPortLatency(t *testing.T) {
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer ln.Close()
|
||||
go func() {
|
||||
conn, err := ln.Accept()
|
||||
if err == nil {
|
||||
conn.Close()
|
||||
}
|
||||
}()
|
||||
latency, msg := probeServerPortLatency(model.ProxyNode{
|
||||
"server": "127.0.0.1",
|
||||
"port": ln.Addr().(*net.TCPAddr).Port,
|
||||
}, time.Second)
|
||||
if msg != "" {
|
||||
t.Fatalf("unexpected latency error: %s", msg)
|
||||
}
|
||||
if latency < 0 {
|
||||
t.Fatalf("latency = %d, want >= 0", latency)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddCachedEgressInfo(t *testing.T) {
|
||||
deps := newTestDeps(t)
|
||||
node := model.ProxyNode{
|
||||
"type": "ss",
|
||||
"name": "n",
|
||||
"server": "127.0.0.1",
|
||||
"port": 8388,
|
||||
}
|
||||
deps.CacheRepo.SafePut(egressCacheKey(node), `{"egressIp":"1.1.1.1","country":"Japan","region":"Tokyo","latencyMs":12}`, nil, 300)
|
||||
nodes := deps.addCachedEgressInfo([]model.ProxyNode{node})
|
||||
if nodes[0]["egressIp"] != "1.1.1.1" || nodes[0]["latencyMs"].(float64) != 12 {
|
||||
t.Fatalf("cached egress not merged: %v", nodes[0])
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// RegisterRoutes integration (uses real middleware)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user