-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
143 lines (116 loc) · 3.69 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"context"
"fmt"
"net/http"
"net/http/cookiejar"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"github.com/9seconds/topographer/providers"
"github.com/9seconds/topographer/topolib"
)
func makeRootContext() (context.Context, context.CancelFunc) {
rootCtx, cancel := context.WithCancel(context.Background())
sigChan := make(chan os.Signal, 1)
go func() {
for range sigChan {
cancel()
}
}()
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
return rootCtx, cancel
}
func makeProviders(conf *config) ([]topolib.Provider, error) {
rv := make([]topolib.Provider, 0, len(conf.GetProviders()))
for _, v := range conf.GetProviders() {
httpClient := makeNewHTTPClient(v)
switch v.GetName() {
case providers.NameDBIPLite:
baseDir, err := ensureDir(conf, v)
if err != nil {
return nil, fmt.Errorf("cannot create base directory for dbip provider: %w", err)
}
rv = append(rv, providers.NewDBIPLite(httpClient, v.GetUpdateEvery(), baseDir))
case providers.NameIP2C:
rv = append(rv, providers.NewIP2C(httpClient))
case providers.NameIP2Location:
baseDir, err := ensureDir(conf, v)
if err != nil {
return nil, fmt.Errorf("cannot create base directory for ip2location provider: %w", err)
}
params := v.GetSpecificParameters()
prov, err := providers.NewIP2Location(httpClient, v.GetUpdateEvery(), baseDir, params["auth_token"], params["db_code"])
if err != nil {
return nil, fmt.Errorf("cannot create ip2location provider: %w", err)
}
rv = append(rv, prov)
case providers.NameIPInfo:
token := v.GetSpecificParameters()["auth_token"]
rv = append(rv, providers.NewIPInfo(httpClient, token))
case providers.NameIPStack:
params := v.GetSpecificParameters()
prov, err := providers.NewIPStack(httpClient,
params["auth_token"], boolParam(params["secure"]))
if err != nil {
return nil, fmt.Errorf("cannot create ipstack provider: %w", err)
}
rv = append(rv, prov)
case providers.NameMaxmindLite:
baseDir, err := ensureDir(conf, v)
if err != nil {
return nil, fmt.Errorf("cannot create base directory for maxmind provider: %w", err)
}
licenseKey := v.GetSpecificParameters()["license_key"]
prov, err := providers.NewMaxmindLite(httpClient, v.GetUpdateEvery(), baseDir,
licenseKey)
if err != nil {
return nil, fmt.Errorf("cannot create ipstack provider: %w", err)
}
rv = append(rv, prov)
case providers.NameSoftware77:
baseDir, err := ensureDir(conf, v)
if err != nil {
return nil, fmt.Errorf("cannot create base directory for sofware77 provider: %w", err)
}
rv = append(rv, providers.NewSoftware77(httpClient, v.GetUpdateEvery(), baseDir))
default:
return nil, fmt.Errorf("unsupported provider name: %s", v.GetName())
}
}
return rv, nil
}
func makeNewHTTPClient(conf configProvider) topolib.HTTPClient {
jar, err := cookiejar.New(nil)
if err != nil {
panic(err)
}
httpClient := &http.Client{
Timeout: conf.GetHTTPTimeout(),
Jar: jar,
}
return topolib.NewHTTPClient(httpClient,
"topographer/"+version,
conf.GetRateLimitInterval(),
conf.GetRateLimitBurst(),
conf.GetCircuitBreakerOpenThreshold(),
conf.GetCircuitBreakerHalfOpenTimeout(),
conf.GetCircuitBreakerResetFailuresTimeout())
}
func boolParam(param string) bool {
switch strings.ToLower(param) {
case "1", "true", "enabled", "yes":
return true
default:
return false
}
}
func ensureDir(conf *config, v configProvider) (string, error) {
baseDir := filepath.Join(conf.GetRootDirectory(), v.GetDirectory())
if err := os.MkdirAll(baseDir, 0777); err != nil {
return "", fmt.Errorf("cannot create base directory for dbip provider: %w", err)
}
return baseDir, nil
}