mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
feat: deploy config to remote nodes #359
This commit is contained in:
parent
e75dce92ad
commit
1c1da92363
46 changed files with 1480 additions and 605 deletions
56
internal/middleware/proxy_ws.go
Normal file
56
internal/middleware/proxy_ws.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/internal/logger"
|
||||
"github.com/0xJacky/Nginx-UI/query"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pretty66/websocketproxy"
|
||||
"github.com/spf13/cast"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func ProxyWs() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
nodeID, ok := c.Get("ProxyNodeID")
|
||||
if !ok {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
id := cast.ToInt(nodeID)
|
||||
if id == 0 {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
defer c.Abort()
|
||||
|
||||
env := query.Environment
|
||||
environment, err := env.Where(env.ID.Eq(id)).First()
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
decodedUri, err := environment.GetWebSocketURL(c.Request.RequestURI)
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
logger.Debug("Proxy request", decodedUri)
|
||||
|
||||
wp, err := websocketproxy.NewProxy(decodedUri, func(r *http.Request) error {
|
||||
r.Header.Set("X-Node-Secret", environment.Token)
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
wp.Proxy(c.Writer, c.Request)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue