fix check health for tcp/udp, refactor

This commit is contained in:
yusing
2025-02-13 05:58:15 +08:00
parent 1a93df5886
commit fd50f8fcab
3 changed files with 14 additions and 5 deletions

View File

@@ -1,9 +1,11 @@
package handler
import (
"fmt"
"net/http"
"net/url"
"os"
"strings"
apiUtils "github.com/yusing/go-proxy/internal/api/v1/utils"
"github.com/yusing/go-proxy/internal/net/types"
@@ -51,6 +53,14 @@ func CheckHealth(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
hasPort := strings.Contains(host, ":")
port := query.Get("port")
if port != "" && !hasPort {
host = fmt.Sprintf("%s:%s", host, port)
} else {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
result, err = monitor.NewRawHealthChecker(types.NewURL(&url.URL{
Scheme: scheme,
Host: host,

View File

@@ -198,7 +198,7 @@ func TestCheckHealthTCPUDP(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
query := url.Values{}
query.Set("scheme", "tcp")
query.Set("scheme", tt.scheme)
query.Set("host", tt.host)
query.Set("port", strconv.Itoa(tt.port))