Skip to content

Commit 4e8ab0e

Browse files
committed
add global nameservers in case of internet gw
1 parent b55d512 commit 4e8ab0e

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

controllers/dns.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ func dnsHandlers(r *mux.Router) {
4444
r.HandleFunc("/api/v1/nameserver", logic.SecurityCheck(true, http.HandlerFunc(listNs))).Methods(http.MethodGet)
4545
r.HandleFunc("/api/v1/nameserver", logic.SecurityCheck(true, http.HandlerFunc(updateNs))).Methods(http.MethodPut)
4646
r.HandleFunc("/api/v1/nameserver", logic.SecurityCheck(true, http.HandlerFunc(deleteNs))).Methods(http.MethodDelete)
47+
r.HandleFunc("/api/v1/nameserver/global", logic.SecurityCheck(true, http.HandlerFunc(getGlobalNs))).Methods(http.MethodGet)
48+
}
49+
50+
// @Summary List Global Nameservers
51+
// @Router /api/v1/nameserver/global [get]
52+
// @Tags Auth
53+
// @Accept json
54+
// @Param query network string
55+
// @Success 200 {object} models.SuccessResponse
56+
// @Failure 400 {object} models.ErrorResponse
57+
// @Failure 401 {object} models.ErrorResponse
58+
// @Failure 500 {object} models.ErrorResponse
59+
func getGlobalNs(w http.ResponseWriter, r *http.Request) {
60+
61+
logic.ReturnSuccessResponseWithJson(w, r, logic.GlobalNsList, "fetched nameservers")
4762
}
4863

4964
// @Summary Create Nameserver
@@ -72,6 +87,9 @@ func createNs(w http.ResponseWriter, r *http.Request) {
7287
if req.Tags == nil {
7388
req.Tags = make(datatypes.JSONMap)
7489
}
90+
if gNs, ok := logic.GlobalNsList[req.Name]; ok {
91+
req.Servers = gNs.IPs
92+
}
7593
ns := schema.Nameserver{
7694
ID: uuid.New().String(),
7795
Name: req.Name,

logic/dns.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,41 @@ import (
1919
"github.com/txn2/txeh"
2020
)
2121

22+
type GlobalNs struct {
23+
ID string `json:"id"`
24+
IPs []string `json:"ips"`
25+
}
26+
27+
var GlobalNsList = map[string]GlobalNs{
28+
"Google": {
29+
ID: "Google",
30+
IPs: []string{
31+
"8.8.8.8",
32+
"8.8.4.4",
33+
"2001:4860:4860::8888",
34+
"2001:4860:4860::8844",
35+
},
36+
},
37+
"Cloudflare": {
38+
ID: "Cloudflare",
39+
IPs: []string{
40+
"1.1.1.1",
41+
"1.0.0.1",
42+
"2606:4700:4700::1111",
43+
"2606:4700:4700::1001",
44+
},
45+
},
46+
"Quad9": {
47+
ID: "Quad9",
48+
IPs: []string{
49+
"9.9.9.9",
50+
"149.112.112.112",
51+
"2620:fe::fe",
52+
"2620:fe::9",
53+
},
54+
},
55+
}
56+
2257
// SetDNS - sets the dns on file
2358
func SetDNS() error {
2459
hostfile, err := txeh.NewHosts(&txeh.HostsConfig{})
@@ -393,6 +428,15 @@ func GetNameserversForHost(h *models.Host) (returnNsLi []models.Nameserver) {
393428
}
394429
}
395430
}
431+
if node.IsInternetGateway {
432+
globalNs := models.Nameserver{
433+
MatchDomain: ".",
434+
}
435+
for _, nsI := range GlobalNsList {
436+
globalNs.IPs = append(globalNs.IPs, nsI.IPs...)
437+
}
438+
returnNsLi = append(returnNsLi, globalNs)
439+
}
396440
}
397441
return
398442
}

models/structs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type UserRemoteGws struct {
4646
Status NodeStatus `json:"status"`
4747
DnsAddress string `json:"dns_address"`
4848
Addresses string `json:"addresses"`
49+
MatchDomains []string `json:"match_domains"`
4950
}
5051

5152
// UserRAGs - struct for user access gws

pro/controllers/users.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ func getUserRemoteAccessGwsV1(w http.ResponseWriter, r *http.Request) {
13111311
logic.GetPeerListenPort(host),
13121312
)
13131313
extClient.AllowedIPs = logic.GetExtclientAllowedIPs(extClient)
1314-
gws = append(gws, models.UserRemoteGws{
1314+
gw := models.UserRemoteGws{
13151315
GwID: node.ID.String(),
13161316
GWName: host.Name,
13171317
Network: node.Network,
@@ -1326,7 +1326,14 @@ func getUserRemoteAccessGwsV1(w http.ResponseWriter, r *http.Request) {
13261326
Status: node.Status,
13271327
DnsAddress: node.IngressDNS,
13281328
Addresses: utils.NoEmptyStringToCsv(node.Address.String(), node.Address6.String()),
1329-
})
1329+
}
1330+
if !node.IsInternetGateway {
1331+
hNs := logic.GetNameserversForHost(host)
1332+
for _, nsI := range hNs {
1333+
gw.MatchDomains = append(gw.MatchDomains, nsI.MatchDomain)
1334+
}
1335+
}
1336+
gws = append(gws, gw)
13301337
userGws[node.Network] = gws
13311338
delete(userGwNodes, node.ID.String())
13321339
}
@@ -1357,7 +1364,7 @@ func getUserRemoteAccessGwsV1(w http.ResponseWriter, r *http.Request) {
13571364
}
13581365
gws := userGws[node.Network]
13591366

1360-
gws = append(gws, models.UserRemoteGws{
1367+
gw := models.UserRemoteGws{
13611368
GwID: node.ID.String(),
13621369
GWName: host.Name,
13631370
Network: node.Network,
@@ -1370,7 +1377,14 @@ func getUserRemoteAccessGwsV1(w http.ResponseWriter, r *http.Request) {
13701377
Status: node.Status,
13711378
DnsAddress: node.IngressDNS,
13721379
Addresses: utils.NoEmptyStringToCsv(node.Address.String(), node.Address6.String()),
1373-
})
1380+
}
1381+
if !node.IsInternetGateway {
1382+
hNs := logic.GetNameserversForHost(host)
1383+
for _, nsI := range hNs {
1384+
gw.MatchDomains = append(gw.MatchDomains, nsI.MatchDomain)
1385+
}
1386+
}
1387+
gws = append(gws, gw)
13741388
userGws[node.Network] = gws
13751389
}
13761390

0 commit comments

Comments
 (0)