2020-04-03 14:34:11 +00:00
|
|
|
package soju
|
|
|
|
|
|
|
|
import (
|
2020-04-23 20:25:43 +00:00
|
|
|
"context"
|
2020-04-03 14:34:11 +00:00
|
|
|
"fmt"
|
2020-06-29 08:24:41 +00:00
|
|
|
"io"
|
2020-04-03 14:34:11 +00:00
|
|
|
"net"
|
2020-09-02 15:06:17 +00:00
|
|
|
"strings"
|
2020-04-30 08:35:02 +00:00
|
|
|
"sync"
|
2020-04-03 14:34:11 +00:00
|
|
|
"time"
|
2020-09-02 15:06:17 +00:00
|
|
|
"unicode"
|
2020-04-03 14:34:11 +00:00
|
|
|
|
|
|
|
"gopkg.in/irc.v3"
|
2020-04-23 20:25:43 +00:00
|
|
|
"nhooyr.io/websocket"
|
2020-04-03 14:34:11 +00:00
|
|
|
)
|
|
|
|
|
2020-06-04 15:23:27 +00:00
|
|
|
// ircConn is a generic IRC connection. It's similar to net.Conn but focuses on
|
|
|
|
// reading and writing IRC messages.
|
|
|
|
type ircConn interface {
|
|
|
|
ReadMessage() (*irc.Message, error)
|
|
|
|
WriteMessage(*irc.Message) error
|
|
|
|
Close() error
|
|
|
|
SetReadDeadline(time.Time) error
|
2020-04-23 20:25:43 +00:00
|
|
|
SetWriteDeadline(time.Time) error
|
2020-07-01 15:02:37 +00:00
|
|
|
RemoteAddr() net.Addr
|
2020-08-11 08:34:38 +00:00
|
|
|
LocalAddr() net.Addr
|
2020-06-04 15:23:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 20:25:43 +00:00
|
|
|
func newNetIRCConn(c net.Conn) ircConn {
|
2020-06-04 15:23:27 +00:00
|
|
|
type netConn net.Conn
|
|
|
|
return struct {
|
|
|
|
*irc.Conn
|
|
|
|
netConn
|
|
|
|
}{irc.NewConn(c), c}
|
|
|
|
}
|
|
|
|
|
2020-04-23 20:25:43 +00:00
|
|
|
type websocketIRCConn struct {
|
|
|
|
conn *websocket.Conn
|
|
|
|
readDeadline, writeDeadline time.Time
|
2020-07-01 15:02:37 +00:00
|
|
|
remoteAddr string
|
2020-04-23 20:25:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 15:02:37 +00:00
|
|
|
func newWebsocketIRCConn(c *websocket.Conn, remoteAddr string) ircConn {
|
2021-03-16 08:17:31 +00:00
|
|
|
return &websocketIRCConn{conn: c, remoteAddr: remoteAddr}
|
2020-04-23 20:25:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 08:17:31 +00:00
|
|
|
func (wic *websocketIRCConn) ReadMessage() (*irc.Message, error) {
|
2020-04-23 20:25:43 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
if !wic.readDeadline.IsZero() {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithDeadline(ctx, wic.readDeadline)
|
|
|
|
defer cancel()
|
|
|
|
}
|
|
|
|
_, b, err := wic.conn.Read(ctx)
|
|
|
|
if err != nil {
|
2020-06-29 08:24:41 +00:00
|
|
|
switch websocket.CloseStatus(err) {
|
|
|
|
case websocket.StatusNormalClosure, websocket.StatusGoingAway:
|
|
|
|
return nil, io.EOF
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-23 20:25:43 +00:00
|
|
|
}
|
|
|
|
return irc.ParseMessage(string(b))
|
|
|
|
}
|
|
|
|
|
2021-03-16 08:17:31 +00:00
|
|
|
func (wic *websocketIRCConn) WriteMessage(msg *irc.Message) error {
|
2020-09-02 15:06:17 +00:00
|
|
|
b := []byte(strings.ToValidUTF8(msg.String(), string(unicode.ReplacementChar)))
|
2020-04-23 20:25:43 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
if !wic.writeDeadline.IsZero() {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithDeadline(ctx, wic.writeDeadline)
|
|
|
|
defer cancel()
|
|
|
|
}
|
|
|
|
return wic.conn.Write(ctx, websocket.MessageText, b)
|
|
|
|
}
|
|
|
|
|
2021-03-16 08:17:31 +00:00
|
|
|
func (wic *websocketIRCConn) Close() error {
|
2020-04-23 20:25:43 +00:00
|
|
|
return wic.conn.Close(websocket.StatusNormalClosure, "")
|
|
|
|
}
|
|
|
|
|
2021-03-16 08:17:31 +00:00
|
|
|
func (wic *websocketIRCConn) SetReadDeadline(t time.Time) error {
|
2020-04-23 20:25:43 +00:00
|
|
|
wic.readDeadline = t
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-16 08:17:31 +00:00
|
|
|
func (wic *websocketIRCConn) SetWriteDeadline(t time.Time) error {
|
2020-04-23 20:25:43 +00:00
|
|
|
wic.writeDeadline = t
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-16 08:17:31 +00:00
|
|
|
func (wic *websocketIRCConn) RemoteAddr() net.Addr {
|
2020-07-01 15:02:37 +00:00
|
|
|
return websocketAddr(wic.remoteAddr)
|
|
|
|
}
|
|
|
|
|
2021-03-16 08:17:31 +00:00
|
|
|
func (wic *websocketIRCConn) LocalAddr() net.Addr {
|
2020-08-11 08:34:38 +00:00
|
|
|
// Behind a reverse HTTP proxy, we don't have access to the real listening
|
|
|
|
// address
|
|
|
|
return websocketAddr("")
|
|
|
|
}
|
|
|
|
|
2020-07-01 15:02:37 +00:00
|
|
|
type websocketAddr string
|
|
|
|
|
|
|
|
func (websocketAddr) Network() string {
|
|
|
|
return "ws"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wa websocketAddr) String() string {
|
|
|
|
return string(wa)
|
|
|
|
}
|
|
|
|
|
2020-08-19 17:28:29 +00:00
|
|
|
type rateLimiter struct {
|
2020-08-20 07:13:56 +00:00
|
|
|
C <-chan struct{}
|
|
|
|
ticker *time.Ticker
|
2020-08-19 17:28:29 +00:00
|
|
|
stopped chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRateLimiter(delay time.Duration, burst int) *rateLimiter {
|
|
|
|
ch := make(chan struct{}, burst)
|
|
|
|
for i := 0; i < burst; i++ {
|
|
|
|
ch <- struct{}{}
|
|
|
|
}
|
|
|
|
ticker := time.NewTicker(delay)
|
|
|
|
stopped := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
select {
|
|
|
|
case ch <- struct{}{}:
|
|
|
|
// This space is intentionally left blank
|
|
|
|
case <-stopped:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-stopped:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return &rateLimiter{
|
2020-08-20 07:13:56 +00:00
|
|
|
C: ch,
|
|
|
|
ticker: ticker,
|
2020-08-19 17:28:29 +00:00
|
|
|
stopped: stopped,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rl *rateLimiter) Stop() {
|
|
|
|
rl.ticker.Stop()
|
|
|
|
close(rl.stopped)
|
|
|
|
}
|
|
|
|
|
|
|
|
type connOptions struct {
|
2020-08-20 07:13:56 +00:00
|
|
|
Logger Logger
|
2020-08-19 17:28:29 +00:00
|
|
|
RateLimitDelay time.Duration
|
|
|
|
RateLimitBurst int
|
|
|
|
}
|
|
|
|
|
2020-04-03 14:34:11 +00:00
|
|
|
type conn struct {
|
2020-06-04 15:23:27 +00:00
|
|
|
conn ircConn
|
2020-04-30 08:35:02 +00:00
|
|
|
srv *Server
|
|
|
|
logger Logger
|
|
|
|
|
|
|
|
lock sync.Mutex
|
2020-04-03 14:34:11 +00:00
|
|
|
outgoing chan<- *irc.Message
|
2020-04-30 08:35:02 +00:00
|
|
|
closed bool
|
2020-04-03 14:34:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 17:28:29 +00:00
|
|
|
func newConn(srv *Server, ic ircConn, options *connOptions) *conn {
|
2020-04-03 14:34:11 +00:00
|
|
|
outgoing := make(chan *irc.Message, 64)
|
|
|
|
c := &conn{
|
2020-06-04 15:23:27 +00:00
|
|
|
conn: ic,
|
2020-04-03 14:34:11 +00:00
|
|
|
srv: srv,
|
|
|
|
outgoing: outgoing,
|
2020-08-19 17:28:29 +00:00
|
|
|
logger: options.Logger,
|
2020-04-03 14:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2020-08-19 17:28:29 +00:00
|
|
|
var rl *rateLimiter
|
|
|
|
if options.RateLimitDelay > 0 && options.RateLimitBurst > 0 {
|
|
|
|
rl = newRateLimiter(options.RateLimitDelay, options.RateLimitBurst)
|
|
|
|
defer rl.Stop()
|
|
|
|
}
|
|
|
|
|
2020-04-03 14:34:11 +00:00
|
|
|
for msg := range outgoing {
|
2020-08-19 17:28:29 +00:00
|
|
|
if rl != nil {
|
|
|
|
<-rl.C
|
|
|
|
}
|
|
|
|
|
2020-04-03 14:34:11 +00:00
|
|
|
if c.srv.Debug {
|
|
|
|
c.logger.Printf("sent: %v", msg)
|
|
|
|
}
|
2020-06-04 15:23:27 +00:00
|
|
|
c.conn.SetWriteDeadline(time.Now().Add(writeTimeout))
|
|
|
|
if err := c.conn.WriteMessage(msg); err != nil {
|
2020-04-03 14:34:11 +00:00
|
|
|
c.logger.Printf("failed to write message: %v", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-06-04 15:23:27 +00:00
|
|
|
if err := c.conn.Close(); err != nil {
|
2020-04-03 14:34:11 +00:00
|
|
|
c.logger.Printf("failed to close connection: %v", err)
|
|
|
|
} else {
|
|
|
|
c.logger.Printf("connection closed")
|
|
|
|
}
|
|
|
|
// Drain the outgoing channel to prevent SendMessage from blocking
|
|
|
|
for range outgoing {
|
|
|
|
// This space is intentionally left blank
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
c.logger.Printf("new connection")
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) isClosed() bool {
|
2020-04-30 08:35:02 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
return c.closed
|
2020-04-03 14:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the connection. It is safe to call from any goroutine.
|
|
|
|
func (c *conn) Close() error {
|
2020-04-30 08:35:02 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
|
|
|
if c.closed {
|
2020-04-03 14:34:11 +00:00
|
|
|
return fmt.Errorf("connection already closed")
|
|
|
|
}
|
2020-04-30 08:35:02 +00:00
|
|
|
|
2020-06-04 15:23:27 +00:00
|
|
|
err := c.conn.Close()
|
2020-04-30 08:35:02 +00:00
|
|
|
c.closed = true
|
2020-04-03 14:34:11 +00:00
|
|
|
close(c.outgoing)
|
2020-06-04 10:18:51 +00:00
|
|
|
return err
|
2020-04-03 14:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) ReadMessage() (*irc.Message, error) {
|
2020-06-04 15:23:27 +00:00
|
|
|
msg, err := c.conn.ReadMessage()
|
2020-04-03 14:34:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.srv.Debug {
|
|
|
|
c.logger.Printf("received: %v", msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendMessage queues a new outgoing message. It is safe to call from any
|
|
|
|
// goroutine.
|
2020-04-30 08:35:02 +00:00
|
|
|
//
|
|
|
|
// If the connection is closed before the message is sent, SendMessage silently
|
|
|
|
// drops the message.
|
2020-04-03 14:34:11 +00:00
|
|
|
func (c *conn) SendMessage(msg *irc.Message) {
|
2020-04-30 08:35:02 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
|
|
|
if c.closed {
|
2020-04-03 14:34:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.outgoing <- msg
|
|
|
|
}
|
2020-08-11 08:35:05 +00:00
|
|
|
|
|
|
|
func (c *conn) RemoteAddr() net.Addr {
|
|
|
|
return c.conn.RemoteAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) LocalAddr() net.Addr {
|
|
|
|
return c.conn.LocalAddr()
|
|
|
|
}
|