tungo: additional metadata options

This commit is contained in:
ginuerzh
2025-07-30 10:07:18 +08:00
parent 3bbc10796c
commit 87e454a6f1
9 changed files with 115 additions and 12 deletions
+14
View File
@@ -20,6 +20,7 @@ import (
"github.com/go-gost/x/registry"
"github.com/xjasonlyu/tun2socks/v2/core"
"github.com/xjasonlyu/tun2socks/v2/core/adapter"
"github.com/xjasonlyu/tun2socks/v2/core/option"
"gvisor.dev/gvisor/pkg/tcpip/stack"
)
@@ -129,9 +130,22 @@ func (h *tungoHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
th.ProcessAsync()
defer th.Close()
var cOpts []option.Option
if h.md.tcpModerateReceiveBuffer {
cOpts = append(cOpts, option.WithTCPModerateReceiveBuffer(h.md.tcpModerateReceiveBuffer))
}
if h.md.tcpSendBufferSize > 0 {
cOpts = append(cOpts, option.WithTCPSendBufferSize(h.md.tcpSendBufferSize))
}
if h.md.tcpReceiveBufferSize > 0 {
cOpts = append(cOpts, option.WithTCPReceiveBufferSize(h.md.tcpReceiveBufferSize))
}
stack, err := core.CreateStack(&core.Config{
LinkEndpoint: newEndpoint(conn, config.MTU, log),
TransportHandler: th,
MulticastGroups: h.md.multicastGroups,
Options: cOpts,
})
if err != nil {
return err
+29 -2
View File
@@ -1,6 +1,9 @@
package tungo
import (
"fmt"
"net/netip"
"strings"
"time"
mdata "github.com/go-gost/core/metadata"
@@ -11,7 +14,7 @@ type metadata struct {
udpTimeout time.Duration
sniffing bool
sniffingUDP bool
sniffingUDP bool
sniffingTimeout time.Duration
sniffingResponseTimeout time.Duration
sniffingFallback bool
@@ -21,10 +24,16 @@ type metadata struct {
limiterRefreshInterval time.Duration
limiterCleanupInterval time.Duration
multicastGroups []netip.Addr
tcpSendBufferSize int
tcpReceiveBufferSize int
tcpModerateReceiveBuffer bool
}
func (h *tungoHandler) parseMetadata(md mdata.Metadata) (err error) {
h.md.udpTimeout = mdutil.GetDuration(md, "udpTimeout")
h.md.udpTimeout = mdutil.GetDuration(md, "udpTimeout", "tungo.udpTimeout")
h.md.sniffing = mdutil.GetBool(md, "sniffing")
h.md.sniffingUDP = mdutil.GetBool(md, "sniffing.udp")
@@ -44,5 +53,23 @@ func (h *tungoHandler) parseMetadata(md mdata.Metadata) (err error) {
h.md.limiterRefreshInterval = mdutil.GetDuration(md, "limiter.refreshInterval")
h.md.limiterCleanupInterval = mdutil.GetDuration(md, "limiter.cleanupInterval")
for _, v := range strings.Split(mdutil.GetString(md, "multicastGroups", "tungo.multicastGroups"), ",") {
if v = strings.TrimSpace(v); v == "" {
continue
}
addr, err := netip.ParseAddr(v)
if err != nil {
return err
}
if !addr.IsMulticast() {
return fmt.Errorf("invalid multicast IP: %s", addr)
}
h.md.multicastGroups = append(h.md.multicastGroups, addr)
}
h.md.tcpSendBufferSize = mdutil.GetInt(md, "tcpSendBufferSize", "tungo.tcpSendBufferSize")
h.md.tcpReceiveBufferSize = mdutil.GetInt(md, "tcpReceiveBufferSize", "tungo.tcpReceiveBufferSize")
h.md.tcpModerateReceiveBuffer = mdutil.GetBool(md, "tcpModerateReceiveBuffer", "tungo.tcpModerateReceiveBuffer")
return
}