From baaeaf19dfb9ccde786663318ec1a10edb4dba08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nagy=20K=C3=A1roly=20G=C3=A1briel?= Date: Wed, 23 Dec 2020 13:02:05 +0200 Subject: [PATCH] ranpas: remove all dependencies except stdlib MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nagy Károly Gábriel --- go.mod | 8 ---- go.sum | 50 ------------------------ main.go | 117 ++++++++++++++++++++++++++++++++------------------------ 3 files changed, 67 insertions(+), 108 deletions(-) diff --git a/go.mod b/go.mod index bd08229..d0d77ae 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,3 @@ module git.jpi.io/JPI/ranpass go 1.14 - -require ( - github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect - github.com/labstack/echo v3.3.10+incompatible - github.com/labstack/gommon v0.3.0 // indirect - github.com/valyala/fasttemplate v1.2.1 // indirect - golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect -) diff --git a/go.sum b/go.sum index f1057b7..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,50 +0,0 @@ -github.com/GeertJohan/go.incremental v1.0.0/go.mod h1:6fAjUhbVuX1KcMD3c8TEgVUqmo4seqhv0i0kdATSkM0= -github.com/GeertJohan/go.rice v1.0.2 h1:PtRw+Tg3oa3HYwiDBZyvOJ8LdIyf6lAovJJtr7YOAYk= -github.com/GeertJohan/go.rice v1.0.2/go.mod h1:af5vUNlDNkCjOZeSGFgIJxDje9qdjsO6hshx0gTmZt4= -github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c= -github.com/daaku/go.zipexe v1.0.0 h1:VSOgZtH418pH9L16hC/JrgSNJbbAL26pj7lmD1+CGdY= -github.com/daaku/go.zipexe v1.0.0/go.mod h1:z8IiR6TsVLEYKwXAoE/I+8ys/sDkgTzSL0CLnGVd57E= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dgrijalva/jwt-go v1.0.2 h1:KPldsxuKGsS2FPWsNeg9ZO18aCrGKujPoWXn2yo+KQM= -github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= -github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= -github.com/foolin/echo-template v0.0.0-20190415034849-543a88245eec h1:BurQ3oNjJkB5+zibQGHw97kZOBN/DLlAKYlvfsacBD0= -github.com/foolin/echo-template v0.0.0-20190415034849-543a88245eec/go.mod h1:4ePCtze3Ivy48ps0gJWfSByLgfqvT0rF+RpKQFe4Ja0= -github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= -github.com/labstack/echo v1.4.4 h1:1bEiBNeGSUKxcPDGfZ/7IgdhJJZx8wV/pICJh4W2NJI= -github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg= -github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s= -github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0= -github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= -github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU= -github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= -github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg= -github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= -github.com/nkovacs/streamquote v1.0.0/go.mod h1:BN+NaZ2CmdKqUuTUXUEm9j95B2TRbpOWpxbJYzzgUsc= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/sethvargo/go-password v0.2.0 h1:BTDl4CC/gjf/axHMaDQtw507ogrXLci6XRiLc7i/UHI= -github.com/sethvargo/go-password v0.2.0/go.mod h1:Ym4Mr9JXLBycr02MFuVQ/0JHidNetSgbzutTr3zsYXE= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= -github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= -github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4= -github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY= -golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= -golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index 6c4e62b..f1721b1 100644 --- a/main.go +++ b/main.go @@ -2,11 +2,10 @@ package main import ( "flag" + "fmt" + "log" "net/http" "strconv" - - "github.com/labstack/echo" - "github.com/labstack/echo/middleware" ) type passwordData struct { @@ -20,48 +19,75 @@ type passwordData struct { var d, defaults passwordData -func generatePassword(c echo.Context) error { - +func generatePassword(w http.ResponseWriter, r *http.Request) { var err error - d.Length, err = strconv.Atoi(c.FormValue("length")) - if err != nil { - d.Length = defaults.Length - } - - d.Digits, err = strconv.Atoi(c.FormValue("digits")) - if err != nil { - d.Digits = defaults.Digits - } - - d.Symbols, err = strconv.Atoi(c.FormValue("symbols")) - if err != nil { - d.Symbols = defaults.Symbols - } - - if c.FormValue("noupper") == "on" { - d.NoUpper = true + if r.URL.Path != "/" { + http.Error(w, "404 not found.", http.StatusNotFound) + return } - if c.FormValue("denyrepeat") == "on" { - d.DenyRepeat = true + switch r.Method { + case "GET": + d.Password, err = generate(defaults.Length, defaults.Digits, defaults.Symbols, false, false) + if err != nil { + d.Password = "Error: " + err.Error() + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(d.Password)) + return + } + w.WriteHeader(http.StatusOK) + w.Write([]byte(d.Password)) + case "POST": + if err = r.ParseForm(); err != nil { + w.WriteHeader(http.StatusUnprocessableEntity) + w.Write([]byte(fmt.Sprintf("ParseForm() err: %v", err))) + return + } + d.Length, err = strconv.Atoi(r.FormValue("length")) + if err != nil { + d.Length = defaults.Length + } + + d.Digits, err = strconv.Atoi(r.FormValue("digits")) + if err != nil { + d.Digits = defaults.Digits + } + + d.Symbols, err = strconv.Atoi(r.FormValue("symbols")) + if err != nil { + d.Symbols = defaults.Symbols + } + + if r.FormValue("noupper") == "on" { + d.NoUpper = true + } + + if r.FormValue("denyrepeat") == "on" { + d.DenyRepeat = true + } + + d.Password, err = generate(d.Length, d.Digits, d.Symbols, d.NoUpper, !d.DenyRepeat) + if err != nil { + d.Password = "Error: " + err.Error() + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(d.Password)) + return + } + if d.Length == 0 { + d.Password = "Error: password can not have zero length" + } + w.WriteHeader(http.StatusOK) + w.Write([]byte(d.Password)) + default: + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("Sorry, only GET and POST methods are supported.")) } - - // Generate a password - d.Password, err = generate(d.Length, d.Digits, d.Symbols, d.NoUpper, !d.DenyRepeat) - if err != nil { - d.Password = "Error: " + err.Error() - } - if d.Length == 0 { - d.Password = "Error: password can not have zero length" - } - - return c.String(http.StatusOK, d.Password) } -func checkHealth(c echo.Context) error { - s := `{"status":"OK"}` - return c.String(http.StatusOK, s) +func checkHealth(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte("Status:OK")) } func main() { @@ -77,17 +103,8 @@ func main() { defaults.Digits = *digitsPtr defaults.Symbols = *symbolsPtr - e := echo.New() - e.HideBanner = true - - // Middleware - e.Use(middleware.Logger()) - e.Use(middleware.Recover()) - - e.GET("/", generatePassword) - e.POST("/", generatePassword) - - e.GET("/health", checkHealth) + http.HandleFunc("/", generatePassword) + http.HandleFunc("/health", checkHealth) - e.Logger.Fatal(e.Start(":" + strconv.Itoa(*portPtr))) + log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*portPtr), nil)) }