main.go 2.1 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 23
	verbose 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
	demo    string
Richer Maximilien's avatar
Richer Maximilien committed
24 25 26 27 28
)

func init() {

	flag.BoolVar(&verbose, "v", false, "Print verbose messages")
29 30 31
	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")
32
	flag.StringVar(&demo, "d", "", "Demonstrator address and port (empty string disables debug)")
Richer Maximilien's avatar
Richer Maximilien committed
33

34
	flag.StringVar(&port, "p", "9020", "Default port listening")
Richer Maximilien's avatar
Richer Maximilien committed
35 36 37 38 39 40 41 42 43 44 45 46
	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:")
47 48
		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
49 50 51 52 53 54 55 56 57 58 59 60 61
		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)
62
	dapi.Configure(demo != "", demo, "ttp")
Richer Maximilien's avatar
Richer Maximilien committed
63 64 65 66 67

	switch command {
	case "version":
		fmt.Println("v"+dfss.Version, runtime.GOOS, runtime.GOARCH)
	case "start":
68 69 70 71 72 73 74 75 76 77
		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
78 79 80 81 82 83
	default:
		flag.Usage()
	}

	dapi.DClose()
}