tungo: additional metadata options
This commit is contained in:
@@ -20,6 +20,7 @@ import (
|
|||||||
"github.com/go-gost/x/registry"
|
"github.com/go-gost/x/registry"
|
||||||
"github.com/xjasonlyu/tun2socks/v2/core"
|
"github.com/xjasonlyu/tun2socks/v2/core"
|
||||||
"github.com/xjasonlyu/tun2socks/v2/core/adapter"
|
"github.com/xjasonlyu/tun2socks/v2/core/adapter"
|
||||||
|
"github.com/xjasonlyu/tun2socks/v2/core/option"
|
||||||
"gvisor.dev/gvisor/pkg/tcpip/stack"
|
"gvisor.dev/gvisor/pkg/tcpip/stack"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -129,9 +130,22 @@ func (h *tungoHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
|
|||||||
th.ProcessAsync()
|
th.ProcessAsync()
|
||||||
defer th.Close()
|
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{
|
stack, err := core.CreateStack(&core.Config{
|
||||||
LinkEndpoint: newEndpoint(conn, config.MTU, log),
|
LinkEndpoint: newEndpoint(conn, config.MTU, log),
|
||||||
TransportHandler: th,
|
TransportHandler: th,
|
||||||
|
MulticastGroups: h.md.multicastGroups,
|
||||||
|
Options: cOpts,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package tungo
|
package tungo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/netip"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
mdata "github.com/go-gost/core/metadata"
|
mdata "github.com/go-gost/core/metadata"
|
||||||
@@ -11,7 +14,7 @@ type metadata struct {
|
|||||||
udpTimeout time.Duration
|
udpTimeout time.Duration
|
||||||
|
|
||||||
sniffing bool
|
sniffing bool
|
||||||
sniffingUDP bool
|
sniffingUDP bool
|
||||||
sniffingTimeout time.Duration
|
sniffingTimeout time.Duration
|
||||||
sniffingResponseTimeout time.Duration
|
sniffingResponseTimeout time.Duration
|
||||||
sniffingFallback bool
|
sniffingFallback bool
|
||||||
@@ -21,10 +24,16 @@ type metadata struct {
|
|||||||
|
|
||||||
limiterRefreshInterval time.Duration
|
limiterRefreshInterval time.Duration
|
||||||
limiterCleanupInterval time.Duration
|
limiterCleanupInterval time.Duration
|
||||||
|
|
||||||
|
multicastGroups []netip.Addr
|
||||||
|
|
||||||
|
tcpSendBufferSize int
|
||||||
|
tcpReceiveBufferSize int
|
||||||
|
tcpModerateReceiveBuffer bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *tungoHandler) parseMetadata(md mdata.Metadata) (err error) {
|
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.sniffing = mdutil.GetBool(md, "sniffing")
|
||||||
h.md.sniffingUDP = mdutil.GetBool(md, "sniffing.udp")
|
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.limiterRefreshInterval = mdutil.GetDuration(md, "limiter.refreshInterval")
|
||||||
h.md.limiterCleanupInterval = mdutil.GetDuration(md, "limiter.cleanupInterval")
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
// tcpWaitTimeout implements a TCP half-close timeout.
|
// tcpWaitTimeout implements a TCP half-close timeout.
|
||||||
tcpWaitTimeout = 30 * time.Second
|
tcpWaitTimeout = 10 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
func Pipe(ctx context.Context, rw1, rw2 io.ReadWriter) error {
|
func Pipe(ctx context.Context, rw1, rw2 io.ReadWriter) error {
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ const (
|
|||||||
|
|
||||||
type metadata struct {
|
type metadata struct {
|
||||||
config *tun_util.Config
|
config *tun_util.Config
|
||||||
|
guid string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *tunListener) parseMetadata(md mdata.Metadata) (err error) {
|
func (l *tunListener) parseMetadata(md mdata.Metadata) (err error) {
|
||||||
@@ -102,5 +103,7 @@ func (l *tunListener) parseMetadata(md mdata.Metadata) (err error) {
|
|||||||
|
|
||||||
l.md.config = config
|
l.md.config = config
|
||||||
|
|
||||||
|
l.md.guid = mdutil.GetString(md, "guid", "tun.guid")
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/sys/windows"
|
||||||
|
"golang.zx2c4.com/wireguard/tun"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -14,15 +17,40 @@ const (
|
|||||||
writeOffset = 0
|
writeOffset = 0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
tun.WintunTunnelType = "GOST"
|
||||||
|
}
|
||||||
|
|
||||||
func (l *tunListener) createTun() (ifce io.ReadWriteCloser, name string, ip net.IP, err error) {
|
func (l *tunListener) createTun() (ifce io.ReadWriteCloser, name string, ip net.IP, err error) {
|
||||||
if l.md.config.Name == "" {
|
if l.md.config.Name == "" {
|
||||||
l.md.config.Name = defaultTunName
|
l.md.config.Name = defaultTunName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if l.md.guid != "" {
|
||||||
|
var guid windows.GUID
|
||||||
|
guid, err = windows.GUIDFromString(l.md.guid)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tun.WintunStaticRequestedGUID = &guid
|
||||||
|
}
|
||||||
|
|
||||||
ifce, name, err = l.createTunDevice()
|
ifce, name, err = l.createTunDevice()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if l.md.config.MTU > 0 {
|
||||||
|
cmd := fmt.Sprintf("netsh interface ip set subinterface %s mtu=%d", name, l.md.config.MTU)
|
||||||
|
l.log.Debug(cmd)
|
||||||
|
|
||||||
|
args := strings.Split(cmd, " ")
|
||||||
|
if er := exec.Command(args[0], args[1:]...).Run(); er != nil {
|
||||||
|
err = fmt.Errorf("%s: %v", cmd, er)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(l.md.config.Net) > 0 {
|
if len(l.md.config.Net) > 0 {
|
||||||
ipNet := l.md.config.Net[0]
|
ipNet := l.md.config.Net[0]
|
||||||
cmd := fmt.Sprintf("netsh interface ip set address name=%s "+
|
cmd := fmt.Sprintf("netsh interface ip set address name=%s "+
|
||||||
|
|||||||
@@ -12,12 +12,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultMTU = 1420
|
defaultMTU = 1420
|
||||||
defaultName = "tungo"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type metadata struct {
|
type metadata struct {
|
||||||
config *tun_util.Config
|
config *tun_util.Config
|
||||||
|
guid string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *tunListener) parseMetadata(md mdata.Metadata) (err error) {
|
func (l *tunListener) parseMetadata(md mdata.Metadata) (err error) {
|
||||||
@@ -29,9 +29,6 @@ func (l *tunListener) parseMetadata(md mdata.Metadata) (err error) {
|
|||||||
if config.MTU <= 0 {
|
if config.MTU <= 0 {
|
||||||
config.MTU = defaultMTU
|
config.MTU = defaultMTU
|
||||||
}
|
}
|
||||||
if config.Name == "" {
|
|
||||||
config.Name = defaultName
|
|
||||||
}
|
|
||||||
if gw := mdutil.GetString(md, "gw", "tun.gw"); gw != "" {
|
if gw := mdutil.GetString(md, "gw", "tun.gw"); gw != "" {
|
||||||
config.Gateway = net.ParseIP(gw)
|
config.Gateway = net.ParseIP(gw)
|
||||||
}
|
}
|
||||||
@@ -93,5 +90,7 @@ func (l *tunListener) parseMetadata(md mdata.Metadata) (err error) {
|
|||||||
|
|
||||||
l.md.config = config
|
l.md.config = config
|
||||||
|
|
||||||
|
l.md.guid = mdutil.GetString(md, "guid", "tun.guid")
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,11 +11,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
readOffset = 0
|
defaultTunName = "tungo"
|
||||||
writeOffset = 16
|
readOffset = 0
|
||||||
|
writeOffset = 16
|
||||||
)
|
)
|
||||||
|
|
||||||
func (l *tunListener) createTun() (dev io.ReadWriteCloser, name string, ip net.IP, err error) {
|
func (l *tunListener) createTun() (dev io.ReadWriteCloser, name string, ip net.IP, err error) {
|
||||||
|
if l.md.config.Name == "" {
|
||||||
|
l.md.config.Name = defaultTunName
|
||||||
|
}
|
||||||
dev, name, err = l.createTunDevice()
|
dev, name, err = l.createTunDevice()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultTunName = "tun0"
|
defaultTunName = "tungo"
|
||||||
readOffset = 4
|
readOffset = 4
|
||||||
writeOffset = 4
|
writeOffset = 4
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -6,23 +6,51 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/sys/windows"
|
||||||
|
"golang.zx2c4.com/wireguard/tun"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultTunName = "wintun"
|
defaultTunName = "tungo"
|
||||||
readOffset = 0
|
readOffset = 0
|
||||||
writeOffset = 0
|
writeOffset = 0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
tun.WintunTunnelType = "GOST"
|
||||||
|
}
|
||||||
|
|
||||||
func (l *tunListener) createTun() (ifce io.ReadWriteCloser, name string, ip net.IP, err error) {
|
func (l *tunListener) createTun() (ifce io.ReadWriteCloser, name string, ip net.IP, err error) {
|
||||||
if l.md.config.Name == "" {
|
if l.md.config.Name == "" {
|
||||||
l.md.config.Name = defaultTunName
|
l.md.config.Name = defaultTunName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if l.md.guid != "" {
|
||||||
|
var guid windows.GUID
|
||||||
|
guid, err = windows.GUIDFromString(l.md.guid)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tun.WintunStaticRequestedGUID = &guid
|
||||||
|
}
|
||||||
|
|
||||||
ifce, name, err = l.createTunDevice()
|
ifce, name, err = l.createTunDevice()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if l.md.config.MTU > 0 {
|
||||||
|
cmd := fmt.Sprintf("netsh interface ip set subinterface %s mtu=%d", name, l.md.config.MTU)
|
||||||
|
l.log.Debug(cmd)
|
||||||
|
|
||||||
|
args := strings.Split(cmd, " ")
|
||||||
|
if er := exec.Command(args[0], args[1:]...).Run(); er != nil {
|
||||||
|
err = fmt.Errorf("%s: %v", cmd, er)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(l.md.config.Net) > 0 {
|
if len(l.md.config.Net) > 0 {
|
||||||
ipNet := l.md.config.Net[0]
|
ipNet := l.md.config.Net[0]
|
||||||
cmd := fmt.Sprintf("netsh interface ip set address name=%s "+
|
cmd := fmt.Sprintf("netsh interface ip set address name=%s "+
|
||||||
|
|||||||
Reference in New Issue
Block a user