-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add --dns-map-exec flag to automatically reconfigure dns server
- Loading branch information
1 parent
fb22b5b
commit 44528f1
Showing
7 changed files
with
139 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package dns | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/miekg/dns" | ||
) | ||
|
||
type DomainsFunc func() (map[string]IPResolver, error) | ||
|
||
type DomainsMux struct { | ||
mux *dns.ServeMux | ||
domainsFunc DomainsFunc | ||
prevDomains map[string]struct{} | ||
|
||
logTag string | ||
logger Logger | ||
} | ||
|
||
var _ dns.Handler = &DomainsMux{} | ||
|
||
func NewDomainsMux(mux *dns.ServeMux, domainsFunc DomainsFunc, logger Logger) *DomainsMux { | ||
return &DomainsMux{mux, domainsFunc, map[string]struct{}{}, "dns.DomainsMux", logger} | ||
} | ||
|
||
func (m *DomainsMux) ServeDNS(w dns.ResponseWriter, r *dns.Msg) { | ||
m.mux.ServeDNS(w, r) | ||
} | ||
|
||
func (m *DomainsMux) UpdateDomainsContiniously() { | ||
for { | ||
err := m.updateDomainsOnce() | ||
if err != nil { | ||
m.logger.Debug(m.logTag, "Failed updating DNS domain handlers: %s", err) | ||
} | ||
time.Sleep(30 * time.Second) | ||
} | ||
} | ||
|
||
func (m *DomainsMux) updateDomainsOnce() error { | ||
domains, err := m.domainsFunc() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for domain, resolver := range domains { | ||
delete(m.prevDomains, domain) | ||
m.mux.Handle(domain, NewCustomHandler(resolver, m.logger)) | ||
} | ||
|
||
// Delete previously registered handlers that were not replaced | ||
for domain, _ := range m.prevDomains { | ||
m.mux.HandleRemove(domain) | ||
} | ||
|
||
m.prevDomains = map[string]struct{}{} | ||
|
||
// Record what was registered again | ||
for domain, _ := range domains { | ||
m.prevDomains[domain] = struct{}{} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package dns | ||
|
||
import ( | ||
"net" | ||
) | ||
|
||
type StaticIPsResolver struct { | ||
ips []net.IP | ||
} | ||
|
||
var _ IPResolver = StaticIPsResolver{} | ||
|
||
func NewStaticIPsResolver(ips []net.IP) StaticIPsResolver { | ||
return StaticIPsResolver{ips} | ||
} | ||
|
||
func (r StaticIPsResolver) ResolveIPv4(question string) ([]net.IP, bool, error) { | ||
return r.ips, true, nil | ||
} | ||
|
||
func (r StaticIPsResolver) ResolveIPv6(question string) ([]net.IP, bool, error) { | ||
return nil, true, nil | ||
} |