2022-05-22 19:16:42 +00:00
|
|
|
package main
|
2022-02-21 12:49:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-04-14 17:03:44 +00:00
|
|
|
"github.com/gorilla/mux"
|
2022-02-21 12:49:44 +00:00
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
log "maunium.net/go/maulogger/v2"
|
|
|
|
|
2023-01-13 12:53:45 +00:00
|
|
|
"maunium.net/go/mautrix"
|
2023-01-13 12:22:56 +00:00
|
|
|
"maunium.net/go/mautrix/bridge/bridgeconfig"
|
2022-02-21 12:49:44 +00:00
|
|
|
"maunium.net/go/mautrix/id"
|
|
|
|
|
2023-02-18 20:53:51 +00:00
|
|
|
"go.mau.fi/mautrix-discord/database"
|
2022-05-20 13:37:41 +00:00
|
|
|
"go.mau.fi/mautrix-discord/remoteauth"
|
2022-02-21 12:49:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
SecWebSocketProtocol = "com.gitlab.beeper.discord"
|
|
|
|
)
|
|
|
|
|
2023-01-13 12:53:45 +00:00
|
|
|
const (
|
|
|
|
ErrCodeNotConnected = "FI.MAU.DISCORD.NOT_CONNECTED"
|
|
|
|
ErrCodeAlreadyLoggedIn = "FI.MAU.DISCORD.ALREADY_LOGGED_IN"
|
|
|
|
ErrCodeAlreadyConnected = "FI.MAU.DISCORD.ALREADY_CONNECTED"
|
|
|
|
ErrCodeConnectFailed = "FI.MAU.DISCORD.CONNECT_FAILED"
|
|
|
|
ErrCodeDisconnectFailed = "FI.MAU.DISCORD.DISCONNECT_FAILED"
|
|
|
|
ErrCodeGuildBridgeFailed = "M_UNKNOWN"
|
|
|
|
ErrCodeGuildUnbridgeFailed = "M_UNKNOWN"
|
|
|
|
ErrCodeGuildNotBridged = "FI.MAU.DISCORD.GUILD_NOT_BRIDGED"
|
|
|
|
ErrCodeLoginPrepareFailed = "FI.MAU.DISCORD.LOGIN_PREPARE_FAILED"
|
|
|
|
ErrCodeLoginConnectionFailed = "FI.MAU.DISCORD.LOGIN_CONN_FAILED"
|
|
|
|
ErrCodeLoginFailed = "FI.MAU.DISCORD.LOGIN_FAILED"
|
|
|
|
ErrCodePostLoginConnFailed = "FI.MAU.DISCORD.POST_LOGIN_CONNECTION_FAILED"
|
|
|
|
)
|
|
|
|
|
2022-02-21 12:49:44 +00:00
|
|
|
type ProvisioningAPI struct {
|
2022-05-22 19:16:42 +00:00
|
|
|
bridge *DiscordBridge
|
2022-02-21 12:49:44 +00:00
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
2022-05-22 19:16:42 +00:00
|
|
|
func newProvisioningAPI(br *DiscordBridge) *ProvisioningAPI {
|
2022-02-21 12:49:44 +00:00
|
|
|
p := &ProvisioningAPI{
|
2022-05-22 19:16:42 +00:00
|
|
|
bridge: br,
|
|
|
|
log: br.Log.Sub("Provisioning"),
|
2022-02-21 12:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 19:16:42 +00:00
|
|
|
prefix := br.Config.Bridge.Provisioning.Prefix
|
2022-02-21 12:49:44 +00:00
|
|
|
|
|
|
|
p.log.Debugln("Enabling provisioning API at", prefix)
|
|
|
|
|
2022-05-22 19:16:42 +00:00
|
|
|
r := br.AS.Router.PathPrefix(prefix).Subrouter()
|
2022-02-21 12:49:44 +00:00
|
|
|
|
|
|
|
r.Use(p.authMiddleware)
|
|
|
|
|
2023-01-13 12:10:17 +00:00
|
|
|
r.HandleFunc("/v1/disconnect", p.disconnect).Methods(http.MethodPost)
|
|
|
|
r.HandleFunc("/v1/ping", p.ping).Methods(http.MethodGet)
|
|
|
|
r.HandleFunc("/v1/login/qr", p.qrLogin).Methods(http.MethodGet)
|
|
|
|
r.HandleFunc("/v1/login/token", p.tokenLogin).Methods(http.MethodPost)
|
|
|
|
r.HandleFunc("/v1/logout", p.logout).Methods(http.MethodPost)
|
|
|
|
r.HandleFunc("/v1/reconnect", p.reconnect).Methods(http.MethodPost)
|
|
|
|
|
|
|
|
r.HandleFunc("/v1/guilds", p.guildsList).Methods(http.MethodGet)
|
2023-01-13 12:22:56 +00:00
|
|
|
r.HandleFunc("/v1/guilds/{guildID}", p.guildsBridge).Methods(http.MethodPost)
|
|
|
|
r.HandleFunc("/v1/guilds/{guildID}", p.guildsUnbridge).Methods(http.MethodDelete)
|
2022-04-14 17:03:44 +00:00
|
|
|
|
2022-02-21 12:49:44 +00:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonResponse(w http.ResponseWriter, status int, response interface{}) {
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(status)
|
2023-01-13 12:03:08 +00:00
|
|
|
_ = json.NewEncoder(w).Encode(response)
|
2022-02-21 12:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Response structs
|
|
|
|
type Response struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Error struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
ErrCode string `json:"errcode"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapped http.ResponseWriter to capture the status code
|
|
|
|
type responseWrap struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
statusCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ http.Hijacker = (*responseWrap)(nil)
|
|
|
|
|
|
|
|
func (rw *responseWrap) WriteHeader(statusCode int) {
|
|
|
|
rw.ResponseWriter.WriteHeader(statusCode)
|
|
|
|
rw.statusCode = statusCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *responseWrap) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
hijacker, ok := rw.ResponseWriter.(http.Hijacker)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, errors.New("response does not implement http.Hijacker")
|
|
|
|
}
|
|
|
|
return hijacker.Hijack()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Middleware
|
|
|
|
func (p *ProvisioningAPI) authMiddleware(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
auth := r.Header.Get("Authorization")
|
|
|
|
|
|
|
|
// Special case the login endpoint to use the discord qrcode auth
|
|
|
|
if auth == "" && strings.HasSuffix(r.URL.Path, "/login") {
|
|
|
|
authParts := strings.Split(r.Header.Get("Sec-WebSocket-Protocol"), ",")
|
|
|
|
for _, part := range authParts {
|
|
|
|
part = strings.TrimSpace(part)
|
|
|
|
if strings.HasPrefix(part, SecWebSocketProtocol+"-") {
|
|
|
|
auth = part[len(SecWebSocketProtocol+"-"):]
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if strings.HasPrefix(auth, "Bearer ") {
|
|
|
|
auth = auth[len("Bearer "):]
|
|
|
|
}
|
|
|
|
|
2022-05-22 19:16:42 +00:00
|
|
|
if auth != p.bridge.Config.Bridge.Provisioning.SharedSecret {
|
2023-01-13 12:53:45 +00:00
|
|
|
jsonResponse(w, http.StatusUnauthorized, map[string]interface{}{
|
2022-02-21 12:49:44 +00:00
|
|
|
"error": "Invalid auth token",
|
2023-01-13 12:53:45 +00:00
|
|
|
"errcode": mautrix.MUnknownToken.ErrCode,
|
2022-02-21 12:49:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
userID := r.URL.Query().Get("user_id")
|
|
|
|
user := p.bridge.GetUserByMXID(id.UserID(userID))
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
wWrap := &responseWrap{w, 200}
|
|
|
|
h.ServeHTTP(wWrap, r.WithContext(context.WithValue(r.Context(), "user", user)))
|
|
|
|
duration := time.Now().Sub(start).Seconds()
|
|
|
|
|
|
|
|
p.log.Infofln("%s %s from %s took %.2f seconds and returned status %d", r.Method, r.URL.Path, user.MXID, duration, wWrap.statusCode)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// websocket upgrader
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
Subprotocols: []string{SecWebSocketProtocol},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handlers
|
2022-02-22 13:56:15 +00:00
|
|
|
func (p *ProvisioningAPI) disconnect(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user := r.Context().Value("user").(*User)
|
|
|
|
|
|
|
|
if !user.Connected() {
|
|
|
|
jsonResponse(w, http.StatusConflict, Error{
|
|
|
|
Error: "You're not connected to discord",
|
2023-01-13 12:53:45 +00:00
|
|
|
ErrCode: ErrCodeNotConnected,
|
2022-02-22 13:56:15 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := user.Disconnect(); err != nil {
|
2023-01-13 12:53:45 +00:00
|
|
|
p.log.Errorfln("Failed to disconnect %s: %v", user.MXID, err)
|
2022-02-22 13:56:15 +00:00
|
|
|
jsonResponse(w, http.StatusInternalServerError, Error{
|
|
|
|
Error: "Failed to disconnect from discord",
|
2023-01-13 12:53:45 +00:00
|
|
|
ErrCode: ErrCodeDisconnectFailed,
|
2022-02-22 13:56:15 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
jsonResponse(w, http.StatusOK, Response{
|
|
|
|
Success: true,
|
|
|
|
Status: "Disconnected from Discord",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 12:40:21 +00:00
|
|
|
type respPing struct {
|
|
|
|
Discord struct {
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
LoggedIn bool `json:"logged_in"`
|
|
|
|
Connected bool `json:"connected"`
|
|
|
|
Conn struct {
|
|
|
|
LastHeartbeatAck int64 `json:"last_heartbeat_ack,omitempty"`
|
|
|
|
LastHeartbeatSent int64 `json:"last_heartbeat_sent,omitempty"`
|
|
|
|
} `json:"conn"`
|
|
|
|
}
|
|
|
|
MXID id.UserID `json:"mxid"`
|
|
|
|
ManagementRoom id.RoomID `json:"management_room"`
|
|
|
|
}
|
|
|
|
|
2022-02-22 13:56:15 +00:00
|
|
|
func (p *ProvisioningAPI) ping(w http.ResponseWriter, r *http.Request) {
|
2022-02-21 12:49:44 +00:00
|
|
|
user := r.Context().Value("user").(*User)
|
|
|
|
|
2023-01-13 12:40:21 +00:00
|
|
|
resp := respPing{
|
|
|
|
MXID: user.MXID,
|
|
|
|
ManagementRoom: user.ManagementRoom,
|
2022-02-21 12:49:44 +00:00
|
|
|
}
|
2023-01-13 12:40:21 +00:00
|
|
|
resp.Discord.LoggedIn = user.IsLoggedIn()
|
|
|
|
resp.Discord.Connected = user.Connected()
|
|
|
|
resp.Discord.ID = user.DiscordID
|
2022-02-21 12:49:44 +00:00
|
|
|
if user.Session != nil {
|
2023-01-13 12:40:21 +00:00
|
|
|
resp.Discord.Conn.LastHeartbeatAck = user.Session.LastHeartbeatAck.UnixMilli()
|
|
|
|
resp.Discord.Conn.LastHeartbeatSent = user.Session.LastHeartbeatSent.UnixMilli()
|
2022-02-21 12:49:44 +00:00
|
|
|
}
|
|
|
|
jsonResponse(w, http.StatusOK, resp)
|
|
|
|
}
|
|
|
|
|
2022-02-22 13:56:15 +00:00
|
|
|
func (p *ProvisioningAPI) logout(w http.ResponseWriter, r *http.Request) {
|
2022-02-21 12:49:44 +00:00
|
|
|
user := r.Context().Value("user").(*User)
|
2022-11-26 13:18:42 +00:00
|
|
|
var msg string
|
|
|
|
if user.DiscordID != "" {
|
|
|
|
msg = "Logged out successfully."
|
|
|
|
} else {
|
|
|
|
msg = "User wasn't logged in."
|
2022-02-21 12:49:44 +00:00
|
|
|
}
|
2022-11-26 13:18:42 +00:00
|
|
|
user.Logout()
|
|
|
|
jsonResponse(w, http.StatusOK, Response{true, msg})
|
2022-02-21 12:49:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 12:03:08 +00:00
|
|
|
func (p *ProvisioningAPI) qrLogin(w http.ResponseWriter, r *http.Request) {
|
2022-02-21 12:49:44 +00:00
|
|
|
userID := r.URL.Query().Get("user_id")
|
|
|
|
user := p.bridge.GetUserByMXID(id.UserID(userID))
|
|
|
|
|
|
|
|
c, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
p.log.Errorln("Failed to upgrade connection to websocket:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-13 12:03:08 +00:00
|
|
|
log := p.log.Sub("QRLogin").Sub(user.MXID.String())
|
|
|
|
|
2022-02-21 12:49:44 +00:00
|
|
|
defer func() {
|
|
|
|
err := c.Close()
|
|
|
|
if err != nil {
|
2023-01-13 12:03:08 +00:00
|
|
|
log.Debugln("Error closing websocket:", err)
|
2022-02-21 12:49:44 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Read everything so SetCloseHandler() works
|
|
|
|
for {
|
2022-02-22 13:56:15 +00:00
|
|
|
_, _, err := c.ReadMessage()
|
2022-02-21 12:49:44 +00:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
c.SetCloseHandler(func(code int, text string) error {
|
2023-01-13 12:03:08 +00:00
|
|
|
log.Debugfln("Login websocket closed (%d), cancelling login", code)
|
2022-02-21 12:49:44 +00:00
|
|
|
cancel()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-05-22 19:16:42 +00:00
|
|
|
if user.IsLoggedIn() {
|
2023-01-13 12:03:08 +00:00
|
|
|
_ = c.WriteJSON(Error{
|
2022-02-22 13:56:15 +00:00
|
|
|
Error: "You're already logged into Discord",
|
2023-01-13 12:53:45 +00:00
|
|
|
ErrCode: ErrCodeAlreadyLoggedIn,
|
2022-02-22 13:56:15 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:49:44 +00:00
|
|
|
client, err := remoteauth.New()
|
|
|
|
if err != nil {
|
2023-01-13 12:03:08 +00:00
|
|
|
log.Errorln("Failed to prepare login:", err)
|
|
|
|
_ = c.WriteJSON(Error{
|
|
|
|
Error: "Failed to prepare login",
|
2023-01-13 12:53:45 +00:00
|
|
|
ErrCode: ErrCodeLoginPrepareFailed,
|
2022-02-21 12:49:44 +00:00
|
|
|
})
|
2023-01-13 12:03:08 +00:00
|
|
|
return
|
2022-02-21 12:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qrChan := make(chan string)
|
|
|
|
doneChan := make(chan struct{})
|
|
|
|
|
2023-01-13 12:03:08 +00:00
|
|
|
log.Debugln("Started login via provisioning API")
|
2022-02-21 12:49:44 +00:00
|
|
|
|
|
|
|
err = client.Dial(ctx, qrChan, doneChan)
|
|
|
|
if err != nil {
|
2023-01-13 12:03:08 +00:00
|
|
|
log.Errorln("Failed to connect to Discord login websocket:", err)
|
2022-02-21 12:49:44 +00:00
|
|
|
close(qrChan)
|
|
|
|
close(doneChan)
|
2023-01-13 12:03:08 +00:00
|
|
|
_ = c.WriteJSON(Error{
|
2023-01-13 12:53:45 +00:00
|
|
|
Error: "Failed to connect to Discord login websocket",
|
|
|
|
ErrCode: ErrCodeLoginConnectionFailed,
|
2023-01-13 12:03:08 +00:00
|
|
|
})
|
|
|
|
return
|
2022-02-21 12:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case qrCode, ok := <-qrChan:
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2023-01-13 12:03:08 +00:00
|
|
|
err = c.WriteJSON(map[string]interface{}{
|
2022-02-21 12:49:44 +00:00
|
|
|
"code": qrCode,
|
|
|
|
"timeout": 120, // TODO: move this to the library or something
|
|
|
|
})
|
2023-01-13 12:03:08 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Failed to write QR code to websocket:", err)
|
|
|
|
}
|
2022-02-21 12:49:44 +00:00
|
|
|
case <-doneChan:
|
2023-01-13 12:03:08 +00:00
|
|
|
var discordUser remoteauth.User
|
|
|
|
discordUser, err = client.Result()
|
2022-02-21 12:49:44 +00:00
|
|
|
if err != nil {
|
2023-01-13 12:03:08 +00:00
|
|
|
log.Errorln("Discord login websocket returned error:", err)
|
|
|
|
_ = c.WriteJSON(Error{
|
|
|
|
Error: "Failed to log in",
|
2023-01-13 12:53:45 +00:00
|
|
|
ErrCode: ErrCodeLoginFailed,
|
2022-02-21 12:49:44 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-13 12:03:08 +00:00
|
|
|
log.Infofln("Logged in as %s#%s (%s)", discordUser.Username, discordUser.Discriminator, discordUser.UserID)
|
2022-05-28 20:03:24 +00:00
|
|
|
user.DiscordID = discordUser.UserID
|
2022-02-22 13:56:15 +00:00
|
|
|
user.Update()
|
|
|
|
|
2023-01-13 12:03:08 +00:00
|
|
|
if err = user.Login(discordUser.Token); err != nil {
|
|
|
|
log.Errorln("Failed to connect after logging in:", err)
|
|
|
|
_ = c.WriteJSON(Error{
|
|
|
|
Error: "Failed to connect to Discord after logging in",
|
2023-01-13 12:53:45 +00:00
|
|
|
ErrCode: ErrCodePostLoginConnFailed,
|
2022-02-21 12:49:44 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-13 12:03:08 +00:00
|
|
|
err = c.WriteJSON(respLogin{
|
|
|
|
Success: true,
|
|
|
|
ID: user.DiscordID,
|
|
|
|
Username: discordUser.Username,
|
|
|
|
Discriminator: discordUser.Discriminator,
|
2022-02-21 12:49:44 +00:00
|
|
|
})
|
2023-01-13 12:03:08 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Failed to write login success to websocket:", err)
|
|
|
|
}
|
2022-02-21 12:49:44 +00:00
|
|
|
return
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 13:56:15 +00:00
|
|
|
|
2023-01-13 12:03:08 +00:00
|
|
|
type respLogin struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
ID string `json:"id"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Discriminator string `json:"discriminator"`
|
|
|
|
}
|
|
|
|
|
2023-01-13 12:09:39 +00:00
|
|
|
type reqTokenLogin struct {
|
|
|
|
Token string `json:"token"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProvisioningAPI) tokenLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userID := r.URL.Query().Get("user_id")
|
|
|
|
user := p.bridge.GetUserByMXID(id.UserID(userID))
|
|
|
|
log := p.log.Sub("TokenLogin").Sub(user.MXID.String())
|
|
|
|
if user.IsLoggedIn() {
|
|
|
|
jsonResponse(w, http.StatusConflict, Error{
|
|
|
|
Error: "You're already logged into Discord",
|
2023-01-13 12:53:45 +00:00
|
|
|
ErrCode: ErrCodeAlreadyLoggedIn,
|
2023-01-13 12:09:39 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var body reqTokenLogin
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
|
|
log.Errorln("Failed to parse login request:", err)
|
|
|
|
jsonResponse(w, http.StatusBadRequest, Error{
|
|
|
|
Error: "Failed to parse request body",
|
2023-01-13 12:53:45 +00:00
|
|
|
ErrCode: mautrix.MBadJSON.ErrCode,
|
2023-01-13 12:09:39 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := user.Login(body.Token); err != nil {
|
|
|
|
log.Errorln("Failed to connect with provided token:", err)
|
|
|
|
jsonResponse(w, http.StatusUnauthorized, Error{
|
|
|
|
Error: "Failed to connect to Discord",
|
2023-01-13 12:53:45 +00:00
|
|
|
ErrCode: ErrCodePostLoginConnFailed,
|
2023-01-13 12:09:39 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Infoln("Successfully logged in")
|
|
|
|
jsonResponse(w, http.StatusOK, respLogin{
|
|
|
|
Success: true,
|
|
|
|
ID: user.DiscordID,
|
|
|
|
Username: user.Session.State.User.Username,
|
|
|
|
Discriminator: user.Session.State.User.Discriminator,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-22 13:56:15 +00:00
|
|
|
func (p *ProvisioningAPI) reconnect(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user := r.Context().Value("user").(*User)
|
|
|
|
|
|
|
|
if user.Connected() {
|
|
|
|
jsonResponse(w, http.StatusConflict, Error{
|
|
|
|
Error: "You're already connected to discord",
|
2023-01-13 12:53:45 +00:00
|
|
|
ErrCode: ErrCodeAlreadyConnected,
|
2022-02-22 13:56:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := user.Connect(); err != nil {
|
|
|
|
jsonResponse(w, http.StatusInternalServerError, Error{
|
|
|
|
Error: "Failed to connect to discord",
|
2023-01-13 12:53:45 +00:00
|
|
|
ErrCode: ErrCodeConnectFailed,
|
2022-02-22 13:56:15 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
jsonResponse(w, http.StatusOK, Response{
|
|
|
|
Success: true,
|
|
|
|
Status: "Connected to Discord",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-04-14 17:03:44 +00:00
|
|
|
|
2023-01-13 12:22:56 +00:00
|
|
|
type guildEntry struct {
|
2023-02-18 20:53:51 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
AvatarURL id.ContentURI `json:"avatar_url"`
|
|
|
|
MXID id.RoomID `json:"mxid"`
|
|
|
|
AutoBridge bool `json:"auto_bridge_channels"`
|
|
|
|
BridgingMode string `json:"bridging_mode"`
|
2023-01-13 12:22:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type respGuildsList struct {
|
|
|
|
Guilds []guildEntry `json:"guilds"`
|
|
|
|
}
|
|
|
|
|
2022-04-14 17:03:44 +00:00
|
|
|
func (p *ProvisioningAPI) guildsList(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user := r.Context().Value("user").(*User)
|
|
|
|
|
2023-01-13 12:22:56 +00:00
|
|
|
var resp respGuildsList
|
|
|
|
resp.Guilds = []guildEntry{}
|
2022-05-28 20:41:34 +00:00
|
|
|
for _, userGuild := range user.GetPortals() {
|
|
|
|
guild := p.bridge.GetGuildByID(userGuild.DiscordID, false)
|
2022-05-28 20:03:24 +00:00
|
|
|
if guild == nil {
|
|
|
|
continue
|
2022-04-14 17:03:44 +00:00
|
|
|
}
|
2023-01-13 12:22:56 +00:00
|
|
|
resp.Guilds = append(resp.Guilds, guildEntry{
|
2023-02-18 20:53:51 +00:00
|
|
|
ID: guild.ID,
|
|
|
|
Name: guild.PlainName,
|
|
|
|
AvatarURL: guild.AvatarURL,
|
|
|
|
MXID: guild.MXID,
|
|
|
|
AutoBridge: guild.BridgingMode == database.GuildBridgeEverything,
|
|
|
|
BridgingMode: guild.BridgingMode.String(),
|
2022-05-28 20:03:24 +00:00
|
|
|
})
|
2022-04-14 17:03:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 12:22:56 +00:00
|
|
|
jsonResponse(w, http.StatusOK, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
type reqBridgeGuild struct {
|
|
|
|
AutoCreateChannels bool `json:"auto_create_channels"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type respBridgeGuild struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
MXID id.RoomID `json:"mxid"`
|
2022-04-14 17:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProvisioningAPI) guildsBridge(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user := r.Context().Value("user").(*User)
|
2023-01-13 12:22:56 +00:00
|
|
|
guildID := mux.Vars(r)["guildID"]
|
2022-04-14 17:03:44 +00:00
|
|
|
|
2023-01-13 12:22:56 +00:00
|
|
|
var body reqBridgeGuild
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
|
|
p.log.Errorln("Failed to parse bridge request:", err)
|
|
|
|
jsonResponse(w, http.StatusBadRequest, Error{
|
|
|
|
Error: "Failed to parse request body",
|
2023-01-13 12:53:45 +00:00
|
|
|
ErrCode: mautrix.MBadJSON.ErrCode,
|
2023-01-13 12:22:56 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-04-14 17:03:44 +00:00
|
|
|
|
2023-01-13 12:22:56 +00:00
|
|
|
guild := user.bridge.GetGuildByID(guildID, false)
|
|
|
|
if guild == nil {
|
2022-04-14 17:03:44 +00:00
|
|
|
jsonResponse(w, http.StatusNotFound, Error{
|
2023-01-13 12:22:56 +00:00
|
|
|
Error: "Guild not found",
|
2023-01-13 12:53:45 +00:00
|
|
|
ErrCode: mautrix.MNotFound.ErrCode,
|
2022-04-14 17:03:44 +00:00
|
|
|
})
|
2023-01-13 12:22:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
alreadyExists := guild.MXID == ""
|
|
|
|
if err := user.bridgeGuild(guildID, body.AutoCreateChannels); err != nil {
|
|
|
|
p.log.Errorfln("Error bridging %s: %v", guildID, err)
|
|
|
|
jsonResponse(w, http.StatusInternalServerError, Error{
|
|
|
|
Error: "Internal error while trying to bridge guild",
|
2023-01-13 12:53:45 +00:00
|
|
|
ErrCode: ErrCodeGuildBridgeFailed,
|
2023-01-13 12:22:56 +00:00
|
|
|
})
|
|
|
|
} else if alreadyExists {
|
|
|
|
jsonResponse(w, http.StatusOK, respBridgeGuild{
|
|
|
|
Success: true,
|
|
|
|
MXID: guild.MXID,
|
|
|
|
})
|
2022-04-14 17:03:44 +00:00
|
|
|
} else {
|
2023-01-13 12:22:56 +00:00
|
|
|
jsonResponse(w, http.StatusCreated, respBridgeGuild{
|
|
|
|
Success: true,
|
|
|
|
MXID: guild.MXID,
|
|
|
|
})
|
2022-04-14 17:03:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProvisioningAPI) guildsUnbridge(w http.ResponseWriter, r *http.Request) {
|
2023-01-13 12:22:56 +00:00
|
|
|
guildID := mux.Vars(r)["guildID"]
|
2022-04-14 17:03:44 +00:00
|
|
|
user := r.Context().Value("user").(*User)
|
2023-01-13 12:22:56 +00:00
|
|
|
if user.PermissionLevel < bridgeconfig.PermissionLevelAdmin {
|
|
|
|
jsonResponse(w, http.StatusForbidden, Error{
|
|
|
|
Error: "Only bridge admins can unbridge guilds",
|
2023-01-13 12:53:45 +00:00
|
|
|
ErrCode: mautrix.MForbidden.ErrCode,
|
2023-01-13 12:22:56 +00:00
|
|
|
})
|
|
|
|
} else if guild := user.bridge.GetGuildByID(guildID, false); guild == nil {
|
2022-04-14 17:03:44 +00:00
|
|
|
jsonResponse(w, http.StatusNotFound, Error{
|
2023-01-13 12:22:56 +00:00
|
|
|
Error: "Guild not found",
|
2023-01-13 12:53:45 +00:00
|
|
|
ErrCode: mautrix.MNotFound.ErrCode,
|
2022-04-14 17:03:44 +00:00
|
|
|
})
|
2023-02-18 20:53:51 +00:00
|
|
|
} else if guild.BridgingMode == database.GuildBridgeNothing && guild.MXID == "" {
|
2022-04-14 17:03:44 +00:00
|
|
|
jsonResponse(w, http.StatusNotFound, Error{
|
2023-01-13 12:22:56 +00:00
|
|
|
Error: "That guild is not bridged",
|
2023-01-13 12:53:45 +00:00
|
|
|
ErrCode: ErrCodeGuildNotBridged,
|
2023-01-13 12:22:56 +00:00
|
|
|
})
|
|
|
|
} else if err := user.unbridgeGuild(guildID); err != nil {
|
|
|
|
p.log.Errorfln("Error unbridging %s: %v", guildID, err)
|
|
|
|
jsonResponse(w, http.StatusInternalServerError, Error{
|
|
|
|
Error: "Internal error while trying to unbridge guild",
|
2023-01-13 12:53:45 +00:00
|
|
|
ErrCode: ErrCodeGuildUnbridgeFailed,
|
2022-04-14 17:03:44 +00:00
|
|
|
})
|
|
|
|
} else {
|
2023-01-13 12:22:56 +00:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2022-04-14 17:03:44 +00:00
|
|
|
}
|
|
|
|
}
|