From 05ac08d1b0f87de02d5d6c3144673994f2e2cb4b Mon Sep 17 00:00:00 2001 From: Kali Kaneko Date: Thu, 13 Jun 2019 12:43:30 +0200 Subject: some error handling --- Makefile | 8 ++++++++ main.go | 32 ++++++++++++++++++++++++++++++-- public/2 | 1 + test/1/configs.json | 1 + test/1/service.json | 1 + 5 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 Makefile create mode 120000 public/2 create mode 100644 test/1/configs.json create mode 100644 test/1/service.json diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..918b612 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +build: + go build +demo: + ./vpnweb -caCrt test/files/ca.crt -caKey test/files/ca.key +clean: + rm -f public/1/* +populate: + cp test/1/* public/1/ diff --git a/main.go b/main.go index 233a26d..ffa9c5c 100644 --- a/main.go +++ b/main.go @@ -2,12 +2,21 @@ package main import ( "flag" + "fmt" + "log" "net/http" + "os" + "strconv" ) const keySize = 2048 const expiryDays = 28 +func errExit(errmsg string) { + fmt.Printf("ERROR: %s\n", errmsg) + os.Exit(1) +} + type certHandler struct { cainfo caInfo } @@ -16,15 +25,34 @@ func (ch *certHandler) certResponder(w http.ResponseWriter, r *http.Request) { ch.cainfo.CertWriter(w) } +func doFilesSanityCheck(caCrt string, caKey string) { + if _, err := os.Stat(caCrt); os.IsNotExist(err) { + errExit("cannot find caCrt file") + } + if _, err := os.Stat(caKey); os.IsNotExist(err) { + errExit("cannot find caKey file") + } +} + 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") flag.Parse() + if *caCrt == "" { + errExit("missing caCrt parameter") + } + if *caKey == "" { + errExit("missing caKey parameter") + } + + doFilesSanityCheck(*caCrt, *caKey) + ci := newCaInfo(*caCrt, *caKey) ch := certHandler{ci} - http.HandleFunc("/cert", ch.certResponder) - http.ListenAndServe(":8000", nil) + http.HandleFunc("/1/cert", ch.certResponder) + log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*port), nil)) } diff --git a/public/2 b/public/2 new file mode 120000 index 0000000..56a6051 --- /dev/null +++ b/public/2 @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/test/1/configs.json b/test/1/configs.json new file mode 100644 index 0000000..2eb61a6 --- /dev/null +++ b/test/1/configs.json @@ -0,0 +1 @@ +{"services":{"eip":"/1/configs/eip-service.json"}} diff --git a/test/1/service.json b/test/1/service.json new file mode 100644 index 0000000..fa8a8cb --- /dev/null +++ b/test/1/service.json @@ -0,0 +1 @@ +{"name":"anonymous","description":"anonymous access to the VPN","eip_rate_limit":false} \ No newline at end of file -- cgit v1.2.3