fix tun device
This commit is contained in:
+15
-10
@@ -2,12 +2,14 @@ package tun
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/handler"
|
||||
md "github.com/go-gost/core/metadata"
|
||||
ctxvalue "github.com/go-gost/x/ctx"
|
||||
tun_util "github.com/go-gost/x/internal/util/tun"
|
||||
"github.com/go-gost/x/registry"
|
||||
"github.com/xjasonlyu/tun2socks/v2/core"
|
||||
)
|
||||
@@ -44,11 +46,21 @@ func (h *tunHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
|
||||
|
||||
log := h.options.Logger
|
||||
|
||||
var config *tun_util.Config
|
||||
if v, _ := conn.(md.Metadatable); v != nil {
|
||||
config = v.Metadata().Get("config").(*tun_util.Config)
|
||||
}
|
||||
if config == nil {
|
||||
err := errors.New("tun: wrong connection type")
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
log = log.WithFields(map[string]any{
|
||||
"remote": conn.RemoteAddr().String(),
|
||||
"local": conn.LocalAddr().String(),
|
||||
"sid": ctxvalue.SidFromContext(ctx),
|
||||
"sid": string(ctxvalue.SidFromContext(ctx)),
|
||||
})
|
||||
|
||||
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
|
||||
@@ -58,17 +70,12 @@ func (h *tunHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
|
||||
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
|
||||
}()
|
||||
|
||||
ep, err := newEndpoint(conn, 1420, 0, log)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
th := newTransportHandler(log)
|
||||
th := newTransportHandler(&h.options)
|
||||
th.ProcessAsync()
|
||||
defer th.Close()
|
||||
|
||||
stack, err := core.CreateStack(&core.Config{
|
||||
LinkEndpoint: ep,
|
||||
LinkEndpoint: newEndpoint(conn, config.MTU, log),
|
||||
TransportHandler: th,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -76,8 +83,6 @@ func (h *tunHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
|
||||
}
|
||||
defer stack.Close()
|
||||
|
||||
log.Debugf("is attached: %v", ep.IsAttached())
|
||||
|
||||
<-ctx.Done()
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user