relay.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package controller
  2. import (
  3. "bufio"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "io"
  7. "net/http"
  8. "one-api/common"
  9. "strings"
  10. )
  11. func Relay(c *gin.Context) {
  12. channelType := c.GetInt("channel")
  13. baseURL := common.ChannelBaseURLs[channelType]
  14. if channelType == common.ChannelTypeCustom {
  15. baseURL = c.GetString("base_url")
  16. }
  17. req, err := http.NewRequest(c.Request.Method, fmt.Sprintf("%s%s", baseURL, c.Request.URL.String()), c.Request.Body)
  18. if err != nil {
  19. c.JSON(http.StatusOK, gin.H{
  20. "error": gin.H{
  21. "message": err.Error(),
  22. "type": "one_api_error",
  23. },
  24. })
  25. return
  26. }
  27. //req.Header = c.Request.Header.Clone()
  28. // Fix HTTP Decompression failed
  29. // https://github.com/stoplightio/prism/issues/1064#issuecomment-824682360
  30. //req.Header.Del("Accept-Encoding")
  31. req.Header.Set("Authorization", c.Request.Header.Get("Authorization"))
  32. req.Header.Set("Content-Type", c.Request.Header.Get("Content-Type"))
  33. req.Header.Set("Accept", c.Request.Header.Get("Accept"))
  34. req.Header.Set("Connection", c.Request.Header.Get("Connection"))
  35. client := &http.Client{}
  36. resp, err := client.Do(req)
  37. if err != nil {
  38. c.JSON(http.StatusOK, gin.H{
  39. "error": gin.H{
  40. "message": err.Error(),
  41. "type": "one_api_error",
  42. },
  43. })
  44. return
  45. }
  46. defer resp.Body.Close()
  47. isStream := resp.Header.Get("Content-Type") == "text/event-stream"
  48. if isStream {
  49. scanner := bufio.NewScanner(resp.Body)
  50. scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
  51. if atEOF && len(data) == 0 {
  52. return 0, nil, nil
  53. }
  54. if i := strings.Index(string(data), "\n\n"); i >= 0 {
  55. return i + 2, data[0:i], nil
  56. }
  57. if atEOF {
  58. return len(data), data, nil
  59. }
  60. return 0, nil, nil
  61. })
  62. dataChan := make(chan string)
  63. stopChan := make(chan bool)
  64. go func() {
  65. for scanner.Scan() {
  66. data := scanner.Text()
  67. dataChan <- data
  68. }
  69. stopChan <- true
  70. }()
  71. c.Writer.Header().Set("Content-Type", "text/event-stream")
  72. c.Writer.Header().Set("Cache-Control", "no-cache")
  73. c.Writer.Header().Set("Connection", "keep-alive")
  74. c.Writer.Header().Set("Transfer-Encoding", "chunked")
  75. c.Stream(func(w io.Writer) bool {
  76. select {
  77. case data := <-dataChan:
  78. c.Render(-1, common.CustomEvent{Data: data})
  79. return true
  80. case <-stopChan:
  81. return false
  82. }
  83. })
  84. return
  85. } else {
  86. for k, v := range resp.Header {
  87. c.Writer.Header().Set(k, v[0])
  88. }
  89. _, err = io.Copy(c.Writer, resp.Body)
  90. if err != nil {
  91. c.JSON(http.StatusOK, gin.H{
  92. "error": gin.H{
  93. "message": err.Error(),
  94. "type": "one_api_error",
  95. },
  96. })
  97. return
  98. }
  99. }
  100. }