[98] | 1 | package soju
|
---|
[1] | 2 |
|
---|
| 3 | import (
|
---|
[652] | 4 | "context"
|
---|
[656] | 5 | "errors"
|
---|
[1] | 6 | "fmt"
|
---|
[656] | 7 | "io"
|
---|
[37] | 8 | "log"
|
---|
[472] | 9 | "mime"
|
---|
[1] | 10 | "net"
|
---|
[323] | 11 | "net/http"
|
---|
[689] | 12 | "runtime/debug"
|
---|
[24] | 13 | "sync"
|
---|
[323] | 14 | "sync/atomic"
|
---|
[67] | 15 | "time"
|
---|
[1] | 16 |
|
---|
[707] | 17 | "github.com/prometheus/client_golang/prometheus"
|
---|
[708] | 18 | "github.com/prometheus/client_golang/prometheus/promauto"
|
---|
[1] | 19 | "gopkg.in/irc.v3"
|
---|
[323] | 20 | "nhooyr.io/websocket"
|
---|
[370] | 21 |
|
---|
| 22 | "git.sr.ht/~emersion/soju/config"
|
---|
[1] | 23 | )
|
---|
| 24 |
|
---|
[67] | 25 | // TODO: make configurable
|
---|
[735] | 26 | var retryConnectMinDelay = time.Minute
|
---|
| 27 | var retryConnectMaxDelay = 10 * time.Minute
|
---|
| 28 | var retryConnectJitter = time.Minute
|
---|
[206] | 29 | var connectTimeout = 15 * time.Second
|
---|
[205] | 30 | var writeTimeout = 10 * time.Second
|
---|
[398] | 31 | var upstreamMessageDelay = 2 * time.Second
|
---|
| 32 | var upstreamMessageBurst = 10
|
---|
[675] | 33 | var backlogTimeout = 10 * time.Second
|
---|
| 34 | var handleDownstreamMessageTimeout = 10 * time.Second
|
---|
[704] | 35 | var downstreamRegisterTimeout = 30 * time.Second
|
---|
[670] | 36 | var chatHistoryLimit = 1000
|
---|
| 37 | var backlogLimit = 4000
|
---|
[67] | 38 |
|
---|
[9] | 39 | type Logger interface {
|
---|
| 40 | Printf(format string, v ...interface{})
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[21] | 43 | type prefixLogger struct {
|
---|
| 44 | logger Logger
|
---|
| 45 | prefix string
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | var _ Logger = (*prefixLogger)(nil)
|
---|
| 49 |
|
---|
| 50 | func (l *prefixLogger) Printf(format string, v ...interface{}) {
|
---|
| 51 | v = append([]interface{}{l.prefix}, v...)
|
---|
| 52 | l.logger.Printf("%v"+format, v...)
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[709] | 55 | type int64Gauge struct {
|
---|
| 56 | v int64 // atomic
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | func (g *int64Gauge) Add(delta int64) {
|
---|
| 60 | atomic.AddInt64(&g.v, delta)
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | func (g *int64Gauge) Value() int64 {
|
---|
| 64 | return atomic.LoadInt64(&g.v)
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | func (g *int64Gauge) Float64() float64 {
|
---|
| 68 | return float64(g.Value())
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[691] | 71 | type Config struct {
|
---|
[612] | 72 | Hostname string
|
---|
[662] | 73 | Title string
|
---|
[612] | 74 | LogPath string
|
---|
| 75 | Debug bool
|
---|
| 76 | HTTPOrigins []string
|
---|
| 77 | AcceptProxyIPs config.IPSet
|
---|
| 78 | MaxUserNetworks int
|
---|
[694] | 79 | MultiUpstream bool
|
---|
[691] | 80 | MOTD string
|
---|
[705] | 81 | UpstreamUserIPs []*net.IPNet
|
---|
[691] | 82 | }
|
---|
[22] | 83 |
|
---|
[691] | 84 | type Server struct {
|
---|
[707] | 85 | Logger Logger
|
---|
| 86 | Identd *Identd // can be nil
|
---|
| 87 | MetricsRegistry prometheus.Registerer // can be nil
|
---|
[691] | 88 |
|
---|
[709] | 89 | config atomic.Value // *Config
|
---|
| 90 | db Database
|
---|
| 91 | stopWG sync.WaitGroup
|
---|
[77] | 92 |
|
---|
[449] | 93 | lock sync.Mutex
|
---|
| 94 | listeners map[net.Listener]struct{}
|
---|
| 95 | users map[string]*user
|
---|
[709] | 96 |
|
---|
| 97 | metrics struct {
|
---|
| 98 | downstreams int64Gauge
|
---|
[710] | 99 | upstreams int64Gauge
|
---|
[711] | 100 |
|
---|
| 101 | upstreamOutMessagesTotal prometheus.Counter
|
---|
| 102 | upstreamInMessagesTotal prometheus.Counter
|
---|
| 103 | downstreamOutMessagesTotal prometheus.Counter
|
---|
| 104 | downstreamInMessagesTotal prometheus.Counter
|
---|
[734] | 105 |
|
---|
| 106 | upstreamConnectErrorsTotal prometheus.Counter
|
---|
[709] | 107 | }
|
---|
[10] | 108 | }
|
---|
| 109 |
|
---|
[531] | 110 | func NewServer(db Database) *Server {
|
---|
[636] | 111 | srv := &Server{
|
---|
[691] | 112 | Logger: log.New(log.Writer(), "", log.LstdFlags),
|
---|
| 113 | db: db,
|
---|
| 114 | listeners: make(map[net.Listener]struct{}),
|
---|
| 115 | users: make(map[string]*user),
|
---|
[37] | 116 | }
|
---|
[694] | 117 | srv.config.Store(&Config{
|
---|
| 118 | Hostname: "localhost",
|
---|
| 119 | MaxUserNetworks: -1,
|
---|
| 120 | MultiUpstream: true,
|
---|
| 121 | })
|
---|
[636] | 122 | return srv
|
---|
[37] | 123 | }
|
---|
| 124 |
|
---|
[5] | 125 | func (s *Server) prefix() *irc.Prefix {
|
---|
[691] | 126 | return &irc.Prefix{Name: s.Config().Hostname}
|
---|
[5] | 127 | }
|
---|
| 128 |
|
---|
[691] | 129 | func (s *Server) Config() *Config {
|
---|
| 130 | return s.config.Load().(*Config)
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | func (s *Server) SetConfig(cfg *Config) {
|
---|
| 134 | s.config.Store(cfg)
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[449] | 137 | func (s *Server) Start() error {
|
---|
[708] | 138 | s.registerMetrics()
|
---|
| 139 |
|
---|
[652] | 140 | users, err := s.db.ListUsers(context.TODO())
|
---|
[77] | 141 | if err != nil {
|
---|
| 142 | return err
|
---|
| 143 | }
|
---|
[71] | 144 |
|
---|
[77] | 145 | s.lock.Lock()
|
---|
[378] | 146 | for i := range users {
|
---|
| 147 | s.addUserLocked(&users[i])
|
---|
[71] | 148 | }
|
---|
[37] | 149 | s.lock.Unlock()
|
---|
| 150 |
|
---|
[449] | 151 | return nil
|
---|
[10] | 152 | }
|
---|
| 153 |
|
---|
[708] | 154 | func (s *Server) registerMetrics() {
|
---|
| 155 | factory := promauto.With(s.MetricsRegistry)
|
---|
| 156 |
|
---|
| 157 | factory.NewGaugeFunc(prometheus.GaugeOpts{
|
---|
| 158 | Name: "soju_users_active",
|
---|
| 159 | Help: "Current number of active users",
|
---|
| 160 | }, func() float64 {
|
---|
| 161 | s.lock.Lock()
|
---|
| 162 | n := len(s.users)
|
---|
| 163 | s.lock.Unlock()
|
---|
| 164 | return float64(n)
|
---|
| 165 | })
|
---|
| 166 |
|
---|
| 167 | factory.NewGaugeFunc(prometheus.GaugeOpts{
|
---|
| 168 | Name: "soju_downstreams_active",
|
---|
| 169 | Help: "Current number of downstream connections",
|
---|
[709] | 170 | }, s.metrics.downstreams.Float64)
|
---|
[710] | 171 |
|
---|
| 172 | factory.NewGaugeFunc(prometheus.GaugeOpts{
|
---|
| 173 | Name: "soju_upstreams_active",
|
---|
| 174 | Help: "Current number of upstream connections",
|
---|
| 175 | }, s.metrics.upstreams.Float64)
|
---|
[711] | 176 |
|
---|
| 177 | s.metrics.upstreamOutMessagesTotal = factory.NewCounter(prometheus.CounterOpts{
|
---|
| 178 | Name: "soju_upstream_out_messages_total",
|
---|
| 179 | Help: "Total number of outgoing messages sent to upstream servers",
|
---|
| 180 | })
|
---|
| 181 |
|
---|
| 182 | s.metrics.upstreamInMessagesTotal = factory.NewCounter(prometheus.CounterOpts{
|
---|
| 183 | Name: "soju_upstream_in_messages_total",
|
---|
| 184 | Help: "Total number of incoming messages received from upstream servers",
|
---|
| 185 | })
|
---|
| 186 |
|
---|
| 187 | s.metrics.downstreamOutMessagesTotal = factory.NewCounter(prometheus.CounterOpts{
|
---|
| 188 | Name: "soju_downstream_out_messages_total",
|
---|
| 189 | Help: "Total number of outgoing messages sent to downstream clients",
|
---|
| 190 | })
|
---|
| 191 |
|
---|
| 192 | s.metrics.downstreamInMessagesTotal = factory.NewCounter(prometheus.CounterOpts{
|
---|
| 193 | Name: "soju_downstream_in_messages_total",
|
---|
| 194 | Help: "Total number of incoming messages received from downstream clients",
|
---|
| 195 | })
|
---|
[734] | 196 |
|
---|
| 197 | s.metrics.upstreamConnectErrorsTotal = factory.NewCounter(prometheus.CounterOpts{
|
---|
| 198 | Name: "soju_upstream_connect_errors_total",
|
---|
| 199 | Help: "Total number of upstream connection errors",
|
---|
| 200 | })
|
---|
[708] | 201 | }
|
---|
| 202 |
|
---|
[449] | 203 | func (s *Server) Shutdown() {
|
---|
| 204 | s.lock.Lock()
|
---|
| 205 | for ln := range s.listeners {
|
---|
| 206 | if err := ln.Close(); err != nil {
|
---|
| 207 | s.Logger.Printf("failed to stop listener: %v", err)
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | for _, u := range s.users {
|
---|
| 211 | u.events <- eventStop{}
|
---|
| 212 | }
|
---|
| 213 | s.lock.Unlock()
|
---|
| 214 |
|
---|
| 215 | s.stopWG.Wait()
|
---|
[599] | 216 |
|
---|
| 217 | if err := s.db.Close(); err != nil {
|
---|
| 218 | s.Logger.Printf("failed to close DB: %v", err)
|
---|
| 219 | }
|
---|
[449] | 220 | }
|
---|
| 221 |
|
---|
[680] | 222 | func (s *Server) createUser(ctx context.Context, user *User) (*user, error) {
|
---|
[329] | 223 | s.lock.Lock()
|
---|
| 224 | defer s.lock.Unlock()
|
---|
| 225 |
|
---|
| 226 | if _, ok := s.users[user.Username]; ok {
|
---|
| 227 | return nil, fmt.Errorf("user %q already exists", user.Username)
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[680] | 230 | err := s.db.StoreUser(ctx, user)
|
---|
[329] | 231 | if err != nil {
|
---|
| 232 | return nil, fmt.Errorf("could not create user in db: %v", err)
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[378] | 235 | return s.addUserLocked(user), nil
|
---|
[329] | 236 | }
|
---|
| 237 |
|
---|
[563] | 238 | func (s *Server) forEachUser(f func(*user)) {
|
---|
| 239 | s.lock.Lock()
|
---|
| 240 | for _, u := range s.users {
|
---|
| 241 | f(u)
|
---|
| 242 | }
|
---|
| 243 | s.lock.Unlock()
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[38] | 246 | func (s *Server) getUser(name string) *user {
|
---|
| 247 | s.lock.Lock()
|
---|
| 248 | u := s.users[name]
|
---|
| 249 | s.lock.Unlock()
|
---|
| 250 | return u
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[378] | 253 | func (s *Server) addUserLocked(user *User) *user {
|
---|
| 254 | s.Logger.Printf("starting bouncer for user %q", user.Username)
|
---|
| 255 | u := newUser(s, user)
|
---|
| 256 | s.users[u.Username] = u
|
---|
| 257 |
|
---|
[449] | 258 | s.stopWG.Add(1)
|
---|
| 259 |
|
---|
[378] | 260 | go func() {
|
---|
[689] | 261 | defer func() {
|
---|
| 262 | if err := recover(); err != nil {
|
---|
| 263 | s.Logger.Printf("panic serving user %q: %v\n%v", user.Username, err, debug.Stack())
|
---|
| 264 | }
|
---|
| 265 | }()
|
---|
| 266 |
|
---|
[378] | 267 | u.run()
|
---|
| 268 |
|
---|
| 269 | s.lock.Lock()
|
---|
| 270 | delete(s.users, u.Username)
|
---|
| 271 | s.lock.Unlock()
|
---|
[449] | 272 |
|
---|
| 273 | s.stopWG.Done()
|
---|
[378] | 274 | }()
|
---|
| 275 |
|
---|
| 276 | return u
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[323] | 279 | var lastDownstreamID uint64 = 0
|
---|
| 280 |
|
---|
[347] | 281 | func (s *Server) handle(ic ircConn) {
|
---|
[689] | 282 | defer func() {
|
---|
| 283 | if err := recover(); err != nil {
|
---|
| 284 | s.Logger.Printf("panic serving downstream %q: %v\n%v", ic.RemoteAddr(), err, debug.Stack())
|
---|
| 285 | }
|
---|
| 286 | }()
|
---|
| 287 |
|
---|
[709] | 288 | s.metrics.downstreams.Add(1)
|
---|
[323] | 289 | id := atomic.AddUint64(&lastDownstreamID, 1)
|
---|
[347] | 290 | dc := newDownstreamConn(s, ic, id)
|
---|
[323] | 291 | if err := dc.runUntilRegistered(); err != nil {
|
---|
[655] | 292 | if !errors.Is(err, io.EOF) {
|
---|
[746] | 293 | dc.logger.Printf("%v", err)
|
---|
[655] | 294 | }
|
---|
[323] | 295 | } else {
|
---|
| 296 | dc.user.events <- eventDownstreamConnected{dc}
|
---|
| 297 | if err := dc.readMessages(dc.user.events); err != nil {
|
---|
[746] | 298 | dc.logger.Printf("%v", err)
|
---|
[323] | 299 | }
|
---|
| 300 | dc.user.events <- eventDownstreamDisconnected{dc}
|
---|
| 301 | }
|
---|
| 302 | dc.Close()
|
---|
[709] | 303 | s.metrics.downstreams.Add(-1)
|
---|
[323] | 304 | }
|
---|
| 305 |
|
---|
[3] | 306 | func (s *Server) Serve(ln net.Listener) error {
|
---|
[449] | 307 | s.lock.Lock()
|
---|
| 308 | s.listeners[ln] = struct{}{}
|
---|
| 309 | s.lock.Unlock()
|
---|
| 310 |
|
---|
| 311 | s.stopWG.Add(1)
|
---|
| 312 |
|
---|
| 313 | defer func() {
|
---|
| 314 | s.lock.Lock()
|
---|
| 315 | delete(s.listeners, ln)
|
---|
| 316 | s.lock.Unlock()
|
---|
| 317 |
|
---|
| 318 | s.stopWG.Done()
|
---|
| 319 | }()
|
---|
| 320 |
|
---|
[1] | 321 | for {
|
---|
[323] | 322 | conn, err := ln.Accept()
|
---|
[601] | 323 | if isErrClosed(err) {
|
---|
[449] | 324 | return nil
|
---|
| 325 | } else if err != nil {
|
---|
[1] | 326 | return fmt.Errorf("failed to accept connection: %v", err)
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[347] | 329 | go s.handle(newNetIRCConn(conn))
|
---|
[1] | 330 | }
|
---|
| 331 | }
|
---|
[323] | 332 |
|
---|
| 333 | func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
---|
| 334 | conn, err := websocket.Accept(w, req, &websocket.AcceptOptions{
|
---|
[597] | 335 | Subprotocols: []string{"text.ircv3.net"}, // non-compliant, fight me
|
---|
[691] | 336 | OriginPatterns: s.Config().HTTPOrigins,
|
---|
[323] | 337 | })
|
---|
| 338 | if err != nil {
|
---|
| 339 | s.Logger.Printf("failed to serve HTTP connection: %v", err)
|
---|
| 340 | return
|
---|
| 341 | }
|
---|
[345] | 342 |
|
---|
[370] | 343 | isProxy := false
|
---|
[345] | 344 | if host, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
|
---|
| 345 | if ip := net.ParseIP(host); ip != nil {
|
---|
[691] | 346 | isProxy = s.Config().AcceptProxyIPs.Contains(ip)
|
---|
[345] | 347 | }
|
---|
| 348 | }
|
---|
| 349 |
|
---|
[474] | 350 | // Only trust the Forwarded header field if this is a trusted proxy IP
|
---|
[345] | 351 | // to prevent users from spoofing the remote address
|
---|
[344] | 352 | remoteAddr := req.RemoteAddr
|
---|
[472] | 353 | if isProxy {
|
---|
| 354 | forwarded := parseForwarded(req.Header)
|
---|
[473] | 355 | if forwarded["for"] != "" {
|
---|
| 356 | remoteAddr = forwarded["for"]
|
---|
[472] | 357 | }
|
---|
[344] | 358 | }
|
---|
[345] | 359 |
|
---|
[347] | 360 | s.handle(newWebsocketIRCConn(conn, remoteAddr))
|
---|
[323] | 361 | }
|
---|
[472] | 362 |
|
---|
| 363 | func parseForwarded(h http.Header) map[string]string {
|
---|
| 364 | forwarded := h.Get("Forwarded")
|
---|
| 365 | if forwarded == "" {
|
---|
[474] | 366 | return map[string]string{
|
---|
| 367 | "for": h.Get("X-Forwarded-For"),
|
---|
| 368 | "proto": h.Get("X-Forwarded-Proto"),
|
---|
| 369 | "host": h.Get("X-Forwarded-Host"),
|
---|
| 370 | }
|
---|
[472] | 371 | }
|
---|
| 372 | // Hack to easily parse header parameters
|
---|
| 373 | _, params, _ := mime.ParseMediaType("hack; " + forwarded)
|
---|
| 374 | return params
|
---|
| 375 | }
|
---|
[605] | 376 |
|
---|
| 377 | type ServerStats struct {
|
---|
| 378 | Users int
|
---|
| 379 | Downstreams int64
|
---|
[710] | 380 | Upstreams int64
|
---|
[605] | 381 | }
|
---|
| 382 |
|
---|
| 383 | func (s *Server) Stats() *ServerStats {
|
---|
| 384 | var stats ServerStats
|
---|
| 385 | s.lock.Lock()
|
---|
| 386 | stats.Users = len(s.users)
|
---|
| 387 | s.lock.Unlock()
|
---|
[709] | 388 | stats.Downstreams = s.metrics.downstreams.Value()
|
---|
[710] | 389 | stats.Upstreams = s.metrics.upstreams.Value()
|
---|
[605] | 390 | return &stats
|
---|
| 391 | }
|
---|