Skip to content

Commit

Permalink
Merge pull request #4 from d0m84/feature/quicker_reloads
Browse files Browse the repository at this point in the history
quicker config reloads
  • Loading branch information
d0m84 authored Jul 6, 2023
2 parents d6298bf + 74dc14c commit cd87aa8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 28 deletions.
2 changes: 1 addition & 1 deletion cmd/ip-monitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func main() {
cli_args := cli.Arguments()
config := cfg.LoadConfiguration(cli_args.ConfigPath)
sigs := make(chan os.Signal)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)

logger.Infoln("Starting IP-Monitor Daemon")
Expand Down
18 changes: 10 additions & 8 deletions internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,21 @@ var (
func Start(config cfg.Configuration) {
go func() {
for {
select {
case <-Exit:
state = make(map[int]net.IP)
return
default:
Orchestrate(config)
time.Sleep(time.Duration(config.Interval) * time.Second)
Run(config)
for i := 1; i <= config.Interval; i++ {
select {
case <-Exit:
state = make(map[int]net.IP)
return
default:
time.Sleep(1 * time.Second)
}
}
}
}()
}

func Orchestrate(config cfg.Configuration) {
func Run(config cfg.Configuration) {
var wg = &sync.WaitGroup{}
for i := range config.Monitors {
wg.Add(1)
Expand Down
22 changes: 13 additions & 9 deletions pkg/dns_resolver/dns_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"github.com/d0m84/ip-monitor/pkg/logger"
)

var (
timeout int = 10
)

func CheckIfCNAME(domain string) (string, bool, error) {
target, err := net.LookupCNAME(domain)
if err != nil {
Expand All @@ -29,24 +33,25 @@ func FindFinalTarget(domain string) (string, error) {
var err error
var target string = domain
var is_cname bool
for i := 0; i < 50; i++ {
for i := 0; i < 2; i++ {
target, is_cname, err = CheckIfCNAME(target)
if err != nil {
logger.Errorf("Error checking if %s is a CNAME: %s", target, err)
logger.Errorf("Error checking if %s is a CNAME: %s", domain, err)
return "", err
}
if !is_cname {
logger.Debugf("Final target for domain %s is %s", domain, target)
return target, nil
}
}
return "", errors.New("dns cname check limit reached")
logger.Errorf("Maximum CNAME lookup limit reached for %s", domain)
return "", errors.New("dns cname lookup limit reached")
}

func FindNameServers(domain string) ([]*net.NS, error) {
domainParts := strings.Split(domain, ".")
for i := range domainParts {
t := domainParts[i:len(domainParts):len(domainParts)]
domain_parts := strings.Split(domain, ".")
for i := range domain_parts {
t := domain_parts[i:len(domain_parts):len(domain_parts)]
d := strings.Join(t, ".")

nameservers, err := net.LookupNS(d)
Expand Down Expand Up @@ -83,7 +88,7 @@ func LookupAuthorative(domain string, ip_version string) ([]net.IP, error) {
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{
Timeout: time.Millisecond * time.Duration(10000),
Timeout: time.Second * time.Duration(timeout),
}
return d.DialContext(ctx, network, nameserver)
},
Expand All @@ -106,8 +111,7 @@ func Resolve(domain string, ip_version string) (net.IP, error) {

target, err := FindFinalTarget(domain)
if err != nil {
logger.Errorf("Error checking for final target of %s: %s", domain, err)
return nil, errors.New("dns cname check error")
return nil, errors.New("dns cname lookup error")
}

ips, err := LookupAuthorative(target, ip_version)
Expand Down
24 changes: 14 additions & 10 deletions pkg/http_resolver/http_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,37 @@ import (
"github.com/d0m84/ip-monitor/pkg/logger"
)

var (
timeout int = 10
)

func Resolve(provider string, ip_version string) (net.IP, error) {

var zeroDialer net.Dialer
var httpClient = &http.Client{Timeout: 10 * time.Second}
var tcpVersion string = ip_version
var dialer net.Dialer
var client = &http.Client{Timeout: time.Second * time.Duration(timeout)}
var tcp_version string = "tcp"

if ip_version == "ip4" {
tcpVersion = "tcp4"
tcp_version = "tcp4"
} else if ip_version == "ip6" {
tcpVersion = "tcp6"
tcp_version = "tcp6"
}

transport := http.DefaultTransport.(*http.Transport).Clone()
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return zeroDialer.DialContext(ctx, tcpVersion, addr)
return dialer.DialContext(ctx, tcp_version, addr)
}
httpClient.Transport = transport
client.Transport = transport

resp, err := httpClient.Get(provider)
resp, err := client.Get(provider)
if err != nil {
logger.Errorf("Error connecting to HTTP IP provider: %s", err)
return nil, errors.New("http error")
}
defer resp.Body.Close()

statusOK := resp.StatusCode >= 200 && resp.StatusCode < 300
if !statusOK {
status_ok := resp.StatusCode >= 200 && resp.StatusCode < 300
if !status_ok {
logger.Errorf("HTTP status error from IP provider: %s", provider)
return nil, errors.New("status error")
}
Expand Down

0 comments on commit cd87aa8

Please sign in to comment.