diff --git a/appservice/http.go b/appservice/http.go index 85b2893a..b280b299 100644 --- a/appservice/http.go +++ b/appservice/http.go @@ -295,6 +295,19 @@ func (as *AppService) PostPing(w http.ResponseWriter, r *http.Request) { if !as.CheckServerToken(w, r) { return } + body, err := io.ReadAll(r.Body) + if err != nil || len(body) == 0 || !json.Valid(body) { + Error{ + ErrorCode: ErrNotJSON, + HTTPStatus: http.StatusBadRequest, + Message: "Missing request body", + }.Write(w) + return + } + + var txn mautrix.ReqAppservicePing + _ = json.Unmarshal(body, &txn) + as.Log.Debug().Str("txn_id", txn.TxnID).Msg("Received ping from homeserver") w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusOK) diff --git a/bridge/bridge.go b/bridge/bridge.go index 63f12e90..c9917e89 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -302,9 +302,10 @@ func (br *Bridge) ensureConnection() { } if br.SpecVersions.UnstableFeatures["fi.mau.msc2659"] && br.AS.Host.Port != 0 { - resp, err := br.Bot.AppservicePing() + txnID := br.Bot.TxnID() + resp, err := br.Bot.AppservicePing(br.Config.AppService.ID, txnID) if err != nil { - evt := br.ZLog.WithLevel(zerolog.FatalLevel).Err(err) + evt := br.ZLog.WithLevel(zerolog.FatalLevel).Err(err).Str("txn_id", txnID) var httpErr mautrix.HTTPError if errors.As(err, &httpErr) && httpErr.RespError != nil { if val, ok := httpErr.RespError.ExtraData["body"].(string); ok { @@ -320,7 +321,10 @@ func (br *Bridge) ensureConnection() { evt.Msg("Homeserver -> bridge connection is not working") os.Exit(13) } - br.ZLog.Debug().Int64("duration_ms", resp.DurationMS).Msg("Homeserver -> bridge connection works") + br.ZLog.Debug(). + Str("txn_id", txnID). + Int64("duration_ms", resp.DurationMS). + Msg("Homeserver -> bridge connection works") } else { br.ZLog.Debug().Msg("Homeserver does not support checking status of homeserver -> bridge connection") } diff --git a/client.go b/client.go index ed94b802..f6e8b2d1 100644 --- a/client.go +++ b/client.go @@ -1963,10 +1963,11 @@ func (cli *Client) BatchSend(roomID id.RoomID, req *ReqBatchSend) (resp *RespBat return } -func (cli *Client) AppservicePing() (resp *RespAppservicePing, err error) { +func (cli *Client) AppservicePing(id, txnID string) (resp *RespAppservicePing, err error) { _, err = cli.MakeFullRequest(FullRequest{ Method: http.MethodPost, - URL: cli.BuildClientURL("unstable", "fi.mau.msc2659", "appservice", "ping"), + URL: cli.BuildClientURL("unstable", "fi.mau.msc2659", "appservice", id, "ping"), + RequestJSON: &ReqAppservicePing{TxnID: txnID}, ResponseJSON: &resp, // This endpoint intentionally returns 50x, so don't retry MaxAttempts: 1, diff --git a/requests.go b/requests.go index dd8e1b66..8c7571de 100644 --- a/requests.go +++ b/requests.go @@ -392,6 +392,10 @@ func (req *ReqHierarchy) Query() map[string]string { return query } +type ReqAppservicePing struct { + TxnID string `json:"transaction_id,omitempty"` +} + type ReqBeeperMergeRoom struct { NewRoom ReqCreateRoom `json:"create"` Key string `json:"key"`