middleware.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package server
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "github.com/gin-gonic/gin"
  7. "github.com/langgenius/dify-plugin-daemon/internal/server/constants"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  9. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  10. )
  11. func CheckingKey(key string) gin.HandlerFunc {
  12. return func(c *gin.Context) {
  13. // get header X-Api-Key
  14. if c.GetHeader(constants.X_API_KEY) != key {
  15. fmt.Println(c.GetHeader(constants.X_API_KEY))
  16. fmt.Println(key)
  17. c.JSON(401, gin.H{"error": "Unauthorized"})
  18. c.Abort()
  19. return
  20. }
  21. c.Next()
  22. }
  23. }
  24. type ginContextReader struct {
  25. reader *bytes.Reader
  26. }
  27. func (g *ginContextReader) Read(p []byte) (n int, err error) {
  28. return g.reader.Read(p)
  29. }
  30. func (g *ginContextReader) Close() error {
  31. return nil
  32. }
  33. // RedirectPluginInvoke redirects the request to the correct cluster node
  34. func (app *App) RedirectPluginInvoke() gin.HandlerFunc {
  35. return func(ctx *gin.Context) {
  36. // get plugin identity
  37. raw, err := ctx.GetRawData()
  38. if err != nil {
  39. ctx.AbortWithStatusJSON(400, gin.H{"error": "Invalid request"})
  40. return
  41. }
  42. ctx.Request.Body = &ginContextReader{
  43. reader: bytes.NewReader(raw),
  44. }
  45. identity, err := plugin_entities.NewPluginUniqueIdentifier(ctx.Request.Header.Get(constants.X_PLUGIN_IDENTIFIER))
  46. if err != nil {
  47. ctx.AbortWithStatusJSON(400, gin.H{"error": "Invalid request, " + err.Error()})
  48. return
  49. }
  50. // check if plugin in current node
  51. if !app.cluster.IsPluginNoCurrentNode(
  52. identity,
  53. ) {
  54. app.redirectPluginInvokeByPluginIdentifier(ctx, identity)
  55. ctx.Abort()
  56. } else {
  57. ctx.Next()
  58. }
  59. }
  60. }
  61. func (app *App) redirectPluginInvokeByPluginIdentifier(
  62. ctx *gin.Context,
  63. plugin_unique_identifier plugin_entities.PluginUniqueIdentifier,
  64. ) {
  65. // try find the correct node
  66. nodes, err := app.cluster.FetchPluginAvailableNodesById(plugin_unique_identifier.String())
  67. if err != nil {
  68. ctx.AbortWithStatusJSON(500, gin.H{"error": "Internal server error"})
  69. return
  70. } else if len(nodes) == 0 {
  71. ctx.AbortWithStatusJSON(404, gin.H{"error": "No available node"})
  72. return
  73. }
  74. // redirect to the correct node
  75. node_id := nodes[0]
  76. status_code, header, body, err := app.cluster.RedirectRequest(node_id, ctx.Request)
  77. if err != nil {
  78. log.Error("redirect request failed: %s", err.Error())
  79. ctx.AbortWithStatusJSON(500, gin.H{"error": "Internal server error"})
  80. return
  81. }
  82. // set status code
  83. ctx.Writer.WriteHeader(status_code)
  84. // set header
  85. for key, values := range header {
  86. for _, value := range values {
  87. ctx.Writer.Header().Set(key, value)
  88. }
  89. }
  90. for {
  91. buf := make([]byte, 1024)
  92. n, err := body.Read(buf)
  93. if err != nil && err != io.EOF {
  94. break
  95. } else if err != nil {
  96. ctx.Writer.Write(buf[:n])
  97. break
  98. }
  99. if n > 0 {
  100. ctx.Writer.Write(buf[:n])
  101. }
  102. }
  103. }
  104. func (app *App) InitClusterID() gin.HandlerFunc {
  105. return func(ctx *gin.Context) {
  106. ctx.Set("cluster_id", app.cluster.ID())
  107. ctx.Next()
  108. }
  109. }