From 03bbf440c7d92c08982f255a1ac231323e55e334 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Wed, 14 Sep 2022 15:55:17 +0100 Subject: [PATCH 01/25] Initial OAuth draft. --- go.mod | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/go.mod b/go.mod index 07e44b4..6e424a5 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.18 require ( github.com/fsnotify/fsnotify v1.5.4 // indirect github.com/goccy/go-json v0.9.10 // indirect + github.com/golang/protobuf v1.5.2 // indirect github.com/gorilla/feeds v1.1.1 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/kataras/hcaptcha v0.0.0-20200711031247-2927d4faf32f // indirect @@ -18,8 +19,12 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/viper v1.12.0 // indirect github.com/subosito/gotenv v1.3.0 // indirect + golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect + golang.org/x/oauth2 v0.0.0-20220909003341-f21342109be1 //indirect golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect golang.org/x/text v0.3.7 // indirect + google.golang.org/appengine v1.6.7 // indirect + google.golang.org/protobuf v1.28.0 // indirect gopkg.in/ini.v1 v1.66.4 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect From 5244119ffed0e89c63c6dbe3a4ff11619f482970 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Wed, 14 Sep 2022 15:56:59 +0100 Subject: [PATCH 02/25] Initial OAuth Draft --- api/oauth.go | 56 +++++++++++++++++++++++++++++++++++++ config/oauthclientid.go | 18 ++++++++++++ config/oauthclientsecret.go | 18 ++++++++++++ config/oauthredirect.go | 18 ++++++++++++ config/oauthurl.go | 18 ++++++++++++ data/config.example.toml | 8 +++++- go.sum | 13 +++++++++ 7 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 api/oauth.go create mode 100644 config/oauthclientid.go create mode 100644 config/oauthclientsecret.go create mode 100644 config/oauthredirect.go create mode 100644 config/oauthurl.go diff --git a/api/oauth.go b/api/oauth.go new file mode 100644 index 0000000..4d8239c --- /dev/null +++ b/api/oauth.go @@ -0,0 +1,56 @@ +package oauth + +import ( + "errors" + "io" + "io/ioutil" + "log" + "net/http" + "os" + "time" + "x/oauth2" + "github.com/ProjectSegfault/segfautils/config" + "github.com/goccy/go-json" +) + +var ( + clientID = config.OAuthClientID() + clientSecret = config.OAuthClientSecret() + redirectURL = config.RedirectURL() + authURL = config.AuthURL() +) + +func LoginOAuth() { +// Create a new redirect route route +http.HandleFunc("/oauth/redirect", func(w http.ResponseWriter, r *http.Request) { + err := r.ParseForm() + if err != nil { + fmt.Fprintf(os.Stdout, "could not parse query: %v", err) + w.WriteHeader(http.StatusBadRequest) + } + code := r.FormValue("code") + reqURL := fmt.Sprintf("%s/login/oauth/access_token?client_id=%s&client_secret=%s&code=%s", authURL, clientID, clientSecret, code) + req, err := http.NewRequest(http.MethodPost, reqURL, nil) + if err != nil { + fmt.Fprintf(os.Stdout, "could not create HTTP request: %v", err) + w.WriteHeader(http.StatusBadRequest) + } + req.Header.Set("accept", "application/json") + res, err := httpClient.Do(req) + if err != nil { + fmt.Fprintf(os.Stdout, "could not send HTTP request: %v", err) + w.WriteHeader(http.StatusInternalServerError) + } + defer res.Body.Close() + var t OAuthAccessResponse + if err := json.NewDecoder(res.Body).Decode(&t); err != nil { + fmt.Fprintf(os.Stdout, "could not parse JSON response: %v", err) + w.WriteHeader(http.StatusBadRequest) + } + w.Header().Set("Location", "/announcements/?access_token="+t.AccessToken) + w.WriteHeader(http.StatusFound) + } + + type OAuthAccessResponse struct { + AccessToken string `json:"access_token"` + } diff --git a/config/oauthclientid.go b/config/oauthclientid.go new file mode 100644 index 0000000..9aaa4e8 --- /dev/null +++ b/config/oauthclientid.go @@ -0,0 +1,18 @@ +package config + +import ( + "log" + + "github.com/spf13/viper" +) + +func OAuthClientID() string { + viper.SetConfigName("config") + viper.AddConfigPath("./data") + err := viper.ReadInConfig() + if err != nil { + log.Println("Error reading config for getting oauth.client_id", err.Error()) + } + result := viper.GetString("oauth.client_id") + return result +} diff --git a/config/oauthclientsecret.go b/config/oauthclientsecret.go new file mode 100644 index 0000000..851a267 --- /dev/null +++ b/config/oauthclientsecret.go @@ -0,0 +1,18 @@ +package config + +import ( + "log" + + "github.com/spf13/viper" +) + +func OAuthClientSecret() string { + viper.SetConfigName("config") + viper.AddConfigPath("./data") + err := viper.ReadInConfig() + if err != nil { + log.Println("Error reading config for getting oauth.client_secret", err.Error()) + } + result := viper.GetString("oauth.client_secret") + return result +} diff --git a/config/oauthredirect.go b/config/oauthredirect.go new file mode 100644 index 0000000..32b839a --- /dev/null +++ b/config/oauthredirect.go @@ -0,0 +1,18 @@ +package config + +import ( + "log" + + "github.com/spf13/viper" +) + +func RedirectURL() string { + viper.SetConfigName("config") + viper.AddConfigPath("./data") + err := viper.ReadInConfig() + if err != nil { + log.Println("Error reading config for getting oauth.redirect_url", err.Error()) + } + result := viper.GetString("oauth.redirect_url") + return result +} diff --git a/config/oauthurl.go b/config/oauthurl.go new file mode 100644 index 0000000..c89db10 --- /dev/null +++ b/config/oauthurl.go @@ -0,0 +1,18 @@ +package config + +import ( + "log" + + "github.com/spf13/viper" +) + +func AuthURL() string { + viper.SetConfigName("config") + viper.AddConfigPath("./data") + err := viper.ReadInConfig() + if err != nil { + log.Println("Error reading config for getting oauth.auth_url", err.Error()) + } + result := viper.GetString("oauth.auth_url") + return result +} diff --git a/data/config.example.toml b/data/config.example.toml index 84aad0c..5773ebd 100644 --- a/data/config.example.toml +++ b/data/config.example.toml @@ -5,4 +5,10 @@ auth_token = "YOURAUTHTOKEN" [hcaptcha] site_key = "YOURSITEKEY" -secret_key = "YOURSECRETKEY" \ No newline at end of file +secret_key = "YOURSECRETKEY" + +[oauth] +client_id = "YOURCLIENTID" +client_secret = "YOURCLIENTSECRET" +redirect_url = "YOURREDIRECTURL" +auth_url = "YOURAUTHURL" \ No newline at end of file diff --git a/go.sum b/go.sum index 45f95d5..f22f5d3 100644 --- a/go.sum +++ b/go.sum @@ -86,6 +86,9 @@ github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvq github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= @@ -97,6 +100,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -244,6 +248,8 @@ golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwY golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e h1:TsQ7F31D3bUCLeqPT0u+yjp1guoArKaNKmCr22PYgTQ= +golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -253,6 +259,8 @@ golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43/go.mod h1:KelEdhl1UZF7XfJ golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= +golang.org/x/oauth2 v0.0.0-20220909003341-f21342109be1 h1:lxqLZaMad/dJHMFZH0NiNpiEZI/nhgWhe4wgzpE+MuA= +golang.org/x/oauth2 v0.0.0-20220909003341-f21342109be1/go.mod h1:h4gKUeWbJ4rQPri7E0u6Gs4e9Ri2zaLxzw5DI5XGrYg= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -388,6 +396,7 @@ google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7 google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0= google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/appengine v1.6.6/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= +google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= @@ -451,6 +460,10 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2 google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= +google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= From 31556d3a6b452abad04f83b20fc58e74026fb3ea Mon Sep 17 00:00:00 2001 From: Midou36O Date: Fri, 16 Sep 2022 23:10:37 +0100 Subject: [PATCH 03/25] Broken mess, got to fix. --- api/oauth.go | 49 ++------------------- config/oauthredirect.go | 2 +- config/oauthurl.go | 2 +- data/lol.toml | 8 ++++ readme.md | 6 +-- utils/checkconfig.go | 95 +++++++++++++++++++++++++++++------------ 6 files changed, 85 insertions(+), 77 deletions(-) create mode 100644 data/lol.toml diff --git a/api/oauth.go b/api/oauth.go index 4d8239c..4cb37bc 100644 --- a/api/oauth.go +++ b/api/oauth.go @@ -1,56 +1,15 @@ -package oauth +package api import ( - "errors" - "io" - "io/ioutil" - "log" - "net/http" - "os" - "time" - "x/oauth2" "github.com/ProjectSegfault/segfautils/config" - "github.com/goccy/go-json" ) var ( clientID = config.OAuthClientID() clientSecret = config.OAuthClientSecret() - redirectURL = config.RedirectURL() - authURL = config.AuthURL() + redirectURL = config.OAuthRedirectURL() + authURL = config.OAuthURL() ) func LoginOAuth() { -// Create a new redirect route route -http.HandleFunc("/oauth/redirect", func(w http.ResponseWriter, r *http.Request) { - err := r.ParseForm() - if err != nil { - fmt.Fprintf(os.Stdout, "could not parse query: %v", err) - w.WriteHeader(http.StatusBadRequest) - } - code := r.FormValue("code") - reqURL := fmt.Sprintf("%s/login/oauth/access_token?client_id=%s&client_secret=%s&code=%s", authURL, clientID, clientSecret, code) - req, err := http.NewRequest(http.MethodPost, reqURL, nil) - if err != nil { - fmt.Fprintf(os.Stdout, "could not create HTTP request: %v", err) - w.WriteHeader(http.StatusBadRequest) - } - req.Header.Set("accept", "application/json") - res, err := httpClient.Do(req) - if err != nil { - fmt.Fprintf(os.Stdout, "could not send HTTP request: %v", err) - w.WriteHeader(http.StatusInternalServerError) - } - defer res.Body.Close() - var t OAuthAccessResponse - if err := json.NewDecoder(res.Body).Decode(&t); err != nil { - fmt.Fprintf(os.Stdout, "could not parse JSON response: %v", err) - w.WriteHeader(http.StatusBadRequest) - } - w.Header().Set("Location", "/announcements/?access_token="+t.AccessToken) - w.WriteHeader(http.StatusFound) - } - - type OAuthAccessResponse struct { - AccessToken string `json:"access_token"` - } +} diff --git a/config/oauthredirect.go b/config/oauthredirect.go index 32b839a..da32360 100644 --- a/config/oauthredirect.go +++ b/config/oauthredirect.go @@ -6,7 +6,7 @@ import ( "github.com/spf13/viper" ) -func RedirectURL() string { +func OAuthRedirectURL() string { viper.SetConfigName("config") viper.AddConfigPath("./data") err := viper.ReadInConfig() diff --git a/config/oauthurl.go b/config/oauthurl.go index c89db10..852f1f2 100644 --- a/config/oauthurl.go +++ b/config/oauthurl.go @@ -6,7 +6,7 @@ import ( "github.com/spf13/viper" ) -func AuthURL() string { +func OAuthURL() string { viper.SetConfigName("config") viper.AddConfigPath("./data") err := viper.ReadInConfig() diff --git a/data/lol.toml b/data/lol.toml new file mode 100644 index 0000000..940a54b --- /dev/null +++ b/data/lol.toml @@ -0,0 +1,8 @@ +[segfautils] +port = 6893 +webhook_url = "https://127.0.0.1:8080/segfautils" +auth_token = "121212" + +[hcaptcha] +site_key = "lol" +secret_key = "lol2" diff --git a/readme.md b/readme.md index f55bbc9..b7c63f1 100644 --- a/readme.md +++ b/readme.md @@ -2,7 +2,7 @@ Web utilities for Project Segfault ## What does it do? -For now it powers our contact form. In the future we will expand our APIs so you can do more cool things. +For now it powers our contact form and our announcements page. In the future we will expand our APIs so you can do more cool things. (We currently do not have any new idea so please open issues and give us suggestions!!) ## Setup @@ -20,8 +20,8 @@ If you're using Portainer, you should know how to add Segfautils. ``` git clone https://github.com/ProjectSegfault/segfautils cd segfautils/ -# You need to add the environment HCAPTCHA_SITE_KEY, HCAPTCHA_SECRET_KEY, SEGFAUTILS_WEBHOOK_URL and SEGFAUTILS_PORT. +# You need to config file located in data/config.toml. go run main.go # Run this when you've done above, and you're planning on developing, if not, do below go build . -o segfautils ./segfautils -``` \ No newline at end of file +``` diff --git a/utils/checkconfig.go b/utils/checkconfig.go index 34e1088..ca7911d 100644 --- a/utils/checkconfig.go +++ b/utils/checkconfig.go @@ -2,37 +2,78 @@ package utils import ( - "log" + "fmt" + "os" +) - "github.com/ProjectSegfault/segfautils/config" +var ( + is = 0 + arrName = [9]string{"Port", "Authentication token", "Webhook URL", "HCaptcha secret", "Hcaptcha site key", "OAuth client ID", "OAuth client secret", "OAuth redirect URL", "OAuth athentication URL"} + arrCfg = [9]string{"0", "YOURAUTHTOKEN", "YOURWEBHOOKURL", "YOURSECRETKEY", "YOURSITEKEY", "YOURCLIENTID", "YOURCLIENTSECRET", "YOURREDIRECTURL", "YOURAUTHURL"} + testCfg = [9]string{"config.Authtoken()", "config.WebhookURL()", "config.HCaptchaSecretKey()", "config.HCaptchaSiteKey()", "config.OAuthClientID()", "config.OAuthClientSecret()", "config.OAuthRedirectURL()", "config.OAuthURL()"} ) func CheckConfig() { - if config.Port() == "0" { - log.Fatal("[Segfautils] ❌ You need to set the port you'd like to use in the config file. Check documentation for more information.") - } else { - log.Println("[Segfautils] ✅ segfautils.port is set to", config.Port()) - } - if config.AuthToken() == "YOURAUTHTOKEN" || config.AuthToken() == "" { - log.Fatal("[Segfautils] ❌ You need to set the authentication token you'd like to use in the config file. Check documentation for more information.") - } else { - log.Println("[Segfautils] ✅ segfautils.auth_token is set!") - } - if config.WebhookURL() == "YOURWEBHOOKURL" || config.WebhookURL() == "" { - log.Fatal("[Segfautils] ❌ You need to set the Webhook URL you'd like to use in the config file. Check documentation for more information.") - } else { - log.Println("[Segfautils] ✅ segfautils.webhook_url is set!") - } - // Hcaptcha stuff - if config.HCaptchaSecretKey() == "YOURSECRETKEY" || config.HCaptchaSecretKey() == "" { - log.Fatal("[Segfautils] ❌ You need to set the HCaptcha secret you'd like to use in the config file. Check documentation for more information.") - } else { - log.Println("[Segfautils] ✅ segfautils.hcaptcha_secret is set!") + + for i := 0; i < 8; i++ { + if testCfg[i] == arrCfg[i] || testCfg[i] == "" { + fmt.Println(arrCfg[i]) + is = i + Fail() + } else { + fmt.Println(arrCfg[i]) + fmt.Println(testCfg[i]) + is = i + Check() + } } - if config.HCaptchaSiteKey() == "YOURSITEKEY" || config.HCaptchaSiteKey() == "" { - log.Println("[Segfautils] ⚠️ The HCaptcha site key isn't set. You don't have to, but the demo form will not work without it. Check documentation for more information.") - } else { - log.Println("[Segfautils] ✅ hcaptcha.site_key is set!") +} + +/* + func CheckConfig() { + if config.Port() == "0" { + Fail() + } else { + Check() + } + if config.AuthToken() == "YOURAUTHTOKEN" || config.AuthToken() == "" { + log.Fatal("[Segfautils] ❌ You need to set the authentication token you'd like to use in the config file. Check documentation for more information.") + } else { + log.Println("[Segfautils] ✅ segfautils.auth_token is set!") + } + if config.WebhookURL() == "YOURWEBHOOKURL" || config.WebhookURL() == "" { + log.Fatal("[Segfautils] ❌ You need to set the Webhook URL you'd like to use in the config file. Check documentation for more information.") + } else { + log.Println("[Segfautils] ✅ segfautils.webhook_url is set!") + } + // Hcaptcha stuff + if config.HCaptchaSecretKey() == "YOURSECRETKEY" || config.HCaptchaSecretKey() == "" { + log.Fatal("[Segfautils] ❌ You need to set the HCaptcha secret you'd like to use in the config file. Check documentation for more information.") + } else { + log.Println("[Segfautils] ✅ segfautils.hcaptcha_secret is set!") + } + if config.HCaptchaSiteKey() == "YOURSITEKEY" || config.HCaptchaSiteKey() == "" { + log.Println("[Segfautils] ⚠️ The HCaptcha site key isn't set. You don't have to, but the demo form will not work without it. Check documentation for more information.") + } else { + log.Println("[Segfautils] ✅ hcaptcha.site_key is set!") + } + log.Println("[segfautils] ✅ All config checks passed!") + log.Println("[segfautils] ✅ Listening at port ", config.Port()) } - log.Println("[Segfautils] ✅ All config checks passed!") +*/ +func Check() { + fmt.Printf("[Segfautils] ✅ %s is set!\n", arrName[is]) +} + +func Fail() { + fmt.Printf("[Segfautils] ❌ You need to set the %s you'd like to use in the config file. Check documentation for more information.\n", arrName[is]) + os.Exit(Error()) +} + +func OptionalFail() { + fmt.Printf("[Segfautils] ⚠️ The %s isn't set. You don't have to, but the program will not be able to behave correctly. Check documentation for more information.\n", arrName[is]) +} + +func Error() int { + return 12 } From 34bcc3d603d47d5512cbafe683380affb5dddff9 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Fri, 16 Sep 2022 23:16:29 +0100 Subject: [PATCH 04/25] fix woodpecker --- .woodpecker.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.woodpecker.yml b/.woodpecker.yml index d7ada65..d2f0fb4 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -2,11 +2,13 @@ pipeline: build: when: event: [push, pull_request, tag, deployment] + image: golang:latest commands: - go build -o segfautils dockerize_n_publish: when: + branch: [master] event: [push] name: dockerize and publish image: plugins/docker From c5b3c2bd9893118ee8650587f57ddfde51550d97 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sat, 17 Sep 2022 21:50:46 +0100 Subject: [PATCH 05/25] Initial work. --- api/settings.go | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 api/settings.go diff --git a/api/settings.go b/api/settings.go new file mode 100644 index 0000000..eed2485 --- /dev/null +++ b/api/settings.go @@ -0,0 +1,90 @@ +package api + +import ( + "errors" + "io" + "io/ioutil" + "log" + "net/http" + "os" + + "github.com/ProjectSegfault/segfautils/config" + "github.com/goccy/go-json" +) + +var ( + announcements = config.OptAnn() + form = config.OptForm() +) + +func Settings() { + CheckSet() + http.HandleFunc("/api/options", getOpt) +} + +func CheckSet() { + os.Remove("./data/options.json") + if form == "true" && announcements == "false" { + data := map[string]interface{}{ + "Announcements": "false", + "Form": "true", + } + + jsonData, err := json.Marshal(data) + if err != nil { + log.Printf("Could not marshal json : %s\n", err) + return + } + + ioutil.WriteFile("./data/options.json", jsonData, os.ModePerm) + + } else if form == "true" && announcements == "true" { + data := map[string]interface{}{ + "Announcements": "true", + "Form": "true", + } + + jsonData, err := json.Marshal(data) + if err != nil { + log.Printf("Could not marshal json : %s\n", err) + return + } + + ioutil.WriteFile("./data/options.json", jsonData, os.ModePerm) + + } else if form == "false" && announcements == "true" { + data := map[string]interface{}{ + "Announcements": "true", + "Form": "false", + } + + jsonData, err := json.Marshal(data) + if err != nil { + log.Printf("Could not marshal json : %s\n", err) + return + } + + ioutil.WriteFile("./data/options.json", jsonData, os.ModePerm) + + } else { + resp := []byte("The fuck do you want me to do then?") + ioutil.WriteFile("./data/options.json", resp, os.ModePerm) + } +} +func getOpt(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + if _, err := os.Stat("./data/options.json"); errors.Is(err, os.ErrNotExist) { + http.Error(w, "There is nothing to see here.", http.StatusNotFound) + return + } else { + f, err := os.Open("./data/options.json") + if err != nil { + log.Fatal(err) + } + defer f.Close() + io.Copy(w, f) + } +} From dc69cc447de18c2dd40e3307348dbd9ba6ece053 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sat, 17 Sep 2022 21:50:55 +0100 Subject: [PATCH 06/25] Initial work --- api/announcements.go | 18 ++++++++++++++++++ config/optionannounce.go | 18 ++++++++++++++++++ config/optionform.go | 18 ++++++++++++++++++ data/config.example.toml | 6 +++++- data/options.json | 1 + main.go | 3 ++- 6 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 config/optionannounce.go create mode 100644 config/optionform.go create mode 100755 data/options.json diff --git a/api/announcements.go b/api/announcements.go index d6703dc..7200f7f 100644 --- a/api/announcements.go +++ b/api/announcements.go @@ -2,6 +2,7 @@ package api import ( "errors" + "fmt" "io" "io/ioutil" "log" @@ -17,6 +18,23 @@ var ( authToken = config.AuthToken() ) +func CheckAnn() { + jsonFile, err := os.Open("./data/options.json") + if err != nil { + fmt.Println(err) + } + defer jsonFile.Close() + byteValue, _ := ioutil.ReadAll(jsonFile) + var result map[string]interface{} + json.Unmarshal([]byte(byteValue), &result) + res := result["Announcements"] + if res == "true" { + Announcements() + } else { + log.Println("Announcements disabled") + } +} + func Announcements() { http.HandleFunc("/api/announcements", getAnnouncements) http.HandleFunc("/api/announcements/post", handleAnnouncements) diff --git a/config/optionannounce.go b/config/optionannounce.go new file mode 100644 index 0000000..06b1836 --- /dev/null +++ b/config/optionannounce.go @@ -0,0 +1,18 @@ +package config + +import ( + "log" + + "github.com/spf13/viper" +) + +func OptForm() string { + viper.SetConfigName("config") + viper.AddConfigPath("./data") + err := viper.ReadInConfig() + if err != nil { + log.Println("Error reading config for getting options.form", err.Error()) + } + result := viper.GetString("options.form") + return result +} diff --git a/config/optionform.go b/config/optionform.go new file mode 100644 index 0000000..1f81ea7 --- /dev/null +++ b/config/optionform.go @@ -0,0 +1,18 @@ +package config + +import ( + "log" + + "github.com/spf13/viper" +) + +func OptAnn() string { + viper.SetConfigName("config") + viper.AddConfigPath("./data") + err := viper.ReadInConfig() + if err != nil { + log.Println("Error reading config for getting options.announce", err.Error()) + } + result := viper.GetString("options.announce") + return result +} diff --git a/data/config.example.toml b/data/config.example.toml index 84aad0c..a172f14 100644 --- a/data/config.example.toml +++ b/data/config.example.toml @@ -5,4 +5,8 @@ auth_token = "YOURAUTHTOKEN" [hcaptcha] site_key = "YOURSITEKEY" -secret_key = "YOURSECRETKEY" \ No newline at end of file +secret_key = "YOURSECRETKEY" + +[options] +announce = true +form = false \ No newline at end of file diff --git a/data/options.json b/data/options.json new file mode 100755 index 0000000..3866b67 --- /dev/null +++ b/data/options.json @@ -0,0 +1 @@ +{"Announcements":"false","Form":"true"} \ No newline at end of file diff --git a/main.go b/main.go index a5a5ca8..810f05f 100644 --- a/main.go +++ b/main.go @@ -46,8 +46,9 @@ func main() { http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "static/announcements.html") }) + api.Settings() api.Form() - api.Announcements() + api.CheckAnn() log.Println("[HTTP] HTTP server is now running at " + config.Port() + "!") log.Println(http.ListenAndServe(":"+config.Port(), nil)) } From 427bec5081e25474914fd6b576098b39d26cc758 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sat, 17 Sep 2022 21:55:50 +0100 Subject: [PATCH 07/25] make it better --- api/announcements.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/api/announcements.go b/api/announcements.go index 7200f7f..9d212c1 100644 --- a/api/announcements.go +++ b/api/announcements.go @@ -32,6 +32,9 @@ func CheckAnn() { Announcements() } else { log.Println("Announcements disabled") + http.HandleFunc("/api/announcements", func(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "Disabled") + }) } } From 11a792d96c4c02e0e2998bc3964fef78d3e85aee Mon Sep 17 00:00:00 2001 From: Odyssey Date: Sat, 17 Sep 2022 23:03:47 +0200 Subject: [PATCH 08/25] improve grammar here Signed-off-by: Odyssey --- config/authtoken.go | 2 +- config/hcaptchasecretkey.go | 2 +- config/hcaptchasitekey.go | 2 +- config/optionannounce.go | 2 +- config/optionform.go | 2 +- config/port.go | 2 +- config/webhook.go | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/config/authtoken.go b/config/authtoken.go index f9b7b2f..e332783 100644 --- a/config/authtoken.go +++ b/config/authtoken.go @@ -15,7 +15,7 @@ func AuthToken() string { viper.AddConfigPath("./data") err := viper.ReadInConfig() if err != nil { - log.Println("Error reading config for getting segfautils.auth_token", err.Error()) + log.Println("Error reading config. Error getting: segfautils.auth_token", err.Error()) } result := viper.GetString("segfautils.auth_token") return result diff --git a/config/hcaptchasecretkey.go b/config/hcaptchasecretkey.go index e1cedca..b4834ed 100644 --- a/config/hcaptchasecretkey.go +++ b/config/hcaptchasecretkey.go @@ -11,7 +11,7 @@ func HCaptchaSecretKey() string { viper.AddConfigPath("./data") err := viper.ReadInConfig() if err != nil { - log.Println("Error reading config for getting hcaptcha.secret_key", err.Error()) + log.Println("Error reading config. Error getting: hcaptcha.secret_key", err.Error()) } result := viper.GetString("hcaptcha.secret_key") return result diff --git a/config/hcaptchasitekey.go b/config/hcaptchasitekey.go index 599d81e..214ce6d 100644 --- a/config/hcaptchasitekey.go +++ b/config/hcaptchasitekey.go @@ -11,7 +11,7 @@ func HCaptchaSiteKey() string { viper.AddConfigPath("./data") err := viper.ReadInConfig() if err != nil { - log.Println("Error reading config for getting hcaptcha.site_key", err.Error()) + log.Println("Error reading config. Error getting: hcaptcha.site_key", err.Error()) } result := viper.GetString("hcaptcha.site_key") return result diff --git a/config/optionannounce.go b/config/optionannounce.go index 06b1836..db2f184 100644 --- a/config/optionannounce.go +++ b/config/optionannounce.go @@ -11,7 +11,7 @@ func OptForm() string { viper.AddConfigPath("./data") err := viper.ReadInConfig() if err != nil { - log.Println("Error reading config for getting options.form", err.Error()) + log.Println("Error reading config. Error getting: options.form", err.Error()) } result := viper.GetString("options.form") return result diff --git a/config/optionform.go b/config/optionform.go index 1f81ea7..7cb3bba 100644 --- a/config/optionform.go +++ b/config/optionform.go @@ -11,7 +11,7 @@ func OptAnn() string { viper.AddConfigPath("./data") err := viper.ReadInConfig() if err != nil { - log.Println("Error reading config for getting options.announce", err.Error()) + log.Println("Error reading config. Error getting: options.announce", err.Error()) } result := viper.GetString("options.announce") return result diff --git a/config/port.go b/config/port.go index 0830001..66f57b2 100644 --- a/config/port.go +++ b/config/port.go @@ -12,7 +12,7 @@ func Port() string { viper.AddConfigPath("./data") err := viper.ReadInConfig() if err != nil { - log.Println("Error reading config for getting segfautils.port", err.Error()) + log.Println("Error reading config. Error getting: segfautils.port", err.Error()) } result := strconv.Itoa(viper.GetInt("segfautils.port")) return result diff --git a/config/webhook.go b/config/webhook.go index 6c6b03a..2324f18 100644 --- a/config/webhook.go +++ b/config/webhook.go @@ -11,7 +11,7 @@ func WebhookURL() string { viper.AddConfigPath("./data") err := viper.ReadInConfig() if err != nil { - log.Println("Error reading config for getting segfautils.webhook_url", err.Error()) + log.Println("Error reading config. Error getting: segfautils.webhook_url", err.Error()) } result := viper.GetString("segfautils.webhook_url") return result From b05c48cfbb134c04fe04ac13394bc79d385ec2d0 Mon Sep 17 00:00:00 2001 From: Odyssey Date: Sat, 17 Sep 2022 23:04:03 +0200 Subject: [PATCH 09/25] no options.sjon Signed-off-by: Odyssey --- data/options.json | 1 - 1 file changed, 1 deletion(-) delete mode 100755 data/options.json diff --git a/data/options.json b/data/options.json deleted file mode 100755 index 3866b67..0000000 --- a/data/options.json +++ /dev/null @@ -1 +0,0 @@ -{"Announcements":"false","Form":"true"} \ No newline at end of file From 977136d9c2e15ef4b100a0bc270d429bdae12d60 Mon Sep 17 00:00:00 2001 From: Odyssey Date: Sat, 17 Sep 2022 23:08:33 +0200 Subject: [PATCH 10/25] h Signed-off-by: Odyssey --- api/announcements.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/announcements.go b/api/announcements.go index 9d212c1..95903bf 100644 --- a/api/announcements.go +++ b/api/announcements.go @@ -33,7 +33,7 @@ func CheckAnn() { } else { log.Println("Announcements disabled") http.HandleFunc("/api/announcements", func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "Disabled") + http.Error(w, "Announcements are disabled.", http.StatusNotFound) }) } } From 41209f3ee37efadb4d2d2334043ffdec9ae06461 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sat, 17 Sep 2022 22:18:21 +0100 Subject: [PATCH 11/25] ignore options. --- .gitignore | 1 + api/form.go | 23 +++++++++++++++++++++++ main.go | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9184d62..0e7e6c8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ data/config.toml +data/options.json \ No newline at end of file diff --git a/api/form.go b/api/form.go index dade019..1a73e93 100644 --- a/api/form.go +++ b/api/form.go @@ -1,9 +1,12 @@ package api import ( + "io/ioutil" "log" "net/http" + "os" + "github.com/goccy/go-json" "github.com/kataras/hcaptcha" "fmt" @@ -22,6 +25,26 @@ var ( client = hcaptcha.New(secretKey) /* See `Client.FailureHandler` too. */ ) +func FormCheck() { + jsonFile, err := os.Open("./data/options.json") + if err != nil { + fmt.Println(err) + } + defer jsonFile.Close() + byteValue, _ := ioutil.ReadAll(jsonFile) + var result map[string]interface{} + json.Unmarshal([]byte(byteValue), &result) + res := result["Form"] + if res == "true" { + Form() + } else { + log.Println("Forms disabled") + http.HandleFunc("/api/form", func(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "Disabled") + }) + } +} + func Form() { http.HandleFunc("/api/form", client.HandlerFunc(theActualFormCode)) } diff --git a/main.go b/main.go index 810f05f..19fdeba 100644 --- a/main.go +++ b/main.go @@ -47,7 +47,7 @@ func main() { http.ServeFile(w, r, "static/announcements.html") }) api.Settings() - api.Form() + api.FormCheck() api.CheckAnn() log.Println("[HTTP] HTTP server is now running at " + config.Port() + "!") log.Println(http.ListenAndServe(":"+config.Port(), nil)) From e614ab79fa9df6a408c4af946a8ddf055a57b4f0 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sat, 17 Sep 2022 22:33:44 +0100 Subject: [PATCH 12/25] Use viper instead of the clusterfuck of json i was trying to use. --- api/announcements.go | 13 +------ api/form.go | 24 +++--------- api/settings.go | 90 -------------------------------------------- main.go | 1 - 4 files changed, 7 insertions(+), 121 deletions(-) delete mode 100644 api/settings.go diff --git a/api/announcements.go b/api/announcements.go index 95903bf..4b37903 100644 --- a/api/announcements.go +++ b/api/announcements.go @@ -2,7 +2,6 @@ package api import ( "errors" - "fmt" "io" "io/ioutil" "log" @@ -16,19 +15,11 @@ import ( var ( authToken = config.AuthToken() + resAnn = config.OptAnn() ) func CheckAnn() { - jsonFile, err := os.Open("./data/options.json") - if err != nil { - fmt.Println(err) - } - defer jsonFile.Close() - byteValue, _ := ioutil.ReadAll(jsonFile) - var result map[string]interface{} - json.Unmarshal([]byte(byteValue), &result) - res := result["Announcements"] - if res == "true" { + if resAnn == "true" { Announcements() } else { log.Println("Announcements disabled") diff --git a/api/form.go b/api/form.go index 1a73e93..9edce97 100644 --- a/api/form.go +++ b/api/form.go @@ -1,21 +1,15 @@ package api import ( - "io/ioutil" - "log" - "net/http" - "os" - - "github.com/goccy/go-json" - "github.com/kataras/hcaptcha" - "fmt" - "io" + "log" + "net/http" "net/url" "github.com/ProjectSegfault/segfautils/config" "github.com/ProjectSegfault/segfautils/utils" + "github.com/kataras/hcaptcha" ) var ( @@ -23,19 +17,11 @@ var ( secretKey = config.HCaptchaSecretKey() webhookURL = config.WebhookURL() client = hcaptcha.New(secretKey) /* See `Client.FailureHandler` too. */ + resForm = config.OptForm() ) func FormCheck() { - jsonFile, err := os.Open("./data/options.json") - if err != nil { - fmt.Println(err) - } - defer jsonFile.Close() - byteValue, _ := ioutil.ReadAll(jsonFile) - var result map[string]interface{} - json.Unmarshal([]byte(byteValue), &result) - res := result["Form"] - if res == "true" { + if resForm == "true" { Form() } else { log.Println("Forms disabled") diff --git a/api/settings.go b/api/settings.go deleted file mode 100644 index eed2485..0000000 --- a/api/settings.go +++ /dev/null @@ -1,90 +0,0 @@ -package api - -import ( - "errors" - "io" - "io/ioutil" - "log" - "net/http" - "os" - - "github.com/ProjectSegfault/segfautils/config" - "github.com/goccy/go-json" -) - -var ( - announcements = config.OptAnn() - form = config.OptForm() -) - -func Settings() { - CheckSet() - http.HandleFunc("/api/options", getOpt) -} - -func CheckSet() { - os.Remove("./data/options.json") - if form == "true" && announcements == "false" { - data := map[string]interface{}{ - "Announcements": "false", - "Form": "true", - } - - jsonData, err := json.Marshal(data) - if err != nil { - log.Printf("Could not marshal json : %s\n", err) - return - } - - ioutil.WriteFile("./data/options.json", jsonData, os.ModePerm) - - } else if form == "true" && announcements == "true" { - data := map[string]interface{}{ - "Announcements": "true", - "Form": "true", - } - - jsonData, err := json.Marshal(data) - if err != nil { - log.Printf("Could not marshal json : %s\n", err) - return - } - - ioutil.WriteFile("./data/options.json", jsonData, os.ModePerm) - - } else if form == "false" && announcements == "true" { - data := map[string]interface{}{ - "Announcements": "true", - "Form": "false", - } - - jsonData, err := json.Marshal(data) - if err != nil { - log.Printf("Could not marshal json : %s\n", err) - return - } - - ioutil.WriteFile("./data/options.json", jsonData, os.ModePerm) - - } else { - resp := []byte("The fuck do you want me to do then?") - ioutil.WriteFile("./data/options.json", resp, os.ModePerm) - } -} -func getOpt(w http.ResponseWriter, r *http.Request) { - if r.Method != "GET" { - http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) - return - } - if _, err := os.Stat("./data/options.json"); errors.Is(err, os.ErrNotExist) { - http.Error(w, "There is nothing to see here.", http.StatusNotFound) - return - } else { - f, err := os.Open("./data/options.json") - if err != nil { - log.Fatal(err) - } - defer f.Close() - io.Copy(w, f) - } -} diff --git a/main.go b/main.go index 19fdeba..c6f22de 100644 --- a/main.go +++ b/main.go @@ -46,7 +46,6 @@ func main() { http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "static/announcements.html") }) - api.Settings() api.FormCheck() api.CheckAnn() log.Println("[HTTP] HTTP server is now running at " + config.Port() + "!") From 9a504d16383a110ee77a539779f469162ab77615 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sat, 17 Sep 2022 23:12:06 +0100 Subject: [PATCH 13/25] Partial working form and announcements settings. --- api/announcements.go | 12 +++++++++++- api/form.go | 18 ++++++++++++++++++ main.go | 26 +++++++------------------- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/api/announcements.go b/api/announcements.go index 4b37903..e23d30e 100644 --- a/api/announcements.go +++ b/api/announcements.go @@ -21,14 +21,24 @@ var ( func CheckAnn() { if resAnn == "true" { Announcements() + AnnPage() } else { log.Println("Announcements disabled") - http.HandleFunc("/api/announcements", func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) { http.Error(w, "Announcements are disabled.", http.StatusNotFound) }) + http.HandleFunc("/api/announcements", func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "{\"enabled\": \"false\"}", http.StatusNotFound) + }) } } +func AnnPage() { + http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, "static/announcements.html") + }) +} + func Announcements() { http.HandleFunc("/api/announcements", getAnnouncements) http.HandleFunc("/api/announcements/post", handleAnnouncements) diff --git a/api/form.go b/api/form.go index 9edce97..42de194 100644 --- a/api/form.go +++ b/api/form.go @@ -6,6 +6,7 @@ import ( "log" "net/http" "net/url" + "text/template" "github.com/ProjectSegfault/segfautils/config" "github.com/ProjectSegfault/segfautils/utils" @@ -22,6 +23,7 @@ var ( func FormCheck() { if resForm == "true" { + FormPage() Form() } else { log.Println("Forms disabled") @@ -31,6 +33,22 @@ func FormCheck() { } } +func FormPage() { + type StaticThing struct { + HCaptchaSiteKey string + } + + tmpl_form := template.Must(template.ParseFiles("static/form.html")) + http.HandleFunc("/form/", func(w http.ResponseWriter, r *http.Request) { + + hcaptcha_site_key := config.HCaptchaSiteKey() + data := StaticThing{ + HCaptchaSiteKey: hcaptcha_site_key, + } + tmpl_form.Execute(w, data) + }) +} + func Form() { http.HandleFunc("/api/form", client.HandlerFunc(theActualFormCode)) } diff --git a/main.go b/main.go index c6f22de..ba01798 100644 --- a/main.go +++ b/main.go @@ -12,18 +12,13 @@ import ( ) type StaticThingy struct { - Port string - HCaptchaSiteKey string + Port string } -var port string -var shit bool - func main() { log.Println("[Segfautils] Starting") utils.CheckConfig() - log.Println("[HTTP] Starting server") - hcaptcha_site_key := config.HCaptchaSiteKey() + tmpl := template.Must(template.ParseFiles("static/index.html")) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { data := StaticThingy{ @@ -32,22 +27,15 @@ func main() { tmpl.Execute(w, data) }) - tmpl_form := template.Must(template.ParseFiles("static/form.html")) - http.HandleFunc("/form/", func(w http.ResponseWriter, r *http.Request) { - data := StaticThingy{ - HCaptchaSiteKey: hcaptcha_site_key, - } - tmpl_form.Execute(w, data) - }) + log.Println("[HTTP] Starting server") + api.CheckAnn() + api.FormCheck() http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "welcome to hell") }) - http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, "static/announcements.html") - }) - api.FormCheck() - api.CheckAnn() + + api.Form() log.Println("[HTTP] HTTP server is now running at " + config.Port() + "!") log.Println(http.ListenAndServe(":"+config.Port(), nil)) } From 7542cfba4a859e7014ef43d80fe53f31bd932c6c Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sat, 17 Sep 2022 23:16:52 +0100 Subject: [PATCH 14/25] Meanwhile on gitea --- .woodpecker.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.woodpecker.yml b/.woodpecker.yml index d7ada65..904771b 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -9,7 +9,8 @@ pipeline: when: event: [push] name: dockerize and publish - image: plugins/docker + image: plugins/docker + registry: git.projectsegfau.lt/midou/segfautils settings: username: from_secret: username From 65634751b67e0573d98a2bbbbc5049aabe625dac Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sat, 17 Sep 2022 23:20:23 +0100 Subject: [PATCH 15/25] fix --- .woodpecker.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.woodpecker.yml b/.woodpecker.yml index 904771b..5a361b7 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -10,7 +10,7 @@ pipeline: event: [push] name: dockerize and publish image: plugins/docker - registry: git.projectsegfau.lt/midou/segfautils + registry: git.projectsegfau.lt/projectsegfault/segfautils settings: username: from_secret: username From 950d65ca59535972593e899222de850906922e74 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sat, 17 Sep 2022 23:23:39 +0100 Subject: [PATCH 16/25] huh? --- .woodpecker.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.woodpecker.yml b/.woodpecker.yml index 5a361b7..e40ac03 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -10,7 +10,7 @@ pipeline: event: [push] name: dockerize and publish image: plugins/docker - registry: git.projectsegfau.lt/projectsegfault/segfautils + registry: "git.projectsegfau.lt/projectsegfault/segfautils" settings: username: from_secret: username From 64a39ac5e57136413ddaab398b15be247fb1f1d2 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sat, 17 Sep 2022 23:24:32 +0100 Subject: [PATCH 17/25] fuck this was the solution --- .woodpecker.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.woodpecker.yml b/.woodpecker.yml index e40ac03..c4b12e7 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -10,7 +10,7 @@ pipeline: event: [push] name: dockerize and publish image: plugins/docker - registry: "git.projectsegfau.lt/projectsegfault/segfautils" + registry: git.projectsegfau.lt settings: username: from_secret: username From bfdc165346b20ad1f81499a1cba4c3e5cf4bc47b Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sun, 18 Sep 2022 00:26:10 +0100 Subject: [PATCH 18/25] I'm stupid. --- .woodpecker.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.woodpecker.yml b/.woodpecker.yml index c4b12e7..dcb53cb 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -11,6 +11,7 @@ pipeline: name: dockerize and publish image: plugins/docker registry: git.projectsegfau.lt + repo: git.projectsegfau.lt/projectsegfault/segfautils settings: username: from_secret: username From 3abdbde81230c97b596cdb85ae214cebd0343bfd Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sun, 18 Sep 2022 00:33:50 +0100 Subject: [PATCH 19/25] Add dev to docker (there has to be a way to improve this!) --- .woodpecker.yml | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/.woodpecker.yml b/.woodpecker.yml index dcb53cb..f4436f6 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -7,6 +7,7 @@ pipeline: - go build -o segfautils dockerize_n_publish: when: + branch : [master] event: [push] name: dockerize and publish image: plugins/docker @@ -19,6 +20,19 @@ pipeline: from_secret: password repo: projectsegfault/segfautils dockerfile: Dockerfile - + dockerize_dev: + when: + event: [push] + name: dockerize and publish dev + registry: git.projectsegfau.lt + repo: git.projectsegfau.lt/projectsegfault/segfautils + settings: + username: + from_secret: username + password: + from_secret: password + repo: projectsegfau.lt/segfautils + tags: dev + dockerfile: Dockerfile From 0beb8a33deee0e36f484e1ea21ecdad541d848fa Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sun, 18 Sep 2022 00:37:34 +0100 Subject: [PATCH 20/25] Did I forget the image? Maybe I did. --- .woodpecker.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.woodpecker.yml b/.woodpecker.yml index f4436f6..5bca7f5 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -23,6 +23,7 @@ pipeline: dockerize_dev: when: event: [push] + image: plugins/docker name: dockerize and publish dev registry: git.projectsegfau.lt repo: git.projectsegfau.lt/projectsegfault/segfautils From 16ea5c31384f116cafbb677e1a731d6f4ada6472 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sun, 18 Sep 2022 00:40:46 +0100 Subject: [PATCH 21/25] Fix the go error. --- main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/main.go b/main.go index ba01798..e0babda 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,6 @@ func main() { io.WriteString(w, "welcome to hell") }) - api.Form() log.Println("[HTTP] HTTP server is now running at " + config.Port() + "!") log.Println(http.ListenAndServe(":"+config.Port(), nil)) } From af39a529719b85409b9c8d8095d234c0738b981f Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sun, 18 Sep 2022 01:43:17 +0100 Subject: [PATCH 22/25] Ok so, pages don't get disabled, but they don't show up! Half working. --- api/announcements.go | 2 +- api/form.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/api/announcements.go b/api/announcements.go index e23d30e..5cffff6 100644 --- a/api/announcements.go +++ b/api/announcements.go @@ -20,8 +20,8 @@ var ( func CheckAnn() { if resAnn == "true" { - Announcements() AnnPage() + Announcements() } else { log.Println("Announcements disabled") http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) { diff --git a/api/form.go b/api/form.go index 42de194..e1181cc 100644 --- a/api/form.go +++ b/api/form.go @@ -30,6 +30,9 @@ func FormCheck() { http.HandleFunc("/api/form", func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "Disabled") }) + http.HandleFunc("/form", func(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "Disabled") + }) } } From 65d35747fc7f6601267bc19361db49bc1611e0fd Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sun, 18 Sep 2022 17:18:14 +0100 Subject: [PATCH 23/25] Fix Some more stuff, disabling form actually works now. --- api/announcements.go | 4 ++-- api/form.go | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/api/announcements.go b/api/announcements.go index 5cffff6..a9814d2 100644 --- a/api/announcements.go +++ b/api/announcements.go @@ -25,10 +25,10 @@ func CheckAnn() { } else { log.Println("Announcements disabled") http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) { - http.Error(w, "Announcements are disabled.", http.StatusNotFound) + http.Error(w, "Announcements are disabled.", http.StatusServiceUnavailable) }) http.HandleFunc("/api/announcements", func(w http.ResponseWriter, r *http.Request) { - http.Error(w, "{\"enabled\": \"false\"}", http.StatusNotFound) + http.Error(w, "{\"enabled\": \"false\"}", http.StatusServiceUnavailable) }) } } diff --git a/api/form.go b/api/form.go index e1181cc..ee0f780 100644 --- a/api/form.go +++ b/api/form.go @@ -27,11 +27,11 @@ func FormCheck() { Form() } else { log.Println("Forms disabled") - http.HandleFunc("/api/form", func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "Disabled") - }) http.HandleFunc("/form", func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "Disabled") + http.Error(w, "Form is disabled.", http.StatusServiceUnavailable) + }) + http.HandleFunc("/api/form", func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "{\"enabled\": \"false\"}", http.StatusServiceUnavailable) }) } } From 1c2e6e3da9c6ce73631efaf94d9b70771dc6c9d2 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sun, 18 Sep 2022 17:44:37 +0100 Subject: [PATCH 24/25] Fix everything odyssey pointed out. --- .gitignore | 3 +-- api/announcements.go | 5 +++-- api/form.go | 5 +---- main.go | 2 +- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 0e7e6c8..4319c60 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -data/config.toml -data/options.json \ No newline at end of file +data/config.toml \ No newline at end of file diff --git a/api/announcements.go b/api/announcements.go index a9814d2..5cea151 100644 --- a/api/announcements.go +++ b/api/announcements.go @@ -18,12 +18,12 @@ var ( resAnn = config.OptAnn() ) -func CheckAnn() { +func AnnCheck() { if resAnn == "true" { AnnPage() Announcements() } else { - log.Println("Announcements disabled") + log.Println("[Segfautils] ℹ Announcements are disabled") http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) { http.Error(w, "Announcements are disabled.", http.StatusServiceUnavailable) }) @@ -61,6 +61,7 @@ func handleAnnouncements(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) now := time.Now().Unix() data := map[string]interface{}{ + "enabled": "true", "title": r.FormValue("title"), "link": r.FormValue("link"), "severity": r.FormValue("severity"), diff --git a/api/form.go b/api/form.go index ee0f780..5528454 100644 --- a/api/form.go +++ b/api/form.go @@ -26,13 +26,10 @@ func FormCheck() { FormPage() Form() } else { - log.Println("Forms disabled") + log.Println("[Segfautils] ℹ Contact form is disabled") http.HandleFunc("/form", func(w http.ResponseWriter, r *http.Request) { http.Error(w, "Form is disabled.", http.StatusServiceUnavailable) }) - http.HandleFunc("/api/form", func(w http.ResponseWriter, r *http.Request) { - http.Error(w, "{\"enabled\": \"false\"}", http.StatusServiceUnavailable) - }) } } diff --git a/main.go b/main.go index e0babda..14384b3 100644 --- a/main.go +++ b/main.go @@ -28,7 +28,7 @@ func main() { }) log.Println("[HTTP] Starting server") - api.CheckAnn() + api.AnnCheck() api.FormCheck() http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) { From 6bdbf3b83d6e866ad84d09e5f5bc39149a2b3e21 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sun, 18 Sep 2022 19:16:57 +0100 Subject: [PATCH 25/25] Make it compatible with the previous version. --- .gitignore | 3 ++- api/announcements.go | 8 ++++---- api/form.go | 8 ++++---- config/optionannounce.go | 6 +++--- config/optionform.go | 6 +++--- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 4319c60..649bd21 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -data/config.toml \ No newline at end of file +data/config.toml +data/announcements.json \ No newline at end of file diff --git a/api/announcements.go b/api/announcements.go index 5cea151..176dcc1 100644 --- a/api/announcements.go +++ b/api/announcements.go @@ -19,10 +19,7 @@ var ( ) func AnnCheck() { - if resAnn == "true" { - AnnPage() - Announcements() - } else { + if resAnn == "false" { log.Println("[Segfautils] ℹ Announcements are disabled") http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) { http.Error(w, "Announcements are disabled.", http.StatusServiceUnavailable) @@ -30,6 +27,9 @@ func AnnCheck() { http.HandleFunc("/api/announcements", func(w http.ResponseWriter, r *http.Request) { http.Error(w, "{\"enabled\": \"false\"}", http.StatusServiceUnavailable) }) + } else { + AnnPage() + Announcements() } } diff --git a/api/form.go b/api/form.go index 5528454..a576564 100644 --- a/api/form.go +++ b/api/form.go @@ -22,14 +22,14 @@ var ( ) func FormCheck() { - if resForm == "true" { - FormPage() - Form() - } else { + if resForm == "false" { log.Println("[Segfautils] ℹ Contact form is disabled") http.HandleFunc("/form", func(w http.ResponseWriter, r *http.Request) { http.Error(w, "Form is disabled.", http.StatusServiceUnavailable) }) + } else { + FormPage() + Form() } } diff --git a/config/optionannounce.go b/config/optionannounce.go index db2f184..7cb3bba 100644 --- a/config/optionannounce.go +++ b/config/optionannounce.go @@ -6,13 +6,13 @@ import ( "github.com/spf13/viper" ) -func OptForm() string { +func OptAnn() string { viper.SetConfigName("config") viper.AddConfigPath("./data") err := viper.ReadInConfig() if err != nil { - log.Println("Error reading config. Error getting: options.form", err.Error()) + log.Println("Error reading config. Error getting: options.announce", err.Error()) } - result := viper.GetString("options.form") + result := viper.GetString("options.announce") return result } diff --git a/config/optionform.go b/config/optionform.go index 7cb3bba..db2f184 100644 --- a/config/optionform.go +++ b/config/optionform.go @@ -6,13 +6,13 @@ import ( "github.com/spf13/viper" ) -func OptAnn() string { +func OptForm() string { viper.SetConfigName("config") viper.AddConfigPath("./data") err := viper.ReadInConfig() if err != nil { - log.Println("Error reading config. Error getting: options.announce", err.Error()) + log.Println("Error reading config. Error getting: options.form", err.Error()) } - result := viper.GetString("options.announce") + result := viper.GetString("options.form") return result }