refactor: remove net.URL and net.CIDR types, improved unmarshal handling

This commit is contained in:
yusing
2025-04-13 07:06:21 +08:00
parent 1eac48e899
commit fce96ff3be
37 changed files with 236 additions and 292 deletions

View File

@@ -2,6 +2,7 @@ package rules
import (
"net/http"
"net/url"
"path"
"strconv"
"strings"
@@ -9,7 +10,6 @@ import (
"github.com/yusing/go-proxy/internal/gperr"
gphttp "github.com/yusing/go-proxy/internal/net/gphttp"
"github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy"
"github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/utils/strutils"
)
@@ -95,7 +95,7 @@ var commands = map[string]struct {
},
validate: validateURL,
build: func(args any) CommandHandler {
target := args.(*types.URL).String()
target := args.(*url.URL).String()
return ReturningCommand(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, target, http.StatusTemporaryRedirect)
})
@@ -160,7 +160,7 @@ var commands = map[string]struct {
},
validate: validateAbsoluteURL,
build: func(args any) CommandHandler {
target := args.(*types.URL)
target := args.(*url.URL)
if target.Scheme == "" {
target.Scheme = "http"
}

View File

@@ -1,10 +1,10 @@
package rules
import (
"net"
"net/http"
"github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/utils/strutils"
)
@@ -205,7 +205,7 @@ var checkers = map[string]struct {
},
validate: validateCIDR,
builder: func(args any) CheckFunc {
cidr := args.(types.CIDR)
cidr := args.(*net.IPNet)
return func(cached Cache, r *http.Request) bool {
ip := cached.GetRemoteIP(r)
if ip == nil {

View File

@@ -2,13 +2,14 @@ package rules
import (
"fmt"
"net"
"net/url"
"os"
"path"
"strings"
"github.com/yusing/go-proxy/internal/gperr"
gphttp "github.com/yusing/go-proxy/internal/net/gphttp"
"github.com/yusing/go-proxy/internal/net/types"
)
type (
@@ -48,24 +49,24 @@ func toKVOptionalV(args []string) (any, gperr.Error) {
}
}
// validateURL returns types.URL with the URL validated.
// validateURL returns url.URL with the URL validated.
func validateURL(args []string) (any, gperr.Error) {
if len(args) != 1 {
return nil, ErrExpectOneArg
}
u, err := types.ParseURL(args[0])
u, err := url.Parse(args[0])
if err != nil {
return nil, ErrInvalidArguments.With(err)
}
return u, nil
}
// validateAbsoluteURL returns types.URL with the URL validated.
// validateAbsoluteURL returns url.URL with the URL validated.
func validateAbsoluteURL(args []string) (any, gperr.Error) {
if len(args) != 1 {
return nil, ErrExpectOneArg
}
u, err := types.ParseURL(args[0])
u, err := url.Parse(args[0])
if err != nil {
return nil, ErrInvalidArguments.With(err)
}
@@ -86,7 +87,7 @@ func validateCIDR(args []string) (any, gperr.Error) {
if !strings.Contains(args[0], "/") {
args[0] += "/32"
}
cidr, err := types.ParseCIDR(args[0])
_, cidr, err := net.ParseCIDR(args[0])
if err != nil {
return nil, ErrInvalidArguments.With(err)
}