api.go 5.3 KB

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