statping/handlers/handlers.go

427 lines
11 KiB
Go
Raw Normal View History

2018-12-04 05:57:11 +00:00
// Statping
2018-08-16 06:22:20 +00:00
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
2018-12-04 04:17:29 +00:00
// https://github.com/hunterlong/statping
2018-08-16 06:22:20 +00:00
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2018-06-30 00:57:05 +00:00
package handlers
import (
2019-12-15 15:22:02 +00:00
"crypto/subtle"
2018-12-06 23:20:20 +00:00
"crypto/tls"
"encoding/json"
2018-07-04 09:00:16 +00:00
"fmt"
2020-01-16 09:29:49 +00:00
"github.com/dgrijalva/jwt-go"
2018-06-30 00:57:05 +00:00
"html/template"
"net/http"
"os"
2020-01-13 08:20:23 +00:00
"path"
2020-01-15 04:58:22 +00:00
"reflect"
2018-11-30 18:36:13 +00:00
"strings"
2018-06-30 00:57:05 +00:00
"time"
2019-12-15 15:22:02 +00:00
"github.com/hunterlong/statping/core"
"github.com/hunterlong/statping/source"
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
2018-06-30 00:57:05 +00:00
)
const (
2018-12-04 05:57:11 +00:00
cookieKey = "statping_auth"
timeout = time.Second * 30
2018-06-30 00:57:05 +00:00
)
var (
2020-01-16 09:29:49 +00:00
jwtKey string
httpServer *http.Server
usingSSL bool
mainTmpl = `{{define "main" }} {{ template "base" . }} {{ end }}`
templates = []string{"base.gohtml", "head.gohtml", "nav.gohtml", "footer.gohtml", "scripts.gohtml", "form_service.gohtml", "form_notifier.gohtml", "form_integration.gohtml", "form_group.gohtml", "form_user.gohtml", "form_checkin.gohtml", "form_message.gohtml"}
javascripts = []string{"charts.js", "chart_index.js"}
2018-06-30 00:57:05 +00:00
)
2018-09-25 07:03:49 +00:00
// RunHTTPServer will start a HTTP server on a specific IP and port
2018-08-16 02:22:10 +00:00
func RunHTTPServer(ip string, port int) error {
host := fmt.Sprintf("%v:%v", ip, 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 {
log.Infoln("Statping HTTP Server running on http://" + host)
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
if usingSSL {
cfg := &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
},
}
srv := &http.Server{
Addr: fmt.Sprintf("%v:%v", ip, 443),
Handler: router,
TLSConfig: cfg,
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler), 0),
WriteTimeout: timeout,
ReadTimeout: timeout,
IdleTimeout: timeout,
}
return srv.ListenAndServeTLS(utils.Directory+"/server.crt", utils.Directory+"/server.key")
} else {
httpServer = &http.Server{
Addr: host,
WriteTimeout: timeout,
ReadTimeout: timeout,
IdleTimeout: timeout,
Handler: router,
}
httpServer.SetKeepAlivesEnabled(false)
2018-12-06 23:20:20 +00:00
return httpServer.ListenAndServe()
}
return nil
2018-06-30 00:57:05 +00:00
}
// IsReadAuthenticated will allow Read Only authentication for some routes
func IsReadAuthenticated(r *http.Request) bool {
2019-12-30 08:08:51 +00:00
if core.SetupMode {
return false
}
var token string
query := r.URL.Query()
key := query.Get("api")
2020-01-13 02:12:50 +00:00
if subtle.ConstantTimeCompare([]byte(key), []byte(core.CoreApp.ApiSecret)) == 1 {
return true
}
tokens, ok := r.Header["Authorization"]
if ok && len(tokens) >= 1 {
token = tokens[0]
token = strings.TrimPrefix(token, "Bearer ")
2020-01-13 02:12:50 +00:00
if subtle.ConstantTimeCompare([]byte(token), []byte(core.CoreApp.ApiSecret)) == 1 {
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 {
2018-06-30 00:57:05 +00:00
if os.Getenv("GO_ENV") == "test" {
return true
}
if core.CoreApp == nil {
2018-11-30 18:36:13 +00:00
return true
2018-06-30 00:57:05 +00:00
}
2019-12-30 08:08:51 +00:00
if core.SetupMode {
return false
}
2018-11-30 18:36:13 +00:00
var token string
tokens, ok := r.Header["Authorization"]
if ok && len(tokens) >= 1 {
token = tokens[0]
token = strings.TrimPrefix(token, "Bearer ")
2020-01-13 02:12:50 +00:00
if subtle.ConstantTimeCompare([]byte(token), []byte(core.CoreApp.ApiSecret)) == 1 {
2018-11-30 18:36:13 +00:00
return true
}
2018-06-30 00:57:05 +00:00
}
return IsAdmin(r)
}
2020-01-16 09:29:49 +00:00
func getJwtAuth(r *http.Request) (bool, string) {
c, err := r.Cookie(cookieKey)
if err != nil {
utils.Log.Errorln(err)
if err == http.ErrNoCookie {
return false, ""
}
return false, ""
}
tknStr := c.Value
var claims JwtClaim
tkn, err := jwt.ParseWithClaims(tknStr, &claims, func(token *jwt.Token) (interface{}, error) {
return []byte(jwtKey), nil
})
if err != nil {
utils.Log.Errorln("error getting jwt token: ", err)
if err == jwt.ErrSignatureInvalid {
return false, ""
}
return false, ""
}
if !tkn.Valid {
utils.Log.Errorln("token is not valid")
return false, ""
}
return claims.Admin, claims.Username
}
// IsAdmin returns true if the user session is an administrator
func IsAdmin(r *http.Request) bool {
2019-12-30 08:08:51 +00:00
if core.SetupMode {
return false
}
2020-01-16 09:29:49 +00:00
admin, username := getJwtAuth(r)
if username == "" {
return false
}
2020-01-16 09:29:49 +00:00
return admin
}
// IsUser returns true if the user is registered
func IsUser(r *http.Request) bool {
2019-12-30 08:08:51 +00:00
if core.SetupMode {
return false
}
if os.Getenv("GO_ENV") == "test" {
return true
}
2020-01-16 09:29:49 +00:00
ff, username := getJwtAuth(r)
fmt.Println(ff, username)
return username != ""
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
}
}
2018-10-02 06:21:14 +00:00
// render all javascript files
for _, temp := range javascripts {
tmp, _ := source.JsBox.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
}
2018-09-25 07:03:49 +00:00
// executeJSResponse will render a Javascript response
func executeJSResponse(w http.ResponseWriter, r *http.Request, file string, data interface{}) {
2018-08-10 04:38:54 +00:00
render, err := source.JsBox.String(file)
2018-07-04 01:19:18 +00:00
if err != nil {
log.Errorln(err)
2018-07-04 01:19:18 +00:00
}
2018-12-06 23:20:20 +00:00
if usingSSL {
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
}
2018-07-04 01:19:18 +00:00
t := template.New("charts")
t.Funcs(template.FuncMap{
"safe": func(html string) template.HTML {
return template.HTML(html)
},
"Services": func() []types.ServiceInterface {
return core.CoreApp.Services
},
2018-07-04 01:19:18 +00:00
})
2019-03-05 20:13:25 +00:00
if _, err := t.Parse(render); err != nil {
log.Errorln(err)
}
2019-03-05 20:13:25 +00:00
if err := t.Execute(w, data); err != nil {
log.Errorln(err)
}
2018-07-04 01:19:18 +00:00
}
func returnJson(d interface{}, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(d)
}
2020-01-15 04:58:22 +00:00
func safeTypes(obj interface{}) []string {
if reflect.ValueOf(obj).Kind() == reflect.Ptr {
obj = &obj
}
switch v := obj.(type) {
case types.Service:
return types.SafeService
default:
fmt.Printf("%T\n", v)
}
return nil
}
func expandServices(s []types.ServiceInterface) []*types.Service {
var services []*types.Service
for _, v := range s {
services = append(services, v.Select())
}
return services
}
2020-01-18 04:02:00 +00:00
func toSafeJson(input interface{}, onlyAdmin, onlyUsers bool) map[string]interface{} {
thisData := make(map[string]interface{})
t := reflect.TypeOf(input)
elem := reflect.ValueOf(input)
d, _ := json.Marshal(input)
var raw map[string]*json.RawMessage
json.Unmarshal(d, &raw)
if t.Kind() == reflect.Ptr {
input = &input
2020-01-15 04:58:22 +00:00
}
fmt.Println("Type:", t.Name())
fmt.Println("Kind:", t.Kind())
2020-01-18 04:02:00 +00:00
fmt.Println("Fields:", t.NumField())
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
// Get the field tag value
tag := field.Tag.Get("scope")
tags := strings.Split(tag, ",")
2020-01-18 04:02:00 +00:00
jTags := field.Tag.Get("json")
jsonTag := strings.Split(jTags, ",")
fmt.Println(jsonTag, tag)
if len(jsonTag) == 0 {
continue
}
if jsonTag[0] == "" || jsonTag[0] == "-" {
continue
}
trueValue := elem.Field(i).Interface()
trueValue = fixValue(field, trueValue)
2020-01-18 04:02:00 +00:00
if len(jsonTag) == 2 {
if jsonTag[1] == "omitempty" && trueValue == "" {
continue
}
}
if tag == "" {
2020-01-18 04:02:00 +00:00
thisData[jsonTag[0]] = trueValue
continue
}
2020-01-18 04:02:00 +00:00
if forType(tags, onlyAdmin, onlyUsers) {
thisData[jsonTag[0]] = trueValue
}
fmt.Printf("%d. %v (%v), tags: '%v'\n", i, field.Name, field.Type.Name(), tags)
}
return thisData
2020-01-15 04:58:22 +00:00
}
func returnSafeJson(w http.ResponseWriter, r *http.Request, input interface{}) {
2020-01-18 04:02:00 +00:00
admin, user := IsAdmin(r), IsUser(r)
if reflect.ValueOf(input).Kind() == reflect.Slice {
alldata := make([]map[string]interface{}, 0, 1)
s := reflect.ValueOf(input)
for i := 0; i < s.Len(); i++ {
2020-01-18 04:02:00 +00:00
alldata = append(alldata, toSafeJson(s.Index(i).Interface(), admin, user))
2020-01-15 04:58:22 +00:00
}
returnJson(alldata, w, r)
return
2020-01-15 04:58:22 +00:00
}
2020-01-18 04:02:00 +00:00
returnJson(toSafeJson(input, admin, user), w, r)
}
2020-01-15 04:58:22 +00:00
func fixValue(field reflect.StructField, val interface{}) interface{} {
typeName := field.Type.Name()
switch typeName {
case "NullString":
nullItem := val.(types.NullString)
return nullItem.String
case "NullBool":
nullItem := val.(types.NullBool)
return nullItem.Bool
case "NullFloat64":
nullItem := val.(types.NullFloat64)
return nullItem.Float64
case "NullInt64":
nullItem := val.(types.NullInt64)
return nullItem.Int64
default:
return val
}
}
2020-01-15 04:58:22 +00:00
2020-01-18 04:02:00 +00:00
func forType(tags []string, onlyAdmin, onlyUsers bool) bool {
for _, v := range tags {
2020-01-18 04:02:00 +00:00
if v == "admin" && onlyAdmin {
return true
}
if v == "user" && onlyUsers {
return true
}
2020-01-15 04:58:22 +00:00
}
return false
2020-01-15 04:58:22 +00:00
}
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)
2018-12-06 19:03:55 +00:00
ExecuteResponse(w, r, "error_404.gohtml", nil, nil)
2018-08-16 02:22:10 +00:00
}