main.go 2.89 KB
Newer Older
1 2 3 4
package main

import (
	"dfss"
5
	dapi "dfss/dfssd/api"
6
	"dfss/dfssp/authority"
7
	"dfss/dfssp/server"
8
	"dfss/net"
9 10
	"flag"
	"fmt"
11
	"os"
12 13 14 15
	"runtime"
)

var (
16
	verbose, demo                                      bool
17
	path, country, org, unit, cn, port, address, dbURI string
Loïck Bonniot's avatar
Loïck Bonniot committed
18
	keySize, rootValidity, certValidity                int
19 20 21 22 23
)

func init() {

	flag.BoolVar(&verbose, "v", false, "Print verbose messages")
24
	flag.BoolVar(&demo, "d", false, "Enable demonstrator")
25

26 27 28
	flag.StringVar(&port, "p", "9000", "Default port listening")
	flag.StringVar(&address, "a", "0.0.0.0", "Default address to bind for listening")

29
	flag.StringVar(&path, "path", authority.GetHomeDir(), "Path for the platform's private key and root certificate")
Caro Axel's avatar
Caro Axel committed
30 31 32 33 34 35
	flag.StringVar(&country, "country", "France", "Country for the root certificate")
	flag.StringVar(&org, "org", "DFSS", "Organization for the root certificate")
	flag.StringVar(&unit, "unit", "INSA Rennes", "Organizational unit for the root certificate")
	flag.StringVar(&cn, "cn", "dfssp", "Common name for the root certificate")

	flag.IntVar(&keySize, "keySize", 512, "Encoding size for the private key")
Loïck Bonniot's avatar
Loïck Bonniot committed
36 37
	flag.IntVar(&rootValidity, "rootValidity", 365, "Root certificate's validity duration (days)")
	flag.IntVar(&certValidity, "certValidity", 365, "Validity duration for the certificates generated by this platform (days)")
Caro Axel's avatar
Caro Axel committed
38

39
	flag.StringVar(&dbURI, "db", "mongodb://localhost/dfss", "Name of the environment variable containing the server url in standard MongoDB format")
Caro Axel's avatar
Caro Axel committed
40

41 42 43 44 45
	flag.Usage = func() {
		fmt.Println("DFSS platform v" + dfss.Version)
		fmt.Println("Users and contracts manager")

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

		fmt.Println("\nThe commands are:")
Loïck Bonniot's avatar
Loïck Bonniot committed
49
		fmt.Println("  init     [cn, country, keySize, org, path, unit, rootValidity]")
Caro Axel's avatar
Caro Axel committed
50
		fmt.Println("           create and save the platform's private key and root certificate")
51
		fmt.Println("  start    [path, db, a, p]")
Caro Axel's avatar
Caro Axel committed
52
		fmt.Println("           start the platform after loading its private key and root certificate")
53 54 55 56 57 58 59 60 61 62 63 64 65
		fmt.Println("  help     print this help")
		fmt.Println("  version  print dfss client version")

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

		fmt.Println()
	}
}

func main() {
	flag.Parse()
	command := flag.Arg(0)
66
	dapi.Switch(demo)
67 68 69 70

	switch command {
	case "version":
		fmt.Println("v"+dfss.Version, runtime.GOOS, runtime.GOARCH)
Caro Axel's avatar
Caro Axel committed
71
	case "init":
Loïck Bonniot's avatar
Loïck Bonniot committed
72
		err := authority.Initialize(keySize, rootValidity, country, org, unit, cn, path)
Caro Axel's avatar
Caro Axel committed
73
		if err != nil {
74 75
			fmt.Println("An error occured during the initialization operation:", err)
			os.Exit(1)
Caro Axel's avatar
Caro Axel committed
76
		}
77
		dapi.DLog("Private key generated !")
Caro Axel's avatar
Caro Axel committed
78
	case "start":
Loïck Bonniot's avatar
Loïck Bonniot committed
79
		srv := server.GetServer(path, dbURI, certValidity, verbose)
80
		fmt.Println("Listening on " + address + ":" + port)
81
		dapi.DLog("Platform server started on " + address + ":" + port)
82
		err := net.Listen(address+":"+port, srv)
83
		if err != nil {
Caro Axel's avatar
Caro Axel committed
84 85
			fmt.Println(err)
		}
86 87 88
	default:
		flag.Usage()
	}
89 90

	dapi.DClose()
91
}