Commit Graph

128 Commits

Author SHA1 Message Date
Simon Ser
aa407a46e6 Get rid of io.EOF errors in logs
Closes: https://todo.sr.ht/~emersion/soju/150
2021-10-29 16:03:04 +02:00
Simon Ser
9ec1f1a5b0 Add context args to Database interface
This is a mecanical change, which just lifts up the context.TODO()
calls from inside the DB implementations to the callers.

Future work involves properly wiring up the contexts when it makes
sense.
2021-10-18 19:15:15 +02:00
Simon Ser
a9a066faac Add bouncer MOTD
Closes: https://todo.sr.ht/~emersion/soju/137
2021-10-13 10:58:34 +02:00
Simon Ser
94dbfff11d Add max-user-networks config option 2021-10-07 20:43:10 +02:00
Simon Ser
f93616fb41 Add "server status" command
Right now, it prints the number of active users and number of
downstream connections.
2021-10-05 19:13:53 +02:00
Simon Ser
1626ffb97b Use isErrClosed in Server.Serve 2021-10-05 14:02:59 +02:00
Simon Ser
410ba47277 Close DB on shutdown 2021-10-05 11:53:38 +02:00
Simon Ser
5bedcd24e2 Add support for IRCv3 WebSocket text subprotocol
Technically we aren't spec-compliant since we don't serve
binary.ircv3.net. Any complaints will be redirected to /dev/null.
2021-09-28 21:25:06 +02:00
Simon Ser
d7b1c5a9a2 Allow admins to broadcast message to all bouncer users
Typically done via:

    /notice $<bouncer> <message>

Or, for a connection not bound to a specific network:

    /notice $* <message>

The message is broadcast as BouncerServ, because that's the only
user that can be trusted to belong to the bouncer by users. Any
other prefix would conflict with the upstream network.
2021-06-23 19:23:09 +02:00
Drew DeVault
61b68d6dfb db: refactor into interface
This refactors the SQLite-specific bits into db_sqlite.go. A future
patch will add PostgreSQL support.
2021-05-25 16:35:39 +02:00
Simon Ser
927ee80da1 Stop reading X-Forwarded-Port
X-Forwarded-Port contains the destination port, not the source port,
so it isn't useful for our purposes.

Move parsing of X-Forwarded-* header fields to parseForwarded.
2021-03-18 13:28:46 +01:00
Simon Ser
1b49fff763 Fix Forwarded HTTP header handling
"for" contains the port, if any. "port" doesn't exist.
2021-03-18 13:21:38 +01:00
Simon Ser
9046fda283 Add support for the Forwarded HTTP header
This is the standard replacing X-Forwarded-*.
2021-03-18 12:08:25 +01:00
Simon Ser
5b7205c9c1 Drop "irc" WebSocket subprotocol
The subprotocol hasn't been standardized yet. It looks like the standard
is moving in another direction.
2021-03-18 12:02:36 +01:00
Simon Ser
08b1010939 Add support for graceful shutdown
Closes: https://todo.sr.ht/~emersion/soju/45
2021-02-09 17:34:46 +01:00
Simon Ser
92fece5cd4
Nuke in-memory ring buffer
Instead, always read chat history from logs. Unify the implicit chat
history (pushing history to clients) and explicit chat history
(via the CHATHISTORY command).

Instead of keeping track of ring buffer cursors for each client, use
message IDs.

If necessary, the ring buffer could be re-introduced behind a
common MessageStore interface (could be useful when on-disk logs are
disabled).

References: https://todo.sr.ht/~emersion/soju/80
2020-08-20 20:05:01 +02:00
Simon Ser
bdb132ad98
Implement rate limiting for upstream messages
Allow up to 10 outgoing messages in a burst, then throttle to 1 message
each 2 seconds.

Closes: https://todo.sr.ht/~emersion/soju/87
2020-08-19 19:42:33 +02:00
Simon Ser
65302d3c1e
Add an ident server
Closes: https://todo.sr.ht/~emersion/soju/69
2020-08-11 10:59:06 +02:00
Simon Ser
6598fcf36e
Remove user from Server map when stopped 2020-08-10 15:03:38 +02:00
Simon Ser
ef2dd479bf
Add accept-proxy-ip config directive
This allows to set the list of IPs allowed to act as a proxy. This is
only used for WebSockets right now, but will be expanded to TCP as well
once the PROXY protocol is supported.
2020-07-22 17:03:01 +02:00
Simon Ser
82990fb774
Accept "irc" WebSocket subprotocol 2020-07-02 11:05:49 +02:00
Simon Ser
3397965dea
Add RemoteAddr to ircConn interface 2020-07-01 17:02:37 +02:00
Simon Ser
a9887114d5
Only read X-Forwarded-* if remote address is loopback 2020-06-29 18:33:23 +02:00
Simon Ser
2c172fa8ca
Extract X-Forwarded-* headers for WebSocket connections 2020-06-29 18:27:43 +02:00
delthas
5be25711c7 Add support for the user create admin service command
This adds support for user create, a new service command only accessible
to admin users. This lets users create other users on the fly and makes
soju start the user routine immediately; unlike sojuctl which currently
requires closing soju, creating the user, and starting soju again.
2020-06-08 22:30:09 +02:00
Simon Ser
d0cf1d2882
Add support for WebSocket connections
WebSocket connections allow web-based clients to connect to IRC. This
commit implements the WebSocket sub-protocol as specified by the pending
IRCv3 proposal [1].

WebSocket listeners can now be set up via a "wss" protocol in the
`listen` directive. The new `http-origin` directive allows the CORS
allowed origins to be configured.

[1]: https://github.com/ircv3/ircv3-specifications/pull/342
2020-06-07 14:13:46 +02:00
delthas
f7894e612b Add support for downstream CHATHISTORY
This adds support for the WIP (at the time of this commit)
draft/chathistory extension, based on the draft at [1] and the
additional comments at [2].

This gets the history by parsing the chat logs, and is therefore only
enabled when the logs are enabled and the log path is configured.

Getting the history only from the logs adds some restrictions:
- we cannot get history by msgid (those are not logged)
- we cannot get the users masks (maybe they could be inferred from the
  JOIN etc, but it is not worth the effort and would not work every
  time)

The regular soju network history is not sent to clients that support
draft/chathistory, so that they can fetch what they need by manually
calling CHATHISTORY.

The only supported command is BEFORE for now, because that is the only
required command for an app that offers an "infinite history scrollback"
feature.

Regarding implementation, rather than reading the file from the end in
reverse, we simply start from the beginning of each log file, store each
PRIVMSG into a ring, then add the last lines of that ring into the
history we'll return later. The message parsing implementation must be
kept somewhat fast because an app could potentially request thousands of
messages in several files. Here we are using simple sscanf and indexOf
rather than regexps.

In case some log files do not contain any message (for example because
the user had not joined a channel at that time), we try up to a 100 days
of empty log files before giving up.

[1]: https://github.com/prawnsalad/ircv3-specifications/pull/3/files
[2]: https://github.com/ircv3/ircv3-specifications/pull/393/files#r350210018
2020-06-05 23:50:31 +02:00
Simon Ser
754adc36fb
Remove keepAlivePeriod
This is a remnant of setKeepAlive.

Fixes: 77faf72fa3 ("Remove setKeepAlive")
2020-06-04 18:38:04 +02:00
Simon Ser
2a0696b6bb
Introduce conn for common connection logic
This centralizes the common upstream & downstream bits.
2020-04-03 16:35:08 +02:00
Simon Ser
bca0b2ad76
Set connect timeout
References: https://todo.sr.ht/~emersion/soju/26
2020-04-01 16:41:17 +02:00
Simon Ser
29f2e93ab7
Set write deadlines
References: https://todo.sr.ht/~emersion/soju/26
2020-04-01 16:27:53 +02:00
delthas
0607b940e2 Add support for bouncer logs
Add bouncer logs, in a network/channel/date.log format, in a similar
manner to ZNC log module. PRIVMSG, JOIN, PART, QUIT, MODE are logged.

Add a config directive for the logs file, including a way to disable
them entirely.
2020-03-28 00:07:20 +01:00
Simon Ser
551d41335e
Get rid of Server.downstreamConns
This is unused right now. Let's remove it, we'll add it back if we
really need it.
2020-03-27 22:24:12 +01:00
Simon Ser
c0f5850e5b
Add eventDownstreamDisconnected
This should remove the need for protecting user.downstreamConns with a
mutex.
2020-03-27 17:55:03 +01:00
Simon Ser
36ab6ece09
Add eventDownstreamConnected
In a later commit, we'll be able to move part of downstreamConn.register
into the user goroutine to prevent races.

References: https://todo.sr.ht/~emersion/soju/22
2020-03-27 17:21:05 +01:00
Simon Ser
474f2889d9
Introduce a user.events channel
This allows to easily add new events, and also guarantees ordering
between different event types.
2020-03-27 16:33:19 +01:00
delthas
d0917f0fa1
Add a server-unique id to each downstream
Adding a simple uint64 id to each downstream is preparatory work
for labeled-responses tags targeting a specific downstream.
2020-03-25 23:17:46 +01:00
Simon Ser
3919ee2036
Per-user dispatcher goroutine
This allows message handlers to read upstream/downstream connection
information without causing any race condition.

References: https://todo.sr.ht/~emersion/soju/1
2020-03-16 12:44:59 +01:00
Simon Ser
7fe0986859
Split user logic into its own file 2020-03-16 11:18:41 +01:00
Simon Ser
f3940117d1
Rename project to soju 2020-03-13 18:13:03 +01:00
Simon Ser
85f28daf2d
Auto-save IRC networks 2020-03-12 21:28:09 +01:00
Simon Ser
0ef08dfbb5
Store NICK changes in the DB 2020-03-12 19:17:06 +01:00
Simon Ser
9db953c7e5
go fmt 2020-03-04 19:23:24 +01:00
Simon Ser
84fe3ae255
Add SQLite database
Closes: https://todo.sr.ht/~emersion/jounce/9
2020-03-04 18:22:58 +01:00
Simon Ser
03c546e8bf
Remove unused user.getChannel, move getUpstream to user 2020-03-04 16:00:19 +01:00
Simon Ser
c22ce793a1
Allow clients to specify an upstream name in their username 2020-03-04 15:44:13 +01:00
Simon Ser
c366b5320c
Retry connecting to upstream servers
Rate-limit retries in case connecting immediately fails.
2020-03-03 15:26:19 +01:00
Simon Ser
1141698a92
Enable TCP keep-alive on all connections 2020-02-18 17:26:17 +01:00
Simon Ser
8997a70acb
Don't leave connections half-opened 2020-02-18 16:54:06 +01:00
Simon Ser
286fb4b18c
Add a -debug flag 2020-02-18 16:31:18 +01:00
Simon Ser
9a93c56cdf
Fix issues related to Ring
- RingConsumer is now used directly in the goroutine responsible for
  writing downstream messages. This allows the ring buffer not to be
  consumed on write error.
- RingConsumer now has a channel attached. This allows PRIVMSG messages
  to always use RingConsumer, instead of also directly pushing messages
  to all downstream connections.
- Multiple clients with the same history name are now supported.
- Ring is now protected by a mutex
2020-02-17 15:46:29 +01:00
Simon Ser
fad9d820c1
Add an in-memory ring buffer
References: https://todo.sr.ht/~emersion/jounce/2
2020-02-07 16:43:54 +01:00
Simon Ser
69a35069ef
Handle downstream PART messages 2020-02-07 13:36:32 +01:00
Simon Ser
4de405d3b2
Handle downstream MODE messages 2020-02-07 13:08:27 +01:00
Simon Ser
e17c0b3aca
Add upstreamConn.register 2020-02-07 12:37:44 +01:00
Simon Ser
50fc19c92f
Skip unregistered and closed upstream connections 2020-02-07 12:02:19 +01:00
Simon Ser
636ede13da
Add user.forEachDownstream 2020-02-07 11:56:36 +01:00
Simon Ser
059a799d16
Add user.forEachUpstream 2020-02-07 11:46:44 +01:00
Simon Ser
3586ca3d26
Add Server.getUser 2020-02-07 11:39:56 +01:00
Simon Ser
3b2bb58c60
Per-user connections 2020-02-07 11:36:42 +01:00
Simon Ser
3b0639bacc
Keep track of upstream connections in a list 2020-02-06 22:25:32 +01:00
Simon Ser
5988d10a0b
Remove downstream conn from list on disconnect 2020-02-06 21:30:44 +01:00
Simon Ser
36c404c50c
Allow Server to have access to upstreamConn 2020-02-06 21:20:22 +01:00
Simon Ser
8bbba42aef
Maintain a list of downstream connections 2020-02-06 21:11:35 +01:00
Simon Ser
726d7cb54b
Add per-upstream logger 2020-02-06 20:26:03 +01:00
Simon Ser
ae7f162883
Join channels on upstream servers 2020-02-06 19:22:04 +01:00
Simon Ser
b5f3bad588
Split downstram and upstream code into separate files 2020-02-06 16:18:19 +01:00
Simon Ser
a2d9a64bed
Log upstream server errors 2020-02-06 16:13:29 +01:00
Simon Ser
c14e26769f
Add basic upstream message handler 2020-02-06 16:11:28 +01:00
Simon Ser
56d793543e
Connect to upstream servers 2020-02-06 16:03:07 +01:00
Simon Ser
06cd1ce44f
Add Server.Logger 2020-02-06 15:50:46 +01:00
Simon Ser
003ce38bf8
Rename conn to downstreamConn 2020-02-06 15:22:26 +01:00
Simon Ser
3ccc1bb4e8
Handle PING 2020-02-06 12:18:37 +01:00
Simon Ser
f8a03a25df
Send ERR_NOMOTD on registration 2020-02-06 12:08:54 +01:00
Simon Ser
5547eb7290
Add connection registration 2020-02-04 18:56:07 +01:00
Simon Ser
23ca41b435
Handle NICK and USER 2020-02-04 12:19:18 +01:00
Simon Ser
6d4581a6d0
Send "unknown command" replies 2020-02-04 11:25:53 +01:00
Simon Ser
8e31fde0ab
Add basic IRC listener 2020-02-04 10:46:22 +01:00