From af4405550c6e3c3e9f02e896bf3571d1071bef86 Mon Sep 17 00:00:00 2001 From: Jarno Rankinen Date: Mon, 9 Oct 2023 21:08:26 +0300 Subject: [PATCH] v0.0.27 Added --disable-auth flag and disable_auth: true|false config options. Disables HTTP authentication. build script added to repo. Closes gh-31 --- .gitignore | 3 ++- build.sh | 12 ++++++++++++ go.mod | 5 ++--- go.sum | 7 +++---- main.go | 38 ++++++++++++++++++++++++++------------ 5 files changed, 45 insertions(+), 20 deletions(-) create mode 100755 build.sh diff --git a/.gitignore b/.gitignore index ccec959..55a5f41 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .vscode/ -build.sh +.idea/ BUILD/* +TMP/* diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..ec8882a --- /dev/null +++ b/build.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +VERSION=$(grep -e 'version.*=' main.go | awk '{print $3}' | tr -d '"') + +pushd TMP &> /dev/null || exit 1 + +rm -rf * +tar --exclude ../TMP -ch ../* | tar xf - + +env GOOS=linux GOARCH=arm go build -o ../BUILD/enervent-ctrl-${VERSION}.linux-arm32 . + +popd &> /dev/null \ No newline at end of file diff --git a/go.mod b/go.mod index 91ca9be..17e0c62 100644 --- a/go.mod +++ b/go.mod @@ -3,15 +3,13 @@ module github.com/0ranki/enervent-ctrl go 1.18 require ( - github.com/0ranki/https-go v0.0.0-20230314064508-ba9a558db433 + github.com/0ranki/https-go v0.0.0-20230314073101-4eca22af948c github.com/goburrow/modbus v0.1.0 github.com/gorilla/handlers v1.5.1 github.com/prometheus/client_golang v1.14.0 gopkg.in/yaml.v3 v3.0.1 ) -require golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect - require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect @@ -23,5 +21,6 @@ require ( github.com/prometheus/common v0.37.0 // indirect github.com/prometheus/procfs v0.8.0 // indirect golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect + golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect google.golang.org/protobuf v1.28.1 // indirect ) diff --git a/go.sum b/go.sum index 65d4509..6171c6a 100644 --- a/go.sum +++ b/go.sum @@ -31,8 +31,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/0ranki/https-go v0.0.0-20230314064508-ba9a558db433 h1:QT2IRJnhIdCSr26LJktnZnBpHdiLfTrUFzLSdP3h9Wo= -github.com/0ranki/https-go v0.0.0-20230314064508-ba9a558db433/go.mod h1:r4Jb05+PuiVKHDYwSsSBuSz4LpOlC2DgOY4N58+K8Hk= +github.com/0ranki/https-go v0.0.0-20230314073101-4eca22af948c h1:Tmui5U+C7KF4gYHnpXxe2sfROcrGksSmFheTVJAHdLo= +github.com/0ranki/https-go v0.0.0-20230314073101-4eca22af948c/go.mod h1:r4Jb05+PuiVKHDYwSsSBuSz4LpOlC2DgOY4N58+K8Hk= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -389,9 +389,8 @@ golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk= -golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= diff --git a/main.go b/main.go index 7878220..ef79dd0 100644 --- a/main.go +++ b/main.go @@ -30,7 +30,7 @@ import ( var static embed.FS var ( - version = "0.0.26" + version = "0.0.27" device pingvin.Pingvin config Conf usernamehash [32]byte @@ -42,6 +42,7 @@ type Conf struct { Port int `yaml:"port"` SslCertificate string `yaml:"ssl_certificate"` SslPrivatekey string `yaml:"ssl_privatekey"` + DisableAuth bool `yaml:"disable_auth"` Username string `yaml:"username"` Password string `yaml:"password"` Interval int `yaml:"interval"` @@ -52,9 +53,14 @@ type Conf struct { } // HTTP Basic Authentication middleware for http.HandlerFunc +// This is used for the API func authHandlerFunc(next http.HandlerFunc) http.HandlerFunc { // Based on https://www.alexedwards.net/blog/basic-authentication-in-go return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if config.DisableAuth { + next.ServeHTTP(w, r) + return + } user, pass, ok := r.BasicAuth() if ok { userHash := sha256.Sum256([]byte(user)) @@ -76,8 +82,13 @@ func authHandlerFunc(next http.HandlerFunc) http.HandlerFunc { } // HTTP Basic Authentication middleware for http.Handler +// Used for the HTML monitor views func authHandler(next http.Handler) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if config.DisableAuth { + next.ServeHTTP(w, r) + return + } user, pass, ok := r.BasicAuth() if ok { userHash := sha256.Sum256([]byte(user)) @@ -277,17 +288,18 @@ func parseConfigFile() { // Write the default configuration to $HOME/.config/enervent-ctrl/configuration.yaml func initDefaultConfig(confpath string) { config = Conf{ - "/dev/ttyS0", - 8888, - confpath + "/certificate.pem", - confpath + "/privatekey.pem", - "device", - "enervent", - 4, - false, - "", - false, - false, + SerialAddress: "/dev/ttyS0", + Port: 8888, + SslCertificate: confpath + "/certificate.pem", + SslPrivatekey: confpath + "/privatekey.pem", + DisableAuth: false, + Username: "pingvin", + Password: "enervent", + Interval: 4, + EnableMetrics: false, + LogAccess: false, + LogFile: "", + Debug: false, } conffile := confpath + "/configuration.yaml" confbytes, err := yaml.Marshal(&config) @@ -309,6 +321,7 @@ func configure() { generatecert := flag.Bool("regenerate-certs", false, "Generate a new SSL certificate. A new one is generated on startup as `~/.config/enervent-ctrl/server.crt` if it doesn't exist.") certflag := flag.String("cert", config.SslCertificate, "Path to SSL public key to use for HTTPS") keyflag := flag.String("key", config.SslPrivatekey, "Path to SSL private key to use for HTTPS") + noauthflag := flag.Bool("disable-auth", config.DisableAuth, "Disable HTTP basic authentication") usernflag := flag.String("username", config.Username, "Username for HTTP Basic Authentication") passwflag := flag.String("password", config.Password, "Password for HTTP Basic Authentication") promflag := flag.Bool("enable-metrics", config.EnableMetrics, "Enable the built-in Prometheus exporter") @@ -321,6 +334,7 @@ func configure() { config.LogAccess = *logaccflag config.SslCertificate = *certflag config.SslPrivatekey = *keyflag + config.DisableAuth = *noauthflag config.Username = *usernflag config.Password = *passwflag config.EnableMetrics = *promflag