Adding --external-ip-map and --dns-resolver-address and shorthand flags (#652)

Adding --external-ip-map and --dns-resolver-address to up command and shorthand option to global flags.

Refactor get and read config functions with new ConfigInput type.

updated cobra package to latest release.
This commit is contained in:
Maycon Santos
2023-01-17 19:16:50 +01:00
committed by GitHub
parent 12ae2e93fc
commit dcf6533ed5
21 changed files with 613 additions and 558 deletions

View File

@@ -45,6 +45,7 @@ type DefaultServer struct {
runtimePort int
runtimeIP string
previousConfigHash uint64
customAddress *netip.AddrPort
}
type registrationMap map[string]struct{}
@@ -55,7 +56,7 @@ type muxUpdate struct {
}
// NewDefaultServer returns a new dns server
func NewDefaultServer(ctx context.Context, wgInterface *iface.WGIface) (*DefaultServer, error) {
func NewDefaultServer(ctx context.Context, wgInterface *iface.WGIface, customAddress string) (*DefaultServer, error) {
mux := dns.NewServeMux()
dnsServer := &dns.Server{
@@ -66,6 +67,16 @@ func NewDefaultServer(ctx context.Context, wgInterface *iface.WGIface) (*Default
ctx, stop := context.WithCancel(ctx)
var addrPort *netip.AddrPort
if customAddress != "" {
parsedAddrPort, err := netip.ParseAddrPort(customAddress)
if err != nil {
stop()
return nil, fmt.Errorf("unable to parse the custom dns address, got error: %s", err)
}
addrPort = &parsedAddrPort
}
defaultServer := &DefaultServer{
ctx: ctx,
stop: stop,
@@ -75,12 +86,14 @@ func NewDefaultServer(ctx context.Context, wgInterface *iface.WGIface) (*Default
localResolver: &localResolver{
registeredMap: make(registrationMap),
},
wgInterface: wgInterface,
runtimePort: defaultPort,
wgInterface: wgInterface,
runtimePort: defaultPort,
customAddress: addrPort,
}
hostmanager, err := newHostManager(wgInterface)
if err != nil {
stop()
return nil, err
}
defaultServer.hostManager = hostmanager
@@ -90,13 +103,19 @@ func NewDefaultServer(ctx context.Context, wgInterface *iface.WGIface) (*Default
// Start runs the listener in a go routine
func (s *DefaultServer) Start() {
ip, port, err := s.getFirstListenerAvailable()
if err != nil {
log.Error(err)
return
if s.customAddress != nil {
s.runtimeIP = s.customAddress.Addr().String()
s.runtimePort = int(s.customAddress.Port())
} else {
ip, port, err := s.getFirstListenerAvailable()
if err != nil {
log.Error(err)
return
}
s.runtimeIP = ip
s.runtimePort = port
}
s.runtimeIP = ip
s.runtimePort = port
s.server.Addr = fmt.Sprintf("%s:%d", s.runtimeIP, s.runtimePort)
log.Debugf("starting dns on %s", s.server.Addr)
@@ -105,7 +124,7 @@ func (s *DefaultServer) Start() {
s.setListenerStatus(true)
defer s.setListenerStatus(false)
err = s.server.ListenAndServe()
err := s.server.ListenAndServe()
if err != nil {
log.Errorf("dns server running with %d port returned an error: %v. Will not retry", s.runtimePort, err)
}