-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
241 lines (213 loc) · 7.92 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"os"
"storjnet/core"
"storjnet/nodes"
"storjnet/optimizer"
"storjnet/proxy"
"storjnet/server"
"storjnet/tgbot"
"storjnet/transactions"
"storjnet/updater"
"storjnet/utils"
"storjnet/versions"
"time"
"github.com/ansel1/merry"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
var env = utils.Env{Val: "dev"}
var httpCmdFlags = struct {
serverAddr string
}{}
var pingProxyCmdFlags = struct {
serverAddr string
identityDirPath string
}{}
var tgBotCmdFlags = struct {
botToken string
socks5ProxyAddr string
webhookURL string
webhookListenAddr string
webhookListenPath string
}{}
var nodesCmdFlags = struct {
satelliteAddress string
socksProxy string
}{}
var statNodesGroup string
var nodeLocsSnapFPath string
var transactionsFlags = struct {
summaryStartDate string
}{}
var (
rootCmd = &cobra.Command{
Use: os.Args[0],
Short: "Tool for checking Storj nodes stats",
// SilenceUsage: true,
}
httpCmd = &cobra.Command{
Use: "http",
Short: "start HTTP-server",
RunE: CMDHttp,
}
pingProxyCmd = &cobra.Command{
Use: "ping-proxy",
Short: "start proxy for node dialing and pings",
RunE: CMDPingProxy,
}
updateCmd = &cobra.Command{
Use: "update",
Short: "start updater (pinger, uptime checker, etc.)",
RunE: CMDUpdate,
}
tgBotCmd = &cobra.Command{
Use: "tg-bot",
Short: "start TG bot",
RunE: CMDTGBot,
}
checkVersionsCmd = &cobra.Command{
Use: "check-versions",
Short: "check if versions on github and version.storj.io have changed",
RunE: CMDCheckVersions,
}
fetchTransactionsCmd = &cobra.Command{
Use: "fetch-transactions",
Short: "fetch STORJ transactions from etherscan.io",
RunE: CMDFetchTransactions,
}
fetchNodesCmd = &cobra.Command{
Use: "fetch-nodes",
Short: "fetch some nodes from satellite",
RunE: CMDFetchNodes,
}
probeNodesCmd = &cobra.Command{
Use: "probe-nodes",
Short: "start probing saved nodes and updating activity timestamp",
RunE: CMDProbeNodes,
}
statNodesCmd = &cobra.Command{
Use: "stat-nodes",
Short: "generate and save nodes statistics",
RunE: CMDStatNodes,
}
snapNodeLocationsCmd = &cobra.Command{
Use: "snap-node-locations",
Short: "save snapshot of nodes geolocations",
RunE: CMDSnapNodeLocations,
}
printNodeLocationsCmd = &cobra.Command{
Use: "print-node-locations",
Short: "print snapshot of nodes geolocations",
RunE: CMDPrintNodeLocations,
}
optimizeDBCmd = &cobra.Command{
Use: "optimize-db",
Short: "vacuum some big tables, update partitions",
RunE: CMDOptimizeDB,
}
)
func CMDHttp(cmd *cobra.Command, args []string) error {
return merry.Wrap(server.StartHTTPServer(httpCmdFlags.serverAddr, env))
}
func CMDPingProxy(cmd *cobra.Command, args []string) error {
return merry.Wrap(proxy.StartPingProxy(pingProxyCmdFlags.serverAddr, pingProxyCmdFlags.identityDirPath))
}
func CMDUpdate(cmd *cobra.Command, args []string) error {
return merry.Wrap(updater.StartUpdater())
}
func CMDTGBot(cmd *cobra.Command, args []string) error {
var webhookConfig *tgbot.WebhookConfig
url, addr, path := tgBotCmdFlags.webhookURL, tgBotCmdFlags.webhookListenAddr, tgBotCmdFlags.webhookListenPath
if url != "" && addr != "" && path != "" {
webhookConfig = &tgbot.WebhookConfig{URL: url, ListenAddr: addr, ListenPath: path}
}
return merry.Wrap(tgbot.StartTGBot(tgBotCmdFlags.botToken, tgBotCmdFlags.socks5ProxyAddr, webhookConfig))
}
func CMDCheckVersions(cmd *cobra.Command, args []string) error {
return merry.Wrap(versions.CheckVersions(tgBotCmdFlags.botToken, tgBotCmdFlags.socks5ProxyAddr))
}
func CMDFetchTransactions(cmd *cobra.Command, args []string) error {
var err error
var startDate time.Time
if transactionsFlags.summaryStartDate != "" {
startDate, err = time.Parse("2006-01-02", transactionsFlags.summaryStartDate)
if err != nil {
return merry.Wrap(err)
}
}
return merry.Wrap(transactions.FetchAndProcess(startDate))
}
func CMDFetchNodes(cmd *cobra.Command, args []string) error {
return merry.Wrap(nodes.FetchAndProcess(nodesCmdFlags.satelliteAddress, nodesCmdFlags.socksProxy))
}
func CMDProbeNodes(cmd *cobra.Command, args []string) error {
return merry.Wrap(nodes.StartProber())
}
func CMDStatNodes(cmd *cobra.Command, args []string) error {
nodeStats := statNodesGroup == "all" || statNodesGroup == "nodes"
dailyStats := statNodesGroup == "all" || statNodesGroup == "daily"
offStats := statNodesGroup == "all" || statNodesGroup == "official"
return merry.Wrap(nodes.SaveStats(nodeStats, dailyStats, offStats))
}
func CMDSnapNodeLocations(cmd *cobra.Command, args []string) error {
return merry.Wrap(nodes.SaveLocsSnapshot())
}
func CMDPrintNodeLocations(cmd *cobra.Command, args []string) error {
return merry.Wrap(nodes.PrintLocsSnapshot(nodeLocsSnapFPath))
}
func CMDOptimizeDB(cmd *cobra.Command, args []string) error {
return merry.Wrap(optimizer.OptimizeDB())
}
func init() {
rootCmd.AddCommand(httpCmd)
rootCmd.AddCommand(pingProxyCmd)
rootCmd.AddCommand(updateCmd)
rootCmd.AddCommand(tgBotCmd)
rootCmd.AddCommand(checkVersionsCmd)
rootCmd.AddCommand(fetchTransactionsCmd)
rootCmd.AddCommand(fetchNodesCmd)
rootCmd.AddCommand(probeNodesCmd)
rootCmd.AddCommand(statNodesCmd)
rootCmd.AddCommand(snapNodeLocationsCmd)
rootCmd.AddCommand(printNodeLocationsCmd)
rootCmd.AddCommand(optimizeDBCmd)
flags := httpCmd.Flags()
flags.Var(&env, "env", "evironment, dev or prod")
flags.StringVar(&httpCmdFlags.serverAddr, "addr", "127.0.0.1:9003", "HTTP server address:port")
flags = pingProxyCmd.Flags()
flags.StringVar(&pingProxyCmdFlags.serverAddr, "addr", "127.0.0.1:9005", "ping proxy server address:port")
flags.StringVar(&pingProxyCmdFlags.identityDirPath, "identity-dir", "identity", "path to dir with satellite identity")
flags = tgBotCmd.Flags()
flags.StringVar(&tgBotCmdFlags.botToken, "tg-bot-token", "", "TG bot API token")
flags.StringVar(&tgBotCmdFlags.socks5ProxyAddr, "tg-proxy", "", "SOCKS5 proxy for TG requests")
flags.StringVar(&tgBotCmdFlags.webhookURL, "tg-webhook-url", "", "TG webhook URL, will be sent to TG (http://example.com:8443/requests/listen/path)")
flags.StringVar(&tgBotCmdFlags.webhookListenAddr, "tg-webhook-addr", "", "TG webhook address:port for https server")
flags.StringVar(&tgBotCmdFlags.webhookListenPath, "tg-webhook-path", "", "TG webhook /requests/listen/path for https server")
flags.StringVar(&core.GitHubOAuthToken, "github-oauth-token", "", "GitHub API OAuth token (optional, for increasing API req rate)")
flags = checkVersionsCmd.Flags()
flags.StringVar(&tgBotCmdFlags.botToken, "tg-bot-token", "", "TG bot API token")
flags.StringVar(&tgBotCmdFlags.socks5ProxyAddr, "tg-proxy", "", "SOCKS5 proxy for TG requests")
flags.StringVar(&core.GitHubOAuthToken, "github-oauth-token", "", "GitHub API OAuth token (optional, for increasing API req rate)")
flags = fetchTransactionsCmd.Flags()
flags.StringVar(&transactionsFlags.summaryStartDate, "summary-start-date", "", "start date for updating daily summaries")
flags = fetchNodesCmd.Flags()
flags.StringVar(&nodesCmdFlags.satelliteAddress, "satellite", "", "satellite id@address:port")
flags.StringVar(&nodesCmdFlags.socksProxy, "socks-proxy", "", "proxy for satellite requests, address:port or address:port:user:passwd")
fetchNodesCmd.MarkFlagRequired("satellite")
flags = statNodesCmd.Flags()
flags.StringVar(&statNodesGroup, "group", "all", "stats group: nodes/daily/official/all")
flags = printNodeLocationsCmd.Flags()
flags.StringVar(&nodeLocsSnapFPath, "file", nodes.LastFPathLabel, "path to .bin file")
}
func main() {
// Logger
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
zerolog.ErrorStackMarshaler = func(err error) interface{} { return merry.Details(err) }
zerolog.ErrorStackFieldName = "message" //TODO: https://github.com/rs/zerolog/issues/157
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "2006-01-02 15:04:05.000"})
if err := rootCmd.Execute(); err != nil {
log.Fatal().Msg(merry.Details(err))
}
}