Skip to content

Commit db23090

Browse files
authored
Merge pull request #227 from NLipatov/feature/54
macOS DNS configuration
2 parents d171bb7 + d1e2110 commit db23090

File tree

25 files changed

+435
-69
lines changed

25 files changed

+435
-69
lines changed

docs/TunGo/docs/1. QuickStart.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ See <a href="/docs/Advanced/Linux/Setup server systemd unit">this guide</a> to r
6565
<TabItem value="macos-arm64" label="macOS Apple Silicon">
6666
```bash
6767
curl -L -o tungo https://github.com/NLipatov/TunGo/releases/latest/download/tungo-darwin-arm64
68-
chmod +x tungo
6968
sudo mkdir -p /usr/local/bin
69+
chmod +x tungo
7070
sudo mv tungo /usr/local/bin/
7171
```
7272
</TabItem>

src/infrastructure/PAL/darwin/network_tools/ifconfig/factory.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
package ifconfig
44

5-
import "tungo/infrastructure/PAL"
5+
import (
6+
"tungo/infrastructure/PAL/exec_commander"
7+
)
68

79
type Factory struct {
8-
commander PAL.Commander
10+
commander exec_commander.Commander
911
}
1012

11-
func NewFactory(commander PAL.Commander) *Factory {
13+
func NewFactory(commander exec_commander.Commander) *Factory {
1214
return &Factory{commander: commander}
1315
}
1416

src/infrastructure/PAL/darwin/network_tools/ifconfig/v4.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import (
77
"net"
88
"strconv"
99
"strings"
10-
11-
"tungo/infrastructure/PAL"
10+
"tungo/infrastructure/PAL/exec_commander"
1211
)
1312

1413
type v4 struct {
15-
commander PAL.Commander
14+
commander exec_commander.Commander
1615
}
1716

18-
func newV4(commander PAL.Commander) Contract {
17+
func newV4(commander exec_commander.Commander) Contract {
1918
return &v4{commander: commander}
2019
}
2120

src/infrastructure/PAL/darwin/network_tools/ifconfig/v6.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import (
77
"net"
88
"strconv"
99
"strings"
10-
11-
"tungo/infrastructure/PAL"
10+
"tungo/infrastructure/PAL/exec_commander"
1211
)
1312

1413
type v6 struct {
15-
commander PAL.Commander
14+
commander exec_commander.Commander
1615
}
1716

18-
func newV6(commander PAL.Commander) Contract {
17+
func newV6(commander exec_commander.Commander) Contract {
1918
return &v6{commander: commander}
2019
}
2120

src/infrastructure/PAL/darwin/network_tools/manager/factory.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66
"fmt"
77
"net"
88
"strings"
9+
"tungo/infrastructure/PAL/darwin/network_tools/scutil"
10+
"tungo/infrastructure/PAL/exec_commander"
911

1012
"tungo/application/network/routing/tun"
11-
"tungo/infrastructure/PAL"
1213
ifcfg "tungo/infrastructure/PAL/darwin/network_tools/ifconfig"
1314
rtpkg "tungo/infrastructure/PAL/darwin/network_tools/route"
1415
"tungo/infrastructure/settings"
@@ -19,14 +20,16 @@ type Factory struct {
1920
s settings.Settings
2021
ifcFactory *ifcfg.Factory
2122
rtFactory *rtpkg.Factory
23+
scFactory *scutil.Factory
2224
}
2325

2426
func NewFactory(s settings.Settings) *Factory {
25-
cmd := PAL.NewExecCommander()
27+
cmd := exec_commander.NewExecCommander()
2628
return &Factory{
2729
s: s,
2830
ifcFactory: ifcfg.NewFactory(cmd),
2931
rtFactory: rtpkg.NewFactory(cmd),
32+
scFactory: scutil.NewFactory(),
3033
}
3134
}
3235

@@ -56,12 +59,14 @@ func (f *Factory) Create() (tun.ClientManager, error) {
5659
f.s,
5760
f.ifcFactory.NewV4(),
5861
f.rtFactory.NewV4(),
62+
f.scFactory.NewV4(),
5963
), nil
6064
}
6165
return newV6(
6266
f.s,
6367
f.ifcFactory.NewV6(),
6468
f.rtFactory.NewV6(),
69+
f.scFactory.NewV6(),
6570
), nil
6671
}
6772

src/infrastructure/PAL/darwin/network_tools/manager/v4.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"net"
88
"strings"
9+
"tungo/infrastructure/PAL/darwin/network_tools/scutil"
910

1011
"tungo/application/network/routing/tun"
1112
"tungo/infrastructure/PAL/darwin/network_tools/ifconfig"
@@ -15,20 +16,26 @@ import (
1516
)
1617

1718
type v4 struct {
18-
s settings.Settings
19-
tunDev tun.Device
20-
rawUTUN utun.UTUN
21-
ifc ifconfig.Contract // v4 ifconfig.Contract implementation
22-
rtc route.Contract // v4 route.Contract implementation
23-
ifName string
24-
addedSplit bool
19+
s settings.Settings
20+
tunDev tun.Device
21+
rawUTUN utun.UTUN
22+
ifc ifconfig.Contract // v4 ifconfig.Contract implementation
23+
rtc route.Contract // v4 route.Contract implementation
24+
scc scutil.Contract // v4 scutil.Contract implementation
25+
ifName string
2526
}
2627

27-
func newV4(s settings.Settings, ifc ifconfig.Contract, rt route.Contract) *v4 {
28+
func newV4(
29+
s settings.Settings,
30+
ifc ifconfig.Contract,
31+
rt route.Contract,
32+
scc scutil.Contract,
33+
) *v4 {
2834
return &v4{
2935
s: s,
3036
ifc: ifc,
3137
rtc: rt,
38+
scc: scc,
3239
}
3340
}
3441

@@ -60,12 +67,25 @@ func (m *v4) CreateDevice() (tun.Device, error) {
6067
_ = m.DisposeDevices()
6168
return nil, fmt.Errorf("add v4 split default: %w", addErr)
6269
}
63-
m.addedSplit = true
70+
71+
// Configure DNS after interface and route are all set
72+
if scopedDNSResolverErr := m.scc.AddScopedDNSResolver(
73+
m.ifName,
74+
// ToDo: move dns servers to configurations
75+
[]string{"1.1.1.1", "8.8.8.8"},
76+
nil); scopedDNSResolverErr != nil {
77+
_ = m.DisposeDevices()
78+
return nil, fmt.Errorf("add v4 scoped dns resolver: %w", scopedDNSResolverErr)
79+
}
80+
6481
m.tunDev = utun.NewDarwinTunDevice(raw)
6582
return m.tunDev, nil
6683
}
6784

6885
func (m *v4) DisposeDevices() error {
86+
if m.ifName != "" {
87+
_ = m.scc.RemoveScopedDNSResolver(m.ifName)
88+
}
6989
_ = m.rtc.DelSplit(m.ifName)
7090
if m.s.ConnectionIP != "" {
7191
_ = m.rtc.Del(m.s.ConnectionIP)
@@ -76,7 +96,6 @@ func (m *v4) DisposeDevices() error {
7696
}
7797
m.rawUTUN = nil
7898
m.ifName = ""
79-
m.addedSplit = false
8099
return nil
81100
}
82101

src/infrastructure/PAL/darwin/network_tools/manager/v6.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"net"
88
"strings"
9+
"tungo/infrastructure/PAL/darwin/network_tools/scutil"
910

1011
"tungo/application/network/routing/tun"
1112
"tungo/infrastructure/PAL/darwin/network_tools/ifconfig"
@@ -15,20 +16,26 @@ import (
1516
)
1617

1718
type v6 struct {
18-
s settings.Settings
19-
tunDev tun.Device
20-
rawUTUN utun.UTUN
21-
ifc ifconfig.Contract // v6 ifconfig.Contract implementation
22-
rt route.Contract // v6 route.Contract implementation
23-
ifName string
24-
addedSplit bool
19+
s settings.Settings
20+
tunDev tun.Device
21+
rawUTUN utun.UTUN
22+
ifc ifconfig.Contract // v6 ifconfig.Contract implementation
23+
rt route.Contract // v6 route.Contract implementation
24+
scc scutil.Contract // v6 scutil.Contract implementation
25+
ifName string
2526
}
2627

27-
func newV6(s settings.Settings, ifc ifconfig.Contract, rt route.Contract) *v6 {
28+
func newV6(
29+
s settings.Settings,
30+
ifc ifconfig.Contract,
31+
rt route.Contract,
32+
scc scutil.Contract,
33+
) *v6 {
2834
return &v6{
2935
s: s,
3036
ifc: ifc,
3137
rt: rt,
38+
scc: scc,
3239
}
3340
}
3441

@@ -63,13 +70,25 @@ func (m *v6) CreateDevice() (tun.Device, error) {
6370
_ = m.DisposeDevices()
6471
return nil, fmt.Errorf("add v6 split default: %w", err)
6572
}
66-
m.addedSplit = true
73+
74+
// Configure DNS after interface and route are all set
75+
if scopedDNSResolverErr := m.scc.AddScopedDNSResolver(
76+
m.ifName,
77+
// ToDo: move dns servers to configurations
78+
[]string{"2606:4700:4700::1111", "2001:4860:4860::8888"},
79+
nil); scopedDNSResolverErr != nil {
80+
_ = m.DisposeDevices()
81+
return nil, fmt.Errorf("add v4 scoped dns resolver: %w", scopedDNSResolverErr)
82+
}
6783

6884
m.tunDev = utun.NewDarwinTunDevice(raw)
6985
return m.tunDev, nil
7086
}
7187

7288
func (m *v6) DisposeDevices() error {
89+
if m.ifName != "" {
90+
_ = m.scc.RemoveScopedDNSResolver(m.ifName)
91+
}
7392
_ = m.rt.DelSplit(m.ifName)
7493
if m.s.ConnectionIP != "" {
7594
_ = m.rt.Del(m.s.ConnectionIP)
@@ -80,7 +99,6 @@ func (m *v6) DisposeDevices() error {
8099
}
81100
m.rawUTUN = nil
82101
m.ifName = ""
83-
m.addedSplit = false
84102
return nil
85103
}
86104

src/infrastructure/PAL/darwin/network_tools/route/factory.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
package route
44

5-
import "tungo/infrastructure/PAL"
5+
import (
6+
"tungo/infrastructure/PAL/exec_commander"
7+
)
68

79
type Factory struct {
8-
commander PAL.Commander
10+
commander exec_commander.Commander
911
}
1012

11-
func NewFactory(commander PAL.Commander) *Factory {
13+
func NewFactory(commander exec_commander.Commander) *Factory {
1214
return &Factory{
1315
commander: commander,
1416
}

src/infrastructure/PAL/darwin/network_tools/route/v4.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ package route
55
import (
66
"bytes"
77
"fmt"
8-
"golang.org/x/sync/errgroup"
98
"net/netip"
109
"strings"
11-
"tungo/infrastructure/PAL"
10+
"tungo/infrastructure/PAL/exec_commander"
11+
12+
"golang.org/x/sync/errgroup"
1213
)
1314

1415
const (
@@ -23,10 +24,10 @@ const (
2324
)
2425

2526
type v4 struct {
26-
commander PAL.Commander
27+
commander exec_commander.Commander
2728
}
2829

29-
func newV4(commander PAL.Commander) Contract {
30+
func newV4(commander exec_commander.Commander) Contract {
3031
return &v4{
3132
commander: commander,
3233
}

src/infrastructure/PAL/darwin/network_tools/route/v6.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"fmt"
88
"net/netip"
99
"strings"
10+
"tungo/infrastructure/PAL/exec_commander"
1011

1112
"golang.org/x/sync/errgroup"
12-
"tungo/infrastructure/PAL"
1313
)
1414

1515
const (
@@ -25,10 +25,10 @@ const (
2525
)
2626

2727
type v6 struct {
28-
commander PAL.Commander
28+
commander exec_commander.Commander
2929
}
3030

31-
func newV6(commander PAL.Commander) Contract {
31+
func newV6(commander exec_commander.Commander) Contract {
3232
return &v6{commander: commander}
3333
}
3434

0 commit comments

Comments
 (0)