From 0ffd6b9b944799581af7ff38344b6e9fde782afe Mon Sep 17 00:00:00 2001 From: Kenneth Date: Sat, 10 May 2025 15:28:55 +0100 Subject: [PATCH] put vapid keygen behind a flag --- .idea/runConfigurations/Generate_VAPID.xml | 3 +- ...ym_sh_7am.xml => Start_server_on_8080.xml} | 2 +- generate_vapid.go | 30 ------------------- main.go | 19 ++++++++++++ 4 files changed, 22 insertions(+), 32 deletions(-) rename .idea/runConfigurations/{go_build_code_nym_sh_7am.xml => Start_server_on_8080.xml} (67%) delete mode 100644 generate_vapid.go diff --git a/.idea/runConfigurations/Generate_VAPID.xml b/.idea/runConfigurations/Generate_VAPID.xml index 327648a..95591c1 100644 --- a/.idea/runConfigurations/Generate_VAPID.xml +++ b/.idea/runConfigurations/Generate_VAPID.xml @@ -2,7 +2,8 @@ - + + diff --git a/.idea/runConfigurations/go_build_code_nym_sh_7am.xml b/.idea/runConfigurations/Start_server_on_8080.xml similarity index 67% rename from .idea/runConfigurations/go_build_code_nym_sh_7am.xml rename to .idea/runConfigurations/Start_server_on_8080.xml index 135d74f..0ea8778 100644 --- a/.idea/runConfigurations/go_build_code_nym_sh_7am.xml +++ b/.idea/runConfigurations/Start_server_on_8080.xml @@ -1,5 +1,5 @@ - + diff --git a/generate_vapid.go b/generate_vapid.go deleted file mode 100644 index 83aab1e..0000000 --- a/generate_vapid.go +++ /dev/null @@ -1,30 +0,0 @@ -package main - -import ( - "github.com/SherClockHolmes/webpush-go" - "log" - "os" -) - -func main() { - pub, priv, err := webpush.GenerateVAPIDKeys() - if err != nil { - log.Fatal(err) - } - - f, err := os.OpenFile("vapid_public_key", os.O_CREATE|os.O_WRONLY, 0644) - if err != nil { - log.Fatal(err) - } - defer f.Close() - - f.Write([]byte(pub)) - - f, err = os.OpenFile("vapid_private_key", os.O_CREATE|os.O_WRONLY, 0644) - if err != nil { - log.Fatal(err) - } - defer f.Close() - - f.Write([]byte(priv)) -} diff --git a/main.go b/main.go index 3d0e8f0..1e70b6a 100644 --- a/main.go +++ b/main.go @@ -102,6 +102,14 @@ var supportedLocations = map[string]location{ func main() { port := flag.Int("port", 8080, "the port that the server should listen on") + genKeys := flag.Bool("generate-vapid-keys", false, "generate a new vapid key pair, which will be outputted to stdout.") + + flag.Parse() + + if *genKeys { + generateKeys() + return + } err := godotenv.Load() if err != nil { @@ -201,6 +209,17 @@ func main() { } } +func generateKeys() { + priv, pub, err := webpush.GenerateVAPIDKeys() + if err != nil { + log.Fatal(err) + } + + fmt.Println("all keys are base64 url encoded.") + fmt.Printf("public key: %v\n", pub) + fmt.Printf("private key: %v\n", priv) +} + func handleHTTPRequest(state *state) http.HandlerFunc { return func(writer http.ResponseWriter, request *http.Request) { path := strings.TrimPrefix(request.URL.Path, "/")