tunnel: add multiple entrypoints
This commit is contained in:
+13
-13
@@ -276,7 +276,19 @@ func (h *http2Handler) roundTrip(ctx context.Context, w http.ResponseWriter, req
|
|||||||
ro.SrcAddr = cc.LocalAddr().String()
|
ro.SrcAddr = cc.LocalAddr().String()
|
||||||
ro.DstAddr = cc.RemoteAddr().String()
|
ro.DstAddr = cc.RemoteAddr().String()
|
||||||
|
|
||||||
if req.Method == http.MethodConnect {
|
if req.Method != http.MethodConnect {
|
||||||
|
start := time.Now()
|
||||||
|
log.Infof("%s <-> %s", req.RemoteAddr, host)
|
||||||
|
if err := h.forwardRequest(w, req, cc); err != nil {
|
||||||
|
log.Info("%s - %s: %s", req.RemoteAddr, host, err)
|
||||||
|
}
|
||||||
|
log.WithFields(map[string]any{
|
||||||
|
"duration": time.Since(start),
|
||||||
|
}).Infof("%s >-< %s", req.RemoteAddr, host)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
resp.StatusCode = http.StatusOK
|
resp.StatusCode = http.StatusOK
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
if fw, ok := w.(http.Flusher); ok {
|
if fw, ok := w.(http.Flusher); ok {
|
||||||
@@ -326,18 +338,6 @@ func (h *http2Handler) roundTrip(ctx context.Context, w http.ResponseWriter, req
|
|||||||
log.WithFields(map[string]any{
|
log.WithFields(map[string]any{
|
||||||
"duration": time.Since(start),
|
"duration": time.Since(start),
|
||||||
}).Infof("%s >-< %s", req.RemoteAddr, host)
|
}).Infof("%s >-< %s", req.RemoteAddr, host)
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
start := time.Now()
|
|
||||||
log.Infof("%s <-> %s", req.RemoteAddr, host)
|
|
||||||
if err := h.forwardRequest(w, req, cc); err != nil {
|
|
||||||
log.Info("%s - %s: %s", req.RemoteAddr, host, err)
|
|
||||||
}
|
|
||||||
log.WithFields(map[string]any{
|
|
||||||
"duration": time.Since(start),
|
|
||||||
}).Infof("%s >-< %s", req.RemoteAddr, host)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+68
-46
@@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
"github.com/go-gost/core/auth"
|
"github.com/go-gost/core/auth"
|
||||||
"github.com/go-gost/core/handler"
|
"github.com/go-gost/core/handler"
|
||||||
|
"github.com/go-gost/core/ingress"
|
||||||
"github.com/go-gost/core/limiter"
|
"github.com/go-gost/core/limiter"
|
||||||
"github.com/go-gost/core/limiter/traffic"
|
"github.com/go-gost/core/limiter/traffic"
|
||||||
"github.com/go-gost/core/listener"
|
"github.com/go-gost/core/listener"
|
||||||
@@ -48,8 +49,7 @@ type tunnelHandler struct {
|
|||||||
id string
|
id string
|
||||||
options handler.Options
|
options handler.Options
|
||||||
pool *ConnectorPool
|
pool *ConnectorPool
|
||||||
epSvc service.Service
|
entrypoints []service.Service
|
||||||
ep *entrypoint
|
|
||||||
md metadata
|
md metadata
|
||||||
log logger.Logger
|
log logger.Logger
|
||||||
stats *stats_util.HandlerStats
|
stats *stats_util.HandlerStats
|
||||||
@@ -84,34 +84,7 @@ func (h *tunnelHandler) Init(md md.Metadata) (err error) {
|
|||||||
})
|
})
|
||||||
h.pool = NewConnectorPool(h.id)
|
h.pool = NewConnectorPool(h.id)
|
||||||
|
|
||||||
h.ep = &entrypoint{
|
if err = h.initEntrypoints(); err != nil {
|
||||||
node: h.id,
|
|
||||||
service: h.options.Service,
|
|
||||||
pool: h.pool,
|
|
||||||
ingress: h.md.ingress,
|
|
||||||
sd: h.md.sd,
|
|
||||||
log: h.log.WithFields(map[string]any{
|
|
||||||
"kind": "entrypoint",
|
|
||||||
}),
|
|
||||||
sniffingWebsocket: h.md.sniffingWebsocket,
|
|
||||||
websocketSampleRate: h.md.sniffingWebsocketSampleRate,
|
|
||||||
readTimeout: h.md.entryPointReadTimeout,
|
|
||||||
}
|
|
||||||
h.ep.transport = &http.Transport{
|
|
||||||
DialContext: h.ep.dial,
|
|
||||||
IdleConnTimeout: 30 * time.Second,
|
|
||||||
ResponseHeaderTimeout: h.md.entryPointReadTimeout,
|
|
||||||
DisableKeepAlives: !h.md.entryPointKeepalive,
|
|
||||||
DisableCompression: !h.md.entryPointCompression,
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, ro := range h.options.Recorders {
|
|
||||||
if ro.Record == xrecorder.RecorderServiceHandler {
|
|
||||||
h.ep.recorder = ro
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err = h.initEntrypoint(); err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,20 +107,73 @@ func (h *tunnelHandler) Init(md md.Metadata) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *tunnelHandler) initEntrypoint() (err error) {
|
func (h *tunnelHandler) initEntrypoints() (err error) {
|
||||||
if h.md.entryPoint == "" {
|
if h.md.entryPoint != "" {
|
||||||
|
svc, err := h.createEntrypointService(h.md.entryPoint, h.md.ingress)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
go svc.Serve()
|
||||||
|
|
||||||
|
h.entrypoints = append(h.entrypoints, svc)
|
||||||
|
h.log.Infof("entrypoint: %s", svc.Addr())
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ep := range h.md.entrypoints {
|
||||||
|
if ep.Addr == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
svc, err := h.createEntrypointService(ep.Addr, ep.Ingress)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
go svc.Serve()
|
||||||
|
|
||||||
|
h.entrypoints = append(h.entrypoints, svc)
|
||||||
|
h.log.Infof("entrypoint: %s %s", ep.Name, svc.Addr())
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *tunnelHandler) createEntrypointService(addr string, ingress ingress.Ingress) (service.Service, error) {
|
||||||
|
ep := &entrypoint{
|
||||||
|
node: h.id,
|
||||||
|
service: h.options.Service,
|
||||||
|
pool: h.pool,
|
||||||
|
ingress: ingress,
|
||||||
|
sd: h.md.sd,
|
||||||
|
log: h.log.WithFields(map[string]any{
|
||||||
|
"kind": "entrypoint",
|
||||||
|
}),
|
||||||
|
sniffingWebsocket: h.md.sniffingWebsocket,
|
||||||
|
websocketSampleRate: h.md.sniffingWebsocketSampleRate,
|
||||||
|
readTimeout: h.md.entryPointReadTimeout,
|
||||||
|
}
|
||||||
|
ep.transport = &http.Transport{
|
||||||
|
DialContext: ep.dial,
|
||||||
|
IdleConnTimeout: 30 * time.Second,
|
||||||
|
ResponseHeaderTimeout: h.md.entryPointReadTimeout,
|
||||||
|
DisableKeepAlives: !h.md.entryPointKeepalive,
|
||||||
|
DisableCompression: !h.md.entryPointCompression,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ro := range h.options.Recorders {
|
||||||
|
if ro.Record == xrecorder.RecorderServiceHandler {
|
||||||
|
ep.recorder = ro
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
network := "tcp"
|
network := "tcp"
|
||||||
if xnet.IsIPv4(h.md.entryPoint) {
|
if xnet.IsIPv4(addr) {
|
||||||
network = "tcp4"
|
network = "tcp4"
|
||||||
}
|
}
|
||||||
|
|
||||||
ln, err := net.Listen(network, h.md.entryPoint)
|
ln, err := net.Listen(network, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.log.Error(err)
|
h.log.Error(err)
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
serviceName := fmt.Sprintf("%s-ep-%s", h.options.Service, ln.Addr())
|
serviceName := fmt.Sprintf("%s-ep-%s", h.options.Service, ln.Addr())
|
||||||
@@ -166,23 +192,19 @@ func (h *tunnelHandler) initEntrypoint() (err error) {
|
|||||||
})),
|
})),
|
||||||
)
|
)
|
||||||
if err = epListener.Init(nil); err != nil {
|
if err = epListener.Init(nil); err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
epHandler := &entrypointHandler{
|
epHandler := &entrypointHandler{
|
||||||
ep: h.ep,
|
ep: ep,
|
||||||
}
|
}
|
||||||
if err = epHandler.Init(nil); err != nil {
|
if err = epHandler.Init(nil); err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
h.epSvc = xservice.NewService(
|
return xservice.NewService(
|
||||||
serviceName, epListener, epHandler,
|
serviceName, epListener, epHandler,
|
||||||
xservice.LoggerOption(log),
|
xservice.LoggerOption(log),
|
||||||
)
|
), nil
|
||||||
go h.epSvc.Serve()
|
|
||||||
log.Infof("entrypoint: %s", h.epSvc.Addr())
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *tunnelHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.HandleOption) (err error) {
|
func (h *tunnelHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.HandleOption) (err error) {
|
||||||
@@ -306,8 +328,8 @@ func (h *tunnelHandler) Handle(ctx context.Context, conn net.Conn, opts ...handl
|
|||||||
|
|
||||||
// Close implements io.Closer interface.
|
// Close implements io.Closer interface.
|
||||||
func (h *tunnelHandler) Close() error {
|
func (h *tunnelHandler) Close() error {
|
||||||
if h.epSvc != nil {
|
for _, ep := range h.entrypoints {
|
||||||
h.epSvc.Close()
|
ep.Close()
|
||||||
}
|
}
|
||||||
h.pool.Close()
|
h.pool.Close()
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package tunnel
|
package tunnel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -22,6 +23,8 @@ const (
|
|||||||
type metadata struct {
|
type metadata struct {
|
||||||
readTimeout time.Duration
|
readTimeout time.Duration
|
||||||
|
|
||||||
|
entrypoints []entrypointConfig
|
||||||
|
|
||||||
entryPoint string
|
entryPoint string
|
||||||
entryPointID relay.TunnelID
|
entryPointID relay.TunnelID
|
||||||
entryPointProxyProtocol int
|
entryPointProxyProtocol int
|
||||||
@@ -91,6 +94,39 @@ func (h *tunnelHandler) parseMetadata(md mdata.Metadata) (err error) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if md != nil {
|
||||||
|
if v := md.Get("entrypoints"); v != nil {
|
||||||
|
b, err := json.Marshal(v)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var entrypoints []struct {
|
||||||
|
Addr string
|
||||||
|
Ingress string
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(b, &entrypoints); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ep := range entrypoints {
|
||||||
|
if ep.Addr == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ingress := registry.IngressRegistry().Get(ep.Ingress)
|
||||||
|
if ingress == nil {
|
||||||
|
ingress = h.md.ingress
|
||||||
|
}
|
||||||
|
h.md.entrypoints = append(h.md.entrypoints, entrypointConfig{
|
||||||
|
Addr: ep.Addr,
|
||||||
|
Ingress: ingress,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
h.md.sd = registry.SDRegistry().Get(mdutil.GetString(md, "sd"))
|
h.md.sd = registry.SDRegistry().Get(mdutil.GetString(md, "sd"))
|
||||||
|
|
||||||
h.md.muxCfg = &mux.Config{
|
h.md.muxCfg = &mux.Config{
|
||||||
@@ -123,3 +159,9 @@ func (h *tunnelHandler) parseMetadata(md mdata.Metadata) (err error) {
|
|||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type entrypointConfig struct {
|
||||||
|
Name string
|
||||||
|
Addr string
|
||||||
|
Ingress ingress.Ingress
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user