main.go 2.06 KB
Newer Older
Richer Maximilien's avatar
Richer Maximilien committed
1
2
3
4
5
package main

import (
	"flag"
	"fmt"
6
	"os"
Richer Maximilien's avatar
Richer Maximilien committed
7
	"runtime"
8
9
10
11
12

	"dfss"
	dapi "dfss/dfssd/api"
	"dfss/dfsst/server"
	"dfss/net"
Richer Maximilien's avatar
Richer Maximilien committed
13
14
15
)

var (
16
17
18
19
20
21
22
	verbose, demo bool
	fca           string // Path to the CA
	fcert         string // Path to the certificate
	fkey          string // Path to the private key
	address       string
	dbURI         string
	port          string
Richer Maximilien's avatar
Richer Maximilien committed
23
24
25
26
27
)

func init() {

	flag.BoolVar(&verbose, "v", false, "Print verbose messages")
28
29
30
	flag.StringVar(&fca, "ca", "ca.pem", "Path to the root certificate")
	flag.StringVar(&fcert, "cert", "cert.pem", "Path to the user certificate")
	flag.StringVar(&fkey, "key", "key.pem", "Path to the private key")
Richer Maximilien's avatar
Richer Maximilien committed
31
32
	flag.BoolVar(&demo, "d", false, "Enable demonstrator")

33
	flag.StringVar(&port, "p", "9020", "Default port listening")
Richer Maximilien's avatar
Richer Maximilien committed
34
35
36
37
38
39
40
41
42
43
44
45
	flag.StringVar(&address, "a", "0.0.0.0", "Default address to bind for listening")

	flag.StringVar(&dbURI, "db", "mongodb://localhost/dfss", "Name of the environment variable containing the server url in standard MongoDB format")

	flag.Usage = func() {
		fmt.Println("DFSS TTP v" + dfss.Version)
		fmt.Println("Trusted third party resolver")

		fmt.Println("\nUsage:")
		fmt.Println("  dfssp [flags] command")

		fmt.Println("\nThe commands are:")
46
47
		fmt.Println("  start    start the TTP service")
		fmt.Println("           fill the `DFSS_TTP_PASSWORD` environment variable if the private key is enciphered")
Richer Maximilien's avatar
Richer Maximilien committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
		fmt.Println("  help     print this help")
		fmt.Println("  version  print dfss protocol version")

		fmt.Println("\nFlags:")
		flag.PrintDefaults()

		fmt.Println()
	}
}

func main() {
	flag.Parse()
	command := flag.Arg(0)
	dapi.Switch(demo)

	switch command {
	case "version":
		fmt.Println("v"+dfss.Version, runtime.GOOS, runtime.GOARCH)
	case "start":
67
68
69
70
71
72
73
74
75
76
		password := os.Getenv("DFSS_TTP_PASSWORD")
		srv := server.GetServer(fca, fcert, fkey, password, dbURI, verbose)

		addrPort := address + ":" + port
		fmt.Println("Listening on " + addrPort)
		dapi.DLog("TTP server started on " + addrPort)
		err := net.Listen(addrPort, srv)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
		}
Richer Maximilien's avatar
Richer Maximilien committed
77
78
79
80
81
82
	default:
		flag.Usage()
	}

	dapi.DClose()
}