forked from SkynetLabs/malware-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
120 lines (108 loc) · 3.78 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
package main
import (
"context"
"fmt"
"log"
"os"
"strings"
"github.com/SkynetLabs/malware-scanner/api"
"github.com/SkynetLabs/malware-scanner/clamav"
"github.com/SkynetLabs/malware-scanner/database"
"github.com/SkynetLabs/malware-scanner/scanner"
accdb "github.com/SkynetLabs/skynet-accounts/database"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"gitlab.com/NebulousLabs/errors"
)
// loadDBCredentials creates a new db connection based on credentials found in
// the environment variables.
func loadDBCredentials() (accdb.DBCredentials, error) {
var cds accdb.DBCredentials
var ok bool
if cds.User, ok = os.LookupEnv("SKYNET_DB_USER"); !ok {
return accdb.DBCredentials{}, errors.New("missing env var SKYNET_DB_USER")
}
if cds.Password, ok = os.LookupEnv("SKYNET_DB_PASS"); !ok {
return accdb.DBCredentials{}, errors.New("missing env var SKYNET_DB_PASS")
}
if cds.Host, ok = os.LookupEnv("SKYNET_DB_HOST"); !ok {
return accdb.DBCredentials{}, errors.New("missing env var SKYNET_DB_HOST")
}
if cds.Port, ok = os.LookupEnv("SKYNET_DB_PORT"); !ok {
return accdb.DBCredentials{}, errors.New("missing env var SKYNET_DB_PORT")
}
return cds, nil
}
func main() {
// Load the environment variables from the .env file.
// Existing variables take precedence and won't be overwritten.
_ = godotenv.Load()
// Initialise the global context and logger. These will be used throughout
// the service. Once the context is closed, all background threads will
// wind themselves down.
ctx := context.Background()
logger := logrus.New()
logLevel, err := logrus.ParseLevel(os.Getenv("MALWARE_SCANNER_LOG_LEVEL"))
if err != nil {
logLevel = logrus.InfoLevel
}
logger.SetLevel(logLevel)
// portalAddr tells us which Skynet portal to use for downloading skylinks.
portal := os.Getenv("PORTAL_DOMAIN")
if portal == "" {
log.Println("missing env var PORTAL_DOMAIN, falling back to SERVER_DOMAIN")
portal = os.Getenv("SERVER_DOMAIN")
if portal == "" {
log.Fatal("missing env var PORTAL_DOMAIN and SERVER_DOMAIN")
}
}
if !strings.HasPrefix(portal, "http") {
portal = "https://" + portal
}
// Initialised the database connection.
dbCreds, err := loadDBCredentials()
if err != nil {
log.Fatal(errors.AddContext(err, "failed to fetch db credentials"))
}
db, err := database.New(ctx, dbCreds, logger)
if err != nil {
log.Fatal(errors.AddContext(err, "failed to connect to the db"))
}
// Connect to ClamAV.
clamIP := os.Getenv("CLAMAV_IP")
if clamIP == "" {
log.Fatal(errors.New("missing CLAMAV_IP environment variable - cannot connect to ClamAV"))
}
clamPort := os.Getenv("CLAMAV_PORT")
if clamPort == "" {
log.Fatal(errors.New("missing CLAMAV_PORT environment variable - cannot connect to ClamAV"))
}
clam, err := clamav.New(clamIP, clamPort, portal)
if err != nil {
log.Fatal(errors.AddContext(err, fmt.Sprintf("cannot connect to ClamAV on %s:%s", clamIP, clamPort)))
}
// Connect to Blocker.
scanner.BlockerIP = os.Getenv("BLOCKER_IP")
if scanner.BlockerIP == "" {
log.Fatal(errors.New("missing BLOCKER_IP environment variable - cannot connect to Blocker"))
}
scanner.BlockerPort = os.Getenv("BLOCKER_PORT")
if scanner.BlockerPort == "" {
log.Fatal(errors.New("missing BLOCKER_PORT environment variable - cannot connect to Blocker"))
}
// Initialise and start the background scanner task.
scan, err := scanner.New(ctx, db, clam, logger)
if err != nil {
log.Fatal(errors.AddContext(err, "failed to instantiate scanner"))
}
scan.Start()
// Start the background thread that resets the status of scans that take
// too long and are considered stuck.
scan.StartUnlocker()
// Initialise the server.
server, err := api.New(db, clam, logger)
if err != nil {
log.Fatal(errors.AddContext(err, "failed to build the api"))
}
log.Fatal(server.ListenAndServe(4000))
}