api-router.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package router
  2. import (
  3. "one-api/controller"
  4. "one-api/middleware"
  5. "github.com/gin-contrib/gzip"
  6. "github.com/gin-gonic/gin"
  7. )
  8. func SetApiRouter(router *gin.Engine) {
  9. apiRouter := router.Group("/api")
  10. apiRouter.Use(gzip.Gzip(gzip.DefaultCompression))
  11. apiRouter.Use(middleware.GlobalAPIRateLimit())
  12. {
  13. apiRouter.GET("/setup", controller.GetSetup)
  14. apiRouter.POST("/setup", controller.PostSetup)
  15. apiRouter.GET("/status", controller.GetStatus)
  16. apiRouter.GET("/uptime/status", controller.GetUptimeKumaStatus)
  17. apiRouter.GET("/models", middleware.UserAuth(), controller.DashboardListModels)
  18. apiRouter.GET("/status/test", middleware.AdminAuth(), controller.TestStatus)
  19. apiRouter.GET("/notice", controller.GetNotice)
  20. apiRouter.GET("/about", controller.GetAbout)
  21. //apiRouter.GET("/midjourney", controller.GetMidjourney)
  22. apiRouter.GET("/home_page_content", controller.GetHomePageContent)
  23. apiRouter.GET("/pricing", middleware.TryUserAuth(), controller.GetPricing)
  24. apiRouter.GET("/verification", middleware.EmailVerificationRateLimit(), middleware.TurnstileCheck(), controller.SendEmailVerification)
  25. apiRouter.GET("/reset_password", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.SendPasswordResetEmail)
  26. apiRouter.POST("/user/reset", middleware.CriticalRateLimit(), controller.ResetPassword)
  27. apiRouter.GET("/oauth/github", middleware.CriticalRateLimit(), controller.GitHubOAuth)
  28. apiRouter.GET("/oauth/oidc", middleware.CriticalRateLimit(), controller.OidcAuth)
  29. apiRouter.GET("/oauth/linuxdo", middleware.CriticalRateLimit(), controller.LinuxdoOAuth)
  30. apiRouter.GET("/oauth/state", middleware.CriticalRateLimit(), controller.GenerateOAuthCode)
  31. // OAuth2 Server endpoints
  32. apiRouter.GET("/.well-known/jwks.json", controller.GetJWKS)
  33. apiRouter.GET("/.well-known/openid-configuration", controller.OAuthOIDCConfiguration)
  34. apiRouter.GET("/.well-known/oauth-authorization-server", controller.OAuthServerInfo)
  35. apiRouter.POST("/oauth/token", middleware.CriticalRateLimit(), controller.OAuthTokenEndpoint)
  36. apiRouter.GET("/oauth/authorize", controller.OAuthAuthorizeEndpoint)
  37. apiRouter.POST("/oauth/introspect", middleware.AdminAuth(), controller.OAuthIntrospect)
  38. apiRouter.POST("/oauth/revoke", middleware.CriticalRateLimit(), controller.OAuthRevoke)
  39. apiRouter.GET("/oauth/userinfo", middleware.OAuthJWTAuth(), controller.OAuthUserInfo)
  40. // OAuth2 管理API (前端使用)
  41. apiRouter.GET("/oauth/jwks", controller.GetJWKS)
  42. apiRouter.GET("/oauth/server-info", controller.OAuthServerInfo)
  43. apiRouter.GET("/oauth/wechat", middleware.CriticalRateLimit(), controller.WeChatAuth)
  44. apiRouter.GET("/oauth/wechat/bind", middleware.CriticalRateLimit(), controller.WeChatBind)
  45. apiRouter.GET("/oauth/email/bind", middleware.CriticalRateLimit(), controller.EmailBind)
  46. apiRouter.GET("/oauth/telegram/login", middleware.CriticalRateLimit(), controller.TelegramLogin)
  47. apiRouter.GET("/oauth/telegram/bind", middleware.CriticalRateLimit(), controller.TelegramBind)
  48. apiRouter.GET("/ratio_config", middleware.CriticalRateLimit(), controller.GetRatioConfig)
  49. apiRouter.POST("/stripe/webhook", controller.StripeWebhook)
  50. // OAuth2 admin operations
  51. oauthAdmin := apiRouter.Group("/oauth")
  52. oauthAdmin.Use(middleware.OptionalOAuthAuth(), middleware.RequireOAuthScopeIfPresent("admin"), middleware.RootAuth())
  53. {
  54. oauthAdmin.POST("/keys/rotate", controller.RotateOAuthSigningKey)
  55. oauthAdmin.GET("/keys", controller.ListOAuthSigningKeys)
  56. oauthAdmin.DELETE("/keys/:kid", controller.DeleteOAuthSigningKey)
  57. oauthAdmin.POST("/keys/generate_file", controller.GenerateOAuthSigningKeyFile)
  58. oauthAdmin.POST("/keys/import_pem", controller.ImportOAuthSigningKey)
  59. }
  60. userRoute := apiRouter.Group("/user")
  61. {
  62. userRoute.POST("/register", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.Register)
  63. userRoute.POST("/login", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.Login)
  64. userRoute.POST("/login/2fa", middleware.CriticalRateLimit(), controller.Verify2FALogin)
  65. //userRoute.POST("/tokenlog", middleware.CriticalRateLimit(), controller.TokenLog)
  66. userRoute.GET("/logout", controller.Logout)
  67. userRoute.GET("/epay/notify", controller.EpayNotify)
  68. userRoute.GET("/groups", controller.GetUserGroups)
  69. selfRoute := userRoute.Group("/")
  70. selfRoute.Use(middleware.UserAuth())
  71. {
  72. selfRoute.GET("/self/groups", controller.GetUserGroups)
  73. selfRoute.GET("/self", controller.GetSelf)
  74. selfRoute.GET("/models", controller.GetUserModels)
  75. selfRoute.PUT("/self", controller.UpdateSelf)
  76. selfRoute.DELETE("/self", controller.DeleteSelf)
  77. selfRoute.GET("/token", controller.GenerateAccessToken)
  78. selfRoute.GET("/aff", controller.GetAffCode)
  79. selfRoute.GET("/topup/info", controller.GetTopUpInfo)
  80. selfRoute.POST("/topup", middleware.CriticalRateLimit(), controller.TopUp)
  81. selfRoute.POST("/pay", middleware.CriticalRateLimit(), controller.RequestEpay)
  82. selfRoute.POST("/amount", controller.RequestAmount)
  83. selfRoute.POST("/stripe/pay", middleware.CriticalRateLimit(), controller.RequestStripePay)
  84. selfRoute.POST("/stripe/amount", controller.RequestStripeAmount)
  85. selfRoute.POST("/aff_transfer", controller.TransferAffQuota)
  86. selfRoute.PUT("/setting", controller.UpdateUserSetting)
  87. // 2FA routes
  88. selfRoute.GET("/2fa/status", controller.Get2FAStatus)
  89. selfRoute.POST("/2fa/setup", controller.Setup2FA)
  90. selfRoute.POST("/2fa/enable", controller.Enable2FA)
  91. selfRoute.POST("/2fa/disable", controller.Disable2FA)
  92. selfRoute.POST("/2fa/backup_codes", controller.RegenerateBackupCodes)
  93. }
  94. adminRoute := userRoute.Group("/")
  95. adminRoute.Use(middleware.OptionalOAuthAuth(), middleware.RequireOAuthScopeIfPresent("admin"), middleware.AdminAuth())
  96. {
  97. adminRoute.GET("/", controller.GetAllUsers)
  98. adminRoute.GET("/search", controller.SearchUsers)
  99. adminRoute.GET("/:id", controller.GetUser)
  100. adminRoute.POST("/", controller.CreateUser)
  101. adminRoute.POST("/manage", controller.ManageUser)
  102. adminRoute.PUT("/", controller.UpdateUser)
  103. adminRoute.DELETE("/:id", controller.DeleteUser)
  104. // Admin 2FA routes
  105. adminRoute.GET("/2fa/stats", controller.Admin2FAStats)
  106. adminRoute.DELETE("/:id/2fa", controller.AdminDisable2FA)
  107. }
  108. }
  109. optionRoute := apiRouter.Group("/option")
  110. optionRoute.Use(middleware.OptionalOAuthAuth(), middleware.RequireOAuthScopeIfPresent("admin"), middleware.RootAuth())
  111. {
  112. optionRoute.GET("/", controller.GetOptions)
  113. optionRoute.PUT("/", controller.UpdateOption)
  114. optionRoute.POST("/rest_model_ratio", controller.ResetModelRatio)
  115. optionRoute.POST("/migrate_console_setting", controller.MigrateConsoleSetting) // 用于迁移检测的旧键,下个版本会删除
  116. }
  117. ratioSyncRoute := apiRouter.Group("/ratio_sync")
  118. ratioSyncRoute.Use(middleware.RootAuth())
  119. {
  120. ratioSyncRoute.GET("/channels", controller.GetSyncableChannels)
  121. ratioSyncRoute.POST("/fetch", controller.FetchUpstreamRatios)
  122. }
  123. channelRoute := apiRouter.Group("/channel")
  124. channelRoute.Use(middleware.OptionalOAuthAuth(), middleware.RequireOAuthScopeIfPresent("admin"), middleware.AdminAuth())
  125. {
  126. channelRoute.GET("/", controller.GetAllChannels)
  127. channelRoute.GET("/search", controller.SearchChannels)
  128. channelRoute.GET("/models", controller.ChannelListModels)
  129. channelRoute.GET("/models_enabled", controller.EnabledListModels)
  130. channelRoute.GET("/:id", controller.GetChannel)
  131. channelRoute.POST("/:id/key", middleware.CriticalRateLimit(), middleware.DisableCache(), controller.GetChannelKey)
  132. channelRoute.GET("/test", controller.TestAllChannels)
  133. channelRoute.GET("/test/:id", controller.TestChannel)
  134. channelRoute.GET("/update_balance", controller.UpdateAllChannelsBalance)
  135. channelRoute.GET("/update_balance/:id", controller.UpdateChannelBalance)
  136. channelRoute.POST("/", controller.AddChannel)
  137. channelRoute.PUT("/", controller.UpdateChannel)
  138. channelRoute.DELETE("/disabled", controller.DeleteDisabledChannel)
  139. channelRoute.POST("/tag/disabled", controller.DisableTagChannels)
  140. channelRoute.POST("/tag/enabled", controller.EnableTagChannels)
  141. channelRoute.PUT("/tag", controller.EditTagChannels)
  142. channelRoute.DELETE("/:id", controller.DeleteChannel)
  143. channelRoute.POST("/batch", controller.DeleteChannelBatch)
  144. channelRoute.POST("/fix", controller.FixChannelsAbilities)
  145. channelRoute.GET("/fetch_models/:id", controller.FetchUpstreamModels)
  146. channelRoute.POST("/fetch_models", controller.FetchModels)
  147. channelRoute.POST("/batch/tag", controller.BatchSetChannelTag)
  148. channelRoute.GET("/tag/models", controller.GetTagModels)
  149. channelRoute.POST("/copy/:id", controller.CopyChannel)
  150. channelRoute.POST("/multi_key/manage", controller.ManageMultiKeys)
  151. }
  152. tokenRoute := apiRouter.Group("/token")
  153. tokenRoute.Use(middleware.UserAuth())
  154. {
  155. tokenRoute.GET("/", controller.GetAllTokens)
  156. tokenRoute.GET("/search", controller.SearchTokens)
  157. tokenRoute.GET("/:id", controller.GetToken)
  158. tokenRoute.POST("/", controller.AddToken)
  159. tokenRoute.PUT("/", controller.UpdateToken)
  160. tokenRoute.DELETE("/:id", controller.DeleteToken)
  161. tokenRoute.POST("/batch", controller.DeleteTokenBatch)
  162. }
  163. usageRoute := apiRouter.Group("/usage")
  164. usageRoute.Use(middleware.CriticalRateLimit())
  165. {
  166. tokenUsageRoute := usageRoute.Group("/token")
  167. tokenUsageRoute.Use(middleware.TokenAuth())
  168. {
  169. tokenUsageRoute.GET("/", controller.GetTokenUsage)
  170. }
  171. }
  172. redemptionRoute := apiRouter.Group("/redemption")
  173. redemptionRoute.Use(middleware.OptionalOAuthAuth(), middleware.RequireOAuthScopeIfPresent("admin"), middleware.AdminAuth())
  174. {
  175. redemptionRoute.GET("/", controller.GetAllRedemptions)
  176. redemptionRoute.GET("/search", controller.SearchRedemptions)
  177. redemptionRoute.GET("/:id", controller.GetRedemption)
  178. redemptionRoute.POST("/", controller.AddRedemption)
  179. redemptionRoute.PUT("/", controller.UpdateRedemption)
  180. redemptionRoute.DELETE("/invalid", controller.DeleteInvalidRedemption)
  181. redemptionRoute.DELETE("/:id", controller.DeleteRedemption)
  182. }
  183. logRoute := apiRouter.Group("/log")
  184. logRoute.GET("/", middleware.AdminAuth(), controller.GetAllLogs)
  185. logRoute.DELETE("/", middleware.AdminAuth(), controller.DeleteHistoryLogs)
  186. logRoute.GET("/stat", middleware.AdminAuth(), controller.GetLogsStat)
  187. logRoute.GET("/self/stat", middleware.UserAuth(), controller.GetLogsSelfStat)
  188. logRoute.GET("/search", middleware.AdminAuth(), controller.SearchAllLogs)
  189. logRoute.GET("/self", middleware.UserAuth(), controller.GetUserLogs)
  190. logRoute.GET("/self/search", middleware.UserAuth(), controller.SearchUserLogs)
  191. dataRoute := apiRouter.Group("/data")
  192. dataRoute.GET("/", middleware.AdminAuth(), controller.GetAllQuotaDates)
  193. dataRoute.GET("/self", middleware.UserAuth(), controller.GetUserQuotaDates)
  194. logRoute.Use(middleware.CORS())
  195. {
  196. logRoute.GET("/token", controller.GetLogByKey)
  197. }
  198. groupRoute := apiRouter.Group("/group")
  199. groupRoute.Use(middleware.OptionalOAuthAuth(), middleware.RequireOAuthScopeIfPresent("admin"), middleware.AdminAuth())
  200. {
  201. groupRoute.GET("/", controller.GetGroups)
  202. }
  203. prefillGroupRoute := apiRouter.Group("/prefill_group")
  204. prefillGroupRoute.Use(middleware.OptionalOAuthAuth(), middleware.RequireOAuthScopeIfPresent("admin"), middleware.AdminAuth())
  205. {
  206. prefillGroupRoute.GET("/", controller.GetPrefillGroups)
  207. prefillGroupRoute.POST("/", controller.CreatePrefillGroup)
  208. prefillGroupRoute.PUT("/", controller.UpdatePrefillGroup)
  209. prefillGroupRoute.DELETE("/:id", controller.DeletePrefillGroup)
  210. }
  211. mjRoute := apiRouter.Group("/mj")
  212. mjRoute.GET("/self", middleware.UserAuth(), controller.GetUserMidjourney)
  213. mjRoute.GET("/", middleware.AdminAuth(), controller.GetAllMidjourney)
  214. taskRoute := apiRouter.Group("/task")
  215. {
  216. taskRoute.GET("/self", middleware.UserAuth(), controller.GetUserTask)
  217. taskRoute.GET("/", middleware.AdminAuth(), controller.GetAllTask)
  218. }
  219. vendorRoute := apiRouter.Group("/vendors")
  220. vendorRoute.Use(middleware.AdminAuth())
  221. {
  222. vendorRoute.GET("/", controller.GetAllVendors)
  223. vendorRoute.GET("/search", controller.SearchVendors)
  224. vendorRoute.GET("/:id", controller.GetVendorMeta)
  225. vendorRoute.POST("/", controller.CreateVendorMeta)
  226. vendorRoute.PUT("/", controller.UpdateVendorMeta)
  227. vendorRoute.DELETE("/:id", controller.DeleteVendorMeta)
  228. }
  229. modelsRoute := apiRouter.Group("/models")
  230. modelsRoute.Use(middleware.AdminAuth())
  231. {
  232. modelsRoute.GET("/sync_upstream/preview", controller.SyncUpstreamPreview)
  233. modelsRoute.POST("/sync_upstream", controller.SyncUpstreamModels)
  234. modelsRoute.GET("/missing", controller.GetMissingModels)
  235. modelsRoute.GET("/", controller.GetAllModelsMeta)
  236. modelsRoute.GET("/search", controller.SearchModelsMeta)
  237. modelsRoute.GET("/:id", controller.GetModelMeta)
  238. modelsRoute.POST("/", controller.CreateModelMeta)
  239. modelsRoute.PUT("/", controller.UpdateModelMeta)
  240. modelsRoute.DELETE("/:id", controller.DeleteModelMeta)
  241. }
  242. oauthClientsRoute := apiRouter.Group("/oauth_clients")
  243. oauthClientsRoute.Use(middleware.AdminAuth())
  244. {
  245. oauthClientsRoute.GET("/", controller.GetAllOAuthClients)
  246. oauthClientsRoute.GET("/search", controller.SearchOAuthClients)
  247. oauthClientsRoute.GET("/:id", controller.GetOAuthClient)
  248. oauthClientsRoute.POST("/", controller.CreateOAuthClient)
  249. oauthClientsRoute.PUT("/", controller.UpdateOAuthClient)
  250. oauthClientsRoute.DELETE("/:id", controller.DeleteOAuthClient)
  251. oauthClientsRoute.POST("/:id/regenerate_secret", controller.RegenerateOAuthClientSecret)
  252. }
  253. }
  254. }