From e1afb206ae8a1d8fdc0d544c2724df325dc63e35 Mon Sep 17 00:00:00 2001 From: m15o Date: Sat, 12 Feb 2022 16:14:46 +0100 Subject: [PATCH] formatting --- config/cfg.go | 4 +++ main.go | 7 ++++- vpub/vpub.go | 45 +++++++++++++++++++++++++++++ web/handler/common.go | 2 +- web/handler/handler.go | 6 +++- web/handler/html.go | 7 +++++ web/handler/html/common/layout.html | 2 +- web/handler/html/forum-key.html | 6 ++++ web/handler/html/index.html | 1 + web/handler/settings_show.go | 18 ++++++++++++ web/web.go | 5 ++-- 11 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 vpub/vpub.go create mode 100644 web/handler/html/forum-key.html diff --git a/config/cfg.go b/config/cfg.go index 76c2d98..1ddb182 100644 --- a/config/cfg.go +++ b/config/cfg.go @@ -5,6 +5,8 @@ import "os" type ( Config struct { DatabaseURL string + VpubDatabaseURL string + VpubAESKey string SessionKey string Env string CertFile string @@ -22,6 +24,8 @@ type ( func New() *Config { return &Config{ DatabaseURL: os.Getenv("DATABASE_URL"), + VpubDatabaseURL: os.Getenv("VPUB_DATABASE_URL"), + VpubAESKey: os.Getenv("VPUB_AES_KEY"), SessionKey: os.Getenv("SESSION_KEY"), Env: os.Getenv("ENV"), CertFile: os.Getenv("CERT_FILE"), diff --git a/main.go b/main.go index 200c4ba..a36b1fa 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "log" "status/config" "status/storage" + "status/vpub" "status/web" ) @@ -15,7 +16,11 @@ func main() { log.Fatal(err) } data := storage.New(db) + v, err := vpub.New(cfg.VpubDatabaseURL, []byte(cfg.VpubAESKey)) + if err != nil { + log.Fatal(err) + } log.Fatal( - web.Serve(data, cfg), + web.Serve(data, v, cfg), ) } diff --git a/vpub/vpub.go b/vpub/vpub.go new file mode 100644 index 0000000..9a828a5 --- /dev/null +++ b/vpub/vpub.go @@ -0,0 +1,45 @@ +package vpub + +import ( + "crypto/aes" + "database/sql" + "encoding/hex" + "fmt" +) + +type Vpub struct { + db *sql.DB + key []byte +} + +func New(databaseURL string, key []byte) (Vpub, error) { + db, err := sql.Open("postgres", databaseURL) + return Vpub{db: db, key: key}, err +} + +func EncryptAES(key []byte, plaintext string) string { + c, err := aes.NewCipher(key) + if err != nil { + fmt.Println(err) + return "" + } + out := make([]byte, len(plaintext)) + c.Encrypt(out, []byte(plaintext)) + return hex.EncodeToString(out) +} + +func (v Vpub) FindOrCreateKey(name string) string { + padded := fmt.Sprintf("%16s", name) + key := EncryptAES(v.key, padded)[0:20] + + query := ` + insert into keys (key) values ($1) ON CONFLICT do nothing + ` + + _, err := v.db.Exec(query, key[:20]) + if err != nil { + fmt.Println(err) + } + + return key +} diff --git a/web/handler/common.go b/web/handler/common.go index 45a3bf9..932222b 100644 --- a/web/handler/common.go +++ b/web/handler/common.go @@ -27,7 +27,7 @@ var TplCommonMap = map[string]string{