You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

internal.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package private
  4. import (
  5. "context"
  6. "crypto/tls"
  7. "fmt"
  8. "net"
  9. "net/http"
  10. "os"
  11. "strings"
  12. "time"
  13. "code.gitea.io/gitea/modules/httplib"
  14. "code.gitea.io/gitea/modules/json"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/proxyprotocol"
  17. "code.gitea.io/gitea/modules/setting"
  18. )
  19. // Response is used for internal request response (for user message and error message)
  20. type Response struct {
  21. Err string `json:"err,omitempty"` // server-side error log message, it won't be exposed to end users
  22. UserMsg string `json:"user_msg,omitempty"` // meaningful error message for end users, it will be shown in git client's output.
  23. }
  24. func getClientIP() string {
  25. sshConnEnv := strings.TrimSpace(os.Getenv("SSH_CONNECTION"))
  26. if len(sshConnEnv) == 0 {
  27. return "127.0.0.1"
  28. }
  29. return strings.Fields(sshConnEnv)[0]
  30. }
  31. func newInternalRequest(ctx context.Context, url, method string, body ...any) *httplib.Request {
  32. if setting.InternalToken == "" {
  33. log.Fatal(`The INTERNAL_TOKEN setting is missing from the configuration file: %q.
  34. Ensure you are running in the correct environment or set the correct configuration file with -c.`, setting.CustomConf)
  35. }
  36. req := httplib.NewRequest(url, method).
  37. SetContext(ctx).
  38. Header("X-Real-IP", getClientIP()).
  39. Header("Authorization", fmt.Sprintf("Bearer %s", setting.InternalToken)).
  40. SetTLSClientConfig(&tls.Config{
  41. InsecureSkipVerify: true,
  42. ServerName: setting.Domain,
  43. })
  44. if setting.Protocol == setting.HTTPUnix {
  45. req.SetTransport(&http.Transport{
  46. DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
  47. var d net.Dialer
  48. conn, err := d.DialContext(ctx, "unix", setting.HTTPAddr)
  49. if err != nil {
  50. return conn, err
  51. }
  52. if setting.LocalUseProxyProtocol {
  53. if err = proxyprotocol.WriteLocalHeader(conn); err != nil {
  54. _ = conn.Close()
  55. return nil, err
  56. }
  57. }
  58. return conn, err
  59. },
  60. })
  61. } else if setting.LocalUseProxyProtocol {
  62. req.SetTransport(&http.Transport{
  63. DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
  64. var d net.Dialer
  65. conn, err := d.DialContext(ctx, network, address)
  66. if err != nil {
  67. return conn, err
  68. }
  69. if err = proxyprotocol.WriteLocalHeader(conn); err != nil {
  70. _ = conn.Close()
  71. return nil, err
  72. }
  73. return conn, err
  74. },
  75. })
  76. }
  77. if len(body) == 1 {
  78. req.Header("Content-Type", "application/json")
  79. jsonBytes, _ := json.Marshal(body[0])
  80. req.Body(jsonBytes)
  81. } else if len(body) > 1 {
  82. log.Fatal("Too many arguments for newInternalRequest")
  83. }
  84. req.SetTimeout(10*time.Second, 60*time.Second)
  85. return req
  86. }