2021-02-27 06:26:33 +01:00
|
|
|
package app
|
2021-02-26 15:36:46 +01:00
|
|
|
|
|
|
|
import (
|
2022-07-02 07:49:47 +02:00
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
2021-02-26 15:36:46 +01:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
"github.com/mr-karan/doggo/pkg/models"
|
|
|
|
"github.com/mr-karan/doggo/pkg/resolvers"
|
2022-07-01 03:59:14 +02:00
|
|
|
"github.com/mr-karan/logf"
|
2021-02-26 15:36:46 +01:00
|
|
|
)
|
|
|
|
|
2021-02-27 06:26:33 +01:00
|
|
|
// App represents the structure for all app wide configuration.
|
|
|
|
type App struct {
|
2022-07-01 03:59:14 +02:00
|
|
|
Logger *logf.Logger
|
2021-02-26 15:36:46 +01:00
|
|
|
Version string
|
|
|
|
QueryFlags models.QueryFlags
|
|
|
|
Questions []dns.Question
|
|
|
|
Resolvers []resolvers.Resolver
|
|
|
|
ResolverOpts resolvers.Options
|
|
|
|
Nameservers []models.Nameserver
|
|
|
|
}
|
|
|
|
|
2021-02-27 06:26:33 +01:00
|
|
|
// NewApp initializes an instance of App which holds app wide configuration.
|
2022-07-01 03:59:14 +02:00
|
|
|
func New(logger *logf.Logger, buildVersion string) App {
|
2021-02-27 06:26:33 +01:00
|
|
|
app := App{
|
2021-02-26 15:36:46 +01:00
|
|
|
Logger: logger,
|
|
|
|
Version: buildVersion,
|
|
|
|
QueryFlags: models.QueryFlags{
|
|
|
|
QNames: []string{},
|
|
|
|
QTypes: []string{},
|
|
|
|
QClasses: []string{},
|
|
|
|
Nameservers: []string{},
|
|
|
|
},
|
|
|
|
Nameservers: []models.Nameserver{},
|
|
|
|
}
|
2021-02-27 06:26:33 +01:00
|
|
|
return app
|
2021-02-26 15:36:46 +01:00
|
|
|
}
|
2022-07-02 07:49:47 +02:00
|
|
|
|
|
|
|
// Attempts a DNS Lookup with retries for a given Question.
|
|
|
|
func (app *App) LookupWithRetry(attempts int, resolver resolvers.Resolver, ques dns.Question) (resolvers.Response, error) {
|
|
|
|
resp, err := resolver.Lookup(ques)
|
|
|
|
if err != nil {
|
|
|
|
// Retry lookup.
|
|
|
|
attempts--
|
|
|
|
if attempts > 0 {
|
|
|
|
// Add some random delay.
|
|
|
|
time.Sleep(time.Millisecond*300 + (time.Duration(rand.Int63n(int64(time.Millisecond*100))))/2)
|
|
|
|
app.Logger.Debug("retrying lookup")
|
|
|
|
return app.LookupWithRetry(attempts, resolver, ques)
|
|
|
|
}
|
|
|
|
return resolvers.Response{}, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|