| 1 |
// Copyright (c) 2021 Sumner Evans |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package appservice |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"context" |
| 12 |
"encoding/json" |
| 13 |
"fmt" |
| 14 |
"io/ioutil" |
| 15 |
"net/http" |
| 16 |
"time" |
| 17 |
|
| 18 |
"maunium.net/go/mautrix" |
| 19 |
"maunium.net/go/mautrix/event" |
| 20 |
"maunium.net/go/mautrix/id" |
| 21 |
) |
| 22 |
|
| 23 |
type MessageSendCheckpointStep string |
| 24 |
|
| 25 |
const ( |
| 26 |
StepClient MessageSendCheckpointStep = "CLIENT" |
| 27 |
StepHomeserver MessageSendCheckpointStep = "HOMESERVER" |
| 28 |
StepBridge MessageSendCheckpointStep = "BRIDGE" |
| 29 |
StepDecrypted MessageSendCheckpointStep = "DECRYPTED" |
| 30 |
StepRemote MessageSendCheckpointStep = "REMOTE" |
| 31 |
StepCommand MessageSendCheckpointStep = "COMMAND" |
| 32 |
) |
| 33 |
|
| 34 |
type MessageSendCheckpointStatus string |
| 35 |
|
| 36 |
const ( |
| 37 |
StatusSuccesss MessageSendCheckpointStatus = "SUCCESS" |
| 38 |
StatusWillRetry MessageSendCheckpointStatus = "WILL_RETRY" |
| 39 |
StatusPermFailure MessageSendCheckpointStatus = "PERM_FAILURE" |
| 40 |
StatusUnsupported MessageSendCheckpointStatus = "UNSUPPORTED" |
| 41 |
StatusTimeout MessageSendCheckpointStatus = "TIMEOUT" |
| 42 |
) |
| 43 |
|
| 44 |
type MessageSendCheckpointReportedBy string |
| 45 |
|
| 46 |
const ( |
| 47 |
ReportedByAsmux MessageSendCheckpointReportedBy = "ASMUX" |
| 48 |
ReportedByBridge MessageSendCheckpointReportedBy = "BRIDGE" |
| 49 |
) |
| 50 |
|
| 51 |
type MessageSendCheckpoint struct { |
| 52 |
EventID id.EventID `json:"event_id"` |
| 53 |
RoomID id.RoomID `json:"room_id"` |
| 54 |
Step MessageSendCheckpointStep `json:"step"` |
| 55 |
Timestamp int64 `json:"timestamp"` |
| 56 |
Status MessageSendCheckpointStatus `json:"status"` |
| 57 |
EventType event.Type `json:"event_type"` |
| 58 |
ReportedBy MessageSendCheckpointReportedBy `json:"reported_by"` |
| 59 |
RetryNum int `json:"retry_num"` |
| 60 |
MessageType event.MessageType `json:"message_type,omitempty"` |
| 61 |
Info string `json:"info,omitempty"` |
| 62 |
} |
| 63 |
|
| 64 |
var CheckpointTypes = map[event.Type]struct{}{ |
| 65 |
event.EventRedaction: {}, |
| 66 |
event.EventMessage: {}, |
| 67 |
event.EventEncrypted: {}, |
| 68 |
event.EventSticker: {}, |
| 69 |
event.EventReaction: {}, |
| 70 |
event.CallInvite: {}, |
| 71 |
event.CallCandidates: {}, |
| 72 |
event.CallSelectAnswer: {}, |
| 73 |
event.CallAnswer: {}, |
| 74 |
event.CallHangup: {}, |
| 75 |
event.CallReject: {}, |
| 76 |
event.CallNegotiate: {}, |
| 77 |
} |
| 78 |
|
| 79 |
func NewMessageSendCheckpoint(evt *event.Event, step MessageSendCheckpointStep, status MessageSendCheckpointStatus, retryNum int) *MessageSendCheckpoint { |
| 80 |
checkpoint := MessageSendCheckpoint{ |
| 81 |
EventID: evt.ID, |
| 82 |
RoomID: evt.RoomID, |
| 83 |
Step: step, |
| 84 |
Timestamp: time.Now().UnixNano() / int64(time.Millisecond), |
| 85 |
Status: status, |
| 86 |
EventType: evt.Type, |
| 87 |
ReportedBy: ReportedByBridge, |
| 88 |
RetryNum: retryNum, |
| 89 |
} |
| 90 |
if evt.Type == event.EventMessage { |
| 91 |
checkpoint.MessageType = evt.Content.AsMessage().MsgType |
| 92 |
} |
| 93 |
return &checkpoint |
| 94 |
} |
| 95 |
|
| 96 |
func (as *AppService) SendMessageSendCheckpoint(evt *event.Event, step MessageSendCheckpointStep, retryNum int) { |
| 97 |
checkpoint := NewMessageSendCheckpoint(evt, step, StatusSuccesss, retryNum) |
| 98 |
go checkpoint.Send(as) |
| 99 |
} |
| 100 |
|
| 101 |
func (as *AppService) SendErrorMessageSendCheckpoint(evt *event.Event, step MessageSendCheckpointStep, err error, permanent bool, retryNum int) { |
| 102 |
status := StatusWillRetry |
| 103 |
if permanent { |
| 104 |
status = StatusPermFailure |
| 105 |
} |
| 106 |
checkpoint := NewMessageSendCheckpoint(evt, step, status, retryNum) |
| 107 |
checkpoint.Info = err.Error() |
| 108 |
go checkpoint.Send(as) |
| 109 |
} |
| 110 |
|
| 111 |
func (cp *MessageSendCheckpoint) Send(as *AppService) { |
| 112 |
err := SendCheckpoints(as, []*MessageSendCheckpoint{cp}) |
| 113 |
if err != nil { |
| 114 |
as.Log.Warnfln("Error sending checkpoint %s/%s for %s: %v", cp.Step, cp.Status, cp.EventID, err) |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
type CheckpointsJSON struct { |
| 119 |
Checkpoints []*MessageSendCheckpoint `json:"checkpoints"` |
| 120 |
} |
| 121 |
|
| 122 |
func SendCheckpoints(as *AppService, checkpoints []*MessageSendCheckpoint) error { |
| 123 |
checkpointsJSON := CheckpointsJSON{Checkpoints: checkpoints} |
| 124 |
|
| 125 |
if as.HasWebsocket() { |
| 126 |
return as.SendWebsocket(&WebsocketRequest{ |
| 127 |
Command: "message_checkpoint", |
| 128 |
Data: checkpointsJSON, |
| 129 |
}) |
| 130 |
} |
| 131 |
|
| 132 |
if as.MessageSendCheckpointEndpoint == "" { |
| 133 |
return nil |
| 134 |
} |
| 135 |
|
| 136 |
var body bytes.Buffer |
| 137 |
if err := json.NewEncoder(&body).Encode(checkpointsJSON); err != nil { |
| 138 |
return fmt.Errorf("failed to encode message send checkpoint JSON: %w", err) |
| 139 |
} |
| 140 |
|
| 141 |
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 142 |
defer cancel() |
| 143 |
req, err := http.NewRequestWithContext(ctx, http.MethodPost, as.MessageSendCheckpointEndpoint, &body) |
| 144 |
if err != nil { |
| 145 |
return err |
| 146 |
} |
| 147 |
|
| 148 |
req.Header.Set("Authorization", "Bearer "+as.Registration.AppToken) |
| 149 |
req.Header.Set("User-Agent", mautrix.DefaultUserAgent+" checkpoint sender") |
| 150 |
req.Header.Set("Content-Type", "application/json") |
| 151 |
|
| 152 |
resp, err := http.DefaultClient.Do(req) |
| 153 |
if err != nil { |
| 154 |
return fmt.Errorf("failed to send bridge state update: %w", err) |
| 155 |
} |
| 156 |
defer resp.Body.Close() |
| 157 |
if resp.StatusCode < 200 || resp.StatusCode > 299 { |
| 158 |
respBody, _ := ioutil.ReadAll(resp.Body) |
| 159 |
if respBody != nil { |
| 160 |
respBody = bytes.ReplaceAll(respBody, []byte("\n"), []byte("\\n")) |
| 161 |
} |
| 162 |
return fmt.Errorf("unexpected status code %d sending bridge state update: %s", resp.StatusCode, respBody) |
| 163 |
} |
| 164 |
return nil |
| 165 |
} |