-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
api.go
165 lines (156 loc) · 4.82 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// mautrix-syncproxy - A /sync proxy for encrypted Matrix appservices.
// Copyright (C) 2021 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/gorilla/mux"
log "maunium.net/go/maulogger/v2"
"maunium.net/go/mautrix/appservice"
)
var (
errTargetNotFound = appservice.Error{
HTTPStatus: http.StatusNotFound,
ErrorCode: "M_NOT_FOUND",
Message: "No appservice found with that ID",
}
errTargetNotActive = appservice.Error{
HTTPStatus: http.StatusNotFound,
ErrorCode: "FI.MAU.SYNCPROXY.NOT_ACTIVE",
Message: "That appservice is not active",
}
errUpsertFailed = appservice.Error{
HTTPStatus: http.StatusInternalServerError,
ErrorCode: "FI.MAU.SYNCPROXY.UPSERT_FAILED",
Message: "Failed to insert appservice details into database",
}
)
func startSync(w http.ResponseWriter, r *http.Request) {
if !checkAuth(w, r) {
return
}
vars := mux.Vars(r)
appserviceID := vars["appserviceID"]
switch r.Method {
case http.MethodPut:
var req SyncTarget
if !getJSON(w, r, &req) {
return
}
log.Debugfln("Received PUT request for appservice %s (user: %s, device: %s, address: %s, proxy: %t)", req.AppserviceID, req.UserID, req.DeviceID, req.Address, req.IsProxy)
req.AppserviceID = appserviceID
target := GetOrSetTarget(appserviceID, &req)
changed := true
if target == nil {
target = &req
err := target.Init()
if err != nil {
target.log.Warnln("Failed to initialize new target:", err)
appservice.Error{
HTTPStatus: http.StatusNotFound,
ErrorCode: "FI.MAU.SYNCPROXY.INVALID_ADDRESS",
Message: fmt.Sprintf("Failed to initialize target: %v", err),
}.Write(w)
return
}
} else if target.BotAccessToken != req.BotAccessToken || target.HSToken != req.HSToken ||
target.Address != req.Address || target.UserID != req.UserID || target.DeviceID != req.DeviceID {
target.BotAccessToken = req.BotAccessToken
target.HSToken = req.HSToken
target.Address = req.Address
target.UserID = req.UserID
target.DeviceID = req.DeviceID
if target.client != nil {
target.client.AccessToken = target.BotAccessToken
target.client.UserID = target.UserID
target.client.DeviceID = target.DeviceID
}
} else {
changed = false
}
if changed {
target.log.Debugln("Upserting target for PUT request")
err := target.Upsert()
if err != nil {
target.log.Warnln("Failed to upsert target:", err)
errUpsertFailed.Write(w)
return
}
}
target.log.Debugln("Starting target for PUT request")
go target.Start()
appservice.WriteBlankOK(w)
case http.MethodDelete:
target := GetOrSetTarget(appserviceID, nil)
if target == nil {
log.Debugln("Client requested stopping unknown appservice", appserviceID)
errTargetNotFound.Write(w)
return
} else if !target.Active {
log.Debugln("Client requested stopping inactive appservice", appserviceID)
errTargetNotActive.Write(w)
return
}
target.Stop()
target.log.Debugln("Waiting for syncing to stop")
target.wg.Wait()
target.log.Infoln("Target stopped after DELETE request")
w.WriteHeader(http.StatusNoContent)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func checkAuth(w http.ResponseWriter, r *http.Request) bool {
var token string
authHeader := r.Header.Get("Authorization")
if !strings.HasPrefix(authHeader, "Bearer ") {
token = r.URL.Query().Get("access_token")
} else {
token = authHeader[len("Bearer "):]
}
w.Header().Add("Content-Type", "application/json")
if len(token) == 0 {
appservice.Error{
HTTPStatus: http.StatusUnauthorized,
ErrorCode: "M_MISSING_TOKEN",
Message: "Missing authorization header",
}.Write(w)
return false
}
if token != cfg.SharedSecret {
appservice.Error{
HTTPStatus: http.StatusUnauthorized,
ErrorCode: "M_UNKNOWN_TOKEN",
Message: "Unknown authorization token",
}.Write(w)
return false
}
return true
}
func getJSON(w http.ResponseWriter, r *http.Request, into interface{}) bool {
err := json.NewDecoder(r.Body).Decode(&into)
if err != nil {
appservice.Error{
HTTPStatus: http.StatusBadRequest,
ErrorCode: "M_BAD_JSON",
Message: "Failed to decode request JSON",
}.Write(w)
return false
}
return true
}