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 (
|
2018-09-25 07:30:58 +00:00
|
|
|
"bytes"
|
2018-12-18 05:25:33 +00:00
|
|
|
"fmt"
|
2018-12-04 04:17:29 +00:00
|
|
|
"github.com/hunterlong/statping/core"
|
|
|
|
"github.com/hunterlong/statping/core/notifier"
|
|
|
|
"github.com/hunterlong/statping/source"
|
|
|
|
"github.com/hunterlong/statping/utils"
|
2018-06-30 00:57:05 +00:00
|
|
|
"net/http"
|
2018-09-25 07:30:58 +00:00
|
|
|
"strconv"
|
2018-06-30 00:57:05 +00:00
|
|
|
)
|
|
|
|
|
2018-09-16 07:48:34 +00:00
|
|
|
func dashboardHandler(w http.ResponseWriter, r *http.Request) {
|
2018-12-18 05:25:33 +00:00
|
|
|
if !IsUser(r) {
|
2018-06-30 00:57:05 +00:00
|
|
|
err := core.ErrorResponse{}
|
2018-12-06 19:03:55 +00:00
|
|
|
ExecuteResponse(w, r, "login.gohtml", err, nil)
|
2018-06-30 00:57:05 +00:00
|
|
|
} else {
|
2018-12-06 19:03:55 +00:00
|
|
|
ExecuteResponse(w, r, "dashboard.gohtml", core.CoreApp, nil)
|
2018-06-30 00:57:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-16 07:48:34 +00:00
|
|
|
func loginHandler(w http.ResponseWriter, r *http.Request) {
|
2018-10-06 05:56:08 +00:00
|
|
|
if sessionStore == nil {
|
2018-08-16 20:55:30 +00:00
|
|
|
resetCookies()
|
|
|
|
}
|
2018-10-06 05:56:08 +00:00
|
|
|
session, _ := sessionStore.Get(r, cookieKey)
|
2019-03-05 20:13:25 +00:00
|
|
|
form := parseForm(r)
|
|
|
|
username := form.Get("username")
|
|
|
|
password := form.Get("password")
|
2018-06-30 00:57:05 +00:00
|
|
|
user, auth := core.AuthUser(username, password)
|
|
|
|
if auth {
|
|
|
|
session.Values["authenticated"] = true
|
|
|
|
session.Values["user_id"] = user.Id
|
2018-12-18 05:25:33 +00:00
|
|
|
session.Values["admin"] = user.Admin.Bool
|
2018-06-30 00:57:05 +00:00
|
|
|
session.Save(r, w)
|
2020-01-13 08:20:23 +00:00
|
|
|
utils.Log.Infoln(fmt.Sprintf("User %v logged in from IP %v", user.Username, r.RemoteAddr))
|
|
|
|
http.Redirect(w, r, basePath+"dashboard", http.StatusSeeOther)
|
2018-06-30 00:57:05 +00:00
|
|
|
} else {
|
|
|
|
err := core.ErrorResponse{Error: "Incorrect login information submitted, try again."}
|
2018-12-06 19:03:55 +00:00
|
|
|
ExecuteResponse(w, r, "login.gohtml", err, nil)
|
2018-06-30 00:57:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-16 07:48:34 +00:00
|
|
|
func logoutHandler(w http.ResponseWriter, r *http.Request) {
|
2018-10-06 05:56:08 +00:00
|
|
|
session, _ := sessionStore.Get(r, cookieKey)
|
2018-06-30 00:57:05 +00:00
|
|
|
session.Values["authenticated"] = false
|
2018-12-18 05:25:33 +00:00
|
|
|
session.Values["admin"] = false
|
|
|
|
session.Values["user_id"] = 0
|
2018-06-30 00:57:05 +00:00
|
|
|
session.Save(r, w)
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
2018-09-16 07:48:34 +00:00
|
|
|
func helpHandler(w http.ResponseWriter, r *http.Request) {
|
2018-12-18 05:25:33 +00:00
|
|
|
if !IsUser(r) {
|
2018-06-30 00:57:05 +00:00
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
2018-09-15 01:18:21 +00:00
|
|
|
help := source.HelpMarkdown()
|
2018-12-06 19:03:55 +00:00
|
|
|
ExecuteResponse(w, r, "help.gohtml", help, nil)
|
2018-06-30 00:57:05 +00:00
|
|
|
}
|
2018-07-20 05:26:41 +00:00
|
|
|
|
2018-09-16 07:48:34 +00:00
|
|
|
func logsHandler(w http.ResponseWriter, r *http.Request) {
|
2018-08-19 13:40:32 +00:00
|
|
|
utils.LockLines.Lock()
|
|
|
|
logs := make([]string, 0)
|
2018-11-21 03:39:40 +00:00
|
|
|
length := len(utils.LastLines)
|
2018-08-19 13:40:32 +00:00
|
|
|
// We need string log lines from end to start.
|
2018-11-21 03:39:40 +00:00
|
|
|
for i := length - 1; i >= 0; i-- {
|
2018-08-19 13:40:32 +00:00
|
|
|
logs = append(logs, utils.LastLines[i].FormatForHtml()+"\r\n")
|
|
|
|
}
|
|
|
|
utils.LockLines.Unlock()
|
2018-12-06 19:03:55 +00:00
|
|
|
ExecuteResponse(w, r, "logs.gohtml", logs, nil)
|
2018-07-20 05:26:41 +00:00
|
|
|
}
|
|
|
|
|
2018-09-16 07:48:34 +00:00
|
|
|
func logsLineHandler(w http.ResponseWriter, r *http.Request) {
|
2018-08-19 13:40:32 +00:00
|
|
|
if lastLine := utils.GetLastLine(); lastLine != nil {
|
2018-11-30 11:12:17 +00:00
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
2018-11-30 05:31:20 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2018-08-19 13:40:32 +00:00
|
|
|
w.Write([]byte(lastLine.FormatForHtml()))
|
2018-07-27 06:37:41 +00:00
|
|
|
}
|
2018-07-20 05:26:41 +00:00
|
|
|
}
|
2018-09-25 04:19:59 +00:00
|
|
|
|
|
|
|
func exportHandler(w http.ResponseWriter, r *http.Request) {
|
2018-09-25 07:03:49 +00:00
|
|
|
var notifiers []*notifier.Notification
|
|
|
|
for _, v := range core.CoreApp.Notifications {
|
|
|
|
notifier := v.(notifier.Notifier)
|
|
|
|
notifiers = append(notifiers, notifier.Select())
|
|
|
|
}
|
|
|
|
|
2018-11-25 03:56:09 +00:00
|
|
|
export, _ := core.ExportSettings()
|
2018-09-25 07:03:49 +00:00
|
|
|
|
2018-09-25 07:30:58 +00:00
|
|
|
mime := http.DetectContentType(export)
|
|
|
|
fileSize := len(string(export))
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", mime)
|
|
|
|
w.Header().Set("Content-Disposition", "attachment; filename=export.json")
|
|
|
|
w.Header().Set("Expires", "0")
|
|
|
|
w.Header().Set("Content-Transfer-Encoding", "binary")
|
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(fileSize))
|
|
|
|
w.Header().Set("Content-Control", "private, no-transform, no-store, must-revalidate")
|
|
|
|
|
2019-12-28 09:01:07 +00:00
|
|
|
http.ServeContent(w, r, "export.json", utils.Now(), bytes.NewReader(export))
|
2018-09-25 07:03:49 +00:00
|
|
|
|
2018-09-25 04:19:59 +00:00
|
|
|
}
|