[101] | 1 | package soju
|
---|
| 2 |
|
---|
| 3 | import (
|
---|
[652] | 4 | "context"
|
---|
[385] | 5 | "crypto/sha256"
|
---|
| 6 | "encoding/binary"
|
---|
[395] | 7 | "encoding/hex"
|
---|
[218] | 8 | "fmt"
|
---|
[705] | 9 | "math/big"
|
---|
| 10 | "net"
|
---|
[101] | 11 | "time"
|
---|
[103] | 12 |
|
---|
| 13 | "gopkg.in/irc.v3"
|
---|
[101] | 14 | )
|
---|
| 15 |
|
---|
[165] | 16 | type event interface{}
|
---|
| 17 |
|
---|
| 18 | type eventUpstreamMessage struct {
|
---|
[103] | 19 | msg *irc.Message
|
---|
| 20 | uc *upstreamConn
|
---|
| 21 | }
|
---|
| 22 |
|
---|
[218] | 23 | type eventUpstreamConnectionError struct {
|
---|
| 24 | net *network
|
---|
| 25 | err error
|
---|
| 26 | }
|
---|
| 27 |
|
---|
[196] | 28 | type eventUpstreamConnected struct {
|
---|
| 29 | uc *upstreamConn
|
---|
| 30 | }
|
---|
| 31 |
|
---|
[179] | 32 | type eventUpstreamDisconnected struct {
|
---|
| 33 | uc *upstreamConn
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[218] | 36 | type eventUpstreamError struct {
|
---|
| 37 | uc *upstreamConn
|
---|
| 38 | err error
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[165] | 41 | type eventDownstreamMessage struct {
|
---|
[103] | 42 | msg *irc.Message
|
---|
| 43 | dc *downstreamConn
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[166] | 46 | type eventDownstreamConnected struct {
|
---|
| 47 | dc *downstreamConn
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[167] | 50 | type eventDownstreamDisconnected struct {
|
---|
| 51 | dc *downstreamConn
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[435] | 54 | type eventChannelDetach struct {
|
---|
| 55 | uc *upstreamConn
|
---|
| 56 | name string
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[563] | 59 | type eventBroadcast struct {
|
---|
| 60 | msg *irc.Message
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[376] | 63 | type eventStop struct{}
|
---|
| 64 |
|
---|
[625] | 65 | type eventUserUpdate struct {
|
---|
| 66 | password *string
|
---|
| 67 | admin *bool
|
---|
| 68 | done chan error
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[480] | 71 | type deliveredClientMap map[string]string // client name -> msg ID
|
---|
| 72 |
|
---|
[485] | 73 | type deliveredStore struct {
|
---|
| 74 | m deliveredCasemapMap
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | func newDeliveredStore() deliveredStore {
|
---|
| 78 | return deliveredStore{deliveredCasemapMap{newCasemapMap(0)}}
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | func (ds deliveredStore) HasTarget(target string) bool {
|
---|
| 82 | return ds.m.Value(target) != nil
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | func (ds deliveredStore) LoadID(target, clientName string) string {
|
---|
| 86 | clients := ds.m.Value(target)
|
---|
| 87 | if clients == nil {
|
---|
| 88 | return ""
|
---|
| 89 | }
|
---|
| 90 | return clients[clientName]
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | func (ds deliveredStore) StoreID(target, clientName, msgID string) {
|
---|
| 94 | clients := ds.m.Value(target)
|
---|
| 95 | if clients == nil {
|
---|
| 96 | clients = make(deliveredClientMap)
|
---|
| 97 | ds.m.SetValue(target, clients)
|
---|
| 98 | }
|
---|
| 99 | clients[clientName] = msgID
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | func (ds deliveredStore) ForEachTarget(f func(target string)) {
|
---|
| 103 | for _, entry := range ds.m.innerMap {
|
---|
| 104 | f(entry.originalKey)
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[489] | 108 | func (ds deliveredStore) ForEachClient(f func(clientName string)) {
|
---|
| 109 | clients := make(map[string]struct{})
|
---|
| 110 | for _, entry := range ds.m.innerMap {
|
---|
| 111 | delivered := entry.value.(deliveredClientMap)
|
---|
| 112 | for clientName := range delivered {
|
---|
| 113 | clients[clientName] = struct{}{}
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | for clientName := range clients {
|
---|
| 118 | f(clientName)
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[101] | 122 | type network struct {
|
---|
| 123 | Network
|
---|
[202] | 124 | user *user
|
---|
[501] | 125 | logger Logger
|
---|
[202] | 126 | stopped chan struct{}
|
---|
[131] | 127 |
|
---|
[482] | 128 | conn *upstreamConn
|
---|
| 129 | channels channelCasemapMap
|
---|
[485] | 130 | delivered deliveredStore
|
---|
[482] | 131 | lastError error
|
---|
| 132 | casemap casemapping
|
---|
[101] | 133 | }
|
---|
| 134 |
|
---|
[267] | 135 | func newNetwork(user *user, record *Network, channels []Channel) *network {
|
---|
[501] | 136 | logger := &prefixLogger{user.logger, fmt.Sprintf("network %q: ", record.GetName())}
|
---|
| 137 |
|
---|
[478] | 138 | m := channelCasemapMap{newCasemapMap(0)}
|
---|
[267] | 139 | for _, ch := range channels {
|
---|
[283] | 140 | ch := ch
|
---|
[478] | 141 | m.SetValue(ch.Name, &ch)
|
---|
[267] | 142 | }
|
---|
| 143 |
|
---|
[101] | 144 | return &network{
|
---|
[482] | 145 | Network: *record,
|
---|
| 146 | user: user,
|
---|
[501] | 147 | logger: logger,
|
---|
[482] | 148 | stopped: make(chan struct{}),
|
---|
| 149 | channels: m,
|
---|
[485] | 150 | delivered: newDeliveredStore(),
|
---|
[482] | 151 | casemap: casemapRFC1459,
|
---|
[101] | 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[218] | 155 | func (net *network) forEachDownstream(f func(*downstreamConn)) {
|
---|
| 156 | net.user.forEachDownstream(func(dc *downstreamConn) {
|
---|
[693] | 157 | if dc.network == nil && !dc.isMultiUpstream {
|
---|
[532] | 158 | return
|
---|
| 159 | }
|
---|
[218] | 160 | if dc.network != nil && dc.network != net {
|
---|
| 161 | return
|
---|
| 162 | }
|
---|
| 163 | f(dc)
|
---|
| 164 | })
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[311] | 167 | func (net *network) isStopped() bool {
|
---|
| 168 | select {
|
---|
| 169 | case <-net.stopped:
|
---|
| 170 | return true
|
---|
| 171 | default:
|
---|
| 172 | return false
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[385] | 176 | func userIdent(u *User) string {
|
---|
| 177 | // The ident is a string we will send to upstream servers in clear-text.
|
---|
| 178 | // For privacy reasons, make sure it doesn't expose any meaningful user
|
---|
| 179 | // metadata. We just use the base64-encoded hashed ID, so that people don't
|
---|
| 180 | // start relying on the string being an integer or following a pattern.
|
---|
| 181 | var b [64]byte
|
---|
| 182 | binary.LittleEndian.PutUint64(b[:], uint64(u.ID))
|
---|
| 183 | h := sha256.Sum256(b[:])
|
---|
[395] | 184 | return hex.EncodeToString(h[:16])
|
---|
[385] | 185 | }
|
---|
| 186 |
|
---|
[101] | 187 | func (net *network) run() {
|
---|
[542] | 188 | if !net.Enabled {
|
---|
| 189 | return
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[101] | 192 | var lastTry time.Time
|
---|
| 193 | for {
|
---|
[311] | 194 | if net.isStopped() {
|
---|
[202] | 195 | return
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[398] | 198 | if dur := time.Now().Sub(lastTry); dur < retryConnectDelay {
|
---|
| 199 | delay := retryConnectDelay - dur
|
---|
[501] | 200 | net.logger.Printf("waiting %v before trying to reconnect to %q", delay.Truncate(time.Second), net.Addr)
|
---|
[101] | 201 | time.Sleep(delay)
|
---|
| 202 | }
|
---|
| 203 | lastTry = time.Now()
|
---|
| 204 |
|
---|
| 205 | uc, err := connectToUpstream(net)
|
---|
| 206 | if err != nil {
|
---|
[501] | 207 | net.logger.Printf("failed to connect to upstream server %q: %v", net.Addr, err)
|
---|
[218] | 208 | net.user.events <- eventUpstreamConnectionError{net, fmt.Errorf("failed to connect: %v", err)}
|
---|
[101] | 209 | continue
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[385] | 212 | if net.user.srv.Identd != nil {
|
---|
| 213 | net.user.srv.Identd.Store(uc.RemoteAddr().String(), uc.LocalAddr().String(), userIdent(&net.user.User))
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[710] | 216 | net.user.srv.metrics.upstreams.Add(1)
|
---|
| 217 |
|
---|
[101] | 218 | uc.register()
|
---|
[197] | 219 | if err := uc.runUntilRegistered(); err != nil {
|
---|
[399] | 220 | text := err.Error()
|
---|
| 221 | if regErr, ok := err.(registrationError); ok {
|
---|
| 222 | text = string(regErr)
|
---|
| 223 | }
|
---|
| 224 | uc.logger.Printf("failed to register: %v", text)
|
---|
| 225 | net.user.events <- eventUpstreamConnectionError{net, fmt.Errorf("failed to register: %v", text)}
|
---|
[197] | 226 | uc.Close()
|
---|
| 227 | continue
|
---|
| 228 | }
|
---|
[101] | 229 |
|
---|
[311] | 230 | // TODO: this is racy with net.stopped. If the network is stopped
|
---|
| 231 | // before the user goroutine receives eventUpstreamConnected, the
|
---|
| 232 | // connection won't be closed.
|
---|
[196] | 233 | net.user.events <- eventUpstreamConnected{uc}
|
---|
[165] | 234 | if err := uc.readMessages(net.user.events); err != nil {
|
---|
[101] | 235 | uc.logger.Printf("failed to handle messages: %v", err)
|
---|
[218] | 236 | net.user.events <- eventUpstreamError{uc, fmt.Errorf("failed to handle messages: %v", err)}
|
---|
[101] | 237 | }
|
---|
| 238 | uc.Close()
|
---|
[179] | 239 | net.user.events <- eventUpstreamDisconnected{uc}
|
---|
[385] | 240 |
|
---|
| 241 | if net.user.srv.Identd != nil {
|
---|
| 242 | net.user.srv.Identd.Delete(uc.RemoteAddr().String(), uc.LocalAddr().String())
|
---|
| 243 | }
|
---|
[710] | 244 |
|
---|
| 245 | net.user.srv.metrics.upstreams.Add(-1)
|
---|
[101] | 246 | }
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[309] | 249 | func (net *network) stop() {
|
---|
[311] | 250 | if !net.isStopped() {
|
---|
[202] | 251 | close(net.stopped)
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[279] | 254 | if net.conn != nil {
|
---|
| 255 | net.conn.Close()
|
---|
[202] | 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[435] | 259 | func (net *network) detach(ch *Channel) {
|
---|
| 260 | if ch.Detached {
|
---|
| 261 | return
|
---|
[267] | 262 | }
|
---|
[497] | 263 |
|
---|
[501] | 264 | net.logger.Printf("detaching channel %q", ch.Name)
|
---|
[435] | 265 |
|
---|
[497] | 266 | ch.Detached = true
|
---|
| 267 |
|
---|
| 268 | if net.user.msgStore != nil {
|
---|
| 269 | nameCM := net.casemap(ch.Name)
|
---|
[666] | 270 | lastID, err := net.user.msgStore.LastMsgID(&net.Network, nameCM, time.Now())
|
---|
[497] | 271 | if err != nil {
|
---|
[501] | 272 | net.logger.Printf("failed to get last message ID for channel %q: %v", ch.Name, err)
|
---|
[497] | 273 | }
|
---|
| 274 | ch.DetachedInternalMsgID = lastID
|
---|
| 275 | }
|
---|
| 276 |
|
---|
[435] | 277 | if net.conn != nil {
|
---|
[478] | 278 | uch := net.conn.channels.Value(ch.Name)
|
---|
| 279 | if uch != nil {
|
---|
[435] | 280 | uch.updateAutoDetach(0)
|
---|
| 281 | }
|
---|
[222] | 282 | }
|
---|
[284] | 283 |
|
---|
[435] | 284 | net.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 285 | dc.SendMessage(&irc.Message{
|
---|
| 286 | Prefix: dc.prefix(),
|
---|
| 287 | Command: "PART",
|
---|
| 288 | Params: []string{dc.marshalEntity(net, ch.Name), "Detach"},
|
---|
| 289 | })
|
---|
| 290 | })
|
---|
| 291 | }
|
---|
[284] | 292 |
|
---|
[435] | 293 | func (net *network) attach(ch *Channel) {
|
---|
| 294 | if !ch.Detached {
|
---|
| 295 | return
|
---|
| 296 | }
|
---|
[497] | 297 |
|
---|
[501] | 298 | net.logger.Printf("attaching channel %q", ch.Name)
|
---|
[284] | 299 |
|
---|
[497] | 300 | detachedMsgID := ch.DetachedInternalMsgID
|
---|
| 301 | ch.Detached = false
|
---|
| 302 | ch.DetachedInternalMsgID = ""
|
---|
| 303 |
|
---|
[435] | 304 | var uch *upstreamChannel
|
---|
| 305 | if net.conn != nil {
|
---|
[478] | 306 | uch = net.conn.channels.Value(ch.Name)
|
---|
[284] | 307 |
|
---|
[435] | 308 | net.conn.updateChannelAutoDetach(ch.Name)
|
---|
| 309 | }
|
---|
[284] | 310 |
|
---|
[435] | 311 | net.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 312 | dc.SendMessage(&irc.Message{
|
---|
| 313 | Prefix: dc.prefix(),
|
---|
| 314 | Command: "JOIN",
|
---|
| 315 | Params: []string{dc.marshalEntity(net, ch.Name)},
|
---|
| 316 | })
|
---|
| 317 |
|
---|
| 318 | if uch != nil {
|
---|
| 319 | forwardChannel(dc, uch)
|
---|
[284] | 320 | }
|
---|
| 321 |
|
---|
[497] | 322 | if detachedMsgID != "" {
|
---|
[701] | 323 | dc.sendTargetBacklog(context.TODO(), net, ch.Name, detachedMsgID)
|
---|
[495] | 324 | }
|
---|
[435] | 325 | })
|
---|
[222] | 326 | }
|
---|
| 327 |
|
---|
[676] | 328 | func (net *network) deleteChannel(ctx context.Context, name string) error {
|
---|
[478] | 329 | ch := net.channels.Value(name)
|
---|
| 330 | if ch == nil {
|
---|
[416] | 331 | return fmt.Errorf("unknown channel %q", name)
|
---|
| 332 | }
|
---|
[435] | 333 | if net.conn != nil {
|
---|
[478] | 334 | uch := net.conn.channels.Value(ch.Name)
|
---|
| 335 | if uch != nil {
|
---|
[435] | 336 | uch.updateAutoDetach(0)
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 |
|
---|
[676] | 340 | if err := net.user.srv.db.DeleteChannel(ctx, ch.ID); err != nil {
|
---|
[267] | 341 | return err
|
---|
| 342 | }
|
---|
[478] | 343 | net.channels.Delete(name)
|
---|
[267] | 344 | return nil
|
---|
[222] | 345 | }
|
---|
| 346 |
|
---|
[478] | 347 | func (net *network) updateCasemapping(newCasemap casemapping) {
|
---|
| 348 | net.casemap = newCasemap
|
---|
| 349 | net.channels.SetCasemapping(newCasemap)
|
---|
[485] | 350 | net.delivered.m.SetCasemapping(newCasemap)
|
---|
[684] | 351 | if uc := net.conn; uc != nil {
|
---|
| 352 | uc.channels.SetCasemapping(newCasemap)
|
---|
| 353 | for _, entry := range uc.channels.innerMap {
|
---|
[478] | 354 | uch := entry.value.(*upstreamChannel)
|
---|
| 355 | uch.Members.SetCasemapping(newCasemap)
|
---|
| 356 | }
|
---|
[684] | 357 | uc.monitored.SetCasemapping(newCasemap)
|
---|
[478] | 358 | }
|
---|
[684] | 359 | net.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 360 | dc.monitored.SetCasemapping(newCasemap)
|
---|
| 361 | })
|
---|
[478] | 362 | }
|
---|
| 363 |
|
---|
[489] | 364 | func (net *network) storeClientDeliveryReceipts(clientName string) {
|
---|
| 365 | if !net.user.hasPersistentMsgStore() {
|
---|
| 366 | return
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | var receipts []DeliveryReceipt
|
---|
| 370 | net.delivered.ForEachTarget(func(target string) {
|
---|
| 371 | msgID := net.delivered.LoadID(target, clientName)
|
---|
| 372 | if msgID == "" {
|
---|
| 373 | return
|
---|
| 374 | }
|
---|
| 375 | receipts = append(receipts, DeliveryReceipt{
|
---|
| 376 | Target: target,
|
---|
| 377 | InternalMsgID: msgID,
|
---|
| 378 | })
|
---|
| 379 | })
|
---|
| 380 |
|
---|
[652] | 381 | if err := net.user.srv.db.StoreClientDeliveryReceipts(context.TODO(), net.ID, clientName, receipts); err != nil {
|
---|
[501] | 382 | net.logger.Printf("failed to store delivery receipts for client %q: %v", clientName, err)
|
---|
[489] | 383 | }
|
---|
| 384 | }
|
---|
| 385 |
|
---|
[499] | 386 | func (net *network) isHighlight(msg *irc.Message) bool {
|
---|
| 387 | if msg.Command != "PRIVMSG" && msg.Command != "NOTICE" {
|
---|
| 388 | return false
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | text := msg.Params[1]
|
---|
| 392 |
|
---|
| 393 | nick := net.Nick
|
---|
| 394 | if net.conn != nil {
|
---|
| 395 | nick = net.conn.nick
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | // TODO: use case-mapping aware comparison here
|
---|
| 399 | return msg.Prefix.Name != nick && isHighlight(text, nick)
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | func (net *network) detachedMessageNeedsRelay(ch *Channel, msg *irc.Message) bool {
|
---|
| 403 | highlight := net.isHighlight(msg)
|
---|
| 404 | return ch.RelayDetached == FilterMessage || ((ch.RelayDetached == FilterHighlight || ch.RelayDetached == FilterDefault) && highlight)
|
---|
| 405 | }
|
---|
| 406 |
|
---|
[101] | 407 | type user struct {
|
---|
| 408 | User
|
---|
[493] | 409 | srv *Server
|
---|
| 410 | logger Logger
|
---|
[101] | 411 |
|
---|
[165] | 412 | events chan event
|
---|
[377] | 413 | done chan struct{}
|
---|
[103] | 414 |
|
---|
[101] | 415 | networks []*network
|
---|
| 416 | downstreamConns []*downstreamConn
|
---|
[439] | 417 | msgStore messageStore
|
---|
[101] | 418 | }
|
---|
| 419 |
|
---|
| 420 | func newUser(srv *Server, record *User) *user {
|
---|
[493] | 421 | logger := &prefixLogger{srv.Logger, fmt.Sprintf("user %q: ", record.Username)}
|
---|
| 422 |
|
---|
[439] | 423 | var msgStore messageStore
|
---|
[691] | 424 | if logPath := srv.Config().LogPath; logPath != "" {
|
---|
| 425 | msgStore = newFSMessageStore(logPath, record.Username)
|
---|
[442] | 426 | } else {
|
---|
| 427 | msgStore = newMemoryMessageStore()
|
---|
[423] | 428 | }
|
---|
| 429 |
|
---|
[101] | 430 | return &user{
|
---|
[489] | 431 | User: *record,
|
---|
| 432 | srv: srv,
|
---|
[493] | 433 | logger: logger,
|
---|
[489] | 434 | events: make(chan event, 64),
|
---|
| 435 | done: make(chan struct{}),
|
---|
| 436 | msgStore: msgStore,
|
---|
[101] | 437 | }
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 | func (u *user) forEachNetwork(f func(*network)) {
|
---|
| 441 | for _, network := range u.networks {
|
---|
| 442 | f(network)
|
---|
| 443 | }
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | func (u *user) forEachUpstream(f func(uc *upstreamConn)) {
|
---|
| 447 | for _, network := range u.networks {
|
---|
[279] | 448 | if network.conn == nil {
|
---|
[101] | 449 | continue
|
---|
| 450 | }
|
---|
[279] | 451 | f(network.conn)
|
---|
[101] | 452 | }
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | func (u *user) forEachDownstream(f func(dc *downstreamConn)) {
|
---|
| 456 | for _, dc := range u.downstreamConns {
|
---|
| 457 | f(dc)
|
---|
| 458 | }
|
---|
| 459 | }
|
---|
| 460 |
|
---|
| 461 | func (u *user) getNetwork(name string) *network {
|
---|
| 462 | for _, network := range u.networks {
|
---|
| 463 | if network.Addr == name {
|
---|
| 464 | return network
|
---|
| 465 | }
|
---|
[201] | 466 | if network.Name != "" && network.Name == name {
|
---|
| 467 | return network
|
---|
| 468 | }
|
---|
[101] | 469 | }
|
---|
| 470 | return nil
|
---|
| 471 | }
|
---|
| 472 |
|
---|
[313] | 473 | func (u *user) getNetworkByID(id int64) *network {
|
---|
| 474 | for _, net := range u.networks {
|
---|
| 475 | if net.ID == id {
|
---|
| 476 | return net
|
---|
| 477 | }
|
---|
| 478 | }
|
---|
| 479 | return nil
|
---|
| 480 | }
|
---|
| 481 |
|
---|
[101] | 482 | func (u *user) run() {
|
---|
[423] | 483 | defer func() {
|
---|
| 484 | if u.msgStore != nil {
|
---|
| 485 | if err := u.msgStore.Close(); err != nil {
|
---|
[493] | 486 | u.logger.Printf("failed to close message store for user %q: %v", u.Username, err)
|
---|
[423] | 487 | }
|
---|
| 488 | }
|
---|
| 489 | close(u.done)
|
---|
| 490 | }()
|
---|
[377] | 491 |
|
---|
[652] | 492 | networks, err := u.srv.db.ListNetworks(context.TODO(), u.ID)
|
---|
[101] | 493 | if err != nil {
|
---|
[493] | 494 | u.logger.Printf("failed to list networks for user %q: %v", u.Username, err)
|
---|
[101] | 495 | return
|
---|
| 496 | }
|
---|
| 497 |
|
---|
| 498 | for _, record := range networks {
|
---|
[283] | 499 | record := record
|
---|
[652] | 500 | channels, err := u.srv.db.ListChannels(context.TODO(), record.ID)
|
---|
[267] | 501 | if err != nil {
|
---|
[493] | 502 | u.logger.Printf("failed to list channels for user %q, network %q: %v", u.Username, record.GetName(), err)
|
---|
[359] | 503 | continue
|
---|
[267] | 504 | }
|
---|
| 505 |
|
---|
| 506 | network := newNetwork(u, &record, channels)
|
---|
[101] | 507 | u.networks = append(u.networks, network)
|
---|
| 508 |
|
---|
[489] | 509 | if u.hasPersistentMsgStore() {
|
---|
[652] | 510 | receipts, err := u.srv.db.ListDeliveryReceipts(context.TODO(), record.ID)
|
---|
[489] | 511 | if err != nil {
|
---|
[493] | 512 | u.logger.Printf("failed to load delivery receipts for user %q, network %q: %v", u.Username, network.GetName(), err)
|
---|
[489] | 513 | return
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 | for _, rcpt := range receipts {
|
---|
| 517 | network.delivered.StoreID(rcpt.Target, rcpt.Client, rcpt.InternalMsgID)
|
---|
| 518 | }
|
---|
| 519 | }
|
---|
| 520 |
|
---|
[101] | 521 | go network.run()
|
---|
| 522 | }
|
---|
[103] | 523 |
|
---|
[165] | 524 | for e := range u.events {
|
---|
| 525 | switch e := e.(type) {
|
---|
[196] | 526 | case eventUpstreamConnected:
|
---|
[198] | 527 | uc := e.uc
|
---|
[199] | 528 |
|
---|
| 529 | uc.network.conn = uc
|
---|
| 530 |
|
---|
[198] | 531 | uc.updateAway()
|
---|
[684] | 532 | uc.updateMonitor()
|
---|
[218] | 533 |
|
---|
[532] | 534 | netIDStr := fmt.Sprintf("%v", uc.network.ID)
|
---|
[218] | 535 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[276] | 536 | dc.updateSupportedCaps()
|
---|
[296] | 537 |
|
---|
[543] | 538 | if !dc.caps["soju.im/bouncer-networks"] {
|
---|
| 539 | sendServiceNOTICE(dc, fmt.Sprintf("connected to %s", uc.network.GetName()))
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | dc.updateNick()
|
---|
| 543 | dc.updateRealname()
|
---|
| 544 | })
|
---|
| 545 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
[535] | 546 | if dc.caps["soju.im/bouncer-networks-notify"] {
|
---|
[532] | 547 | dc.SendMessage(&irc.Message{
|
---|
| 548 | Prefix: dc.srv.prefix(),
|
---|
| 549 | Command: "BOUNCER",
|
---|
[544] | 550 | Params: []string{"NETWORK", netIDStr, "state=connected"},
|
---|
[532] | 551 | })
|
---|
| 552 | }
|
---|
[218] | 553 | })
|
---|
| 554 | uc.network.lastError = nil
|
---|
[179] | 555 | case eventUpstreamDisconnected:
|
---|
[313] | 556 | u.handleUpstreamDisconnected(e.uc)
|
---|
| 557 | case eventUpstreamConnectionError:
|
---|
| 558 | net := e.net
|
---|
[199] | 559 |
|
---|
[313] | 560 | stopped := false
|
---|
| 561 | select {
|
---|
| 562 | case <-net.stopped:
|
---|
| 563 | stopped = true
|
---|
| 564 | default:
|
---|
[179] | 565 | }
|
---|
[199] | 566 |
|
---|
[313] | 567 | if !stopped && (net.lastError == nil || net.lastError.Error() != e.err.Error()) {
|
---|
[218] | 568 | net.forEachDownstream(func(dc *downstreamConn) {
|
---|
[223] | 569 | sendServiceNOTICE(dc, fmt.Sprintf("failed connecting/registering to %s: %v", net.GetName(), e.err))
|
---|
[218] | 570 | })
|
---|
| 571 | }
|
---|
| 572 | net.lastError = e.err
|
---|
| 573 | case eventUpstreamError:
|
---|
| 574 | uc := e.uc
|
---|
| 575 |
|
---|
| 576 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[223] | 577 | sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s: %v", uc.network.GetName(), e.err))
|
---|
[218] | 578 | })
|
---|
| 579 | uc.network.lastError = e.err
|
---|
[165] | 580 | case eventUpstreamMessage:
|
---|
| 581 | msg, uc := e.msg, e.uc
|
---|
[175] | 582 | if uc.isClosed() {
|
---|
[133] | 583 | uc.logger.Printf("ignoring message on closed connection: %v", msg)
|
---|
| 584 | break
|
---|
| 585 | }
|
---|
[103] | 586 | if err := uc.handleMessage(msg); err != nil {
|
---|
| 587 | uc.logger.Printf("failed to handle message %q: %v", msg, err)
|
---|
| 588 | }
|
---|
[435] | 589 | case eventChannelDetach:
|
---|
| 590 | uc, name := e.uc, e.name
|
---|
[478] | 591 | c := uc.network.channels.Value(name)
|
---|
| 592 | if c == nil || c.Detached {
|
---|
[435] | 593 | continue
|
---|
| 594 | }
|
---|
| 595 | uc.network.detach(c)
|
---|
[652] | 596 | if err := uc.srv.db.StoreChannel(context.TODO(), uc.network.ID, c); err != nil {
|
---|
[493] | 597 | u.logger.Printf("failed to store updated detached channel %q: %v", c.Name, err)
|
---|
[435] | 598 | }
|
---|
[166] | 599 | case eventDownstreamConnected:
|
---|
| 600 | dc := e.dc
|
---|
[168] | 601 |
|
---|
[684] | 602 | if dc.network != nil {
|
---|
| 603 | dc.monitored.SetCasemapping(dc.network.casemap)
|
---|
| 604 | }
|
---|
| 605 |
|
---|
[701] | 606 | if err := dc.welcome(context.TODO()); err != nil {
|
---|
[168] | 607 | dc.logger.Printf("failed to handle new registered connection: %v", err)
|
---|
| 608 | break
|
---|
| 609 | }
|
---|
| 610 |
|
---|
[166] | 611 | u.downstreamConns = append(u.downstreamConns, dc)
|
---|
[198] | 612 |
|
---|
[467] | 613 | dc.forEachNetwork(func(network *network) {
|
---|
| 614 | if network.lastError != nil {
|
---|
| 615 | sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s: %v", network.GetName(), network.lastError))
|
---|
| 616 | }
|
---|
| 617 | })
|
---|
| 618 |
|
---|
[198] | 619 | u.forEachUpstream(func(uc *upstreamConn) {
|
---|
| 620 | uc.updateAway()
|
---|
| 621 | })
|
---|
[167] | 622 | case eventDownstreamDisconnected:
|
---|
| 623 | dc := e.dc
|
---|
[204] | 624 |
|
---|
[167] | 625 | for i := range u.downstreamConns {
|
---|
| 626 | if u.downstreamConns[i] == dc {
|
---|
| 627 | u.downstreamConns = append(u.downstreamConns[:i], u.downstreamConns[i+1:]...)
|
---|
| 628 | break
|
---|
| 629 | }
|
---|
| 630 | }
|
---|
[198] | 631 |
|
---|
[489] | 632 | dc.forEachNetwork(func(net *network) {
|
---|
| 633 | net.storeClientDeliveryReceipts(dc.clientName)
|
---|
| 634 | })
|
---|
| 635 |
|
---|
[198] | 636 | u.forEachUpstream(func(uc *upstreamConn) {
|
---|
| 637 | uc.updateAway()
|
---|
[684] | 638 | uc.updateMonitor()
|
---|
[198] | 639 | })
|
---|
[165] | 640 | case eventDownstreamMessage:
|
---|
| 641 | msg, dc := e.msg, e.dc
|
---|
[133] | 642 | if dc.isClosed() {
|
---|
| 643 | dc.logger.Printf("ignoring message on closed connection: %v", msg)
|
---|
| 644 | break
|
---|
| 645 | }
|
---|
[704] | 646 | err := dc.handleMessage(context.TODO(), msg)
|
---|
[103] | 647 | if ircErr, ok := err.(ircError); ok {
|
---|
| 648 | ircErr.Message.Prefix = dc.srv.prefix()
|
---|
| 649 | dc.SendMessage(ircErr.Message)
|
---|
| 650 | } else if err != nil {
|
---|
| 651 | dc.logger.Printf("failed to handle message %q: %v", msg, err)
|
---|
| 652 | dc.Close()
|
---|
| 653 | }
|
---|
[563] | 654 | case eventBroadcast:
|
---|
| 655 | msg := e.msg
|
---|
| 656 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 657 | dc.SendMessage(msg)
|
---|
| 658 | })
|
---|
[625] | 659 | case eventUserUpdate:
|
---|
| 660 | // copy the user record because we'll mutate it
|
---|
| 661 | record := u.User
|
---|
| 662 |
|
---|
| 663 | if e.password != nil {
|
---|
| 664 | record.Password = *e.password
|
---|
| 665 | }
|
---|
| 666 | if e.admin != nil {
|
---|
| 667 | record.Admin = *e.admin
|
---|
| 668 | }
|
---|
| 669 |
|
---|
[676] | 670 | e.done <- u.updateUser(context.TODO(), &record)
|
---|
[625] | 671 |
|
---|
| 672 | // If the password was updated, kill all downstream connections to
|
---|
| 673 | // force them to re-authenticate with the new credentials.
|
---|
| 674 | if e.password != nil {
|
---|
| 675 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 676 | dc.Close()
|
---|
| 677 | })
|
---|
| 678 | }
|
---|
[376] | 679 | case eventStop:
|
---|
| 680 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 681 | dc.Close()
|
---|
| 682 | })
|
---|
| 683 | for _, n := range u.networks {
|
---|
| 684 | n.stop()
|
---|
[489] | 685 |
|
---|
| 686 | n.delivered.ForEachClient(func(clientName string) {
|
---|
| 687 | n.storeClientDeliveryReceipts(clientName)
|
---|
| 688 | })
|
---|
[376] | 689 | }
|
---|
| 690 | return
|
---|
[165] | 691 | default:
|
---|
[494] | 692 | panic(fmt.Sprintf("received unknown event type: %T", e))
|
---|
[103] | 693 | }
|
---|
| 694 | }
|
---|
[101] | 695 | }
|
---|
| 696 |
|
---|
[313] | 697 | func (u *user) handleUpstreamDisconnected(uc *upstreamConn) {
|
---|
| 698 | uc.network.conn = nil
|
---|
| 699 |
|
---|
[682] | 700 | uc.endPendingCommands()
|
---|
[313] | 701 |
|
---|
[478] | 702 | for _, entry := range uc.channels.innerMap {
|
---|
| 703 | uch := entry.value.(*upstreamChannel)
|
---|
[435] | 704 | uch.updateAutoDetach(0)
|
---|
| 705 | }
|
---|
| 706 |
|
---|
[532] | 707 | netIDStr := fmt.Sprintf("%v", uc.network.ID)
|
---|
[313] | 708 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 709 | dc.updateSupportedCaps()
|
---|
[543] | 710 | })
|
---|
[583] | 711 |
|
---|
| 712 | // If the network has been removed, don't send a state change notification
|
---|
| 713 | found := false
|
---|
| 714 | for _, net := range u.networks {
|
---|
| 715 | if net == uc.network {
|
---|
| 716 | found = true
|
---|
| 717 | break
|
---|
| 718 | }
|
---|
| 719 | }
|
---|
| 720 | if !found {
|
---|
| 721 | return
|
---|
| 722 | }
|
---|
| 723 |
|
---|
[543] | 724 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
[535] | 725 | if dc.caps["soju.im/bouncer-networks-notify"] {
|
---|
[532] | 726 | dc.SendMessage(&irc.Message{
|
---|
| 727 | Prefix: dc.srv.prefix(),
|
---|
| 728 | Command: "BOUNCER",
|
---|
[544] | 729 | Params: []string{"NETWORK", netIDStr, "state=disconnected"},
|
---|
[532] | 730 | })
|
---|
| 731 | }
|
---|
[313] | 732 | })
|
---|
| 733 |
|
---|
| 734 | if uc.network.lastError == nil {
|
---|
| 735 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[532] | 736 | if !dc.caps["soju.im/bouncer-networks"] {
|
---|
| 737 | sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s", uc.network.GetName()))
|
---|
| 738 | }
|
---|
[313] | 739 | })
|
---|
| 740 | }
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 | func (u *user) addNetwork(network *network) {
|
---|
| 744 | u.networks = append(u.networks, network)
|
---|
| 745 | go network.run()
|
---|
| 746 | }
|
---|
| 747 |
|
---|
| 748 | func (u *user) removeNetwork(network *network) {
|
---|
| 749 | network.stop()
|
---|
| 750 |
|
---|
| 751 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 752 | if dc.network != nil && dc.network == network {
|
---|
| 753 | dc.Close()
|
---|
| 754 | }
|
---|
| 755 | })
|
---|
| 756 |
|
---|
| 757 | for i, net := range u.networks {
|
---|
| 758 | if net == network {
|
---|
| 759 | u.networks = append(u.networks[:i], u.networks[i+1:]...)
|
---|
| 760 | return
|
---|
| 761 | }
|
---|
| 762 | }
|
---|
| 763 |
|
---|
| 764 | panic("tried to remove a non-existing network")
|
---|
| 765 | }
|
---|
| 766 |
|
---|
[500] | 767 | func (u *user) checkNetwork(record *Network) error {
|
---|
| 768 | for _, net := range u.networks {
|
---|
| 769 | if net.GetName() == record.GetName() && net.ID != record.ID {
|
---|
| 770 | return fmt.Errorf("a network with the name %q already exists", record.GetName())
|
---|
| 771 | }
|
---|
| 772 | }
|
---|
| 773 | return nil
|
---|
| 774 | }
|
---|
| 775 |
|
---|
[676] | 776 | func (u *user) createNetwork(ctx context.Context, record *Network) (*network, error) {
|
---|
[313] | 777 | if record.ID != 0 {
|
---|
[144] | 778 | panic("tried creating an already-existing network")
|
---|
| 779 | }
|
---|
| 780 |
|
---|
[500] | 781 | if err := u.checkNetwork(record); err != nil {
|
---|
| 782 | return nil, err
|
---|
| 783 | }
|
---|
| 784 |
|
---|
[691] | 785 | if max := u.srv.Config().MaxUserNetworks; max >= 0 && len(u.networks) >= max {
|
---|
[612] | 786 | return nil, fmt.Errorf("maximum number of networks reached")
|
---|
| 787 | }
|
---|
| 788 |
|
---|
[313] | 789 | network := newNetwork(u, record, nil)
|
---|
[676] | 790 | err := u.srv.db.StoreNetwork(ctx, u.ID, &network.Network)
|
---|
[101] | 791 | if err != nil {
|
---|
| 792 | return nil, err
|
---|
| 793 | }
|
---|
[144] | 794 |
|
---|
[313] | 795 | u.addNetwork(network)
|
---|
[144] | 796 |
|
---|
[532] | 797 | idStr := fmt.Sprintf("%v", network.ID)
|
---|
[535] | 798 | attrs := getNetworkAttrs(network)
|
---|
[532] | 799 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
[535] | 800 | if dc.caps["soju.im/bouncer-networks-notify"] {
|
---|
[532] | 801 | dc.SendMessage(&irc.Message{
|
---|
| 802 | Prefix: dc.srv.prefix(),
|
---|
| 803 | Command: "BOUNCER",
|
---|
[535] | 804 | Params: []string{"NETWORK", idStr, attrs.String()},
|
---|
[532] | 805 | })
|
---|
| 806 | }
|
---|
| 807 | })
|
---|
| 808 |
|
---|
[101] | 809 | return network, nil
|
---|
| 810 | }
|
---|
[202] | 811 |
|
---|
[676] | 812 | func (u *user) updateNetwork(ctx context.Context, record *Network) (*network, error) {
|
---|
[313] | 813 | if record.ID == 0 {
|
---|
| 814 | panic("tried updating a new network")
|
---|
| 815 | }
|
---|
[202] | 816 |
|
---|
[568] | 817 | // If the realname is reset to the default, just wipe the per-network
|
---|
| 818 | // setting
|
---|
| 819 | if record.Realname == u.Realname {
|
---|
| 820 | record.Realname = ""
|
---|
| 821 | }
|
---|
| 822 |
|
---|
[500] | 823 | if err := u.checkNetwork(record); err != nil {
|
---|
| 824 | return nil, err
|
---|
| 825 | }
|
---|
| 826 |
|
---|
[313] | 827 | network := u.getNetworkByID(record.ID)
|
---|
| 828 | if network == nil {
|
---|
| 829 | panic("tried updating a non-existing network")
|
---|
| 830 | }
|
---|
| 831 |
|
---|
[676] | 832 | if err := u.srv.db.StoreNetwork(ctx, u.ID, record); err != nil {
|
---|
[313] | 833 | return nil, err
|
---|
| 834 | }
|
---|
| 835 |
|
---|
| 836 | // Most network changes require us to re-connect to the upstream server
|
---|
| 837 |
|
---|
[478] | 838 | channels := make([]Channel, 0, network.channels.Len())
|
---|
| 839 | for _, entry := range network.channels.innerMap {
|
---|
| 840 | ch := entry.value.(*Channel)
|
---|
[313] | 841 | channels = append(channels, *ch)
|
---|
| 842 | }
|
---|
| 843 |
|
---|
| 844 | updatedNetwork := newNetwork(u, record, channels)
|
---|
| 845 |
|
---|
| 846 | // If we're currently connected, disconnect and perform the necessary
|
---|
| 847 | // bookkeeping
|
---|
| 848 | if network.conn != nil {
|
---|
| 849 | network.stop()
|
---|
| 850 | // Note: this will set network.conn to nil
|
---|
| 851 | u.handleUpstreamDisconnected(network.conn)
|
---|
| 852 | }
|
---|
| 853 |
|
---|
| 854 | // Patch downstream connections to use our fresh updated network
|
---|
| 855 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 856 | if dc.network != nil && dc.network == network {
|
---|
| 857 | dc.network = updatedNetwork
|
---|
[202] | 858 | }
|
---|
[313] | 859 | })
|
---|
[202] | 860 |
|
---|
[313] | 861 | // We need to remove the network after patching downstream connections,
|
---|
| 862 | // otherwise they'll get closed
|
---|
| 863 | u.removeNetwork(network)
|
---|
[202] | 864 |
|
---|
[644] | 865 | // The filesystem message store needs to be notified whenever the network
|
---|
| 866 | // is renamed
|
---|
| 867 | fsMsgStore, isFS := u.msgStore.(*fsMessageStore)
|
---|
| 868 | if isFS && updatedNetwork.GetName() != network.GetName() {
|
---|
[666] | 869 | if err := fsMsgStore.RenameNetwork(&network.Network, &updatedNetwork.Network); err != nil {
|
---|
[644] | 870 | network.logger.Printf("failed to update FS message store network name to %q: %v", updatedNetwork.GetName(), err)
|
---|
| 871 | }
|
---|
| 872 | }
|
---|
| 873 |
|
---|
[313] | 874 | // This will re-connect to the upstream server
|
---|
| 875 | u.addNetwork(updatedNetwork)
|
---|
| 876 |
|
---|
[535] | 877 | // TODO: only broadcast attributes that have changed
|
---|
| 878 | idStr := fmt.Sprintf("%v", updatedNetwork.ID)
|
---|
| 879 | attrs := getNetworkAttrs(updatedNetwork)
|
---|
| 880 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 881 | if dc.caps["soju.im/bouncer-networks-notify"] {
|
---|
| 882 | dc.SendMessage(&irc.Message{
|
---|
| 883 | Prefix: dc.srv.prefix(),
|
---|
| 884 | Command: "BOUNCER",
|
---|
| 885 | Params: []string{"NETWORK", idStr, attrs.String()},
|
---|
| 886 | })
|
---|
| 887 | }
|
---|
| 888 | })
|
---|
[532] | 889 |
|
---|
[313] | 890 | return updatedNetwork, nil
|
---|
| 891 | }
|
---|
| 892 |
|
---|
[676] | 893 | func (u *user) deleteNetwork(ctx context.Context, id int64) error {
|
---|
[313] | 894 | network := u.getNetworkByID(id)
|
---|
| 895 | if network == nil {
|
---|
| 896 | panic("tried deleting a non-existing network")
|
---|
[202] | 897 | }
|
---|
| 898 |
|
---|
[676] | 899 | if err := u.srv.db.DeleteNetwork(ctx, network.ID); err != nil {
|
---|
[313] | 900 | return err
|
---|
| 901 | }
|
---|
| 902 |
|
---|
| 903 | u.removeNetwork(network)
|
---|
[532] | 904 |
|
---|
| 905 | idStr := fmt.Sprintf("%v", network.ID)
|
---|
| 906 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
[535] | 907 | if dc.caps["soju.im/bouncer-networks-notify"] {
|
---|
[532] | 908 | dc.SendMessage(&irc.Message{
|
---|
| 909 | Prefix: dc.srv.prefix(),
|
---|
| 910 | Command: "BOUNCER",
|
---|
| 911 | Params: []string{"NETWORK", idStr, "*"},
|
---|
| 912 | })
|
---|
| 913 | }
|
---|
| 914 | })
|
---|
| 915 |
|
---|
[313] | 916 | return nil
|
---|
[202] | 917 | }
|
---|
[252] | 918 |
|
---|
[676] | 919 | func (u *user) updateUser(ctx context.Context, record *User) error {
|
---|
[572] | 920 | if u.ID != record.ID {
|
---|
| 921 | panic("ID mismatch when updating user")
|
---|
| 922 | }
|
---|
[376] | 923 |
|
---|
[572] | 924 | realnameUpdated := u.Realname != record.Realname
|
---|
[676] | 925 | if err := u.srv.db.StoreUser(ctx, record); err != nil {
|
---|
[568] | 926 | return fmt.Errorf("failed to update user %q: %v", u.Username, err)
|
---|
| 927 | }
|
---|
[572] | 928 | u.User = *record
|
---|
[568] | 929 |
|
---|
[572] | 930 | if realnameUpdated {
|
---|
| 931 | // Re-connect to networks which use the default realname
|
---|
| 932 | var needUpdate []Network
|
---|
| 933 | u.forEachNetwork(func(net *network) {
|
---|
| 934 | if net.Realname == "" {
|
---|
| 935 | needUpdate = append(needUpdate, net.Network)
|
---|
| 936 | }
|
---|
| 937 | })
|
---|
[568] | 938 |
|
---|
[572] | 939 | var netErr error
|
---|
| 940 | for _, net := range needUpdate {
|
---|
[676] | 941 | if _, err := u.updateNetwork(ctx, &net); err != nil {
|
---|
[572] | 942 | netErr = err
|
---|
| 943 | }
|
---|
[568] | 944 | }
|
---|
[572] | 945 | if netErr != nil {
|
---|
| 946 | return netErr
|
---|
| 947 | }
|
---|
[568] | 948 | }
|
---|
| 949 |
|
---|
[572] | 950 | return nil
|
---|
[568] | 951 | }
|
---|
| 952 |
|
---|
[376] | 953 | func (u *user) stop() {
|
---|
| 954 | u.events <- eventStop{}
|
---|
[377] | 955 | <-u.done
|
---|
[376] | 956 | }
|
---|
[489] | 957 |
|
---|
| 958 | func (u *user) hasPersistentMsgStore() bool {
|
---|
| 959 | if u.msgStore == nil {
|
---|
| 960 | return false
|
---|
| 961 | }
|
---|
| 962 | _, isMem := u.msgStore.(*memoryMessageStore)
|
---|
| 963 | return !isMem
|
---|
| 964 | }
|
---|
[705] | 965 |
|
---|
| 966 | // localAddrForHost returns the local address to use when connecting to host.
|
---|
| 967 | // A nil address is returned when the OS should automatically pick one.
|
---|
| 968 | func (u *user) localTCPAddrForHost(host string) (*net.TCPAddr, error) {
|
---|
| 969 | upstreamUserIPs := u.srv.Config().UpstreamUserIPs
|
---|
| 970 | if len(upstreamUserIPs) == 0 {
|
---|
| 971 | return nil, nil
|
---|
| 972 | }
|
---|
| 973 |
|
---|
| 974 | ips, err := net.LookupIP(host)
|
---|
| 975 | if err != nil {
|
---|
| 976 | return nil, err
|
---|
| 977 | }
|
---|
| 978 |
|
---|
| 979 | wantIPv6 := false
|
---|
| 980 | for _, ip := range ips {
|
---|
| 981 | if ip.To4() == nil {
|
---|
| 982 | wantIPv6 = true
|
---|
| 983 | break
|
---|
| 984 | }
|
---|
| 985 | }
|
---|
| 986 |
|
---|
| 987 | var ipNet *net.IPNet
|
---|
| 988 | for _, in := range upstreamUserIPs {
|
---|
| 989 | if wantIPv6 == (in.IP.To4() == nil) {
|
---|
| 990 | ipNet = in
|
---|
| 991 | break
|
---|
| 992 | }
|
---|
| 993 | }
|
---|
| 994 | if ipNet == nil {
|
---|
| 995 | return nil, nil
|
---|
| 996 | }
|
---|
| 997 |
|
---|
| 998 | var ipInt big.Int
|
---|
| 999 | ipInt.SetBytes(ipNet.IP)
|
---|
| 1000 | ipInt.Add(&ipInt, big.NewInt(u.ID+1))
|
---|
| 1001 | ip := net.IP(ipInt.Bytes())
|
---|
| 1002 | if !ipNet.Contains(ip) {
|
---|
| 1003 | return nil, fmt.Errorf("IP network %v too small", ipNet)
|
---|
| 1004 | }
|
---|
| 1005 |
|
---|
| 1006 | return &net.TCPAddr{IP: ip}, nil
|
---|
| 1007 | }
|
---|