1
0

api.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. //Global Area Network APIs
  66. authRouter.HandleFunc("/api/gan/network/info", ganManager.HandleGetNodeID)
  67. authRouter.HandleFunc("/api/gan/network/add", ganManager.HandleAddNetwork)
  68. authRouter.HandleFunc("/api/gan/network/remove", ganManager.HandleRemoveNetwork)
  69. authRouter.HandleFunc("/api/gan/network/list", ganManager.HandleListNetwork)
  70. authRouter.HandleFunc("/api/gan/network/name", ganManager.HandleNetworkNaming)
  71. //authRouter.HandleFunc("/api/gan/network/detail", ganManager.HandleNetworkDetails)
  72. authRouter.HandleFunc("/api/gan/network/setRange", ganManager.HandleSetRanges)
  73. authRouter.HandleFunc("/api/gan/members/list", ganManager.HandleMemberList)
  74. authRouter.HandleFunc("/api/gan/members/ip", ganManager.HandleMemberIP)
  75. authRouter.HandleFunc("/api/gan/members/name", ganManager.HandleMemberNaming)
  76. authRouter.HandleFunc("/api/gan/members/authorize", ganManager.HandleMemberAuthorization)
  77. authRouter.HandleFunc("/api/gan/members/delete", ganManager.HandleMemberDelete)
  78. //mDNS APIs
  79. authRouter.HandleFunc("/api/mdns/list", HandleMdnsListing)
  80. authRouter.HandleFunc("/api/mdns/discover", HandleMdnsScanning)
  81. //Network utilities
  82. authRouter.HandleFunc("/api/tools/ipscan", HandleIpScan)
  83. authRouter.HandleFunc("/api/tools/webssh", HandleCreateProxySession)
  84. authRouter.HandleFunc("/api/tools/websshSupported", HandleWebSshSupportCheck)
  85. authRouter.HandleFunc("/api/tools/wol", HandleWakeOnLan)
  86. authRouter.HandleFunc("/api/tools/smtp/get", HandleSMTPGet)
  87. authRouter.HandleFunc("/api/tools/smtp/set", HandleSMTPSet)
  88. authRouter.HandleFunc("/api/tools/smtp/admin", HandleAdminEmailGet)
  89. authRouter.HandleFunc("/api/tools/smtp/test", HandleTestEmailSend)
  90. //Account Reset
  91. http.HandleFunc("/api/account/reset", HandleAdminAccountResetEmail)
  92. http.HandleFunc("/api/account/new", HandleNewPasswordSetup)
  93. //If you got APIs to add, append them here
  94. }
  95. //Function to renders Auth related APIs
  96. func registerAuthAPIs(requireAuth bool) {
  97. //Auth APIs
  98. http.HandleFunc("/api/auth/login", authAgent.HandleLogin)
  99. http.HandleFunc("/api/auth/logout", authAgent.HandleLogout)
  100. http.HandleFunc("/api/auth/checkLogin", func(w http.ResponseWriter, r *http.Request) {
  101. if requireAuth {
  102. authAgent.CheckLogin(w, r)
  103. } else {
  104. utils.SendJSONResponse(w, "true")
  105. }
  106. })
  107. http.HandleFunc("/api/auth/username", func(w http.ResponseWriter, r *http.Request) {
  108. username, err := authAgent.GetUserName(w, r)
  109. if err != nil {
  110. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  111. return
  112. }
  113. js, _ := json.Marshal(username)
  114. utils.SendJSONResponse(w, string(js))
  115. })
  116. http.HandleFunc("/api/auth/userCount", func(w http.ResponseWriter, r *http.Request) {
  117. uc := authAgent.GetUserCounts()
  118. js, _ := json.Marshal(uc)
  119. utils.SendJSONResponse(w, string(js))
  120. })
  121. http.HandleFunc("/api/auth/register", func(w http.ResponseWriter, r *http.Request) {
  122. if authAgent.GetUserCounts() == 0 {
  123. //Allow register root admin
  124. authAgent.HandleRegisterWithoutEmail(w, r, func(username, reserved string) {
  125. })
  126. } else {
  127. //This function is disabled
  128. utils.SendErrorResponse(w, "Root management account already exists")
  129. }
  130. })
  131. http.HandleFunc("/api/auth/changePassword", func(w http.ResponseWriter, r *http.Request) {
  132. username, err := authAgent.GetUserName(w, r)
  133. if err != nil {
  134. http.Error(w, "401 - Unauthorized", http.StatusUnauthorized)
  135. return
  136. }
  137. oldPassword, err := utils.PostPara(r, "oldPassword")
  138. if err != nil {
  139. utils.SendErrorResponse(w, "empty current password")
  140. return
  141. }
  142. newPassword, err := utils.PostPara(r, "newPassword")
  143. if err != nil {
  144. utils.SendErrorResponse(w, "empty new password")
  145. return
  146. }
  147. confirmPassword, _ := utils.PostPara(r, "confirmPassword")
  148. if newPassword != confirmPassword {
  149. utils.SendErrorResponse(w, "confirm password not match")
  150. return
  151. }
  152. //Check if the old password correct
  153. oldPasswordCorrect, _ := authAgent.ValidateUsernameAndPasswordWithReason(username, oldPassword)
  154. if !oldPasswordCorrect {
  155. utils.SendErrorResponse(w, "Invalid current password given")
  156. return
  157. }
  158. //Change the password of the root user
  159. authAgent.UnregisterUser(username)
  160. authAgent.CreateUserAccount(username, newPassword, "")
  161. })
  162. }