api.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. authRouter.HandleFunc("/api/auth/login", authAgent.HandleLogin)
  32. authRouter.HandleFunc("/api/auth/logout", authAgent.HandleLogout)
  33. authRouter.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. authRouter.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. //Reverse proxy
  55. authRouter.HandleFunc("/api/proxy/enable", ReverseProxyHandleOnOff)
  56. authRouter.HandleFunc("/api/proxy/add", ReverseProxyHandleAddEndpoint)
  57. authRouter.HandleFunc("/api/proxy/status", ReverseProxyStatus)
  58. authRouter.HandleFunc("/api/proxy/list", ReverseProxyList)
  59. authRouter.HandleFunc("/api/proxy/del", DeleteProxyEndpoint)
  60. authRouter.HandleFunc("/api/proxy/setIncoming", HandleIncomingPortSet)
  61. authRouter.HandleFunc("/api/proxy/useHttpsRedirect", HandleUpdateHttpsRedirect)
  62. //TLS / SSL config
  63. authRouter.HandleFunc("/api/cert/tls", handleToggleTLSProxy)
  64. authRouter.HandleFunc("/api/cert/upload", handleCertUpload)
  65. authRouter.HandleFunc("/api/cert/list", handleListCertificate)
  66. authRouter.HandleFunc("/api/cert/checkDefault", handleDefaultCertCheck)
  67. authRouter.HandleFunc("/api/cert/delete", handleCertRemove)
  68. //Redirection config
  69. authRouter.HandleFunc("/api/redirect/list", handleListRedirectionRules)
  70. authRouter.HandleFunc("/api/redirect/add", handleAddRedirectionRule)
  71. authRouter.HandleFunc("/api/redirect/delete", handleDeleteRedirectionRule)
  72. }