setup.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package setup
  26. import (
  27. config "git.semlanik.org/semlanik/gostfix/config"
  28. )
  29. type Setup struct {
  30. sessionStore *sessions.CookieStore
  31. }
  32. func NewSetup() *Setup {
  33. s := &Setup{
  34. sessionStore: sessions.NewCookieStore(make([]byte, 32)),
  35. }
  36. return s
  37. }
  38. func (s *Setup) Run() {
  39. if !config.ConfigInstance().SetupEnabled {
  40. return
  41. }
  42. http.Handle("/", s)
  43. log.Fatal(http.ListenAndServe(":"+config.ConfigInstance().WebPort, nil))
  44. }
  45. func (s *Setup) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  46. urlParts := strings.Split(r.URL.Path, "/")[1:]
  47. if len(urlParts) == 0 || urlParts[0] == "" {
  48. // TODO: Welcome page with password
  49. return
  50. }
  51. if !checkPassword(r) {
  52. http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
  53. return
  54. }
  55. switch urlParts[1] {
  56. case "config":
  57. // TODO: Configure values
  58. case "save":
  59. // TODO: Confirm storing new config and reload server
  60. case "recovery":
  61. // TODO: Recovery mode, where user can resolve system inconsistency
  62. case "admin":
  63. // TODO: Admin panel handling
  64. }
  65. }
  66. func (s *Server) checkPassword(r *http.Request) bool {
  67. switch r.Method {
  68. case "GET":
  69. session, err := s.sessionStore.Get(r, CookieSessionToken)
  70. if err != nil {
  71. log.Printf("Unable to read user session %s\n", err)
  72. return false
  73. }
  74. setupPassword, _ = session.Values["setupPassword"].(string)
  75. case "POST":
  76. setupPassword := r.FormValue("setupPassword")
  77. }
  78. return masterPassword == config.ConfigInstance().SetupPassword;
  79. }