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