-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
targets.go
200 lines (177 loc) · 5.84 KB
/
targets.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
// 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 (
"context"
"errors"
"fmt"
"runtime/debug"
"sync"
"sync/atomic"
log "maunium.net/go/maulogger/v2"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/id"
)
var targets = make(map[string]*SyncTarget)
var targetLock sync.Mutex
type SyncTarget struct {
AppserviceID string `json:"appservice_id"`
BotAccessToken string `json:"bot_access_token"`
HSToken string `json:"hs_token"`
Address string `json:"address"`
UserID id.UserID `json:"user_id"`
DeviceID id.DeviceID `json:"device_id"`
IsProxy bool `json:"is_proxy"`
NextBatch string `json:"-"`
Active bool `json:"-"`
client *mautrix.Client
log log.Logger
running bool
cancel func()
wg sync.WaitGroup
lock sync.Mutex
}
func (target *SyncTarget) Upsert() error {
query := `
INSERT INTO targets (appservice_id, bot_access_token, hs_token, address, user_id, device_id, is_proxy, next_batch, active)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
ON CONFLICT (appservice_id) DO UPDATE
SET bot_access_token=$2, hs_token=$3, address=$4, user_id=$5, device_id=$6, is_proxy=$7
`
if db.scheme == "sqlite3" {
query = "INSERT OR REPLACE INTO targets (appservice_id, bot_access_token, hs_token, address, user_id, device_id, is_proxy, next_batch, active)"
}
_, err := db.conn.Exec(query, target.AppserviceID, target.BotAccessToken, target.HSToken, target.Address, target.UserID, target.DeviceID, target.IsProxy, target.NextBatch, target.Active)
return err
}
func (target *SyncTarget) SetActive(active bool) error {
if target.Active == active {
return nil
}
target.Active = active
_, err := db.conn.Exec("UPDATE targets SET active=$2 WHERE appservice_id=$1", target.AppserviceID, target.Active)
return err
}
func (target *SyncTarget) SetNextBatch(nextBatch string) error {
if target.NextBatch == nextBatch {
return nil
}
target.NextBatch = nextBatch
_, err := db.conn.Exec("UPDATE targets SET next_batch=$2 WHERE appservice_id=$1", target.AppserviceID, target.NextBatch)
return err
}
func GetOrSetTarget(appserviceID string, newTarget *SyncTarget) *SyncTarget {
targetLock.Lock()
defer targetLock.Unlock()
target, ok := targets[appserviceID]
if !ok {
if newTarget != nil {
targets[appserviceID] = newTarget
}
return nil
}
return target
}
func LoadTargets() error {
res, err := db.conn.Query("SELECT appservice_id, bot_access_token, hs_token, address, is_proxy, user_id, device_id, active FROM targets")
if err != nil {
return fmt.Errorf("failed to query targets: %w", err)
}
targetLock.Lock()
defer targetLock.Unlock()
for res.Next() {
var target SyncTarget
err = res.Scan(&target.AppserviceID, &target.BotAccessToken, &target.HSToken, &target.Address, &target.IsProxy, &target.UserID, &target.DeviceID, &target.Active)
if err != nil {
return fmt.Errorf("failed to scan target: %w", err)
}
err = target.Init()
if err != nil {
target.log.Warnln("Failed to initialize target (startup):", err)
} else {
targets[target.AppserviceID] = &target
}
}
return nil
}
var globalSyncID uint64
const logContextKey = "log"
func (target *SyncTarget) Init() error {
target.log = log.Sub(fmt.Sprintf("Target-%s", target.AppserviceID))
var err error
target.client, err = mautrix.NewClient(cfg.HomeserverURL, target.UserID, target.BotAccessToken)
if err != nil {
return fmt.Errorf("failed to create client: %w", err)
}
return nil
}
func (target *SyncTarget) Start() {
syncLog := target.log.Sub(fmt.Sprintf("Sync-%d", atomic.AddUint64(&globalSyncID, 1)))
if target.running {
syncLog.Debugln("There seems to be an existing syncer running, stopping it first")
target.Stop()
}
syncLog.Debugln("Locking mutex to start syncing")
target.lock.Lock()
target.wg = sync.WaitGroup{}
target.wg.Add(1)
target.running = true
defer func() {
target.running = false
target.cancel = nil
target.wg.Done()
syncLog.Debugln("Unlocking mutex")
target.lock.Unlock()
err := recover()
if err != nil {
syncLog.Errorfln("Syncing panicked: %v\n%s", err, debug.Stack())
}
}()
if err := target.SetActive(true); err != nil {
syncLog.Warnln("Failed to mark target as active:", err)
}
defer func() {
if err := target.SetActive(false); err != nil {
syncLog.Warnln("Failed to mark target as inactive:", err)
}
}()
ctx, cancelFunc := context.WithCancel(context.WithValue(context.Background(), logContextKey, syncLog))
target.cancel = cancelFunc
syncLog.Infoln("Starting syncing")
err := target.sync(ctx)
if errors.Is(err, context.Canceled) {
syncLog.Infoln("Syncing stopped")
} else if err != nil {
syncLog.Errorfln("Syncing failed: %v, notifying target...", err)
proxyErr := &errorRequest{
Error: ProxyErrorUnknown,
Message: err.Error(),
}
if errors.Is(err, mautrix.MUnknownToken) {
proxyErr.Error = ProxyErrorLoggedOut
}
err = target.tryPostTransaction(ctx, nil, proxyErr)
if err != nil {
syncLog.Warnln("Failed to notify target about sync error:", err)
}
}
}
func (target *SyncTarget) Stop() {
if cancelFn := target.cancel; cancelFn != nil {
target.log.Debugln("Stopping syncing...")
cancelFn()
}
}