about summary refs log tree commit diff
path: root/src/main.go
blob: 39dd2131cf9878da9ab44f52fedf7d77a741e39b (plain)
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
package main

import (
	"flag"
	"log"
	"math/rand"
	"net/http"
	"os"
	"os/signal"
	"time"
)

func main() {
	// Set up flags
	log.Println("Setting up components")
	registerHTTPFlags()
	registerSessionFlags()
	registerCredentialsFlags()
	registerSeedFlags()
	registerAccessFlags()
	registerLimitFlags()
	flag.Parse()

	// Read challenges from file
	log.Println("Reading seed file")
	readChallengesError := getChallengesFromSeedFile()
	if readChallengesError != nil {
		log.Fatalln(readChallengesError.Error())
	}

	// Seed the random number generator
	log.Println("Seed random number generator")
	rand.Seed(time.Now().UTC().UnixNano())

	// Start our VPN container and network
	log.Printf("Starting VPN container ('%s')", vpnContainerName)
	startVPNError := startVPN()
	if startVPNError != nil {
		log.Fatalln(startVPNError.Error())
	}

	// Launch Limiter
	log.Printf("Starting limiter (end %d, timespan %d)", *endTimestamp, *endAfter)
	startLimitTimer()

	// Set up HTTP server
	log.Printf("Running HTTP server on port %d", *port)
	httpServer := setupHTTPServer()

	// React to system signals
	signalChannel := make(chan os.Signal, 1)
	signal.Notify(signalChannel)
	go cleanup(signalChannel, httpServer)

	// Start our HTTP server
	log.Fatalln(httpServer.ListenAndServe())
}

func cleanup(signalChannel chan os.Signal, server http.Server) {
	// Block until we receive a signal
	<- signalChannel
	log.Println("Requested shutdown")

	// Stop everything
	log.Println("Stopping HTTP server")
	server.Close()
	log.Println("Stopping VPN container")
	stopVPN()
	log.Println("Stopping challenge containers")
	stopAllChallengeContainers()
	log.Println("Deleting Docker networks")
	deleteNetwork(getVPNNetworkName())
	deleteNetwork(getChallengeNetworkName())
	os.Exit(0)
}