Pass remote address in WebSocketMessageHasBeenPosted plugin hook (#27332)

Этот коммит содержится в:
Claudio Costa
2024-06-13 09:01:49 +02:00
коммит произвёл GitHub
родитель d2c3710265
Коммит 4b0ae20ef7
5 изменённых файлов: 110 добавлений и 15 удалений

Просмотреть файл

@@ -61,15 +61,17 @@ type pluginWSPostedHook struct {
}
type WebConnConfig struct {
WebSocket *websocket.Conn
Session model.Session
TFunc i18n.TranslateFunc
Locale string
ConnectionID string
Active bool
ReuseCount int
OriginClient string
PostedAck bool
WebSocket *websocket.Conn
Session model.Session
TFunc i18n.TranslateFunc
Locale string
ConnectionID string
Active bool
ReuseCount int
OriginClient string
PostedAck bool
RemoteAddress string
XForwardedFor string
// These aren't necessary to be exported to api layer.
sequence int
@@ -121,6 +123,10 @@ type WebConn struct {
// The client type behind the connection (i.e. web, desktop or mobile)
originClient string
// The remote address from the original HTTP Upgrade request
remoteAddress string
// The X-Forwarded-For HTTP header value from the origina HTTP Upgrade request
xForwardedFor string
activeChannelID atomic.Value
activeTeamID atomic.Value
@@ -245,6 +251,8 @@ func (ps *PlatformService) NewWebConn(cfg *WebConnConfig, suite SuiteIFace, runn
lastLogTimeSlow: time.Now(),
lastLogTimeFull: time.Now(),
originClient: cfg.OriginClient,
remoteAddress: cfg.RemoteAddress,
xForwardedFor: cfg.XForwardedFor,
}
wc.Active.Store(cfg.Active)
@@ -479,6 +487,12 @@ func (wc *WebConn) readPump() {
clonedReq.Session.Id = session.Id
}
if clonedReq.Data == nil {
clonedReq.Data = map[string]any{}
}
clonedReq.Data[model.WebSocketRemoteAddr] = wc.remoteAddress
clonedReq.Data[model.WebSocketXForwardedFor] = wc.xForwardedFor
wc.pluginPosted <- pluginWSPostedHook{wc.GetConnectionID(), wc.UserId, clonedReq}
}
}

Просмотреть файл

@@ -0,0 +1,28 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package main
import (
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin"
)
type Plugin struct {
plugin.MattermostPlugin
addrCh chan string
}
func (p *Plugin) MessageWillBePosted(_ *plugin.Context, _ *model.Post) (*model.Post, string) {
return nil, <-p.addrCh
}
func (p *Plugin) WebSocketMessageHasBeenPosted(connID, userID string, req *model.WebSocketRequest) {
p.addrCh <- req.Data[model.WebSocketRemoteAddr].(string)
}
func main() {
plugin.ClientMain(&Plugin{
addrCh: make(chan string, 1),
})
}