api.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "imuslab.com/Zoarxy/mod/auth"
  6. "imuslab.com/Zoarxy/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. //Auth APIs
  31. http.HandleFunc("/api/auth/login", authAgent.HandleLogin)
  32. http.HandleFunc("/api/auth/logout", authAgent.HandleLogout)
  33. http.HandleFunc("/api/auth/checkLogin", func(w http.ResponseWriter, r *http.Request) {
  34. if requireAuth {
  35. authAgent.CheckLogin(w, r)
  36. } else {
  37. utils.SendJSONResponse(w, "true")
  38. }
  39. })
  40. http.HandleFunc("/api/auth/username", func(w http.ResponseWriter, r *http.Request) {
  41. username, err := authAgent.GetUserName(w, r)
  42. if err != nil {
  43. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  44. return
  45. }
  46. js, _ := json.Marshal(username)
  47. utils.SendJSONResponse(w, string(js))
  48. })
  49. http.HandleFunc("/api/auth/userCount", func(w http.ResponseWriter, r *http.Request) {
  50. uc := authAgent.GetUserCounts()
  51. js, _ := json.Marshal(uc)
  52. utils.SendJSONResponse(w, string(js))
  53. })
  54. http.HandleFunc("/api/auth/register", func(w http.ResponseWriter, r *http.Request) {
  55. if authAgent.GetUserCounts() == 0 {
  56. //Allow register root admin
  57. authAgent.HandleRegisterWithoutEmail(w, r, func(username, reserved string) {
  58. })
  59. } else {
  60. //This function is disabled
  61. utils.SendErrorResponse(w, "Root management account already exists")
  62. }
  63. })
  64. http.HandleFunc("/api/auth/changePassword", func(w http.ResponseWriter, r *http.Request) {
  65. username, err := authAgent.GetUserName(w, r)
  66. if err != nil {
  67. http.Error(w, "401 - Unauthorized", http.StatusUnauthorized)
  68. return
  69. }
  70. oldPassword, err := utils.PostPara(r, "oldPassword")
  71. if err != nil {
  72. utils.SendErrorResponse(w, "empty current password")
  73. return
  74. }
  75. newPassword, err := utils.PostPara(r, "newPassword")
  76. if err != nil {
  77. utils.SendErrorResponse(w, "empty new password")
  78. return
  79. }
  80. confirmPassword, _ := utils.PostPara(r, "confirmPassword")
  81. if newPassword != confirmPassword {
  82. utils.SendErrorResponse(w, "confirm password not match")
  83. return
  84. }
  85. //Check if the old password correct
  86. oldPasswordCorrect, _ := authAgent.ValidateUsernameAndPasswordWithReason(username, oldPassword)
  87. if !oldPasswordCorrect {
  88. utils.SendErrorResponse(w, "Invalid current password given")
  89. return
  90. }
  91. //Change the password of the root user
  92. authAgent.UnregisterUser(username)
  93. authAgent.CreateUserAccount(username, newPassword, "")
  94. })
  95. //Reverse proxy
  96. authRouter.HandleFunc("/api/proxy/enable", ReverseProxyHandleOnOff)
  97. authRouter.HandleFunc("/api/proxy/add", ReverseProxyHandleAddEndpoint)
  98. authRouter.HandleFunc("/api/proxy/status", ReverseProxyStatus)
  99. authRouter.HandleFunc("/api/proxy/list", ReverseProxyList)
  100. authRouter.HandleFunc("/api/proxy/del", DeleteProxyEndpoint)
  101. authRouter.HandleFunc("/api/proxy/setIncoming", HandleIncomingPortSet)
  102. authRouter.HandleFunc("/api/proxy/useHttpsRedirect", HandleUpdateHttpsRedirect)
  103. //TLS / SSL config
  104. authRouter.HandleFunc("/api/cert/tls", handleToggleTLSProxy)
  105. authRouter.HandleFunc("/api/cert/upload", handleCertUpload)
  106. authRouter.HandleFunc("/api/cert/list", handleListCertificate)
  107. authRouter.HandleFunc("/api/cert/checkDefault", handleDefaultCertCheck)
  108. authRouter.HandleFunc("/api/cert/delete", handleCertRemove)
  109. //Redirection config
  110. authRouter.HandleFunc("/api/redirect/list", handleListRedirectionRules)
  111. authRouter.HandleFunc("/api/redirect/add", handleAddRedirectionRule)
  112. authRouter.HandleFunc("/api/redirect/delete", handleDeleteRedirectionRule)
  113. //Blacklist APIs
  114. authRouter.HandleFunc("/api/blacklist/list", handleListBlacklisted)
  115. authRouter.HandleFunc("/api/blacklist/country/add", handleCountryBlacklistAdd)
  116. authRouter.HandleFunc("/api/blacklist/country/remove", handleCountryBlacklistRemove)
  117. authRouter.HandleFunc("/api/blacklist/ip/add", handleIpBlacklistAdd)
  118. authRouter.HandleFunc("/api/blacklist/ip/remove", handleIpBlacklistRemove)
  119. authRouter.HandleFunc("/api/blacklist/enable", handleBlacklistEnable)
  120. //Upnp
  121. authRouter.HandleFunc("/api/upnp/discover", handleUpnpDiscover)
  122. //If you got APIs to add, append them here
  123. }