-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
208 lines (183 loc) · 5.76 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/SkynetLabs/blocker/api"
"github.com/SkynetLabs/blocker/blocker"
"github.com/SkynetLabs/blocker/database"
"github.com/SkynetLabs/blocker/syncer"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"gitlab.com/NebulousLabs/errors"
"go.mongodb.org/mongo-driver/mongo/options"
)
const (
// defaultSkydHost is where we connect to skyd unless overwritten by
// "API_HOST" environment variables.
defaultSkydHost = "sia"
// defaultSkydPort is where we connect to skyd unless overwritten by
// "API_PORT" environment variables.
defaultSkydPort = 9980
)
func main() {
// Load the environment variables from the .env file.
// Existing variables take precedence and won't be overwritten.
_ = godotenv.Load()
// Create a logger
logger := logrus.New()
logLevel, err := logrus.ParseLevel(os.Getenv("BLOCKER_LOG_LEVEL"))
if err != nil {
logLevel = logrus.InfoLevel
}
logger.SetLevel(logLevel)
// Set the unique id of this server.
database.ServerUID = os.Getenv("SERVER_UID")
if database.ServerUID == "" {
log.Fatal("missing env var SERVER_UID")
}
// Load the database credentials
uri, dbCreds, err := loadDBCredentials()
if err != nil {
log.Fatal(errors.AddContext(err, "failed to fetch db credentials"))
}
// Create a connection to the database
ctx, cancel := context.WithTimeout(context.Background(), database.MongoDefaultTimeout)
defer cancel()
db, err := database.New(ctx, uri, dbCreds, logger)
if err != nil {
log.Fatal(errors.AddContext(err, "failed to connect to the db"))
}
// Blocker env vars.
skydPort := defaultSkydPort
skydPortEnv, err := strconv.Atoi(os.Getenv("API_PORT"))
if err == nil && skydPortEnv > 0 {
skydPort = skydPortEnv
}
skydHost := defaultSkydHost
if skydHostEnv := os.Getenv("API_HOST"); skydHostEnv != "" {
skydHost = skydHostEnv
}
skydAPIPassword := os.Getenv("SIA_API_PASSWORD")
if skydAPIPassword == "" {
log.Fatal(errors.New("SIA_API_PASSWORD is empty, exiting"))
}
// Accounts.
if aHost := os.Getenv("SKYNET_ACCOUNTS_HOST"); aHost != "" {
api.AccountsHost = aHost
}
if aPort := os.Getenv("SKYNET_ACCOUNTS_PORT"); aPort != "" {
api.AccountsPort = aPort
}
// Create a skyd client
skydUrl := fmt.Sprintf("http://%s:%d", skydHost, skydPort)
skydClient := api.NewSkydClient(skydUrl, skydAPIPassword)
if !skydClient.DaemonReady() {
log.Fatal(errors.New("skyd down, exiting"))
}
// Create the blocker.
bl, err := blocker.New(skydClient, db, logger)
if err != nil {
log.Fatal(errors.AddContext(err, "failed to instantiate blocker"))
}
// Start blocker.
err = bl.Start()
if err != nil {
log.Fatal(errors.AddContext(err, "failed to start blocker"))
}
// Create the syncer.
portalURLs := loadPortalURLs()
sync, err := syncer.New(db, portalURLs, logger)
if err != nil {
log.Fatal(errors.AddContext(err, "failed to instantiate syncer"))
}
// Start the syncer.
err = sync.Start()
if err != nil {
log.Fatal(errors.AddContext(err, "failed to start syncer"))
}
// Initialise the server.
server, err := api.New(skydClient, db, logger)
if err != nil {
log.Fatal(errors.AddContext(err, "failed to build the api"))
}
// Start the server
go func() {
err := server.ListenAndServe(4000)
if err != nil {
log.Fatal(errors.AddContext(err, "failed to start server"))
}
}()
// Catch exit signals
exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM)
<-exitSignal
// Shut down all components
err = errors.Compose(
bl.Stop(),
sync.Stop(),
)
if err != nil {
log.Fatal("Failed to cleanly stop all components, err: ", err)
}
// Close the database connection
dbCtx, dbCancel := context.WithTimeout(context.Background(), database.MongoDefaultTimeout)
defer dbCancel()
err = db.Close(dbCtx)
if err != nil {
log.Fatal("Failed to disconnect from the database, err: ", err)
}
logger.Info("Blocker Terminated.")
}
// loadDBCredentials creates a new db connection based on credentials found in
// the environment variables.
func loadDBCredentials() (string, options.Credential, error) {
var creds options.Credential
var ok bool
if creds.Username, ok = os.LookupEnv("SKYNET_DB_USER"); !ok {
return "", options.Credential{}, errors.New("missing env var SKYNET_DB_USER")
}
if creds.Password, ok = os.LookupEnv("SKYNET_DB_PASS"); !ok {
return "", options.Credential{}, errors.New("missing env var SKYNET_DB_PASS")
}
var host, port string
if host, ok = os.LookupEnv("SKYNET_DB_HOST"); !ok {
return "", options.Credential{}, errors.New("missing env var SKYNET_DB_HOST")
}
if port, ok = os.LookupEnv("SKYNET_DB_PORT"); !ok {
return "", options.Credential{}, errors.New("missing env var SKYNET_DB_PORT")
}
return fmt.Sprintf("mongodb://%v:%v", host, port), creds, nil
}
// loadPortalURLs returns a slice of portal urls, configured in the environment
// under the key BLOCKER_SYNC_PORTALS. The blocker will keep in sync the
// blocklist from these portals with the local skyd instance.
func loadPortalURLs() (portalURLs []string) {
portalURLStr := os.Getenv("BLOCKER_PORTALS_SYNC")
for _, portalURL := range strings.Split(portalURLStr, ",") {
portalURL = sanitizePortalURL(portalURL)
if portalURL != "" {
portalURLs = append(portalURLs, portalURL)
}
}
return
}
// sanitizePortalURL is a helper function that sanitizes the given input portal
// URL, stripping away trailing slashes and ensuring it's prefixed with https.
func sanitizePortalURL(portalURL string) string {
portalURL = strings.TrimSpace(portalURL)
portalURL = strings.TrimSuffix(portalURL, "/")
if strings.HasPrefix(portalURL, "https://") {
return portalURL
}
portalURL = strings.TrimPrefix(portalURL, "http://")
if portalURL == "" {
return portalURL
}
return fmt.Sprintf("https://%s", portalURL)
}