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 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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
  5. import (
  6. "context"
  7. "crypto/tls"
  8. "fmt"
  9. "net"
  10. "net/http"
  11. "code.gitea.io/gitea/modules/httplib"
  12. "code.gitea.io/gitea/modules/json"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. )
  16. func newRequest(ctx context.Context, url, method string) *httplib.Request {
  17. if setting.InternalToken == "" {
  18. log.Fatal(`The INTERNAL_TOKEN setting is missing from the configuration file: %q.
  19. Ensure you are running in the correct environment or set the correct configuration file with -c.`, setting.CustomConf)
  20. }
  21. return httplib.NewRequest(url, method).
  22. SetContext(ctx).
  23. Header("Authorization", fmt.Sprintf("Bearer %s", setting.InternalToken))
  24. }
  25. // Response internal request response
  26. type Response struct {
  27. Err string `json:"err"`
  28. }
  29. func decodeJSONError(resp *http.Response) *Response {
  30. var res Response
  31. err := json.NewDecoder(resp.Body).Decode(&res)
  32. if err != nil {
  33. res.Err = err.Error()
  34. }
  35. return &res
  36. }
  37. func newInternalRequest(ctx context.Context, url, method string) *httplib.Request {
  38. req := newRequest(ctx, url, method).SetTLSClientConfig(&tls.Config{
  39. InsecureSkipVerify: true,
  40. ServerName: setting.Domain,
  41. })
  42. if setting.Protocol == setting.UnixSocket {
  43. req.SetTransport(&http.Transport{
  44. DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
  45. var d net.Dialer
  46. return d.DialContext(ctx, "unix", setting.HTTPAddr)
  47. },
  48. })
  49. }
  50. return req
  51. }