Skip to content

Commit 9ea92e9

Browse files
authored
Refactor(engine): parse multicast groups (#518)
1 parent b9aee25 commit 9ea92e9

File tree

5 files changed

+16
-21
lines changed

5 files changed

+16
-21
lines changed

engine/engine.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package engine
33
import (
44
"errors"
55
"net"
6-
"net/netip"
76
"os/exec"
87
"sync"
98
"time"
@@ -190,17 +189,17 @@ func netstack(k *Key) (err error) {
190189
}
191190
}()
192191

193-
if _defaultProxy, err = parseProxy(k.Proxy); err != nil {
192+
multicastGroups, err := parseMulticastGroups(k.MulticastGroups)
193+
if err != nil {
194194
return err
195195
}
196-
tunnel.T().SetProxy(_defaultProxy)
197196

198-
if _defaultDevice, err = parseDevice(k.Device, uint32(k.MTU)); err != nil {
197+
if _defaultProxy, err = parseProxy(k.Proxy); err != nil {
199198
return err
200199
}
200+
tunnel.T().SetProxy(_defaultProxy)
201201

202-
var multicastGroups []netip.Addr
203-
if multicastGroups, err = parseMulticastGroups(k.MulticastGroups); err != nil {
202+
if _defaultDevice, err = parseDevice(k.Device, uint32(k.MTU)); err != nil {
204203
return err
205204
}
206205

engine/key.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type Key struct {
1313
TCPModerateReceiveBuffer bool `yaml:"tcp-moderate-receive-buffer"`
1414
TCPSendBufferSize string `yaml:"tcp-send-buffer-size"`
1515
TCPReceiveBufferSize string `yaml:"tcp-receive-buffer-size"`
16-
MulticastGroups string `yaml:"multicast-groups"`
16+
MulticastGroups []string `yaml:"multicast-groups"`
1717
TUNPreUp string `yaml:"tun-pre-up"`
1818
TUNPostUp string `yaml:"tun-post-up"`
1919
UDPTimeout time.Duration `yaml:"udp-timeout"`

engine/parse.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ import (
1414
"github.com/xjasonlyu/tun2socks/v2/proxy"
1515
)
1616

17-
const (
18-
defaultDeviceType = "tun"
19-
defaultProxyType = "socks5"
20-
)
21-
2217
func parseRestAPI(s string) (*url.URL, error) {
2318
if !strings.Contains(s, "://") {
2419
s = fmt.Sprintf("%s://%s", "http", s)
@@ -48,7 +43,7 @@ func parseRestAPI(s string) (*url.URL, error) {
4843

4944
func parseDevice(s string, mtu uint32) (device.Device, error) {
5045
if !strings.Contains(s, "://") {
51-
s = fmt.Sprintf("%s://%s", defaultDeviceType, s)
46+
s = fmt.Sprintf("%s://%s", tun.Driver, s)
5247
}
5348

5449
u, err := url.Parse(s)
@@ -80,7 +75,7 @@ func parseFD(u *url.URL, mtu uint32) (device.Device, error) {
8075

8176
func parseProxy(s string) (proxy.Proxy, error) {
8277
if !strings.Contains(s, "://") {
83-
s = fmt.Sprintf("%s://%s", defaultProxyType, s)
78+
s = fmt.Sprintf("%s://%s", "socks5" /* default */, s)
8479
}
8580

8681
u, err := url.Parse(s)
@@ -90,8 +85,9 @@ func parseProxy(s string) (proxy.Proxy, error) {
9085
return proxy.Parse(u)
9186
}
9287

93-
func parseMulticastGroups(s string) (multicastGroups []netip.Addr, _ error) {
94-
for _, ip := range strings.Split(s, ",") {
88+
func parseMulticastGroups(v []string) ([]netip.Addr, error) {
89+
groups := make([]netip.Addr, 0, len(v))
90+
for _, ip := range v {
9591
if ip = strings.TrimSpace(ip); ip == "" {
9692
continue
9793
}
@@ -102,7 +98,7 @@ func parseMulticastGroups(s string) (multicastGroups []netip.Addr, _ error) {
10298
if !addr.IsMulticast() {
10399
return nil, fmt.Errorf("invalid multicast IP: %s", addr)
104100
}
105-
multicastGroups = append(multicastGroups, addr)
101+
groups = append(groups, addr)
106102
}
107-
return
103+
return groups, nil
108104
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func init() {
3535
flag.StringVar(&key.TCPSendBufferSize, "tcp-sndbuf", "", "Set TCP send buffer size for netstack")
3636
flag.StringVar(&key.TCPReceiveBufferSize, "tcp-rcvbuf", "", "Set TCP receive buffer size for netstack")
3737
flag.BoolVar(&key.TCPModerateReceiveBuffer, "tcp-auto-tuning", false, "Enable TCP receive buffer auto-tuning")
38-
flag.StringVar(&key.MulticastGroups, "multicast-groups", "", "Set multicast groups, separated by commas")
38+
flag.StringSliceVar(&key.MulticastGroups, "multicast-groups", nil, "Set multicast groups, separated by commas")
3939
flag.StringVar(&key.TUNPreUp, "tun-pre-up", "", "Execute a command before TUN device setup")
4040
flag.StringVar(&key.TUNPostUp, "tun-post-up", "", "Execute a command after TUN device setup")
4141
flag.BoolVarP(&versionFlag, "version", "v", false, "Show version and then quit")

tunnel/tunnel.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ func (t *Tunnel) Close() {
100100

101101
func (t *Tunnel) Proxy() proxy.Proxy {
102102
t.proxyMu.RLock()
103-
d := t.proxy
103+
p := t.proxy
104104
t.proxyMu.RUnlock()
105-
return d
105+
return p
106106
}
107107

108108
func (t *Tunnel) SetProxy(proxy proxy.Proxy) {

0 commit comments

Comments
 (0)