api.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "imuslab.com/arozos/ReverseProxy/mod/auth"
  6. "imuslab.com/arozos/ReverseProxy/mod/utils"
  7. )
  8. /*
  9. API.go
  10. This file contains all the API called by the web management interface
  11. */
  12. func initAPIs() {
  13. requireAuth := !(*noauth || handler.IsUsingExternalPermissionManager())
  14. authRouter := auth.NewManagedHTTPRouter(auth.RouterOption{
  15. AuthAgent: authAgent,
  16. RequireAuth: requireAuth,
  17. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  18. http.Error(w, "401 - Unauthorized", http.StatusUnauthorized)
  19. },
  20. })
  21. //Register the standard web services urls
  22. fs := http.FileServer(http.Dir("./web"))
  23. if requireAuth {
  24. //Add a layer of middleware for auth control
  25. authHandler := AuthFsHandler(fs)
  26. http.Handle("/", authHandler)
  27. } else {
  28. http.Handle("/", fs)
  29. }
  30. //Auth APIs
  31. http.HandleFunc("/api/auth/login", authAgent.HandleLogin)
  32. http.HandleFunc("/api/auth/logout", authAgent.HandleLogout)
  33. http.HandleFunc("/api/auth/checkLogin", func(w http.ResponseWriter, r *http.Request) {
  34. if requireAuth {
  35. authAgent.CheckLogin(w, r)
  36. } else {
  37. utils.SendJSONResponse(w, "true")
  38. }
  39. })
  40. http.HandleFunc("/api/auth/username", func(w http.ResponseWriter, r *http.Request) {
  41. username, err := authAgent.GetUserName(w, r)
  42. if err != nil {
  43. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  44. return
  45. }
  46. js, _ := json.Marshal(username)
  47. utils.SendJSONResponse(w, string(js))
  48. })
  49. http.HandleFunc("/api/auth/userCount", func(w http.ResponseWriter, r *http.Request) {
  50. uc := authAgent.GetUserCounts()
  51. js, _ := json.Marshal(uc)
  52. utils.SendJSONResponse(w, string(js))
  53. })
  54. http.HandleFunc("/api/auth/register", func(w http.ResponseWriter, r *http.Request) {
  55. if authAgent.GetUserCounts() == 0 {
  56. //Allow register root admin
  57. authAgent.HandleRegisterWithoutEmail(w, r, func(username, reserved string) {
  58. })
  59. } else {
  60. //This function is disabled
  61. utils.SendErrorResponse(w, "Root management account already exists")
  62. }
  63. })
  64. //Reverse proxy
  65. authRouter.HandleFunc("/api/proxy/enable", ReverseProxyHandleOnOff)
  66. authRouter.HandleFunc("/api/proxy/add", ReverseProxyHandleAddEndpoint)
  67. authRouter.HandleFunc("/api/proxy/status", ReverseProxyStatus)
  68. authRouter.HandleFunc("/api/proxy/list", ReverseProxyList)
  69. authRouter.HandleFunc("/api/proxy/del", DeleteProxyEndpoint)
  70. authRouter.HandleFunc("/api/proxy/setIncoming", HandleIncomingPortSet)
  71. authRouter.HandleFunc("/api/proxy/useHttpsRedirect", HandleUpdateHttpsRedirect)
  72. //TLS / SSL config
  73. authRouter.HandleFunc("/api/cert/tls", handleToggleTLSProxy)
  74. authRouter.HandleFunc("/api/cert/upload", handleCertUpload)
  75. authRouter.HandleFunc("/api/cert/list", handleListCertificate)
  76. authRouter.HandleFunc("/api/cert/checkDefault", handleDefaultCertCheck)
  77. authRouter.HandleFunc("/api/cert/delete", handleCertRemove)
  78. //Redirection config
  79. authRouter.HandleFunc("/api/redirect/list", handleListRedirectionRules)
  80. authRouter.HandleFunc("/api/redirect/add", handleAddRedirectionRule)
  81. authRouter.HandleFunc("/api/redirect/delete", handleDeleteRedirectionRule)
  82. }