statping/handlers/handlers.go

213 lines
5.0 KiB
Go
Raw Normal View History

2018-06-30 00:57:05 +00:00
package handlers
import (
"encoding/json"
2018-07-04 09:00:16 +00:00
"fmt"
2020-04-17 03:21:17 +00:00
"github.com/statping/statping/types/errors"
2018-06-30 00:57:05 +00:00
"html/template"
"net/http"
2020-01-13 08:20:23 +00:00
"path"
2018-06-30 00:57:05 +00:00
"time"
2019-12-15 15:22:02 +00:00
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/source"
"github.com/statping/statping/utils"
2018-06-30 00:57:05 +00:00
)
const (
cookieName = "statping_auth"
timeout = time.Second * 30
2018-06-30 00:57:05 +00:00
)
var (
2020-06-26 00:15:59 +00:00
jwtKey []byte
2020-01-26 21:01:43 +00:00
httpServer *http.Server
usingSSL bool
mainTmpl = `{{define "main" }} {{ template "base" . }} {{ end }}`
2020-02-19 08:13:09 +00:00
templates = []string{"base.gohtml"}
2018-06-30 00:57:05 +00:00
)
2020-06-05 03:47:35 +00:00
func StopHTTPServer(err error) {
log.Infoln("Stopping HTTP Server")
}
2018-09-25 07:03:49 +00:00
// RunHTTPServer will start a HTTP server on a specific IP and port
2020-06-05 03:47:35 +00:00
func RunHTTPServer() error {
if utils.Params.GetBool("DISABLE_HTTP") {
return nil
}
2018-12-06 23:20:20 +00:00
2020-06-14 01:39:47 +00:00
ip := utils.Params.GetString("SERVER_IP")
host := fmt.Sprintf("%v:%v", ip, utils.Params.GetInt("SERVER_PORT"))
2018-12-06 23:20:20 +00:00
key := utils.FileExists(utils.Directory + "/server.key")
cert := utils.FileExists(utils.Directory + "/server.crt")
if key && cert {
log.Infoln("server.cert and server.key was found in root directory! Starting in SSL mode.")
log.Infoln(fmt.Sprintf("Statping Secure HTTPS Server running on https://%v:%v", ip, 443))
2018-12-06 23:20:20 +00:00
usingSSL = true
} else {
2020-04-16 09:57:00 +00:00
log.Infoln("Statping HTTP Server running on http://" + host + basePath)
2018-06-30 00:57:05 +00:00
}
2018-12-06 23:20:20 +00:00
router = Router()
2018-08-16 20:55:30 +00:00
resetCookies()
2018-12-06 23:20:20 +00:00
2020-07-15 17:09:00 +00:00
if utils.Params.GetBool("LETSENCRYPT_ENABLE") {
return startLetsEncryptServer(ip)
} else if usingSSL {
return startSSLServer(ip)
2018-12-06 23:20:20 +00:00
} else {
return startServer(host)
2018-12-06 23:20:20 +00:00
}
2018-06-30 00:57:05 +00:00
}
// IsReadAuthenticated will allow Read Only authentication for some routes
func IsReadAuthenticated(r *http.Request) bool {
2020-05-02 00:53:35 +00:00
if ok := hasSetupEnv(); ok {
return true
2019-12-30 08:08:51 +00:00
}
2020-05-02 00:53:35 +00:00
if ok := hasAPIQuery(r); ok {
return true
}
2020-05-02 00:53:35 +00:00
if ok := hasAuthorizationHeader(r); ok {
return true
}
return IsFullAuthenticated(r)
}
// IsFullAuthenticated returns true if the HTTP request is authenticated. You can set the environment variable GO_ENV=test
// to bypass the admin authenticate to the dashboard features.
func IsFullAuthenticated(r *http.Request) bool {
2020-05-02 00:53:35 +00:00
if ok := hasSetupEnv(); ok {
2018-06-30 00:57:05 +00:00
return true
}
2020-05-02 00:53:35 +00:00
if ok := hasAPIQuery(r); ok {
2018-11-30 18:36:13 +00:00
return true
2018-06-30 00:57:05 +00:00
}
2020-05-02 00:53:35 +00:00
if ok := hasAuthorizationHeader(r); ok {
return true
2018-06-30 00:57:05 +00:00
}
2020-06-26 00:15:59 +00:00
claim, err := getJwtToken(r)
if err != nil {
return false
}
return claim.Admin
}
2020-05-02 00:53:35 +00:00
// ScopeName will show private JSON fields in the API.
// It will return "admin" if request has valid admin authentication.
2020-01-20 05:02:15 +00:00
func ScopeName(r *http.Request) string {
2020-05-02 00:53:35 +00:00
if ok := hasAPIQuery(r); ok {
return "admin"
}
if ok := hasAuthorizationHeader(r); ok {
return "admin"
}
2020-01-20 05:02:15 +00:00
claim, err := getJwtToken(r)
if err != nil {
return ""
}
if claim.Admin {
return "admin"
}
return "user"
2020-01-16 09:29:49 +00:00
}
// IsAdmin returns true if the user session is an administrator
func IsAdmin(r *http.Request) bool {
2020-01-20 05:02:15 +00:00
claim, err := getJwtToken(r)
if err != nil {
return false
}
2020-01-20 05:02:15 +00:00
return claim.Admin
}
// IsUser returns true if the user is registered
func IsUser(r *http.Request) bool {
2020-05-02 00:53:35 +00:00
if ok := hasSetupEnv(); ok {
return true
}
2020-03-29 01:21:32 +00:00
tk, err := getJwtToken(r)
2020-01-20 05:02:15 +00:00
if err != nil {
return false
}
2020-03-29 01:21:32 +00:00
if err := tk.Valid(); err != nil {
return false
}
2020-01-20 05:02:15 +00:00
return true
2018-06-30 00:57:05 +00:00
}
2020-01-13 07:11:53 +00:00
func loadTemplate(w http.ResponseWriter, r *http.Request) (*template.Template, error) {
var err error
2020-01-13 07:11:53 +00:00
mainTemplate := template.New("main")
mainTemplate, err = mainTemplate.Parse(mainTmpl)
if err != nil {
log.Errorln(err)
2020-01-13 07:11:53 +00:00
return nil, err
}
2020-01-13 07:11:53 +00:00
mainTemplate.Funcs(handlerFuncs(w, r))
2018-10-02 06:21:14 +00:00
// render all templates
for _, temp := range templates {
tmp, _ := source.TmplBox.String(temp)
mainTemplate, err = mainTemplate.Parse(tmp)
2018-10-02 06:21:14 +00:00
if err != nil {
log.Errorln(err)
2020-01-13 07:11:53 +00:00
return nil, err
2018-10-02 06:21:14 +00:00
}
}
2020-01-13 07:11:53 +00:00
return mainTemplate, err
}
2018-10-02 06:21:14 +00:00
// ExecuteResponse will render a HTTP response for the front end user
func ExecuteResponse(w http.ResponseWriter, r *http.Request, file string, data interface{}, redirect interface{}) {
if url, ok := redirect.(string); ok {
2020-01-13 08:20:23 +00:00
http.Redirect(w, r, path.Join(basePath, url), http.StatusSeeOther)
return
}
if usingSSL {
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
}
2020-01-13 07:11:53 +00:00
mainTemplate, err := loadTemplate(w, r)
if err != nil {
log.Errorln(err)
}
render, err := source.TmplBox.String(file)
if err != nil {
log.Errorln(err)
}
2018-10-02 06:21:14 +00:00
// render the page requested
2019-03-05 20:13:25 +00:00
if _, err := mainTemplate.Parse(render); err != nil {
log.Errorln(err)
}
2018-10-02 06:21:14 +00:00
// execute the template
2019-03-05 20:13:25 +00:00
if err := mainTemplate.Execute(w, data); err != nil {
log.Errorln(err)
}
2018-06-30 00:57:05 +00:00
}
2020-04-17 03:21:17 +00:00
func returnJson(d interface{}, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
2020-04-17 03:21:17 +00:00
if e, ok := d.(errors.Error); ok {
w.WriteHeader(e.Status())
json.NewEncoder(w).Encode(e)
return
2020-03-10 05:24:35 +00:00
}
2020-04-19 08:03:50 +00:00
if e, ok := d.(error); ok {
w.WriteHeader(500)
json.NewEncoder(w).Encode(errors.New(e.Error()))
return
}
2020-04-17 03:21:17 +00:00
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(d)
}
2018-09-25 07:03:49 +00:00
// error404Handler is a HTTP handler for 404 error pages
func error404Handler(w http.ResponseWriter, r *http.Request) {
2018-12-06 23:20:20 +00:00
if usingSSL {
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
}
2018-08-16 02:22:10 +00:00
w.WriteHeader(http.StatusNotFound)
2020-04-17 03:21:17 +00:00
ExecuteResponse(w, r, "base.gohtml", nil, nil)
2018-08-16 02:22:10 +00:00
}