-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
149 lines (124 loc) · 3.18 KB
/
main.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
144
145
146
147
148
149
package main
import (
"crypto/tls"
"flag"
"net/http"
"net/url"
"sync"
"time"
log "github.com/sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/net/proxy"
)
var cfg *Config
func init() {
configFile := flag.String("c", "config.yml", "Path to your config file")
flag.Parse()
var err error
err, cfg = LoadConfig(configFile)
if err != nil {
log.WithError(err).Fatal("unable to load the config file")
}
lvl, err := log.ParseLevel(cfg.LogLevel)
if err != nil {
log.WithError(err).Error("unable to parse the log level")
} else {
log.SetLevel(lvl)
}
if cfg.LogFormat == "json" {
log.SetFormatter(&log.JSONFormatter{})
} else {
log.SetFormatter(&log.TextFormatter{})
}
}
func main() {
log.Infof("started Onion Service Exporter on %s", cfg.ListenAddr)
go func() {
for {
log.Debug("start loop to check the targets")
func() {
var wg sync.WaitGroup
for _, target := range cfg.Targets {
log.Debugf(`check target "%s"`, target.Name)
wg.Add(1)
switch target.Type {
case targetTypeHTTP:
checkHTTP(target, &wg)
case targetTypeTCP:
checkTCP(target, &wg)
default:
log.Errorf(`unsupported scheme "%s" for target "%s"`, target.Type, target.Name)
}
}
wg.Wait()
}()
<-time.After(cfg.CheckInterval)
}
}()
prometheus.MustRegister(NewOnionCollector())
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(cfg.ListenAddr, nil))
}
func checkHTTP(target Target, wg *sync.WaitGroup) {
wg.Done()
uri, err := url.Parse(target.URL)
if err != nil {
log.WithError(err).Error("unable to parse the url")
return
}
dialer, err := proxy.SOCKS5("tcp", cfg.TorAddr, nil, proxy.Direct)
if err != nil {
log.WithError(err).Error("failed to initialize the proxy")
}
transport := &http.Transport{Dial: dialer.Dial}
if cfg.InsecureSkipVerify {
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
client := &http.Client{Transport: transport, Timeout: cfg.Timeout}
up := 0.0
start := time.Now()
res, err := client.Get(uri.String())
if err != nil {
log.WithError(err).WithField("url", uri.String()).Warn("unable to get the url")
} else {
if res.StatusCode == http.StatusOK {
up = 1.0
}
defer res.Body.Close()
}
statuses[target.Name] = OnionStatus{
Up: up,
Host: uri.Host,
Type: targetTypeHTTP,
Latency: time.Since(start).Seconds(),
}
log.Debugf(`finished check for "%s"`, target.Name)
}
func checkTCP(target Target, wg *sync.WaitGroup) {
wg.Done()
uri, err := url.Parse(target.URL)
if err != nil {
log.WithError(err).Error("unable to parse the url")
return
}
dialer, err := proxy.SOCKS5("tcp", cfg.TorAddr, nil, proxy.Direct)
if err != nil {
log.WithError(err).Error("failed to initialize the proxy")
}
up := 0.0
start := time.Now()
conn, err := dialer.Dial("tcp", uri.Host)
if err != nil {
log.WithError(err).WithField("url", uri.String()).Warn("unable to get the url")
} else {
up = 1.0
defer conn.Close()
}
statuses[target.Name] = OnionStatus{
Up: up,
Host: uri.Host,
Type: targetTypeTCP,
Latency: time.Since(start).Seconds(),
}
}