2018-06-30 00:57:05 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2019-03-02 01:33:41 +00:00
|
|
|
"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 (
|
2020-06-15 04:46:28 +00:00
|
|
|
cookieName = "statping_auth"
|
2020-12-15 08:40:26 +00:00
|
|
|
|
|
|
|
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 {
|
2019-12-28 09:01:07 +00:00
|
|
|
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") {
|
2020-12-15 08:40:26 +00:00
|
|
|
return startLetsEncryptServer(ip)
|
2020-07-15 04:26:54 +00:00
|
|
|
} else if usingSSL {
|
2020-12-15 08:40:26 +00:00
|
|
|
return startSSLServer(ip)
|
2018-12-06 23:20:20 +00:00
|
|
|
} else {
|
2020-12-15 08:40:26 +00:00
|
|
|
return startServer(host)
|
2018-12-06 23:20:20 +00:00
|
|
|
}
|
2018-06-30 00:57:05 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 09:19:55 +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 {
|
2018-12-14 09:19:55 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-05-02 00:53:35 +00:00
|
|
|
if ok := hasAuthorizationHeader(r); ok {
|
|
|
|
return true
|
2018-12-14 09:19:55 +00:00
|
|
|
}
|
|
|
|
return IsFullAuthenticated(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsFullAuthenticated returns true if the HTTP request is authenticated. You can set the environment variable GO_ENV=test
|
2018-10-06 06:38:33 +00:00
|
|
|
// to bypass the admin authenticate to the dashboard features.
|
2018-12-14 09:19:55 +00:00
|
|
|
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
|
2018-12-18 05:25:33 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-12-18 05:25:33 +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 {
|
2018-12-18 05:25:33 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-01-20 05:02:15 +00:00
|
|
|
return claim.Admin
|
2018-12-18 05:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2018-12-18 07:20:03 +00:00
|
|
|
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) {
|
2019-02-01 04:48:18 +00:00
|
|
|
var err error
|
2020-01-13 07:11:53 +00:00
|
|
|
mainTemplate := template.New("main")
|
2019-02-01 04:48:18 +00:00
|
|
|
mainTemplate, err = mainTemplate.Parse(mainTmpl)
|
2018-08-20 07:20:05 +00:00
|
|
|
if err != nil {
|
2019-12-28 09:01:07 +00:00
|
|
|
log.Errorln(err)
|
2020-01-13 07:11:53 +00:00
|
|
|
return nil, err
|
2018-08-20 07:20:05 +00:00
|
|
|
}
|
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)
|
2019-02-01 04:48:18 +00:00
|
|
|
mainTemplate, err = mainTemplate.Parse(tmp)
|
2018-10-02 06:21:14 +00:00
|
|
|
if err != nil {
|
2019-12-28 09:01:07 +00:00
|
|
|
log.Errorln(err)
|
2020-01-13 07:11:53 +00:00
|
|
|
return nil, err
|
2018-10-02 06:21:14 +00:00
|
|
|
}
|
2018-08-20 07:20:05 +00:00
|
|
|
}
|
2020-01-13 07:11:53 +00:00
|
|
|
return mainTemplate, err
|
2019-02-01 04:48:18 +00:00
|
|
|
}
|
2018-10-02 06:21:14 +00:00
|
|
|
|
2019-02-01 04:48:18 +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)
|
2019-02-01 04:48:18 +00:00
|
|
|
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)
|
|
|
|
}
|
2019-02-01 04:48:18 +00:00
|
|
|
render, err := source.TmplBox.String(file)
|
|
|
|
if err != nil {
|
2019-12-28 09:01:07 +00:00
|
|
|
log.Errorln(err)
|
2019-02-01 04:48:18 +00:00
|
|
|
}
|
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 {
|
2019-12-28 09:01:07 +00:00
|
|
|
log.Errorln(err)
|
2018-09-30 13:39:52 +00:00
|
|
|
}
|
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 {
|
2019-12-28 09:01:07 +00:00
|
|
|
log.Errorln(err)
|
2018-08-20 07:20:05 +00:00
|
|
|
}
|
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) {
|
2019-03-02 01:33:41 +00:00
|
|
|
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)
|
2019-03-02 01:33:41 +00:00
|
|
|
json.NewEncoder(w).Encode(d)
|
|
|
|
}
|
|
|
|
|
2018-09-25 07:03:49 +00:00
|
|
|
// error404Handler is a HTTP handler for 404 error pages
|
2018-09-16 07:48:34 +00:00
|
|
|
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
|
|
|
}
|