You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
272 lines
6.6 KiB
272 lines
6.6 KiB
2 years ago
|
package management
|
||
|
|
||
|
import (
|
||
2 years ago
|
"bytes"
|
||
2 years ago
|
"encoding/json"
|
||
|
"log"
|
||
|
"net/http"
|
||
2 years ago
|
"os"
|
||
2 years ago
|
"path/filepath"
|
||
|
"text/template"
|
||
|
|
||
|
"github.com/asaskevich/govalidator"
|
||
2 years ago
|
"github.com/cr3a70r/shield/internal/domains"
|
||
|
"github.com/cr3a70r/shield/internal/utils"
|
||
|
|
||
2 years ago
|
"github.com/gorilla/mux"
|
||
|
"github.com/gorilla/securecookie"
|
||
|
)
|
||
|
|
||
|
var hashKey = []byte("ckjstkldx-rlkjcmskl-rdlskjtmd")
|
||
|
var blockKey = []byte("opbckswle-sdnfekjtiw-dsmnwhekskd")
|
||
|
var secCookie = securecookie.New(hashKey, blockKey)
|
||
|
|
||
|
func StartWebserver(addr string) {
|
||
|
router := mux.NewRouter()
|
||
|
|
||
|
router.HandleFunc("/", renderIndex)
|
||
|
router.HandleFunc("/config", config)
|
||
|
router.HandleFunc("/dashboard", dashboard)
|
||
2 years ago
|
router.HandleFunc("/routing", routing)
|
||
|
router.HandleFunc("/acls", acls)
|
||
|
router.HandleFunc("/domains", view_domains)
|
||
|
router.HandleFunc("/domain/{id}", view_domain)
|
||
|
|
||
2 years ago
|
router.HandleFunc("/config", config)
|
||
|
|
||
2 years ago
|
router.HandleFunc("/api/domains", api_domains)
|
||
|
router.HandleFunc("/api/domain/{id}", api_domain)
|
||
|
|
||
2 years ago
|
log.Fatal(http.ListenAndServe(addr, router))
|
||
|
}
|
||
|
|
||
|
func health(writer http.ResponseWriter, req *http.Request) {
|
||
2 years ago
|
utils.RespondJSON("Unknown state", 200, writer)
|
||
2 years ago
|
}
|
||
|
|
||
|
func renderIndex(w http.ResponseWriter, r *http.Request) {
|
||
|
if r.Method == "POST" {
|
||
|
|
||
|
r.ParseForm()
|
||
|
if !govalidator.IsEmail(r.FormValue("Email")) {
|
||
|
log.Println("webserver.auth: email not found")
|
||
|
}
|
||
|
|
||
|
success, err := Settings.CheckPassword(r.FormValue("Email"), r.FormValue("Password"))
|
||
|
if err != nil {
|
||
|
log.Println("webserver.auth: unseccessful auth ")
|
||
|
log.Println(err)
|
||
|
|
||
|
} else if success {
|
||
|
log.Println("webserver.auth: login")
|
||
|
log.Println(r.FormValue("Email"))
|
||
|
|
||
|
value := map[string]string{
|
||
|
"email": r.FormValue("Email"),
|
||
|
"password": r.FormValue("Password"),
|
||
|
}
|
||
|
encoded, err := secCookie.Encode("Shield", value)
|
||
|
|
||
|
Settings.SaveCookie(r.FormValue("Email"), encoded)
|
||
|
|
||
|
if err != nil {
|
||
|
log.Println("webserver.auth: failed to encode cookie")
|
||
|
}
|
||
|
cookie := &http.Cookie{
|
||
|
Name: "Shield",
|
||
|
Value: encoded,
|
||
|
Path: "/",
|
||
|
}
|
||
|
http.SetCookie(w, cookie)
|
||
|
r.Header.Set("x-shield", encoded)
|
||
|
|
||
|
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
parsedTemplate, _ := template.ParseFiles("static/index.html")
|
||
|
err := parsedTemplate.Execute(w, "")
|
||
|
if err != nil {
|
||
|
log.Println("Error executing template :", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
defer r.Body.Close()
|
||
|
}
|
||
|
|
||
|
func dashboard(w http.ResponseWriter, r *http.Request) {
|
||
|
fpTemplate := filepath.Join("static", "template.html")
|
||
|
fpPage := filepath.Join("static", "dashboard.html")
|
||
|
|
||
|
tmpl, err := template.ParseFiles(fpPage, fpTemplate)
|
||
|
|
||
|
if err != nil {
|
||
|
log.Println("webserver.dashboard: " + err.Error())
|
||
|
}
|
||
|
|
||
|
err = tmpl.ExecuteTemplate(w, "template.html", nil)
|
||
|
if err != nil {
|
||
|
log.Println("webserver.dashboard: " + err.Error())
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
func routing(w http.ResponseWriter, r *http.Request) {
|
||
|
fpTemplate := filepath.Join("static", "template.html")
|
||
|
fpPage := filepath.Join("static", "routing.html")
|
||
|
|
||
|
tmpl, err := template.ParseFiles(fpPage, fpTemplate)
|
||
|
if err != nil {
|
||
|
log.Println("webserver.protection: " + err.Error())
|
||
|
}
|
||
|
|
||
|
err = tmpl.ExecuteTemplate(w, "template.html", nil)
|
||
|
if err != nil {
|
||
|
log.Println("webserver.protection: " + err.Error())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func acls(w http.ResponseWriter, r *http.Request) {
|
||
|
fpTemplate := filepath.Join("static", "template.html")
|
||
|
fpPage := filepath.Join("static", "acls.html")
|
||
|
|
||
|
tmpl, err := template.ParseFiles(fpPage, fpTemplate)
|
||
|
if err != nil {
|
||
|
log.Println("webserver.protection: " + err.Error())
|
||
|
}
|
||
|
|
||
|
err = tmpl.ExecuteTemplate(w, "template.html", nil)
|
||
|
if err != nil {
|
||
|
log.Println("webserver.protection: " + err.Error())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func view_domains(w http.ResponseWriter, r *http.Request) {
|
||
2 years ago
|
fpTemplate := filepath.Join("static", "template.html")
|
||
2 years ago
|
fpPage := filepath.Join("static", "domains.html")
|
||
2 years ago
|
|
||
|
tmpl, err := template.ParseFiles(fpPage, fpTemplate)
|
||
|
if err != nil {
|
||
|
log.Println("webserver.protection: " + err.Error())
|
||
|
}
|
||
|
|
||
|
err = tmpl.ExecuteTemplate(w, "template.html", nil)
|
||
|
if err != nil {
|
||
|
log.Println("webserver.protection: " + err.Error())
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
type ViewDomains struct {
|
||
|
Domain domains.T_Domain
|
||
|
}
|
||
|
|
||
|
func view_domain(w http.ResponseWriter, r *http.Request) {
|
||
|
fpTemplate := filepath.Join("static", "template.html")
|
||
|
fpPage := filepath.Join("static", "domain.html")
|
||
|
|
||
|
httpVars := mux.Vars(r)
|
||
|
id := httpVars["id"]
|
||
|
|
||
|
var view ViewDomains
|
||
|
view.Domain = Settings.FindDomainByName(id)
|
||
|
|
||
|
tmpl, err := template.ParseFiles(fpPage, fpTemplate)
|
||
|
if err != nil {
|
||
|
log.Println("webserver.protection: " + err.Error())
|
||
|
}
|
||
|
|
||
|
err = tmpl.ExecuteTemplate(w, "template.html", view)
|
||
|
if err != nil {
|
||
|
log.Println("webserver.protection: " + err.Error())
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
type View struct {
|
||
2 years ago
|
Data string
|
||
|
Config string
|
||
2 years ago
|
}
|
||
|
|
||
|
func config(w http.ResponseWriter, r *http.Request) {
|
||
|
fpTemplate := filepath.Join("static", "template.html")
|
||
|
fpPage := filepath.Join("static", "config.html")
|
||
|
|
||
|
tmpl, err := template.ParseFiles(fpPage, fpTemplate)
|
||
|
if err != nil {
|
||
|
log.Println("webserver.config: " + err.Error())
|
||
|
}
|
||
|
|
||
2 years ago
|
logFile, err := os.Open("runlog.log")
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
} else {
|
||
|
log.SetOutput(logFile)
|
||
|
}
|
||
|
buf := new(bytes.Buffer)
|
||
|
buf.ReadFrom(logFile)
|
||
2 years ago
|
|
||
2 years ago
|
defer logFile.Close()
|
||
|
|
||
|
bt, err := json.MarshalIndent(Settings, "", " ")
|
||
|
if err != nil {
|
||
|
log.Println("webserver.config: " + err.Error())
|
||
|
}
|
||
|
vd := View{string(bt), buf.String()}
|
||
2 years ago
|
|
||
|
err = tmpl.ExecuteTemplate(w, "template.html", vd)
|
||
|
if err != nil {
|
||
|
log.Println("webserver.config: " + err.Error())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func requireAuth(h http.Handler) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
if cookie, err := r.Cookie("Shield"); err == nil {
|
||
|
value := make(map[string]string)
|
||
|
if err = secCookie.Decode("Shield", cookie.Value, &value); err == nil {
|
||
|
log.Println("webserver.requireAuth: unauthorized access denied " + r.RemoteAddr)
|
||
|
}
|
||
|
}
|
||
|
h.ServeHTTP(w, r)
|
||
|
})
|
||
|
}
|
||
2 years ago
|
|
||
|
func api_domains(w http.ResponseWriter, r *http.Request) {
|
||
|
if r.Method == "GET" {
|
||
2 years ago
|
utils.RespondJSON(Settings.Domains, http.StatusOK, w)
|
||
2 years ago
|
}
|
||
|
if r.Method == "POST" {
|
||
2 years ago
|
var msgJSON utils.Msg
|
||
2 years ago
|
var req domains.T_Domain
|
||
|
err := json.NewDecoder(r.Body).Decode(&req)
|
||
|
if err != nil {
|
||
|
log.Println("webserver.api_domains: " + err.Error())
|
||
|
|
||
|
msgJSON.Msg = err.Error()
|
||
2 years ago
|
utils.RespondJSON(msgJSON, http.StatusBadRequest, w)
|
||
2 years ago
|
}
|
||
|
|
||
|
Settings.AddDomainByDomain(req)
|
||
|
|
||
|
err = Settings.SaveConfig()
|
||
|
if err != nil {
|
||
|
log.Println("webserver.api_domains: " + err.Error())
|
||
|
|
||
|
msgJSON.Msg = err.Error()
|
||
2 years ago
|
utils.RespondJSON(msgJSON, http.StatusBadRequest, w)
|
||
2 years ago
|
}
|
||
|
|
||
|
msgJSON.Msg = "OK"
|
||
2 years ago
|
utils.RespondJSON(msgJSON, http.StatusOK, w)
|
||
2 years ago
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func api_domain(w http.ResponseWriter, r *http.Request) {
|
||
|
httpVars := mux.Vars(r)
|
||
|
id := httpVars["id"]
|
||
|
|
||
2 years ago
|
var msgJSON utils.Msg
|
||
2 years ago
|
msgJSON.Msg = id
|
||
|
|
||
2 years ago
|
utils.RespondJSON(msgJSON, http.StatusBadRequest, w)
|
||
2 years ago
|
}
|