forked from kelaresg/go-skype-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provisioning.go
345 lines (329 loc) · 10.5 KB
/
provisioning.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package main
import (
"context"
"encoding/json"
"github.com/gorilla/websocket"
log "maunium.net/go/maulogger/v2"
"net/http"
"maunium.net/go/mautrix/id"
)
type ProvisioningAPI struct {
bridge *Bridge
log log.Logger
}
func (prov *ProvisioningAPI) Init() {
prov.log = prov.bridge.Log.Sub("Provisioning")
prov.log.Debugln("Enabling provisioning API at", prov.bridge.Config.AppService.Provisioning.Prefix)
r := prov.bridge.AS.Router.PathPrefix(prov.bridge.Config.AppService.Provisioning.Prefix).Subrouter()
r.Use(prov.AuthMiddleware)
r.HandleFunc("/ping", prov.Ping).Methods(http.MethodGet)
r.HandleFunc("/login", prov.Login)
r.HandleFunc("/logout", prov.Logout).Methods(http.MethodPost)
r.HandleFunc("/delete_session", prov.DeleteSession).Methods(http.MethodPost)
r.HandleFunc("/delete_connection", prov.DeleteConnection).Methods(http.MethodPost)
r.HandleFunc("/disconnect", prov.Disconnect).Methods(http.MethodPost)
r.HandleFunc("/reconnect", prov.Reconnect).Methods(http.MethodPost)
}
func (prov *ProvisioningAPI) AuthMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
auth = auth[len("Bearer "):]
if auth != prov.bridge.Config.AppService.Provisioning.SharedSecret {
jsonResponse(w, http.StatusForbidden, map[string]interface{}{
"error": "Invalid auth token",
"errcode": "M_FORBIDDEN",
})
return
}
userID := r.URL.Query().Get("user_id")
user := prov.bridge.GetUserByMXID(id.UserID(userID))
h.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), "user", user)))
})
}
type Error struct {
Success bool `json:"success"`
Error string `json:"error"`
ErrCode string `json:"errcode"`
}
type Response struct {
Success bool `json:"success"`
Status string `json:"status"`
}
func (prov *ProvisioningAPI) DeleteSession(w http.ResponseWriter, r *http.Request) {
user := r.Context().Value("user").(*User)
if user.Session == nil && user.Conn == nil {
jsonResponse(w, http.StatusNotFound, Error{
Error: "Nothing to purge: no session information stored and no active connection.",
ErrCode: "no session",
})
return
}
user.SetSession(nil)
if user.Conn != nil {
//_, _ = user.Conn.Disconnect()
user.Conn.RemoveHandlers()
user.Conn = nil
}
jsonResponse(w, http.StatusOK, Response{true, "Session information purged"})
}
func (prov *ProvisioningAPI) DeleteConnection(w http.ResponseWriter, r *http.Request) {
//user := r.Context().Value("user").(*User)
//if user.Conn == nil {
// jsonResponse(w, http.StatusNotFound, Error{
// Error: "You don't have a WhatsApp connection.",
// ErrCode: "not connected",
// })
// return
//}
//sess, err := user.Conn.Disconnect()
//if err == nil && len(sess.Wid) > 0 {
// user.SetSession(&sess)
//}
//user.Conn.RemoveHandlers()
//user.Conn = nil
//jsonResponse(w, http.StatusOK, Response{true, "Disconnected from WhatsApp and connection deleted"})
}
func (prov *ProvisioningAPI) Disconnect(w http.ResponseWriter, r *http.Request) {
//user := r.Context().Value("user").(*User)
//if user.Conn == nil {
// jsonResponse(w, http.StatusNotFound, Error{
// Error: "You don't have a WhatsApp connection.",
// ErrCode: "no connection",
// })
// return
//}
//sess, err := user.Conn.Disconnect()
//if err == whatsapp.ErrNotConnected {
// jsonResponse(w, http.StatusNotFound, Error{
// Error: "You were not connected",
// ErrCode: "not connected",
// })
// return
//} else if err != nil {
// user.log.Warnln("Error while disconnecting:", err)
// jsonResponse(w, http.StatusInternalServerError, Error{
// Error: fmt.Sprintf("Unknown error while disconnecting: %v", err),
// ErrCode: err.Error(),
// })
// return
//} else if len(sess.Wid) > 0 {
// user.SetSession(&sess)
//}
//jsonResponse(w, http.StatusOK, Response{true, "Disconnected from WhatsApp"})
}
func (prov *ProvisioningAPI) Reconnect(w http.ResponseWriter, r *http.Request) {
//user := r.Context().Value("user").(*User)
//if user.Conn == nil {
// if user.Session == nil {
// jsonResponse(w, http.StatusForbidden, Error{
// Error: "No existing connection and no session. Please log in first.",
// ErrCode: "no session",
// })
// } else {
// user.Connect(false)
// jsonResponse(w, http.StatusOK, Response{true, "Created connection to WhatsApp."})
// }
// return
//}
//
//wasConnected := true
//sess, err := user.Conn.Disconnect()
//if err == whatsapp.ErrNotConnected {
// wasConnected = false
//} else if err != nil {
// user.log.Warnln("Error while disconnecting:", err)
//} else if len(sess.Wid) > 0 {
// user.SetSession(&sess)
//}
//
//err = user.Conn.Restore()
//if err == whatsapp.ErrInvalidSession {
// if user.Session != nil {
// user.log.Debugln("Got invalid session error when reconnecting, but user has session. Retrying using RestoreWithSession()...")
// var sess whatsapp.Session
// sess, err = user.Conn.RestoreWithSession(*user.Session)
// if err == nil {
// user.SetSession(&sess)
// }
// } else {
// jsonResponse(w, http.StatusForbidden, Error{
// Error: "You're not logged in",
// ErrCode: "not logged in",
// })
// return
// }
//} else if err == whatsapp.ErrLoginInProgress {
// jsonResponse(w, http.StatusConflict, Error{
// Error: "A login or reconnection is already in progress.",
// ErrCode: "login in progress",
// })
// return
//} else if err == whatsapp.ErrAlreadyLoggedIn {
// jsonResponse(w, http.StatusConflict, Error{
// Error: "You were already connected.",
// ErrCode: err.Error(),
// })
// return
//}
//if err != nil {
// user.log.Warnln("Error while reconnecting:", err)
// if err.Error() == "restore session connection timed out" {
// jsonResponse(w, http.StatusForbidden, Error{
// Error: "Reconnection timed out. Is WhatsApp on your phone reachable?",
// ErrCode: err.Error(),
// })
// } else {
// jsonResponse(w, http.StatusForbidden, Error{
// Error: fmt.Sprintf("Unknown error while reconnecting: %v", err),
// ErrCode: err.Error(),
// })
// }
// user.log.Debugln("Disconnecting due to failed session restore in reconnect command...")
// sess, err := user.Conn.Disconnect()
// if err != nil {
// user.log.Errorln("Failed to disconnect after failed session restore in reconnect command:", err)
// } else if len(sess.Wid) > 0 {
// user.SetSession(&sess)
// }
// return
//}
//user.ConnectionErrors = 0
//user.PostLogin()
//
//var msg string
//if wasConnected {
// msg = "Reconnected successfully."
//} else {
// msg = "Connected successfully."
//}
//
//jsonResponse(w, http.StatusOK, Response{true, msg})
}
func (prov *ProvisioningAPI) Ping(w http.ResponseWriter, r *http.Request) {
//user := r.Context().Value("user").(*User)
//wa := map[string]interface{}{
// "has_session": user.Session != nil,
// "management_room": user.ManagementRoom,
// "jid": user.JID,
// "conn": nil,
// "ping": nil,
//}
//if user.Conn != nil {
// wa["conn"] = map[string]interface{}{
// "is_connected": user.Conn.IsConnected(),
// "is_logged_in": user.Conn.IsLoggedIn(),
// "is_login_in_progress": user.Conn.IsLoginInProgress(),
// }
// ok, err := user.Conn.AdminTest()
// wa["ping"] = map[string]interface{}{
// "ok": ok,
// "err": err,
// }
//}
//resp := map[string]interface{}{
// "mxid": user.MXID,
// "admin": user.Admin,
// "whitelisted": user.Whitelisted,
// "relaybot_whitelisted": user.RelaybotWhitelisted,
// "whatsapp": wa,
//}
//jsonResponse(w, http.StatusOK, resp)
}
func jsonResponse(w http.ResponseWriter, status int, response interface{}) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(response)
}
func (prov *ProvisioningAPI) Logout(w http.ResponseWriter, r *http.Request) {
//user := r.Context().Value("user").(*User)
//if user.Session == nil {
// jsonResponse(w, http.StatusNotFound, Error{
// Error: "You're not logged in",
// ErrCode: "not logged in",
// })
// return
//}
//
//err := user.Conn.Logout()
//if err != nil {
// user.log.Warnln("Error while logging out:", err)
// jsonResponse(w, http.StatusInternalServerError, Error{
// Error: fmt.Sprintf("Unknown error while logging out: %v", err),
// ErrCode: err.Error(),
// })
// return
//}
//_, err = user.Conn.Disconnect()
//if err != nil {
// user.log.Warnln("Error while disconnecting after logout:", err)
//}
//user.Conn.RemoveHandlers()
//user.Conn = nil
//user.removeFromJIDMap()
//// TODO this causes a foreign key violation, which should be fixed
////ce.User.JID = ""
//user.SetSession(nil)
//jsonResponse(w, http.StatusOK, Response{true, "Logged out successfully."})
}
var upgrader = websocket.Upgrader{}
func (prov *ProvisioningAPI) Login(w http.ResponseWriter, r *http.Request) {
//userID := r.URL.Query().Get("user_id")
//user := prov.bridge.GetUserByMXID(id.UserID(userID))
//
//c, err := upgrader.Upgrade(w, r, nil)
//if err != nil {
// prov.log.Errorfln("Failed to upgrade connection to websocket:", err)
// return
//}
//defer c.Close()
//
//if !user.Connect(true) {
// user.log.Debugln("Connect() returned false, assuming error was logged elsewhere and canceling login.")
// _ = c.WriteJSON(Error{
// Error: "Failed to connect to WhatsApp",
// ErrCode: "connection error",
// })
// return
//}
//
//qrChan := make(chan string, 3)
//go func() {
// for code := range qrChan {
// if code == "stop" {
// return
// }
// _ = c.WriteJSON(map[string]interface{}{
// "code": code,
// })
// }
//}()
//session, err := user.Conn.LoginWithRetry(qrChan, user.bridge.Config.Bridge.LoginQRRegenCount)
//qrChan <- "stop"
//if err != nil {
// var msg string
// if err == whatsapp.ErrAlreadyLoggedIn {
// msg = "You're already logged in"
// } else if err == whatsapp.ErrLoginInProgress {
// msg = "You have a login in progress already."
// } else if err == whatsapp.ErrLoginTimedOut {
// msg = "QR code scan timed out. Please try again."
// } else {
// user.log.Warnln("Failed to log in:", err)
// msg = fmt.Sprintf("Unknown error while logging in: %v", err)
// }
// _ = c.WriteJSON(Error{
// Error: msg,
// ErrCode: err.Error(),
// })
// return
//}
//user.ConnectionErrors = 0
//user.JID = strings.Replace(user.Conn.Info.Wid, whatsappExt.OldUserSuffix, whatsappExt.NewUserSuffix, 1)
//user.addToJIDMap()
//user.SetSession(&session)
//_ = c.WriteJSON(map[string]interface{}{
// "success": true,
// "jid": user.JID,
//})
//user.PostLogin()
}