155 lines
3.4 KiB
Go
155 lines
3.4 KiB
Go
package unix
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/go-gost/core/chain"
|
|
"github.com/go-gost/core/handler"
|
|
"github.com/go-gost/core/hop"
|
|
"github.com/go-gost/core/logger"
|
|
md "github.com/go-gost/core/metadata"
|
|
"github.com/go-gost/core/recorder"
|
|
ctxvalue "github.com/go-gost/x/ctx"
|
|
xnet "github.com/go-gost/x/internal/net"
|
|
xrecorder "github.com/go-gost/x/recorder"
|
|
"github.com/go-gost/x/registry"
|
|
)
|
|
|
|
func init() {
|
|
registry.HandlerRegistry().Register("unix", NewHandler)
|
|
}
|
|
|
|
type unixHandler struct {
|
|
hop hop.Hop
|
|
md metadata
|
|
options handler.Options
|
|
recorder recorder.RecorderObject
|
|
}
|
|
|
|
func NewHandler(opts ...handler.Option) handler.Handler {
|
|
options := handler.Options{}
|
|
for _, opt := range opts {
|
|
opt(&options)
|
|
}
|
|
|
|
return &unixHandler{
|
|
options: options,
|
|
}
|
|
}
|
|
|
|
func (h *unixHandler) Init(md md.Metadata) (err error) {
|
|
if err = h.parseMetadata(md); err != nil {
|
|
return
|
|
}
|
|
|
|
for _, ro := range h.options.Recorders {
|
|
if ro.Record == xrecorder.RecorderServiceHandler {
|
|
h.recorder = ro
|
|
break
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// Forward implements handler.Forwarder.
|
|
func (h *unixHandler) Forward(hop hop.Hop) {
|
|
h.hop = hop
|
|
}
|
|
|
|
func (h *unixHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.HandleOption) (err error) {
|
|
defer conn.Close()
|
|
|
|
start := time.Now()
|
|
|
|
ro := &xrecorder.HandlerRecorderObject{
|
|
Service: h.options.Service,
|
|
Network: "unix",
|
|
RemoteAddr: conn.RemoteAddr().String(),
|
|
LocalAddr: conn.LocalAddr().String(),
|
|
Time: start,
|
|
SID: string(ctxvalue.SidFromContext(ctx)),
|
|
}
|
|
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
|
|
|
|
log := h.options.Logger.WithFields(map[string]any{
|
|
"remote": conn.RemoteAddr().String(),
|
|
"local": conn.LocalAddr().String(),
|
|
"sid": ctxvalue.SidFromContext(ctx),
|
|
})
|
|
|
|
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
|
|
defer func() {
|
|
if err != nil {
|
|
ro.Err = err.Error()
|
|
}
|
|
ro.Duration = time.Since(start)
|
|
ro.Record(ctx, h.recorder.Recorder)
|
|
|
|
log.WithFields(map[string]any{
|
|
"duration": time.Since(start),
|
|
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
|
|
}()
|
|
|
|
if h.hop != nil {
|
|
target := h.hop.Select(ctx)
|
|
if target == nil {
|
|
err = errors.New("target not available")
|
|
log.Error(err)
|
|
return err
|
|
}
|
|
log = log.WithFields(map[string]any{
|
|
"node": target.Name,
|
|
"dst": target.Addr,
|
|
})
|
|
ro.Host = target.Addr
|
|
|
|
return h.forwardUnix(ctx, conn, target, log)
|
|
}
|
|
|
|
cc, err := h.options.Router.Dial(ctx, "tcp", "@")
|
|
if err != nil {
|
|
log.Error(err)
|
|
return err
|
|
}
|
|
defer cc.Close()
|
|
|
|
t := time.Now()
|
|
log.Infof("%s <-> %s", conn.LocalAddr(), "@")
|
|
xnet.Transport(conn, cc)
|
|
log.WithFields(map[string]any{
|
|
"duration": time.Since(t),
|
|
}).Infof("%s >-< %s", conn.LocalAddr(), "@")
|
|
|
|
return nil
|
|
}
|
|
|
|
func (h *unixHandler) forwardUnix(ctx context.Context, conn net.Conn, target *chain.Node, log logger.Logger) (err error) {
|
|
log.Debugf("%s >> %s", conn.LocalAddr(), target.Addr)
|
|
var cc io.ReadWriteCloser
|
|
|
|
if opts := h.options.Router.Options(); opts != nil && opts.Chain != nil {
|
|
cc, err = h.options.Router.Dial(ctx, "unix", target.Addr)
|
|
} else {
|
|
cc, err = (&net.Dialer{}).DialContext(ctx, "unix", target.Addr)
|
|
}
|
|
if err != nil {
|
|
log.Error(err)
|
|
return err
|
|
}
|
|
defer cc.Close()
|
|
|
|
t := time.Now()
|
|
log.Infof("%s <-> %s", conn.LocalAddr(), target.Addr)
|
|
xnet.Transport(conn, cc)
|
|
log.WithFields(map[string]any{
|
|
"duration": time.Since(t),
|
|
}).Infof("%s >-< %s", conn.LocalAddr(), target.Addr)
|
|
|
|
return nil
|
|
}
|