add mux config
This commit is contained in:
133
handler/file/handler.go
Normal file
133
handler/file/handler.go
Normal file
@ -0,0 +1,133 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/handler"
|
||||
md "github.com/go-gost/core/metadata"
|
||||
"github.com/go-gost/x/registry"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.HandlerRegistry().Register("file", NewHandler)
|
||||
}
|
||||
|
||||
type fileHandler struct {
|
||||
handler http.Handler
|
||||
server *http.Server
|
||||
ln *singleConnListener
|
||||
md metadata
|
||||
options handler.Options
|
||||
}
|
||||
|
||||
func NewHandler(opts ...handler.Option) handler.Handler {
|
||||
options := handler.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
return &fileHandler{
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *fileHandler) Init(md md.Metadata) (err error) {
|
||||
if err = h.parseMetadata(md); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
h.handler = http.FileServer(http.Dir(h.md.dir))
|
||||
h.server = &http.Server{
|
||||
Handler: http.HandlerFunc(h.handleFunc),
|
||||
}
|
||||
|
||||
h.ln = &singleConnListener{
|
||||
conn: make(chan net.Conn),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
go h.server.Serve(h.ln)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (h *fileHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.HandleOption) error {
|
||||
h.options.Logger.WithFields(map[string]any{
|
||||
"remote": conn.RemoteAddr().String(),
|
||||
"local": conn.LocalAddr().String(),
|
||||
}).Infof("%s - %s", conn.RemoteAddr(), conn.LocalAddr())
|
||||
|
||||
h.ln.send(conn)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *fileHandler) Close() error {
|
||||
return h.server.Close()
|
||||
}
|
||||
|
||||
func (h *fileHandler) handleFunc(w http.ResponseWriter, r *http.Request) {
|
||||
if auther := h.options.Auther; auther != nil {
|
||||
u, p, _ := r.BasicAuth()
|
||||
if _, ok := auther.Authenticate(r.Context(), u, p); !ok {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
log := h.options.Logger
|
||||
start := time.Now()
|
||||
|
||||
h.handler.ServeHTTP(w, r)
|
||||
|
||||
log = log.WithFields(map[string]any{
|
||||
"remote": r.RemoteAddr,
|
||||
"duration": time.Since(start),
|
||||
})
|
||||
log.Infof("%s %s", r.Method, r.RequestURI)
|
||||
}
|
||||
|
||||
type singleConnListener struct {
|
||||
conn chan net.Conn
|
||||
addr net.Addr
|
||||
done chan struct{}
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (l *singleConnListener) Accept() (net.Conn, error) {
|
||||
select {
|
||||
case conn := <-l.conn:
|
||||
return conn, nil
|
||||
|
||||
case <-l.done:
|
||||
return nil, net.ErrClosed
|
||||
}
|
||||
}
|
||||
|
||||
func (l *singleConnListener) Close() error {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
select {
|
||||
case <-l.done:
|
||||
default:
|
||||
close(l.done)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *singleConnListener) Addr() net.Addr {
|
||||
return l.addr
|
||||
}
|
||||
|
||||
func (l *singleConnListener) send(conn net.Conn) {
|
||||
select {
|
||||
case l.conn <- conn:
|
||||
case <-l.done:
|
||||
return
|
||||
}
|
||||
}
|
15
handler/file/metadata.go
Normal file
15
handler/file/metadata.go
Normal file
@ -0,0 +1,15 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
mdata "github.com/go-gost/core/metadata"
|
||||
mdutil "github.com/go-gost/core/metadata/util"
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
dir string
|
||||
}
|
||||
|
||||
func (h *fileHandler) parseMetadata(md mdata.Metadata) (err error) {
|
||||
h.md.dir = mdutil.GetString(md, "file.dir", "dir")
|
||||
return
|
||||
}
|
@ -276,7 +276,11 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, log l
|
||||
log.Warnf("read response from node %s(%s): %v", target.Name, target.Addr, err)
|
||||
return resp.Write(rw)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if log.IsLevelEnabled(logger.TraceLevel) {
|
||||
dump, _ := httputil.DumpResponse(res, false)
|
||||
log.Trace(string(dump))
|
||||
}
|
||||
|
||||
return res.Write(rw)
|
||||
}()
|
||||
|
@ -281,7 +281,11 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, remot
|
||||
log.Warnf("read response from node %s(%s): %v", target.Name, target.Addr, err)
|
||||
return resp.Write(rw)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if log.IsLevelEnabled(logger.TraceLevel) {
|
||||
dump, _ := httputil.DumpResponse(res, false)
|
||||
log.Trace(string(dump))
|
||||
}
|
||||
|
||||
return res.Write(rw)
|
||||
}()
|
||||
|
134
handler/metrics/handler.go
Normal file
134
handler/metrics/handler.go
Normal file
@ -0,0 +1,134 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/handler"
|
||||
md "github.com/go-gost/core/metadata"
|
||||
xmetrics "github.com/go-gost/x/metrics"
|
||||
"github.com/go-gost/x/registry"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.HandlerRegistry().Register("metrics", NewHandler)
|
||||
}
|
||||
|
||||
type metricsHandler struct {
|
||||
handler http.Handler
|
||||
server *http.Server
|
||||
ln *singleConnListener
|
||||
md metadata
|
||||
options handler.Options
|
||||
}
|
||||
|
||||
func NewHandler(opts ...handler.Option) handler.Handler {
|
||||
options := handler.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
return &metricsHandler{
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *metricsHandler) Init(md md.Metadata) (err error) {
|
||||
if err = h.parseMetadata(md); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
xmetrics.Init(xmetrics.NewMetrics())
|
||||
h.handler = promhttp.Handler()
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle(h.md.path, http.HandlerFunc(h.handleFunc))
|
||||
h.server = &http.Server{
|
||||
Handler: mux,
|
||||
}
|
||||
|
||||
h.ln = &singleConnListener{
|
||||
conn: make(chan net.Conn),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
go h.server.Serve(h.ln)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (h *metricsHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.HandleOption) error {
|
||||
h.ln.send(conn)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *metricsHandler) Close() error {
|
||||
return h.server.Close()
|
||||
}
|
||||
|
||||
func (h *metricsHandler) handleFunc(w http.ResponseWriter, r *http.Request) {
|
||||
if auther := h.options.Auther; auther != nil {
|
||||
u, p, _ := r.BasicAuth()
|
||||
if _, ok := auther.Authenticate(r.Context(), u, p); !ok {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
log := h.options.Logger
|
||||
start := time.Now()
|
||||
|
||||
h.handler.ServeHTTP(w, r)
|
||||
|
||||
log = log.WithFields(map[string]any{
|
||||
"remote": r.RemoteAddr,
|
||||
"duration": time.Since(start),
|
||||
})
|
||||
log.Debugf("%s %s", r.Method, r.RequestURI)
|
||||
}
|
||||
|
||||
type singleConnListener struct {
|
||||
conn chan net.Conn
|
||||
addr net.Addr
|
||||
done chan struct{}
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (l *singleConnListener) Accept() (net.Conn, error) {
|
||||
select {
|
||||
case conn := <-l.conn:
|
||||
return conn, nil
|
||||
|
||||
case <-l.done:
|
||||
return nil, net.ErrClosed
|
||||
}
|
||||
}
|
||||
|
||||
func (l *singleConnListener) Close() error {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
select {
|
||||
case <-l.done:
|
||||
default:
|
||||
close(l.done)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *singleConnListener) Addr() net.Addr {
|
||||
return l.addr
|
||||
}
|
||||
|
||||
func (l *singleConnListener) send(conn net.Conn) {
|
||||
select {
|
||||
case l.conn <- conn:
|
||||
case <-l.done:
|
||||
return
|
||||
}
|
||||
}
|
22
handler/metrics/metadata.go
Normal file
22
handler/metrics/metadata.go
Normal file
@ -0,0 +1,22 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
mdata "github.com/go-gost/core/metadata"
|
||||
mdutil "github.com/go-gost/core/metadata/util"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultPath = "/metrics"
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
path string
|
||||
}
|
||||
|
||||
func (h *metricsHandler) parseMetadata(md mdata.Metadata) (err error) {
|
||||
h.md.path = mdutil.GetString(md, "metrics.path", "path")
|
||||
if h.md.path == "" {
|
||||
h.md.path = DefaultPath
|
||||
}
|
||||
return
|
||||
}
|
@ -81,7 +81,7 @@ func (h *relayHandler) bindTCP(ctx context.Context, conn net.Conn, network, addr
|
||||
}
|
||||
|
||||
// Upgrade connection to multiplex session.
|
||||
session, err := mux.ClientSession(conn)
|
||||
session, err := mux.ClientSession(conn, nil)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
@ -219,7 +219,7 @@ func (h *relayHandler) handleBindTunnel(ctx context.Context, conn net.Conn, netw
|
||||
resp.WriteTo(conn)
|
||||
|
||||
// Upgrade connection to multiplex session.
|
||||
session, err := mux.ClientSession(conn)
|
||||
session, err := mux.ClientSession(conn, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -17,7 +17,6 @@ import (
|
||||
md "github.com/go-gost/core/metadata"
|
||||
"github.com/go-gost/relay"
|
||||
admission "github.com/go-gost/x/admission/wrapper"
|
||||
netpkg "github.com/go-gost/x/internal/net"
|
||||
xnet "github.com/go-gost/x/internal/net"
|
||||
"github.com/go-gost/x/internal/net/proxyproto"
|
||||
"github.com/go-gost/x/internal/util/forward"
|
||||
@ -126,7 +125,7 @@ func (h *tcpHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
|
||||
|
||||
t := time.Now()
|
||||
log.Debugf("%s <-> %s", conn.RemoteAddr(), cc.RemoteAddr())
|
||||
netpkg.Transport(conn, cc)
|
||||
xnet.Transport(conn, cc)
|
||||
log.WithFields(map[string]any{"duration": time.Since(t)}).
|
||||
Debugf("%s >-< %s", conn.RemoteAddr(), cc.RemoteAddr())
|
||||
return nil
|
||||
|
@ -70,7 +70,7 @@ func (h *socks5Handler) muxBindLocal(ctx context.Context, conn net.Conn, network
|
||||
|
||||
func (h *socks5Handler) serveMuxBind(ctx context.Context, conn net.Conn, ln net.Listener, log logger.Logger) error {
|
||||
// Upgrade connection to multiplex stream.
|
||||
session, err := mux.ClientSession(conn)
|
||||
session, err := mux.ClientSession(conn, nil)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
|
@ -48,7 +48,7 @@ func (h *tunnelHandler) handleBind(ctx context.Context, conn net.Conn, network,
|
||||
resp.WriteTo(conn)
|
||||
|
||||
// Upgrade connection to multiplex session.
|
||||
session, err := mux.ClientSession(conn)
|
||||
session, err := mux.ClientSession(conn, h.md.muxCfg)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -10,9 +10,14 @@ import (
|
||||
mdutil "github.com/go-gost/core/metadata/util"
|
||||
"github.com/go-gost/relay"
|
||||
xingress "github.com/go-gost/x/ingress"
|
||||
"github.com/go-gost/x/internal/util/mux"
|
||||
"github.com/go-gost/x/registry"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultMuxVersion = 2
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
readTimeout time.Duration
|
||||
noDelay bool
|
||||
@ -20,6 +25,7 @@ type metadata struct {
|
||||
directTunnel bool
|
||||
entryPointID relay.TunnelID
|
||||
ingress ingress.Ingress
|
||||
muxCfg *mux.Config
|
||||
}
|
||||
|
||||
func (h *tunnelHandler) parseMetadata(md mdata.Metadata) (err error) {
|
||||
@ -54,5 +60,18 @@ func (h *tunnelHandler) parseMetadata(md mdata.Metadata) (err error) {
|
||||
}
|
||||
}
|
||||
|
||||
h.md.muxCfg = &mux.Config{
|
||||
Version: mdutil.GetInt(md, "mux.version"),
|
||||
KeepAliveInterval: mdutil.GetDuration(md, "mux.keepaliveInterval"),
|
||||
KeepAliveDisabled: mdutil.GetBool(md, "mux.keepaliveDisabled"),
|
||||
KeepAliveTimeout: mdutil.GetDuration(md, "mux.keepaliveTimeout"),
|
||||
MaxFrameSize: mdutil.GetInt(md, "mux.maxFrameSize"),
|
||||
MaxReceiveBuffer: mdutil.GetInt(md, "mux.maxReceiveBuffer"),
|
||||
MaxStreamBuffer: mdutil.GetInt(md, "mux.maxStreamBuffer"),
|
||||
}
|
||||
if h.md.muxCfg.Version == 0 {
|
||||
h.md.muxCfg.Version = defaultMuxVersion
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user