/
client.go
112 lines (95 loc) · 3.27 KB
/
client.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
// mautrix-signal - A Matrix-signal puppeting bridge.
// Copyright (C) 2023 Scott Weber
//
// 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 signalmeow
import (
"context"
"errors"
"net/url"
"sync"
"time"
"github.com/rs/zerolog"
"go.mau.fi/mautrix-signal/pkg/libsignalgo"
"go.mau.fi/mautrix-signal/pkg/signalmeow/events"
"go.mau.fi/mautrix-signal/pkg/signalmeow/store"
"go.mau.fi/mautrix-signal/pkg/signalmeow/web"
)
type Client struct {
Store *store.Device
SenderCertificate *libsignalgo.SenderCertificate
GroupCredentials *GroupCredentials
GroupCache *GroupCache
ProfileCache *ProfileCache
GroupCallCache *map[string]bool
LastContactRequestTime time.Time
encryptionLock sync.Mutex
AuthedWS *web.SignalWebsocket
UnauthedWS *web.SignalWebsocket
WSCancel context.CancelFunc
lastConnectionStatus SignalConnectionStatus
EventHandler func(events.SignalEvent)
storageAuthLock sync.Mutex
storageAuth *basicExpiringCredentials
cdAuthLock sync.Mutex
cdAuth *basicExpiringCredentials
cdToken []byte
}
func (cli *Client) handleEvent(evt events.SignalEvent) {
if cli.EventHandler != nil {
cli.EventHandler(evt)
}
}
func (cli *Client) IsConnected() bool {
if cli == nil {
return false
}
return cli.AuthedWS.IsConnected() && cli.UnauthedWS.IsConnected()
}
func (cli *Client) ConnectAuthedWS(ctx context.Context, requestHandler web.RequestHandlerFunc) (chan web.SignalWebsocketConnectionStatus, error) {
if cli.AuthedWS != nil {
return nil, errors.New("authed websocket already connected")
}
username, password := cli.Store.BasicAuthCreds()
log := zerolog.Ctx(ctx).With().
Str("websocket_type", "authed").
Str("username", username).
Logger()
ctx = log.WithContext(ctx)
username = url.QueryEscape(username)
password = url.QueryEscape(password)
path := web.WebsocketPath +
"?login=" + username +
"&password=" + password
authedWS := web.NewSignalWebsocket(path, &username, &password)
statusChan := authedWS.Connect(ctx, &requestHandler)
cli.AuthedWS = authedWS
return statusChan, nil
}
func (cli *Client) ConnectUnauthedWS(ctx context.Context) (chan web.SignalWebsocketConnectionStatus, error) {
if cli.UnauthedWS != nil {
return nil, errors.New("unauthed websocket already connected")
}
log := zerolog.Ctx(ctx).With().
Str("websocket_type", "unauthed").
Logger()
ctx = log.WithContext(ctx)
unauthedWS := web.NewSignalWebsocket(web.WebsocketPath, nil, nil)
statusChan := unauthedWS.Connect(ctx, nil)
cli.UnauthedWS = unauthedWS
return statusChan, nil
}
func (cli *Client) IsLoggedIn() bool {
return cli.Store != nil && cli.Store.IsDeviceLoggedIn()
}