Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

internal.go 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. // Package private includes all internal routes. The package name internal is ideal but Golang is not allowed, so we use private as package name instead.
  5. package private
  6. import (
  7. "strings"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/private"
  11. "code.gitea.io/gitea/modules/setting"
  12. "gitea.com/macaron/binding"
  13. "gitea.com/macaron/macaron"
  14. )
  15. // CheckInternalToken check internal token is set
  16. func CheckInternalToken(ctx *macaron.Context) {
  17. tokens := ctx.Req.Header.Get("Authorization")
  18. fields := strings.Fields(tokens)
  19. if len(fields) != 2 || fields[0] != "Bearer" || fields[1] != setting.InternalToken {
  20. log.Debug("Forbidden attempt to access internal url: Authorization header: %s", tokens)
  21. ctx.Error(403)
  22. }
  23. }
  24. //GetRepositoryByOwnerAndName chainload to models.GetRepositoryByOwnerAndName
  25. func GetRepositoryByOwnerAndName(ctx *macaron.Context) {
  26. //TODO use repo.Get(ctx *context.APIContext) ?
  27. ownerName := ctx.Params(":owner")
  28. repoName := ctx.Params(":repo")
  29. repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName)
  30. if err != nil {
  31. ctx.JSON(500, map[string]interface{}{
  32. "err": err.Error(),
  33. })
  34. return
  35. }
  36. ctx.JSON(200, repo)
  37. }
  38. //CheckUnitUser chainload to models.CheckUnitUser
  39. func CheckUnitUser(ctx *macaron.Context) {
  40. repoID := ctx.ParamsInt64(":repoid")
  41. userID := ctx.ParamsInt64(":userid")
  42. repo, err := models.GetRepositoryByID(repoID)
  43. if err != nil {
  44. ctx.JSON(500, map[string]interface{}{
  45. "err": err.Error(),
  46. })
  47. return
  48. }
  49. var user *models.User
  50. if userID > 0 {
  51. user, err = models.GetUserByID(userID)
  52. if err != nil {
  53. ctx.JSON(500, map[string]interface{}{
  54. "err": err.Error(),
  55. })
  56. return
  57. }
  58. }
  59. perm, err := models.GetUserRepoPermission(repo, user)
  60. if err != nil {
  61. ctx.JSON(500, map[string]interface{}{
  62. "err": err.Error(),
  63. })
  64. return
  65. }
  66. ctx.JSON(200, perm.UnitAccessMode(models.UnitType(ctx.QueryInt("unitType"))))
  67. }
  68. // RegisterRoutes registers all internal APIs routes to web application.
  69. // These APIs will be invoked by internal commands for example `gitea serv` and etc.
  70. func RegisterRoutes(m *macaron.Macaron) {
  71. bind := binding.Bind
  72. m.Group("/", func() {
  73. m.Post("/ssh/authorized_keys", AuthorizedPublicKeyByContent)
  74. m.Post("/ssh/:id/update/:repoid", UpdatePublicKeyInRepo)
  75. m.Post("/hook/pre-receive/:owner/:repo", bind(private.HookOptions{}), HookPreReceive)
  76. m.Post("/hook/post-receive/:owner/:repo", bind(private.HookOptions{}), HookPostReceive)
  77. m.Post("/hook/set-default-branch/:owner/:repo/:branch", SetDefaultBranch)
  78. m.Get("/serv/none/:keyid", ServNoCommand)
  79. m.Get("/serv/command/:keyid/:owner/:repo", ServCommand)
  80. m.Post("/manager/shutdown", Shutdown)
  81. m.Post("/manager/restart", Restart)
  82. m.Post("/manager/flush-queues", bind(private.FlushOptions{}), FlushQueues)
  83. }, CheckInternalToken)
  84. }