Refactor(engine): parse multicast groups (#518)

This commit is contained in:
Jason Lyu
2026-01-10 13:48:31 -05:00
committed by GitHub
parent b9aee255ab
commit 9ea92e91b2
5 changed files with 16 additions and 21 deletions
+5 -6
View File
@@ -3,7 +3,6 @@ package engine
import (
"errors"
"net"
"net/netip"
"os/exec"
"sync"
"time"
@@ -190,6 +189,11 @@ func netstack(k *Key) (err error) {
}
}()
multicastGroups, err := parseMulticastGroups(k.MulticastGroups)
if err != nil {
return err
}
if _defaultProxy, err = parseProxy(k.Proxy); err != nil {
return err
}
@@ -199,11 +203,6 @@ func netstack(k *Key) (err error) {
return err
}
var multicastGroups []netip.Addr
if multicastGroups, err = parseMulticastGroups(k.MulticastGroups); err != nil {
return err
}
var opts []option.Option
if k.TCPModerateReceiveBuffer {
opts = append(opts, option.WithTCPModerateReceiveBuffer(true))
+1 -1
View File
@@ -13,7 +13,7 @@ type Key struct {
TCPModerateReceiveBuffer bool `yaml:"tcp-moderate-receive-buffer"`
TCPSendBufferSize string `yaml:"tcp-send-buffer-size"`
TCPReceiveBufferSize string `yaml:"tcp-receive-buffer-size"`
MulticastGroups string `yaml:"multicast-groups"`
MulticastGroups []string `yaml:"multicast-groups"`
TUNPreUp string `yaml:"tun-pre-up"`
TUNPostUp string `yaml:"tun-post-up"`
UDPTimeout time.Duration `yaml:"udp-timeout"`
+7 -11
View File
@@ -14,11 +14,6 @@ import (
"github.com/xjasonlyu/tun2socks/v2/proxy"
)
const (
defaultDeviceType = "tun"
defaultProxyType = "socks5"
)
func parseRestAPI(s string) (*url.URL, error) {
if !strings.Contains(s, "://") {
s = fmt.Sprintf("%s://%s", "http", s)
@@ -48,7 +43,7 @@ func parseRestAPI(s string) (*url.URL, error) {
func parseDevice(s string, mtu uint32) (device.Device, error) {
if !strings.Contains(s, "://") {
s = fmt.Sprintf("%s://%s", defaultDeviceType, s)
s = fmt.Sprintf("%s://%s", tun.Driver, s)
}
u, err := url.Parse(s)
@@ -80,7 +75,7 @@ func parseFD(u *url.URL, mtu uint32) (device.Device, error) {
func parseProxy(s string) (proxy.Proxy, error) {
if !strings.Contains(s, "://") {
s = fmt.Sprintf("%s://%s", defaultProxyType, s)
s = fmt.Sprintf("%s://%s", "socks5" /* default */, s)
}
u, err := url.Parse(s)
@@ -90,8 +85,9 @@ func parseProxy(s string) (proxy.Proxy, error) {
return proxy.Parse(u)
}
func parseMulticastGroups(s string) (multicastGroups []netip.Addr, _ error) {
for _, ip := range strings.Split(s, ",") {
func parseMulticastGroups(v []string) ([]netip.Addr, error) {
groups := make([]netip.Addr, 0, len(v))
for _, ip := range v {
if ip = strings.TrimSpace(ip); ip == "" {
continue
}
@@ -102,7 +98,7 @@ func parseMulticastGroups(s string) (multicastGroups []netip.Addr, _ error) {
if !addr.IsMulticast() {
return nil, fmt.Errorf("invalid multicast IP: %s", addr)
}
multicastGroups = append(multicastGroups, addr)
groups = append(groups, addr)
}
return
return groups, nil
}
+1 -1
View File
@@ -35,7 +35,7 @@ func init() {
flag.StringVar(&key.TCPSendBufferSize, "tcp-sndbuf", "", "Set TCP send buffer size for netstack")
flag.StringVar(&key.TCPReceiveBufferSize, "tcp-rcvbuf", "", "Set TCP receive buffer size for netstack")
flag.BoolVar(&key.TCPModerateReceiveBuffer, "tcp-auto-tuning", false, "Enable TCP receive buffer auto-tuning")
flag.StringVar(&key.MulticastGroups, "multicast-groups", "", "Set multicast groups, separated by commas")
flag.StringSliceVar(&key.MulticastGroups, "multicast-groups", nil, "Set multicast groups, separated by commas")
flag.StringVar(&key.TUNPreUp, "tun-pre-up", "", "Execute a command before TUN device setup")
flag.StringVar(&key.TUNPostUp, "tun-post-up", "", "Execute a command after TUN device setup")
flag.BoolVarP(&versionFlag, "version", "v", false, "Show version and then quit")
+2 -2
View File
@@ -100,9 +100,9 @@ func (t *Tunnel) Close() {
func (t *Tunnel) Proxy() proxy.Proxy {
t.proxyMu.RLock()
d := t.proxy
p := t.proxy
t.proxyMu.RUnlock()
return d
return p
}
func (t *Tunnel) SetProxy(proxy proxy.Proxy) {