api.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "imuslab.com/zoraxy/mod/auth"
  6. "imuslab.com/zoraxy/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. //Authentication APIs
  31. registerAuthAPIs(requireAuth)
  32. //Reverse proxy
  33. authRouter.HandleFunc("/api/proxy/enable", ReverseProxyHandleOnOff)
  34. authRouter.HandleFunc("/api/proxy/add", ReverseProxyHandleAddEndpoint)
  35. authRouter.HandleFunc("/api/proxy/status", ReverseProxyStatus)
  36. authRouter.HandleFunc("/api/proxy/list", ReverseProxyList)
  37. authRouter.HandleFunc("/api/proxy/del", DeleteProxyEndpoint)
  38. authRouter.HandleFunc("/api/proxy/setIncoming", HandleIncomingPortSet)
  39. authRouter.HandleFunc("/api/proxy/useHttpsRedirect", HandleUpdateHttpsRedirect)
  40. authRouter.HandleFunc("/api/proxy/requestIsProxied", HandleManagementProxyCheck)
  41. //TLS / SSL config
  42. authRouter.HandleFunc("/api/cert/tls", handleToggleTLSProxy)
  43. authRouter.HandleFunc("/api/cert/upload", handleCertUpload)
  44. authRouter.HandleFunc("/api/cert/list", handleListCertificate)
  45. authRouter.HandleFunc("/api/cert/checkDefault", handleDefaultCertCheck)
  46. authRouter.HandleFunc("/api/cert/delete", handleCertRemove)
  47. //Redirection config
  48. authRouter.HandleFunc("/api/redirect/list", handleListRedirectionRules)
  49. authRouter.HandleFunc("/api/redirect/add", handleAddRedirectionRule)
  50. authRouter.HandleFunc("/api/redirect/delete", handleDeleteRedirectionRule)
  51. //Blacklist APIs
  52. authRouter.HandleFunc("/api/blacklist/list", handleListBlacklisted)
  53. authRouter.HandleFunc("/api/blacklist/country/add", handleCountryBlacklistAdd)
  54. authRouter.HandleFunc("/api/blacklist/country/remove", handleCountryBlacklistRemove)
  55. authRouter.HandleFunc("/api/blacklist/ip/add", handleIpBlacklistAdd)
  56. authRouter.HandleFunc("/api/blacklist/ip/remove", handleIpBlacklistRemove)
  57. authRouter.HandleFunc("/api/blacklist/enable", handleBlacklistEnable)
  58. //Statistic & uptime monitoring API
  59. authRouter.HandleFunc("/api/stats/summary", statisticCollector.HandleTodayStatLoad)
  60. authRouter.HandleFunc("/api/stats/countries", HandleCountryDistrSummary)
  61. authRouter.HandleFunc("/api/utm/list", HandleUptimeMonitorListing)
  62. //If you got APIs to add, append them here
  63. }
  64. //Function to renders Auth related APIs
  65. func registerAuthAPIs(requireAuth bool) {
  66. //Auth APIs
  67. http.HandleFunc("/api/auth/login", authAgent.HandleLogin)
  68. http.HandleFunc("/api/auth/logout", authAgent.HandleLogout)
  69. http.HandleFunc("/api/auth/checkLogin", func(w http.ResponseWriter, r *http.Request) {
  70. if requireAuth {
  71. authAgent.CheckLogin(w, r)
  72. } else {
  73. utils.SendJSONResponse(w, "true")
  74. }
  75. })
  76. http.HandleFunc("/api/auth/username", func(w http.ResponseWriter, r *http.Request) {
  77. username, err := authAgent.GetUserName(w, r)
  78. if err != nil {
  79. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  80. return
  81. }
  82. js, _ := json.Marshal(username)
  83. utils.SendJSONResponse(w, string(js))
  84. })
  85. http.HandleFunc("/api/auth/userCount", func(w http.ResponseWriter, r *http.Request) {
  86. uc := authAgent.GetUserCounts()
  87. js, _ := json.Marshal(uc)
  88. utils.SendJSONResponse(w, string(js))
  89. })
  90. http.HandleFunc("/api/auth/register", func(w http.ResponseWriter, r *http.Request) {
  91. if authAgent.GetUserCounts() == 0 {
  92. //Allow register root admin
  93. authAgent.HandleRegisterWithoutEmail(w, r, func(username, reserved string) {
  94. })
  95. } else {
  96. //This function is disabled
  97. utils.SendErrorResponse(w, "Root management account already exists")
  98. }
  99. })
  100. http.HandleFunc("/api/auth/changePassword", func(w http.ResponseWriter, r *http.Request) {
  101. username, err := authAgent.GetUserName(w, r)
  102. if err != nil {
  103. http.Error(w, "401 - Unauthorized", http.StatusUnauthorized)
  104. return
  105. }
  106. oldPassword, err := utils.PostPara(r, "oldPassword")
  107. if err != nil {
  108. utils.SendErrorResponse(w, "empty current password")
  109. return
  110. }
  111. newPassword, err := utils.PostPara(r, "newPassword")
  112. if err != nil {
  113. utils.SendErrorResponse(w, "empty new password")
  114. return
  115. }
  116. confirmPassword, _ := utils.PostPara(r, "confirmPassword")
  117. if newPassword != confirmPassword {
  118. utils.SendErrorResponse(w, "confirm password not match")
  119. return
  120. }
  121. //Check if the old password correct
  122. oldPasswordCorrect, _ := authAgent.ValidateUsernameAndPasswordWithReason(username, oldPassword)
  123. if !oldPasswordCorrect {
  124. utils.SendErrorResponse(w, "Invalid current password given")
  125. return
  126. }
  127. //Change the password of the root user
  128. authAgent.UnregisterUser(username)
  129. authAgent.CreateUserAccount(username, newPassword, "")
  130. })
  131. }