Skip to content

Commit c70f1bf

Browse files
committed
Fix: Support for IPv6 ( issue #2 ) and backup IPv4 DNS, ability to override DNS option inside client configs
1 parent 4405b2a commit c70f1bf

File tree

7 files changed

+67
-29
lines changed

7 files changed

+67
-29
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ client configuration files.
9393

9494
`ClientRoute` - AllowedIPs for client config
9595

96-
`ClientDNS` - DNS for client config
96+
`ClientDNS` - DNS configuration value for all clients
9797

9898
`ClientServerEndpoint` - Endpoint for client config
9999

@@ -105,7 +105,10 @@ client configuration files.
105105

106106
Contains `ClientRoute` that overrides the one from server config.
107107

108-
`MTU` - Override server MTU with a different value for this client
108+
`MTU` - Override server MTU with a different value for this client. Set to -1 to omit MTU from this WireGuard client config.
109+
110+
`DNS` - Override server `ClientDNS` setting for all clients. Specify a comma separated IP list.
111+
Set to `no` or `none` to omit DNS from this WireGuard client config.
109112

110113
Client configuration files contain `PrivateKey` field.
111114
If you find it unacceptable, you can remove it from the file after you exported

backend/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Client struct {
2121
PrivateKey string
2222
ClientRoute string
2323
MTU int
24+
DNS string
2425
}
2526

2627
func ReadClient(dir string, fileName string, ipNum int, name string) (*Client, error) {

backend/generator.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ func (c *Client) generateClientConfig(server *Server, w io.Writer) error {
4444
_, _ = fmt.Fprintln(w, "PrivateKey =", c.PrivateKey)
4545
_, _ = fmt.Fprintln(w, "Address =", c.AllowedIps(server))
4646

47-
if server.ClientDNS != "" {
48-
_, _ = fmt.Fprintln(w, "DNS =", server.ClientDNS)
47+
if c.DNS != "no" && c.DNS != "none" {
48+
if c.DNS == "" && server.ClientDNS != "" {
49+
_, _ = fmt.Fprintln(w, "DNS =", server.ClientDNS)
50+
} else if c.DNS != "" {
51+
_, _ = fmt.Fprintln(w, "DNS =", c.DNS)
52+
}
4953
}
5054

5155
_, _ = fmt.Fprintln(w, "\n[Peer]")
@@ -65,9 +69,9 @@ func (c *Client) generateClientConfig(server *Server, w io.Writer) error {
6569
_, _ = fmt.Fprintln(w, "PersistentKeepalive =", server.ClientPersistentKeepalive)
6670
}
6771

68-
if c.MTU != 0 {
72+
if c.MTU > 0 {
6973
_, _ = fmt.Fprintln(w, "MTU =", c.MTU)
70-
} else if server.MTU != 0 {
74+
} else if server.MTU != 0 && c.MTU == 0 {
7175
_, _ = fmt.Fprintln(w, "MTU =", server.MTU)
7276
}
7377

backend/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type ServerBlueprint struct {
2424
Nat6 bool
2525
Net4 string
2626
Net6 string
27-
DNS string
27+
DNS []string
2828
}
2929

3030
type Server struct {
@@ -108,7 +108,7 @@ func NewServerWithBlueprint(b ServerBlueprint) *Server {
108108
}
109109
}
110110

111-
s.ClientDNS = b.DNS
111+
s.ClientDNS = dnsString(b.DNS, b.Net6 != "" && b.Nat6)
112112
s.ClientServerEndpoint = b.Endpoint
113113
s.ClientPersistentKeepalive = 42
114114
return &s

backend/utils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,15 @@ func concatIfNotEmpty(str string, add string) string {
6161
}
6262
return str
6363
}
64+
65+
func dnsString(dns []string, ipv6 bool) string {
66+
var dnsStr string
67+
for _, d := range dns {
68+
if strings.Contains(d, ":") && !ipv6 {
69+
continue
70+
}
71+
dnsStr = concatIfNotEmpty(dnsStr, ", ")
72+
dnsStr += d
73+
}
74+
return dnsStr
75+
}

wizard/dns.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package wizard
2+
3+
var dnsDict = map[string][]string{
4+
"google": {"2001:4860:4860::8888", "2001:4860:4860::8844", "8.8.8.8", "8.8.4.4"},
5+
"cloudflare": {"2606:4700:4700::1111", "2606:4700:4700::1001", "1.1.1.1", "1.0.0.1"},
6+
"quad9": {"2620:fe::fe", "2620:fe::9", "9.9.9.9", "149.112.112.112"},
7+
"opendns": {"2620:119:35::35", "2620:119:53::53", "208.67.222.222", "208.67.220.220"},
8+
}

wizard/wizard.go

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,22 @@ func (m RootModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5050
m.sSize = msg
5151
}
5252

53+
if do, m, c := m.WizardStateUpdate(msg); do {
54+
return m, c
55+
}
56+
57+
if m.currentModel != nil {
58+
w, c := m.currentModel.Update(msg)
59+
m.currentModel = w
60+
return m, c
61+
}
62+
return m, nil
63+
}
64+
65+
func (m RootModel) WizardStateUpdate(msg tea.Msg) (bool, tea.Model, tea.Cmd) {
5366
if _, ok := msg.(welcomeScreenResult); ok {
5467
m.currentModel = newInterfaceScreen(m.app, m.sSize)
55-
return m, m.currentModel.Init()
68+
return true, m, m.currentModel.Init()
5669
}
5770

5871
if msg, ok := msg.(interfaceScreenResult); ok {
@@ -61,28 +74,29 @@ func (m RootModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
6174

6275
portStep := newPortScreen(m.sSize)
6376
m.currentModel = portStep
64-
return m, portStep.Init()
77+
return true, m, portStep.Init()
6578
}
6679

6780
if msg, ok := msg.(portStepResult); ok {
6881
m.blueprint.Port = uint16(msg)
6982

7083
endStep := newEndpointStep(m.sSize)
7184
m.currentModel = endStep
72-
return m, endStep.Init()
85+
return true, m, endStep.Init()
7386
}
7487

7588
if msg, ok := msg.(endpointStepResult); ok {
7689
m.blueprint.Endpoint = string(msg)
7790
netStep := newNetScreen(m.sSize)
7891
m.currentModel = netStep
79-
return m, netStep.Init()
92+
return true, m, netStep.Init()
8093
}
8194

8295
if msg, ok := msg.(netStepResult); ok {
8396
m.blueprint.Net4 = msg.net4
8497
m.blueprint.Net6 = msg.net6
85-
return m.presentNatDialog()
98+
m, c := m.presentNatDialog()
99+
return true, m, c
86100
}
87101

88102
if msg, ok := msg.(optScreenResult); ok {
@@ -99,14 +113,15 @@ func (m RootModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
99113
m.blueprint.Nat6 = false
100114
}
101115

102-
return m.presentDNSDialog()
116+
m, c := m.presentDNSDialog()
117+
return true, m, c
103118
}
104119

105120
if msg.id == optIdDNS {
106-
m.blueprint.DNS = msg.result.id
121+
m.blueprint.DNS = dnsDict[msg.result.id]
107122
doneStep := newDoneScreen(m.app, m.sSize, m.blueprint)
108123
m.currentModel = doneStep
109-
return m, doneStep.Init()
124+
return true, m, doneStep.Init()
110125
}
111126

112127
}
@@ -118,27 +133,22 @@ func (m RootModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
118133
// extra step to check NAT and setup systemd
119134
rootLinux := newLinuxMoreScreen(m.app, m.sSize, m.blueprint)
120135
m.currentModel = rootLinux
121-
return m, rootLinux.Init()
136+
return true, m, rootLinux.Init()
122137
}
123138

124-
return m, func() tea.Msg {
139+
return true, m, func() tea.Msg {
125140
return Done{InterfaceName: m.blueprint.InterfaceName}
126141
}
127142
}
128143

129144
if _, ok := msg.(linuxMoreDone); ok {
130145
m.currentModel = nil
131-
return m, func() tea.Msg {
146+
return true, m, func() tea.Msg {
132147
return Done{InterfaceName: m.blueprint.InterfaceName}
133148
}
134149
}
135150

136-
if m.currentModel != nil {
137-
w, c := m.currentModel.Update(msg)
138-
m.currentModel = w
139-
return m, c
140-
}
141-
return m, nil
151+
return false, nil, nil
142152
}
143153

144154
func (m RootModel) View() string {
@@ -168,10 +178,10 @@ func (m RootModel) presentNatDialog() (tea.Model, tea.Cmd) {
168178

169179
func (m RootModel) presentDNSDialog() (tea.Model, tea.Cmd) {
170180
opts := []opt{
171-
{"1.1.1.1", "Use Cloudflare DNS https://1.1.1.1"},
172-
{"8.8.8.8", "Use Google DNS https://developers.google.com/speed/public-dns"},
173-
{"9.9.9.9", "Use Quad9 DNS https://www.quad9.net/"},
174-
{"208.67.222.222", "Use OpenDNS https://use.opendns.com/"},
181+
{"cloudflare", "Use Cloudflare DNS https://1.1.1.1"},
182+
{"google", "Use Google DNS https://developers.google.com/speed/public-dns"},
183+
{"quad9", "Use Quad9 DNS https://www.quad9.net/"},
184+
{"opendns", "Use OpenDNS https://use.opendns.com/"},
175185
}
176186

177187
// shuffle options, so no service will get default treatment (on avarage)

0 commit comments

Comments
 (0)