package management import ( "bytes" "domains" "encoding/json" "log" "net/http" "os" "path/filepath" "text/template" "github.com/asaskevich/govalidator" "github.com/cr3a70r/shield/Utils" "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) router.HandleFunc("/routing", routing) router.HandleFunc("/acls", acls) router.HandleFunc("/domains", view_domains) router.HandleFunc("/domain/{id}", view_domain) router.HandleFunc("/config", config) router.HandleFunc("/api/domains", api_domains) router.HandleFunc("/api/domain/{id}", api_domain) log.Fatal(http.ListenAndServe(addr, router)) } func health(writer http.ResponseWriter, req *http.Request) { Utils.RespondJSON("Unknown state", 200, writer) } 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()) } } 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) { fpTemplate := filepath.Join("static", "template.html") fpPage := filepath.Join("static", "domains.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()) } } 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()) } } type View struct { Data string Config string } 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()) } logFile, err := os.Open("runlog.log") if err != nil { log.Fatal(err) } else { log.SetOutput(logFile) } buf := new(bytes.Buffer) buf.ReadFrom(logFile) defer logFile.Close() bt, err := json.MarshalIndent(Settings, "", " ") if err != nil { log.Println("webserver.config: " + err.Error()) } vd := View{string(bt), buf.String()} 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) }) } func api_domains(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { Utils.RespondJSON(Settings.Domains, http.StatusOK, w) } if r.Method == "POST" { var msgJSON Utils.Msg 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() Utils.RespondJSON(msgJSON, http.StatusBadRequest, w) } Settings.AddDomainByDomain(req) err = Settings.SaveConfig() if err != nil { log.Println("webserver.api_domains: " + err.Error()) msgJSON.Msg = err.Error() Utils.RespondJSON(msgJSON, http.StatusBadRequest, w) } msgJSON.Msg = "OK" Utils.RespondJSON(msgJSON, http.StatusOK, w) } } func api_domain(w http.ResponseWriter, r *http.Request) { httpVars := mux.Vars(r) id := httpVars["id"] var msgJSON Utils.Msg msgJSON.Msg = id Utils.RespondJSON(msgJSON, http.StatusBadRequest, w) }