api.go 5.0 KB

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