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.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "code.gitea.io/gitea/modules/httplib"
  13. "code.gitea.io/gitea/modules/json"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/proxyprotocol"
  16. "code.gitea.io/gitea/modules/setting"
  17. )
  18. func newRequest(ctx context.Context, url, method, sourceIP string) *httplib.Request {
  19. if setting.InternalToken == "" {
  20. log.Fatal(`The INTERNAL_TOKEN setting is missing from the configuration file: %q.
  21. Ensure you are running in the correct environment or set the correct configuration file with -c.`, setting.CustomConf)
  22. }
  23. return httplib.NewRequest(url, method).
  24. SetContext(ctx).
  25. Header("X-Real-IP", sourceIP).
  26. Header("Authorization", fmt.Sprintf("Bearer %s", setting.InternalToken))
  27. }
  28. // Response internal request response
  29. type Response struct {
  30. Err string `json:"err"`
  31. }
  32. func decodeJSONError(resp *http.Response) *Response {
  33. var res Response
  34. err := json.NewDecoder(resp.Body).Decode(&res)
  35. if err != nil {
  36. res.Err = err.Error()
  37. }
  38. return &res
  39. }
  40. func getClientIP() string {
  41. sshConnEnv := strings.TrimSpace(os.Getenv("SSH_CONNECTION"))
  42. if len(sshConnEnv) == 0 {
  43. return "127.0.0.1"
  44. }
  45. return strings.Fields(sshConnEnv)[0]
  46. }
  47. func newInternalRequest(ctx context.Context, url, method string) *httplib.Request {
  48. req := newRequest(ctx, url, method, getClientIP()).SetTLSClientConfig(&tls.Config{
  49. InsecureSkipVerify: true,
  50. ServerName: setting.Domain,
  51. })
  52. if setting.Protocol == setting.HTTPUnix {
  53. req.SetTransport(&http.Transport{
  54. DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
  55. var d net.Dialer
  56. conn, err := d.DialContext(ctx, "unix", setting.HTTPAddr)
  57. if err != nil {
  58. return conn, err
  59. }
  60. if setting.LocalUseProxyProtocol {
  61. if err = proxyprotocol.WriteLocalHeader(conn); err != nil {
  62. _ = conn.Close()
  63. return nil, err
  64. }
  65. }
  66. return conn, err
  67. },
  68. })
  69. } else if setting.LocalUseProxyProtocol {
  70. req.SetTransport(&http.Transport{
  71. DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
  72. var d net.Dialer
  73. conn, err := d.DialContext(ctx, network, address)
  74. if err != nil {
  75. return conn, err
  76. }
  77. if err = proxyprotocol.WriteLocalHeader(conn); err != nil {
  78. _ = conn.Close()
  79. return nil, err
  80. }
  81. return conn, err
  82. },
  83. })
  84. }
  85. return req
  86. }