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