From 18807e1a0a8e006b692a470a328f6fa55bf196e6 Mon Sep 17 00:00:00 2001 From: "kali kaneko (leap communications)" Date: Fri, 24 Jan 2020 14:32:10 -0600 Subject: refactor flag initialization --- Makefile | 2 +- README.md | 6 +++++ go.mod | 3 +++ main.go | 88 ++++++++++++++++++++++++++++++++++++++++++++------------------- 4 files changed, 72 insertions(+), 27 deletions(-) create mode 100644 README.md create mode 100644 go.mod diff --git a/Makefile b/Makefile index 06ed11c..0ff7174 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ build: go build demo: - ./vpnweb -caCrt test/files/ca.crt -caKey test/files/ca.key + . config/CONFIG && ./vpnweb -caCrt test/files/ca.crt -caKey test/files/ca.key -notls clean: rm -f public/1/* rm public/ca.crt diff --git a/README.md b/README.md new file mode 100644 index 0000000..5848fb2 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +vpnweb +====== +A minimalistic webapp in Go to service API/JSON for the LEAP VPN + + + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..cb75645 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module 0xacab.org/leap/vpnweb + +go 1.12 diff --git a/main.go b/main.go index 8992dae..2f61ec8 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "flag" - "fmt" "log" "net/http" "os" @@ -13,11 +12,6 @@ import ( const keySize = 2048 const expiryDays = 28 -func errExit(errmsg string) { - fmt.Printf("ERROR: %s\n", errmsg) - os.Exit(1) -} - type certHandler struct { cainfo caInfo } @@ -26,12 +20,21 @@ func (ch *certHandler) certResponder(w http.ResponseWriter, r *http.Request) { ch.cainfo.CertWriter(w) } -func doFilesSanityCheck(caCrt string, caKey string) { +func doCaFilesSanityCheck(caCrt string, caKey string) { if _, err := os.Stat(caCrt); os.IsNotExist(err) { - errExit("cannot find caCrt file") + log.Fatal("cannot find caCrt file") } if _, err := os.Stat(caKey); os.IsNotExist(err) { - errExit("cannot find caKey file") + log.Fatal("cannot find caKey file") + } +} + +func doTlsFilesSanityCheck(tlsCrt string, tlsKey string) { + if _, err := os.Stat(tlsCrt); os.IsNotExist(err) { + log.Fatal("cannot find tlsCrt file") + } + if _, err := os.Stat(tlsKey); os.IsNotExist(err) { + log.Fatal("cannot find tlsKey file") } } @@ -41,27 +44,59 @@ func httpFileHandler(route string, path string) { }) } -func main() { - var caCrt = flag.String("caCrt", "", "path to the CA public key") - var caKey = flag.String("caKey", "", "path to the CA private key") - var port = flag.Int("port", 8000, "port where the server will listen") - var notls = flag.Bool("notls", false, "disable TLS on the service") - var tlskey = flag.String("tls_key", "", "path to the key file for TLS") - var tlscrt = flag.String("tls_crt", "", "path to the cert file for TLS") - flag.Parse() +type Opts struct { + caCrt string + caKey string + port int + notls bool + tlsCrt string + tlsKey string +} +func initializeFlags(opts *Opts) { + flag.StringVar(&opts.caCrt, "caCrt", "", "path to the CA public key") + flag.StringVar(&opts.caKey, "caKey", "", "path to the CA private key") + flag.IntVar(&opts.port, "port", 8000, "port where the server will listen") + flag.BoolVar(&opts.notls, "notls", false, "disable TLS on the service") + flag.StringVar(&opts.tlsCrt, "tls_crt", "", "path to the cert file for TLS") + flag.StringVar(&opts.tlsKey, "tls_key", "", "path to the key file for TLS") flag.Parse() - if *caCrt == "" { - errExit("missing caCrt parameter") + auth := os.Getenv("AUTH") + log.Println("AUTH-->", auth) + +} + +func checkConfigurationOptions(opts *Opts) { + + if opts.caCrt == "" { + log.Fatal("missing caCrt parameter") + } + if opts.caKey == "" { + log.Fatal("missing caKey parameter") + } + + if opts.notls == false { + if opts.tlsCrt == "" { + log.Fatal("missing tls_crt parameter. maybe use -notls?") + } + if opts.tlsKey == "" { + log.Fatal("missing tls_key parameter. maybe use -notls?") + } } - if *caKey == "" { - errExit("missing caKey parameter") + + doCaFilesSanityCheck(opts.caCrt, opts.caKey) + if opts.notls == false { + doTlsFilesSanityCheck(opts.tlsCrt, opts.tlsKey) } +} - doFilesSanityCheck(*caCrt, *caKey) +func main() { + opts := new(Opts) + initializeFlags(opts) + checkConfigurationOptions(opts) - ci := newCaInfo(*caCrt, *caKey) + ci := newCaInfo(opts.caCrt, opts.caKey) ch := certHandler{ci} // add routes here @@ -73,11 +108,12 @@ func main() { httpFileHandler("/ca.crt", "./public/ca.crt") httpFileHandler("/3/ca.crt", "./public/ca.crt") - pstr := ":" + strconv.Itoa(*port) + pstr := ":" + strconv.Itoa(opts.port) - if *notls == true { + if opts.notls == true { log.Fatal(http.ListenAndServe(pstr, nil)) } else { - log.Fatal(http.ListenAndServeTLS(pstr, *tlscrt, *tlskey, nil)) + log.Fatal(http.ListenAndServeTLS(pstr, opts.tlsCrt, opts.tlsKey, nil)) + } } -- cgit v1.2.3