api.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "imuslab.com/zoraxy/mod/auth"
  6. "imuslab.com/zoraxy/mod/netstat"
  7. "imuslab.com/zoraxy/mod/utils"
  8. )
  9. /*
  10. API.go
  11. This file contains all the API called by the web management interface
  12. */
  13. var requireAuth = true
  14. func initAPIs() {
  15. authRouter := auth.NewManagedHTTPRouter(auth.RouterOption{
  16. AuthAgent: authAgent,
  17. RequireAuth: requireAuth,
  18. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  19. http.Error(w, "401 - Unauthorized", http.StatusUnauthorized)
  20. },
  21. })
  22. //Register the standard web services urls
  23. fs := http.FileServer(http.FS(webres))
  24. if development {
  25. fs = http.FileServer(http.Dir("web/"))
  26. }
  27. //Add a layer of middleware for advance control
  28. advHandler := FSHandler(fs)
  29. http.Handle("/", advHandler)
  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/tlscheck", HandleCheckSiteSupportTLS)
  39. authRouter.HandleFunc("/api/proxy/setIncoming", HandleIncomingPortSet)
  40. authRouter.HandleFunc("/api/proxy/useHttpsRedirect", HandleUpdateHttpsRedirect)
  41. authRouter.HandleFunc("/api/proxy/requestIsProxied", HandleManagementProxyCheck)
  42. //TLS / SSL config
  43. authRouter.HandleFunc("/api/cert/tls", handleToggleTLSProxy)
  44. authRouter.HandleFunc("/api/cert/upload", handleCertUpload)
  45. authRouter.HandleFunc("/api/cert/list", handleListCertificate)
  46. authRouter.HandleFunc("/api/cert/checkDefault", handleDefaultCertCheck)
  47. authRouter.HandleFunc("/api/cert/delete", handleCertRemove)
  48. //Redirection config
  49. authRouter.HandleFunc("/api/redirect/list", handleListRedirectionRules)
  50. authRouter.HandleFunc("/api/redirect/add", handleAddRedirectionRule)
  51. authRouter.HandleFunc("/api/redirect/delete", handleDeleteRedirectionRule)
  52. //Blacklist APIs
  53. authRouter.HandleFunc("/api/blacklist/list", handleListBlacklisted)
  54. authRouter.HandleFunc("/api/blacklist/country/add", handleCountryBlacklistAdd)
  55. authRouter.HandleFunc("/api/blacklist/country/remove", handleCountryBlacklistRemove)
  56. authRouter.HandleFunc("/api/blacklist/ip/add", handleIpBlacklistAdd)
  57. authRouter.HandleFunc("/api/blacklist/ip/remove", handleIpBlacklistRemove)
  58. authRouter.HandleFunc("/api/blacklist/enable", handleBlacklistEnable)
  59. //Statistic & uptime monitoring API
  60. authRouter.HandleFunc("/api/stats/summary", statisticCollector.HandleTodayStatLoad)
  61. authRouter.HandleFunc("/api/stats/countries", HandleCountryDistrSummary)
  62. authRouter.HandleFunc("/api/stats/netstat", netstat.HandleGetNetworkInterfaceStats)
  63. authRouter.HandleFunc("/api/stats/listnic", netstat.HandleListNetworkInterfaces)
  64. authRouter.HandleFunc("/api/utm/list", HandleUptimeMonitorListing)
  65. //mDNS APIs
  66. authRouter.HandleFunc("/api/mdns/list", HandleMdnsListing)
  67. authRouter.HandleFunc("/api/mdns/discover", HandleMdnsScanning)
  68. //Network utilities
  69. authRouter.HandleFunc("/api/tools/ipscan", HandleIpScan)
  70. authRouter.HandleFunc("/api/tools/webssh", HandleCreateProxySession)
  71. authRouter.HandleFunc("/api/tools/websshSupported", HandleWebSshSupportCheck)
  72. authRouter.HandleFunc("/api/tools/wol", HandleWakeOnLan)
  73. //If you got APIs to add, append them here
  74. }
  75. //Function to renders Auth related APIs
  76. func registerAuthAPIs(requireAuth bool) {
  77. //Auth APIs
  78. http.HandleFunc("/api/auth/login", authAgent.HandleLogin)
  79. http.HandleFunc("/api/auth/logout", authAgent.HandleLogout)
  80. http.HandleFunc("/api/auth/checkLogin", func(w http.ResponseWriter, r *http.Request) {
  81. if requireAuth {
  82. authAgent.CheckLogin(w, r)
  83. } else {
  84. utils.SendJSONResponse(w, "true")
  85. }
  86. })
  87. http.HandleFunc("/api/auth/username", func(w http.ResponseWriter, r *http.Request) {
  88. username, err := authAgent.GetUserName(w, r)
  89. if err != nil {
  90. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  91. return
  92. }
  93. js, _ := json.Marshal(username)
  94. utils.SendJSONResponse(w, string(js))
  95. })
  96. http.HandleFunc("/api/auth/userCount", func(w http.ResponseWriter, r *http.Request) {
  97. uc := authAgent.GetUserCounts()
  98. js, _ := json.Marshal(uc)
  99. utils.SendJSONResponse(w, string(js))
  100. })
  101. http.HandleFunc("/api/auth/register", func(w http.ResponseWriter, r *http.Request) {
  102. if authAgent.GetUserCounts() == 0 {
  103. //Allow register root admin
  104. authAgent.HandleRegisterWithoutEmail(w, r, func(username, reserved string) {
  105. })
  106. } else {
  107. //This function is disabled
  108. utils.SendErrorResponse(w, "Root management account already exists")
  109. }
  110. })
  111. http.HandleFunc("/api/auth/changePassword", func(w http.ResponseWriter, r *http.Request) {
  112. username, err := authAgent.GetUserName(w, r)
  113. if err != nil {
  114. http.Error(w, "401 - Unauthorized", http.StatusUnauthorized)
  115. return
  116. }
  117. oldPassword, err := utils.PostPara(r, "oldPassword")
  118. if err != nil {
  119. utils.SendErrorResponse(w, "empty current password")
  120. return
  121. }
  122. newPassword, err := utils.PostPara(r, "newPassword")
  123. if err != nil {
  124. utils.SendErrorResponse(w, "empty new password")
  125. return
  126. }
  127. confirmPassword, _ := utils.PostPara(r, "confirmPassword")
  128. if newPassword != confirmPassword {
  129. utils.SendErrorResponse(w, "confirm password not match")
  130. return
  131. }
  132. //Check if the old password correct
  133. oldPasswordCorrect, _ := authAgent.ValidateUsernameAndPasswordWithReason(username, oldPassword)
  134. if !oldPasswordCorrect {
  135. utils.SendErrorResponse(w, "Invalid current password given")
  136. return
  137. }
  138. //Change the password of the root user
  139. authAgent.UnregisterUser(username)
  140. authAgent.CreateUserAccount(username, newPassword, "")
  141. })
  142. }