This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
main.go
89 lines (76 loc) · 2.4 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
package main
import (
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
"strings"
"syscall"
"github.com/twitchyliquid64/subnet/subnet"
"github.com/twitchyliquid64/subnet/subnet/cert"
)
func main() {
parseFlags()
fatalErrChan := make(chan error)
if crlPathVar != "" && (modeVar == "client" || modeVar == "server") {
crlStartErr := cert.InitCRL(crlPathVar)
checkErr(crlStartErr, "init-crl")
}
switch modeVar {
case "client":
var additionalAddrs []net.IP
for _, addrStr := range strings.Split(additionalClientAddrs, ",") {
if addrStr != "" {
additionalAddrs = append(additionalAddrs, net.ParseIP(addrStr))
}
}
c, err := subnet.NewClient(serverAddressVar, connPortVar, networkAddrVar, interfaceNameVar, gatewayVar, ourCertPathVar, ourKeyPathVar, caCertPathVar, additionalAddrs)
checkErr(err, "subnet.NewClient()")
c.Run()
defer func() { checkErr(c.Close(), "client.Close()") }()
waitInterrupt(fatalErrChan)
case "server":
s, err := subnet.NewServer(serverAddressVar, connPortVar, networkAddrVar, interfaceNameVar, ourCertPathVar, ourKeyPathVar, caCertPathVar)
checkErr(err, "subnet.NewServer()")
s.Run()
defer func() { checkErr(s.Close(), "server.Close()") }()
waitInterrupt(fatalErrChan)
case "init-server-certs":
err := cert.MakeServerCert(ourCertPathVar, ourKeyPathVar, caCertPathVar, caKeyPathVar)
checkErr(err, "init-server-certs")
fmt.Println("NOTICE: Certificates expire (and will need to be rotated) one year from now.")
case "make-client-cert":
err := cert.IssueClientCert(caCertPathVar, caKeyPathVar, flag.Arg(0), flag.Arg(1))
fmt.Println("NOTICE: Certificates expire (and will need to be rotated) one year from now.")
checkErr(err, "make-client-cert")
case "blacklist-cert":
err := cert.AddToCRL(crlPathVar, flag.Arg(0), flag.Arg(1))
checkErr(err, "blacklist-cert")
default:
fmt.Fprintf(os.Stderr, "Err: Unrecognised mode. Mode must be either client/server.\n")
os.Exit(3)
}
}
func checkErr(err error, component string) {
if err != nil {
log.Printf("%s err: %s", component, err.Error())
os.Exit(1)
}
}
func waitInterrupt(fatalErrChan chan error) {
sig := make(chan os.Signal, 2)
done := make(chan bool, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sig
done <- true
}()
select {
case <-done:
log.Println("Recieved interrupt, shutting down.")
case err := <-fatalErrChan:
log.Println("Fatal internal error: ", err)
}
}