api.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. //Upnp
  63. authRouter.HandleFunc("/api/upnp/discover", handleUpnpDiscover)
  64. //If you got APIs to add, append them here
  65. }
  66. //Function to renders Auth related APIs
  67. func registerAuthAPIs(requireAuth bool) {
  68. //Auth APIs
  69. http.HandleFunc("/api/auth/login", authAgent.HandleLogin)
  70. http.HandleFunc("/api/auth/logout", authAgent.HandleLogout)
  71. http.HandleFunc("/api/auth/checkLogin", func(w http.ResponseWriter, r *http.Request) {
  72. if requireAuth {
  73. authAgent.CheckLogin(w, r)
  74. } else {
  75. utils.SendJSONResponse(w, "true")
  76. }
  77. })
  78. http.HandleFunc("/api/auth/username", func(w http.ResponseWriter, r *http.Request) {
  79. username, err := authAgent.GetUserName(w, r)
  80. if err != nil {
  81. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  82. return
  83. }
  84. js, _ := json.Marshal(username)
  85. utils.SendJSONResponse(w, string(js))
  86. })
  87. http.HandleFunc("/api/auth/userCount", func(w http.ResponseWriter, r *http.Request) {
  88. uc := authAgent.GetUserCounts()
  89. js, _ := json.Marshal(uc)
  90. utils.SendJSONResponse(w, string(js))
  91. })
  92. http.HandleFunc("/api/auth/register", func(w http.ResponseWriter, r *http.Request) {
  93. if authAgent.GetUserCounts() == 0 {
  94. //Allow register root admin
  95. authAgent.HandleRegisterWithoutEmail(w, r, func(username, reserved string) {
  96. })
  97. } else {
  98. //This function is disabled
  99. utils.SendErrorResponse(w, "Root management account already exists")
  100. }
  101. })
  102. http.HandleFunc("/api/auth/changePassword", func(w http.ResponseWriter, r *http.Request) {
  103. username, err := authAgent.GetUserName(w, r)
  104. if err != nil {
  105. http.Error(w, "401 - Unauthorized", http.StatusUnauthorized)
  106. return
  107. }
  108. oldPassword, err := utils.PostPara(r, "oldPassword")
  109. if err != nil {
  110. utils.SendErrorResponse(w, "empty current password")
  111. return
  112. }
  113. newPassword, err := utils.PostPara(r, "newPassword")
  114. if err != nil {
  115. utils.SendErrorResponse(w, "empty new password")
  116. return
  117. }
  118. confirmPassword, _ := utils.PostPara(r, "confirmPassword")
  119. if newPassword != confirmPassword {
  120. utils.SendErrorResponse(w, "confirm password not match")
  121. return
  122. }
  123. //Check if the old password correct
  124. oldPasswordCorrect, _ := authAgent.ValidateUsernameAndPasswordWithReason(username, oldPassword)
  125. if !oldPasswordCorrect {
  126. utils.SendErrorResponse(w, "Invalid current password given")
  127. return
  128. }
  129. //Change the password of the root user
  130. authAgent.UnregisterUser(username)
  131. authAgent.CreateUserAccount(username, newPassword, "")
  132. })
  133. }