-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
111 lines (99 loc) · 2.57 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
package main
import (
"context"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"strings"
"time"
"github.com/EFForg/starttls-backend/api"
"github.com/EFForg/starttls-backend/db"
"github.com/EFForg/starttls-backend/email"
"github.com/EFForg/starttls-backend/policy"
"github.com/EFForg/starttls-backend/stats"
"github.com/EFForg/starttls-backend/util"
"github.com/EFForg/starttls-backend/validator"
"github.com/getsentry/raven-go"
_ "github.com/joho/godotenv/autoload"
)
// ServePublicEndpoints serves all public HTTP endpoints.
func ServePublicEndpoints(a *api.API, cfg *db.Config) {
mux := http.NewServeMux()
mainHandler := a.RegisterHandlers(mux)
portString, err := util.ValidPort(cfg.Port)
if err != nil {
log.Fatal(err)
}
server := http.Server{
Addr: portString,
Handler: mainHandler,
}
exited := make(chan struct{})
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
<-sigint
if err := server.Shutdown(context.Background()); err != nil {
log.Printf("HTTP server Shutdown: %v", err)
}
close(exited)
}()
log.Fatal(server.ListenAndServe())
<-exited
}
// Loads a map of domains (effectively a set for fast lookup) to blacklist.
// if `DOMAIN_BLACKLIST` is not set, returns an empty map.
func loadDontScan() map[string]bool {
filepath := os.Getenv("DOMAIN_BLACKLIST")
if len(filepath) == 0 {
return make(map[string]bool)
}
data, err := ioutil.ReadFile(filepath)
if err != nil {
log.Fatal(err)
}
domainlist := strings.Split(string(data), "\n")
domainset := make(map[string]bool)
for _, domain := range domainlist {
if len(domain) > 0 {
domainset[domain] = true
}
}
return domainset
}
func main() {
raven.SetDSN(os.Getenv("SENTRY_URL"))
cfg, err := db.LoadEnvironmentVariables()
if err != nil {
log.Fatal(err)
}
db, err := db.InitSQLDatabase(cfg)
if err != nil {
log.Fatal(err)
}
emailConfig, err := email.MakeConfigFromEnv(db)
if err != nil {
log.Printf("couldn't connect to mailserver: %v", err)
log.Println("======NOT SENDING EMAIL======")
}
list := policy.MakeUpdatedList()
a := api.API{
Database: db,
List: list,
DontScan: loadDontScan(),
Emailer: emailConfig,
}
a.ParseTemplates("views")
if os.Getenv("VALIDATE_LIST") == "1" {
log.Println("[Starting list validator]")
go validator.ValidateRegularly("Live policy list", list, 24*time.Hour)
}
if os.Getenv("VALIDATE_QUEUED") == "1" {
log.Println("[Starting queued validator]")
go validator.ValidateRegularly("Testing domains", db, 24*time.Hour)
}
go stats.UpdateRegularly(db, time.Hour)
ServePublicEndpoints(&a, &cfg)
}