refactor: rename net/types to nettypes

This commit is contained in:
yusing 2025-06-08 17:59:48 +08:00
parent 01773976d1
commit 421aaecba4
26 changed files with 79 additions and 97 deletions

View file

@ -10,7 +10,7 @@ import (
"github.com/yusing/go-proxy/internal/net/gphttp" "github.com/yusing/go-proxy/internal/net/gphttp"
"github.com/yusing/go-proxy/internal/net/gphttp/httpheaders" "github.com/yusing/go-proxy/internal/net/gphttp/httpheaders"
"github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy" "github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy"
"github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
) )
func SystemInfo(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) { func SystemInfo(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
@ -41,7 +41,7 @@ func SystemInfo(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Reques
} }
gphttp.WriteBody(w, respData) gphttp.WriteBody(w, respData)
} else { } else {
rp := reverseproxy.NewReverseProxy("agent", types.NewURL(agentPkg.AgentURL), agent.Transport()) rp := reverseproxy.NewReverseProxy("agent", nettypes.NewURL(agentPkg.AgentURL), agent.Transport())
header := r.Header.Clone() header := r.Header.Clone()
r, err := http.NewRequestWithContext(r.Context(), r.Method, agentPkg.EndpointSystemInfo+"?"+query.Encode(), nil) r, err := http.NewRequestWithContext(r.Context(), r.Method, agentPkg.EndpointSystemInfo+"?"+query.Encode(), nil)
if err != nil { if err != nil {

View file

@ -3,7 +3,7 @@ package homepage
import ( import (
"net/http" "net/http"
gpnet "github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/utils/pool" "github.com/yusing/go-proxy/internal/utils/pool"
) )
@ -11,7 +11,7 @@ type route interface {
pool.Object pool.Object
ProviderName() string ProviderName() string
References() []string References() []string
TargetURL() *gpnet.URL TargetURL() *nettypes.URL
} }
type httpRoute interface { type httpRoute interface {

View file

@ -3,13 +3,13 @@ package idlewatcher
import ( import (
"net/http" "net/http"
net "github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/watcher/health" "github.com/yusing/go-proxy/internal/watcher/health"
) )
type Waker interface { type Waker interface {
health.HealthMonitor health.HealthMonitor
http.Handler http.Handler
net.Stream nettypes.Stream
Wake() error Wake() error
} }

View file

@ -14,7 +14,7 @@ import (
"github.com/yusing/go-proxy/internal/idlewatcher/provider" "github.com/yusing/go-proxy/internal/idlewatcher/provider"
idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types" idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types"
"github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy" "github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy"
net "github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/route/routes" "github.com/yusing/go-proxy/internal/route/routes"
"github.com/yusing/go-proxy/internal/task" "github.com/yusing/go-proxy/internal/task"
U "github.com/yusing/go-proxy/internal/utils" U "github.com/yusing/go-proxy/internal/utils"
@ -30,7 +30,7 @@ type (
routeHelper struct { routeHelper struct {
route routes.Route route routes.Route
rp *reverseproxy.ReverseProxy rp *reverseproxy.ReverseProxy
stream net.Stream stream nettypes.Stream
hc health.HealthChecker hc health.HealthChecker
} }

View file

@ -6,7 +6,7 @@ import (
"strings" "strings"
"github.com/yusing/go-proxy/internal/gperr" "github.com/yusing/go-proxy/internal/gperr"
gpnet "github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/utils/strutils" "github.com/yusing/go-proxy/internal/utils/strutils"
) )
@ -25,7 +25,7 @@ type (
} }
Host string Host string
CIDR struct { CIDR struct {
gpnet.CIDR nettypes.CIDR
} }
) )

View file

@ -6,7 +6,7 @@ import (
"testing" "testing"
. "github.com/yusing/go-proxy/internal/logging/accesslog" . "github.com/yusing/go-proxy/internal/logging/accesslog"
gpnet "github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/utils/strutils" "github.com/yusing/go-proxy/internal/utils/strutils"
expect "github.com/yusing/go-proxy/internal/utils/testing" expect "github.com/yusing/go-proxy/internal/utils/testing"
) )
@ -157,7 +157,7 @@ func TestHeaderFilter(t *testing.T) {
} }
func TestCIDRFilter(t *testing.T) { func TestCIDRFilter(t *testing.T) {
cidr := []*CIDR{{gpnet.CIDR{ cidr := []*CIDR{{nettypes.CIDR{
IP: net.ParseIP("192.168.10.0"), IP: net.ParseIP("192.168.10.0"),
Mask: net.CIDRMask(24, 32), Mask: net.CIDRMask(24, 32),
}}} }}}

View file

@ -4,7 +4,7 @@ import (
"net/http" "net/http"
idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types" idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types"
net "github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
U "github.com/yusing/go-proxy/internal/utils" U "github.com/yusing/go-proxy/internal/utils"
"github.com/yusing/go-proxy/internal/watcher/health" "github.com/yusing/go-proxy/internal/watcher/health"
) )
@ -14,7 +14,7 @@ type (
_ U.NoCopy _ U.NoCopy
name string name string
url *net.URL url *nettypes.URL
weight Weight weight Weight
http.Handler `json:"-"` http.Handler `json:"-"`
@ -26,14 +26,14 @@ type (
health.HealthMonitor health.HealthMonitor
Name() string Name() string
Key() string Key() string
URL() *net.URL URL() *nettypes.URL
Weight() Weight Weight() Weight
SetWeight(weight Weight) SetWeight(weight Weight)
TryWake() error TryWake() error
} }
) )
func NewServer(name string, url *net.URL, weight Weight, handler http.Handler, healthMon health.HealthMonitor) Server { func NewServer(name string, url *nettypes.URL, weight Weight, handler http.Handler, healthMon health.HealthMonitor) Server {
srv := &server{ srv := &server{
name: name, name: name,
url: url, url: url,
@ -47,7 +47,7 @@ func NewServer(name string, url *net.URL, weight Weight, handler http.Handler, h
func TestNewServer[T ~int | ~float32 | ~float64](weight T) Server { func TestNewServer[T ~int | ~float32 | ~float64](weight T) Server {
srv := &server{ srv := &server{
weight: Weight(weight), weight: Weight(weight),
url: net.MustParseURL("http://localhost"), url: nettypes.MustParseURL("http://localhost"),
} }
return srv return srv
} }
@ -56,7 +56,7 @@ func (srv *server) Name() string {
return srv.name return srv.name
} }
func (srv *server) URL() *net.URL { func (srv *server) URL() *nettypes.URL {
return srv.url return srv.url
} }

View file

@ -10,7 +10,7 @@ import (
"github.com/yusing/go-proxy/internal/entrypoint" "github.com/yusing/go-proxy/internal/entrypoint"
. "github.com/yusing/go-proxy/internal/net/gphttp/middleware" . "github.com/yusing/go-proxy/internal/net/gphttp/middleware"
"github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy" "github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy"
"github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/route" "github.com/yusing/go-proxy/internal/route"
routeTypes "github.com/yusing/go-proxy/internal/route/types" routeTypes "github.com/yusing/go-proxy/internal/route/types"
"github.com/yusing/go-proxy/internal/task" "github.com/yusing/go-proxy/internal/task"
@ -99,7 +99,7 @@ func (f fakeRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
} }
func TestReverseProxyBypass(t *testing.T) { func TestReverseProxyBypass(t *testing.T) {
rp := reverseproxy.NewReverseProxy("test", types.MustParseURL("http://example.com"), fakeRoundTripper{}) rp := reverseproxy.NewReverseProxy("test", nettypes.MustParseURL("http://example.com"), fakeRoundTripper{})
err := PatchReverseProxy(rp, map[string]OptionsRaw{ err := PatchReverseProxy(rp, map[string]OptionsRaw{
"response": { "response": {
"bypass": "path /test/* | path /api", "bypass": "path /test/* | path /api",

View file

@ -6,7 +6,7 @@ import (
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
gphttp "github.com/yusing/go-proxy/internal/net/gphttp" gphttp "github.com/yusing/go-proxy/internal/net/gphttp"
"github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/serialization" "github.com/yusing/go-proxy/internal/serialization"
F "github.com/yusing/go-proxy/internal/utils/functional" F "github.com/yusing/go-proxy/internal/utils/functional"
) )
@ -17,8 +17,8 @@ type (
cachedAddr F.Map[string, bool] // cache for trusted IPs cachedAddr F.Map[string, bool] // cache for trusted IPs
} }
CIDRWhitelistOpts struct { CIDRWhitelistOpts struct {
Allow []*types.CIDR `validate:"min=1"` Allow []*nettypes.CIDR `validate:"min=1"`
StatusCode int `json:"status_code" aliases:"status" validate:"omitempty,status_code"` StatusCode int `json:"status_code" aliases:"status" validate:"omitempty,status_code"`
Message string Message string
} }
) )
@ -26,7 +26,7 @@ type (
var ( var (
CIDRWhiteList = NewMiddleware[cidrWhitelist]() CIDRWhiteList = NewMiddleware[cidrWhitelist]()
cidrWhitelistDefaults = CIDRWhitelistOpts{ cidrWhitelistDefaults = CIDRWhitelistOpts{
Allow: []*types.CIDR{}, Allow: []*nettypes.CIDR{},
StatusCode: http.StatusForbidden, StatusCode: http.StatusForbidden,
Message: "IP not allowed", Message: "IP not allowed",
} }

View file

@ -12,7 +12,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/yusing/go-proxy/internal/common" "github.com/yusing/go-proxy/internal/common"
"github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/utils/atomic" "github.com/yusing/go-proxy/internal/utils/atomic"
"github.com/yusing/go-proxy/internal/utils/strutils" "github.com/yusing/go-proxy/internal/utils/strutils"
) )
@ -33,7 +33,7 @@ var (
cfCIDRsMu sync.Mutex cfCIDRsMu sync.Mutex
// RFC 1918. // RFC 1918.
localCIDRs = []*types.CIDR{ localCIDRs = []*nettypes.CIDR{
{IP: net.IPv4(127, 0, 0, 1), Mask: net.IPv4Mask(255, 255, 255, 255)}, // 127.0.0.1/32 {IP: net.IPv4(127, 0, 0, 1), Mask: net.IPv4Mask(255, 255, 255, 255)}, // 127.0.0.1/32
{IP: net.IPv4(10, 0, 0, 0), Mask: net.IPv4Mask(255, 0, 0, 0)}, // 10.0.0.0/8 {IP: net.IPv4(10, 0, 0, 0), Mask: net.IPv4Mask(255, 0, 0, 0)}, // 10.0.0.0/8
{IP: net.IPv4(172, 16, 0, 0), Mask: net.IPv4Mask(255, 240, 0, 0)}, // 172.16.0.0/12 {IP: net.IPv4(172, 16, 0, 0), Mask: net.IPv4Mask(255, 240, 0, 0)}, // 172.16.0.0/12
@ -60,7 +60,7 @@ func (cri *cloudflareRealIP) before(w http.ResponseWriter, r *http.Request) bool
return cri.realIP.before(w, r) return cri.realIP.before(w, r)
} }
func tryFetchCFCIDR() (cfCIDRs []*types.CIDR) { func tryFetchCFCIDR() (cfCIDRs []*nettypes.CIDR) {
if time.Since(cfCIDRsLastUpdate.Load()) < cfCIDRsUpdateInterval { if time.Since(cfCIDRsLastUpdate.Load()) < cfCIDRsUpdateInterval {
return return
} }
@ -75,7 +75,7 @@ func tryFetchCFCIDR() (cfCIDRs []*types.CIDR) {
if common.IsTest { if common.IsTest {
cfCIDRs = localCIDRs cfCIDRs = localCIDRs
} else { } else {
cfCIDRs = make([]*types.CIDR, 0, 30) cfCIDRs = make([]*nettypes.CIDR, 0, 30)
err := errors.Join( err := errors.Join(
fetchUpdateCFIPRange(cfIPv4CIDRsEndpoint, &cfCIDRs), fetchUpdateCFIPRange(cfIPv4CIDRsEndpoint, &cfCIDRs),
fetchUpdateCFIPRange(cfIPv6CIDRsEndpoint, &cfCIDRs), fetchUpdateCFIPRange(cfIPv6CIDRsEndpoint, &cfCIDRs),
@ -95,7 +95,7 @@ func tryFetchCFCIDR() (cfCIDRs []*types.CIDR) {
return return
} }
func fetchUpdateCFIPRange(endpoint string, cfCIDRs *[]*types.CIDR) error { func fetchUpdateCFIPRange(endpoint string, cfCIDRs *[]*nettypes.CIDR) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() defer cancel()
@ -124,7 +124,7 @@ func fetchUpdateCFIPRange(endpoint string, cfCIDRs *[]*types.CIDR) error {
return fmt.Errorf("cloudflare responeded an invalid CIDR: %s", line) return fmt.Errorf("cloudflare responeded an invalid CIDR: %s", line)
} }
*cfCIDRs = append(*cfCIDRs, (*types.CIDR)(cidr)) *cfCIDRs = append(*cfCIDRs, (*nettypes.CIDR)(cidr))
} }
*cfCIDRs = append(*cfCIDRs, localCIDRs...) *cfCIDRs = append(*cfCIDRs, localCIDRs...)
return nil return nil

View file

@ -7,7 +7,7 @@ import (
"slices" "slices"
"testing" "testing"
"github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
. "github.com/yusing/go-proxy/internal/utils/testing" . "github.com/yusing/go-proxy/internal/utils/testing"
) )
@ -51,8 +51,8 @@ func TestModifyRequest(t *testing.T) {
}) })
t.Run("request_headers", func(t *testing.T) { t.Run("request_headers", func(t *testing.T) {
reqURL := types.MustParseURL("https://my.app/?arg_1=b") reqURL := nettypes.MustParseURL("https://my.app/?arg_1=b")
upstreamURL := types.MustParseURL("http://test.example.com") upstreamURL := nettypes.MustParseURL("http://test.example.com")
result, err := newMiddlewareTest(ModifyRequest, &testArgs{ result, err := newMiddlewareTest(ModifyRequest, &testArgs{
middlewareOpt: opts, middlewareOpt: opts,
reqURL: reqURL, reqURL: reqURL,
@ -128,8 +128,8 @@ func TestModifyRequest(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
reqURL := types.MustParseURL("https://my.app" + tt.path) reqURL := nettypes.MustParseURL("https://my.app" + tt.path)
upstreamURL := types.MustParseURL(tt.upstreamURL) upstreamURL := nettypes.MustParseURL(tt.upstreamURL)
opts["add_prefix"] = tt.addPrefix opts["add_prefix"] = tt.addPrefix
result, err := newMiddlewareTest(ModifyRequest, &testArgs{ result, err := newMiddlewareTest(ModifyRequest, &testArgs{

View file

@ -7,7 +7,7 @@ import (
"slices" "slices"
"testing" "testing"
"github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
. "github.com/yusing/go-proxy/internal/utils/testing" . "github.com/yusing/go-proxy/internal/utils/testing"
) )
@ -54,8 +54,8 @@ func TestModifyResponse(t *testing.T) {
}) })
t.Run("response_headers", func(t *testing.T) { t.Run("response_headers", func(t *testing.T) {
reqURL := types.MustParseURL("https://my.app/?arg_1=b") reqURL := nettypes.MustParseURL("https://my.app/?arg_1=b")
upstreamURL := types.MustParseURL("http://test.example.com") upstreamURL := nettypes.MustParseURL("http://test.example.com")
result, err := newMiddlewareTest(ModifyResponse, &testArgs{ result, err := newMiddlewareTest(ModifyResponse, &testArgs{
middlewareOpt: opts, middlewareOpt: opts,
reqURL: reqURL, reqURL: reqURL,

View file

@ -5,7 +5,7 @@ import (
"net/http" "net/http"
"github.com/yusing/go-proxy/internal/net/gphttp/httpheaders" "github.com/yusing/go-proxy/internal/net/gphttp/httpheaders"
"github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
) )
// https://nginx.org/en/docs/http/ngx_http_realip_module.html // https://nginx.org/en/docs/http/ngx_http_realip_module.html
@ -16,7 +16,7 @@ type (
// Header is the name of the header to use for the real client IP // Header is the name of the header to use for the real client IP
Header string `validate:"required"` Header string `validate:"required"`
// From is a list of Address / CIDRs to trust // From is a list of Address / CIDRs to trust
From []*types.CIDR `validate:"required,min=1"` From []*nettypes.CIDR `validate:"required,min=1"`
/* /*
If recursive search is disabled, If recursive search is disabled,
the original client address that matches one of the trusted addresses is replaced by the original client address that matches one of the trusted addresses is replaced by
@ -33,7 +33,7 @@ var (
RealIP = NewMiddleware[realIP]() RealIP = NewMiddleware[realIP]()
realIPOptsDefault = RealIPOpts{ realIPOptsDefault = RealIPOpts{
Header: "X-Real-IP", Header: "X-Real-IP",
From: []*types.CIDR{}, From: []*nettypes.CIDR{},
} }
) )

View file

@ -7,7 +7,7 @@ import (
"testing" "testing"
"github.com/yusing/go-proxy/internal/net/gphttp/httpheaders" "github.com/yusing/go-proxy/internal/net/gphttp/httpheaders"
"github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
. "github.com/yusing/go-proxy/internal/utils/testing" . "github.com/yusing/go-proxy/internal/utils/testing"
) )
@ -23,7 +23,7 @@ func TestSetRealIPOpts(t *testing.T) {
} }
optExpected := &RealIPOpts{ optExpected := &RealIPOpts{
Header: httpheaders.HeaderXRealIP, Header: httpheaders.HeaderXRealIP,
From: []*types.CIDR{ From: []*nettypes.CIDR{
{ {
IP: net.ParseIP("127.0.0.0"), IP: net.ParseIP("127.0.0.0"),
Mask: net.IPv4Mask(255, 0, 0, 0), Mask: net.IPv4Mask(255, 0, 0, 0),

View file

@ -4,13 +4,13 @@ import (
"net/http" "net/http"
"testing" "testing"
"github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
. "github.com/yusing/go-proxy/internal/utils/testing" . "github.com/yusing/go-proxy/internal/utils/testing"
) )
func TestRedirectToHTTPs(t *testing.T) { func TestRedirectToHTTPs(t *testing.T) {
result, err := newMiddlewareTest(RedirectHTTP, &testArgs{ result, err := newMiddlewareTest(RedirectHTTP, &testArgs{
reqURL: types.MustParseURL("http://example.com"), reqURL: nettypes.MustParseURL("http://example.com"),
}) })
ExpectNoError(t, err) ExpectNoError(t, err)
ExpectEqual(t, result.ResponseStatus, http.StatusPermanentRedirect) ExpectEqual(t, result.ResponseStatus, http.StatusPermanentRedirect)
@ -19,7 +19,7 @@ func TestRedirectToHTTPs(t *testing.T) {
func TestNoRedirect(t *testing.T) { func TestNoRedirect(t *testing.T) {
result, err := newMiddlewareTest(RedirectHTTP, &testArgs{ result, err := newMiddlewareTest(RedirectHTTP, &testArgs{
reqURL: types.MustParseURL("https://example.com"), reqURL: nettypes.MustParseURL("https://example.com"),
}) })
ExpectNoError(t, err) ExpectNoError(t, err)
ExpectEqual(t, result.ResponseStatus, http.StatusOK) ExpectEqual(t, result.ResponseStatus, http.StatusOK)

View file

@ -11,7 +11,7 @@ import (
"github.com/yusing/go-proxy/internal/common" "github.com/yusing/go-proxy/internal/common"
"github.com/yusing/go-proxy/internal/gperr" "github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy" "github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy"
"github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
. "github.com/yusing/go-proxy/internal/utils/testing" . "github.com/yusing/go-proxy/internal/utils/testing"
) )
@ -80,11 +80,11 @@ type TestResult struct {
type testArgs struct { type testArgs struct {
middlewareOpt OptionsRaw middlewareOpt OptionsRaw
upstreamURL *types.URL upstreamURL *nettypes.URL
realRoundTrip bool realRoundTrip bool
reqURL *types.URL reqURL *nettypes.URL
reqMethod string reqMethod string
headers http.Header headers http.Header
body []byte body []byte
@ -96,13 +96,13 @@ type testArgs struct {
func (args *testArgs) setDefaults() { func (args *testArgs) setDefaults() {
if args.reqURL == nil { if args.reqURL == nil {
args.reqURL = Must(types.ParseURL("https://example.com")) args.reqURL = Must(nettypes.ParseURL("https://example.com"))
} }
if args.reqMethod == "" { if args.reqMethod == "" {
args.reqMethod = http.MethodGet args.reqMethod = http.MethodGet
} }
if args.upstreamURL == nil { if args.upstreamURL == nil {
args.upstreamURL = Must(types.ParseURL("https://10.0.0.1:8443")) // dummy url, no actual effect args.upstreamURL = Must(nettypes.ParseURL("https://10.0.0.1:8443")) // dummy url, no actual effect
} }
if args.respHeaders == nil { if args.respHeaders == nil {
args.respHeaders = http.Header{} args.respHeaders = http.Header{}

View file

@ -28,7 +28,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/yusing/go-proxy/internal/logging/accesslog" "github.com/yusing/go-proxy/internal/logging/accesslog"
"github.com/yusing/go-proxy/internal/net/gphttp/httpheaders" "github.com/yusing/go-proxy/internal/net/gphttp/httpheaders"
"github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
U "github.com/yusing/go-proxy/internal/utils" U "github.com/yusing/go-proxy/internal/utils"
"golang.org/x/net/http/httpguts" "golang.org/x/net/http/httpguts"
) )
@ -93,7 +93,7 @@ type ReverseProxy struct {
HandlerFunc http.HandlerFunc HandlerFunc http.HandlerFunc
TargetName string TargetName string
TargetURL *types.URL TargetURL *nettypes.URL
} }
func singleJoiningSlash(a, b string) string { func singleJoiningSlash(a, b string) string {
@ -133,7 +133,7 @@ func joinURLPath(a, b *url.URL) (path, rawpath string) {
// URLs to the scheme, host, and base path provided in target. If the // URLs to the scheme, host, and base path provided in target. If the
// target's path is "/base" and the incoming request was for "/dir", // target's path is "/base" and the incoming request was for "/dir",
// the target request will be for /base/dir. // the target request will be for /base/dir.
func NewReverseProxy(name string, target *types.URL, transport http.RoundTripper) *ReverseProxy { func NewReverseProxy(name string, target *nettypes.URL, transport http.RoundTripper) *ReverseProxy {
if transport == nil { if transport == nil {
panic("nil transport") panic("nil transport")
} }

View file

@ -1,4 +1,4 @@
package types package nettypes
import ( import (
"net" "net"

View file

@ -1,32 +1,14 @@
package types package nettypes
import ( import (
"fmt" "context"
"net" "net"
) )
type ( type Stream interface {
Stream interface { ListenAndServe(ctx context.Context, preDial PreDialFunc)
fmt.Stringer LocalAddr() net.Addr
StreamListener Close() error
Setup() error
Handle(conn StreamConn) error
}
StreamListener interface {
Addr() net.Addr
Accept() (StreamConn, error)
Close() error
}
StreamConn any
NetListenerWrapper struct {
net.Listener
}
)
func NetListener(l net.Listener) StreamListener {
return NetListenerWrapper{Listener: l}
} }
func (l NetListenerWrapper) Accept() (StreamConn, error) { type PreDialFunc func(ctx context.Context) error
return l.Listener.Accept()
}

View file

@ -1,4 +1,4 @@
package types package nettypes
import ( import (
urlPkg "net/url" urlPkg "net/url"

View file

@ -16,7 +16,7 @@ import (
loadbalance "github.com/yusing/go-proxy/internal/net/gphttp/loadbalancer/types" loadbalance "github.com/yusing/go-proxy/internal/net/gphttp/loadbalancer/types"
"github.com/yusing/go-proxy/internal/net/gphttp/middleware" "github.com/yusing/go-proxy/internal/net/gphttp/middleware"
"github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy" "github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy"
"github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/route/routes" "github.com/yusing/go-proxy/internal/route/routes"
"github.com/yusing/go-proxy/internal/task" "github.com/yusing/go-proxy/internal/task"
"github.com/yusing/go-proxy/internal/watcher/health/monitor" "github.com/yusing/go-proxy/internal/watcher/health/monitor"
@ -42,7 +42,7 @@ func NewReverseProxyRoute(base *Route) (*ReveseProxyRoute, gperr.Error) {
a := base.Agent() a := base.Agent()
if a != nil { if a != nil {
trans = a.Transport() trans = a.Transport()
proxyURL = types.NewURL(agent.HTTPProxyURL) proxyURL = nettypes.NewURL(agent.HTTPProxyURL)
} else { } else {
trans = gphttp.NewTransport() trans = gphttp.NewTransport()
if httpConfig.NoTLSVerify { if httpConfig.NoTLSVerify {

View file

@ -7,13 +7,13 @@ import (
"github.com/yusing/go-proxy/internal/docker" "github.com/yusing/go-proxy/internal/docker"
"github.com/yusing/go-proxy/internal/homepage" "github.com/yusing/go-proxy/internal/homepage"
idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types" idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types"
net "github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/task" "github.com/yusing/go-proxy/internal/task"
"github.com/yusing/go-proxy/internal/utils/pool" "github.com/yusing/go-proxy/internal/utils/pool"
"github.com/yusing/go-proxy/internal/watcher/health" "github.com/yusing/go-proxy/internal/watcher/health"
loadbalance "github.com/yusing/go-proxy/internal/net/gphttp/loadbalancer/types" loadbalance "github.com/yusing/go-proxy/internal/net/gphttp/loadbalancer/types"
"github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy" "github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy"
nettypes "github.com/yusing/go-proxy/internal/net/types"
) )
type ( type (
@ -24,7 +24,7 @@ type (
pool.Object pool.Object
ProviderName() string ProviderName() string
GetProvider() Provider GetProvider() Provider
TargetURL() *net.URL TargetURL() *nettypes.URL
HealthMonitor() health.HealthMonitor HealthMonitor() health.HealthMonitor
SetHealthMonitor(m health.HealthMonitor) SetHealthMonitor(m health.HealthMonitor)
References() []string References() []string
@ -57,7 +57,7 @@ type (
} }
StreamRoute interface { StreamRoute interface {
Route Route
net.Stream nettypes.Stream
} }
Provider interface { Provider interface {
GetRoute(alias string) (r Route, ok bool) GetRoute(alias string) (r Route, ok bool)

View file

@ -9,7 +9,7 @@ import (
"github.com/yusing/go-proxy/internal/gperr" "github.com/yusing/go-proxy/internal/gperr"
gphttp "github.com/yusing/go-proxy/internal/net/gphttp" 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/gphttp/reverseproxy"
"github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/utils/strutils" "github.com/yusing/go-proxy/internal/utils/strutils"
) )
@ -95,7 +95,7 @@ var commands = map[string]struct {
}, },
validate: validateURL, validate: validateURL,
build: func(args any) CommandHandler { build: func(args any) CommandHandler {
target := args.(*types.URL).String() target := args.(*nettypes.URL).String()
return ReturningCommand(func(w http.ResponseWriter, r *http.Request) { return ReturningCommand(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, target, http.StatusTemporaryRedirect) http.Redirect(w, r, target, http.StatusTemporaryRedirect)
}) })
@ -160,7 +160,7 @@ var commands = map[string]struct {
}, },
validate: validateAbsoluteURL, validate: validateAbsoluteURL,
build: func(args any) CommandHandler { build: func(args any) CommandHandler {
target := args.(*types.URL) target := args.(*nettypes.URL)
if target.Scheme == "" { if target.Scheme == "" {
target.Scheme = "http" target.Scheme = "http"
} }

View file

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

View file

@ -10,7 +10,7 @@ import (
"github.com/gobwas/glob" "github.com/gobwas/glob"
"github.com/yusing/go-proxy/internal/gperr" "github.com/yusing/go-proxy/internal/gperr"
gphttp "github.com/yusing/go-proxy/internal/net/gphttp" gphttp "github.com/yusing/go-proxy/internal/net/gphttp"
"github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
) )
type ( type (
@ -62,7 +62,7 @@ func validateURL(args []string) (any, gperr.Error) {
if len(args) != 1 { if len(args) != 1 {
return nil, ErrExpectOneArg return nil, ErrExpectOneArg
} }
u, err := types.ParseURL(args[0]) u, err := nettypes.ParseURL(args[0])
if err != nil { if err != nil {
return nil, ErrInvalidArguments.With(err) return nil, ErrInvalidArguments.With(err)
} }
@ -74,7 +74,7 @@ func validateAbsoluteURL(args []string) (any, gperr.Error) {
if len(args) != 1 { if len(args) != 1 {
return nil, ErrExpectOneArg return nil, ErrExpectOneArg
} }
u, err := types.ParseURL(args[0]) u, err := nettypes.ParseURL(args[0])
if err != nil { if err != nil {
return nil, ErrInvalidArguments.With(err) return nil, ErrInvalidArguments.With(err)
} }
@ -95,7 +95,7 @@ func validateCIDR(args []string) (any, gperr.Error) {
if !strings.Contains(args[0], "/") { if !strings.Contains(args[0], "/") {
args[0] += "/32" args[0] += "/32"
} }
cidr, err := types.ParseCIDR(args[0]) cidr, err := nettypes.ParseCIDR(args[0])
if err != nil { if err != nil {
return nil, ErrInvalidArguments.With(err) return nil, ErrInvalidArguments.With(err)
} }

View file

@ -8,7 +8,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/yusing/go-proxy/internal/gperr" "github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/idlewatcher" "github.com/yusing/go-proxy/internal/idlewatcher"
net "github.com/yusing/go-proxy/internal/net/types" nettypes "github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/route/routes" "github.com/yusing/go-proxy/internal/route/routes"
"github.com/yusing/go-proxy/internal/task" "github.com/yusing/go-proxy/internal/task"
"github.com/yusing/go-proxy/internal/watcher/health/monitor" "github.com/yusing/go-proxy/internal/watcher/health/monitor"
@ -17,7 +17,7 @@ import (
// TODO: support stream load balance. // TODO: support stream load balance.
type StreamRoute struct { type StreamRoute struct {
*Route *Route
net.Stream `json:"-"` nettypes.Stream `json:"-"`
l zerolog.Logger l zerolog.Logger
} }