1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
package soju
import (
"context"
"errors"
"io"
"net"
"strings"
"sync/atomic"
"time"
"unicode"
"github.com/coder/websocket"
"golang.org/x/time/rate"
"gopkg.in/irc.v4"
)
// 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
SetWriteDeadline(time.Time) error
RemoteAddr() net.Addr
LocalAddr() net.Addr
}
func newNetIRCConn(c net.Conn) ircConn {
type netConn net.Conn
return struct {
*irc.Conn
netConn
}{irc.NewConn(c), c}
}
type websocketIRCConn struct {
conn *websocket.Conn
readDeadline, writeDeadline time.Time
remoteAddr string
}
func newWebsocketIRCConn(c *websocket.Conn, remoteAddr string) ircConn {
return &websocketIRCConn{conn: c, remoteAddr: remoteAddr}
}
func (wic *websocketIRCConn) ReadMessage() (*irc.Message, error) {
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 {
switch websocket.CloseStatus(err) {
case websocket.StatusNormalClosure, websocket.StatusGoingAway:
return nil, io.EOF
default:
return nil, err
}
}
return irc.ParseMessage(string(b))
}
func (wic *websocketIRCConn) WriteMessage(msg *irc.Message) error {
b := []byte(strings.ToValidUTF8(msg.String(), string(unicode.ReplacementChar)))
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)
}
func (wic *websocketIRCConn) Close() error {
return wic.conn.Close(websocket.StatusNormalClosure, "")
}
func (wic *websocketIRCConn) SetReadDeadline(t time.Time) error {
wic.readDeadline = t
return nil
}
func (wic *websocketIRCConn) SetWriteDeadline(t time.Time) error {
wic.writeDeadline = t
return nil
}
func (wic *websocketIRCConn) RemoteAddr() net.Addr {
return websocketAddr(wic.remoteAddr)
}
func (wic *websocketIRCConn) LocalAddr() net.Addr {
// Behind a reverse HTTP proxy, we don't have access to the real listening
// address
return websocketAddr("")
}
type websocketAddr string
func (websocketAddr) Network() string {
return "ws"
}
func (wa websocketAddr) String() string {
return string(wa)
}
type connOptions struct {
Logger Logger
RateLimitDelay time.Duration
RateLimitBurst int
}
type conn struct {
conn ircConn
srv *Server
logger Logger
closed atomic.Bool
outgoingCh chan<- *irc.Message
closedCh chan struct{}
rateLimit bool
}
func newConn(srv *Server, ic ircConn, options *connOptions) *conn {
outgoingCh := make(chan *irc.Message, 64)
c := &conn{
conn: ic,
srv: srv,
logger: options.Logger,
outgoingCh: outgoingCh,
closedCh: make(chan struct{}),
rateLimit: true,
}
go func() {
ctx, cancel := c.NewContext(context.Background())
defer cancel()
rl := rate.NewLimiter(rate.Every(options.RateLimitDelay), options.RateLimitBurst)
for {
var msg *irc.Message
select {
case msg = <-outgoingCh:
case <-c.closedCh:
}
if msg == nil {
break
}
if c.rateLimit {
if err := rl.Wait(ctx); err != nil {
break
}
}
c.logger.Debugf("sent: %v", msg)
c.conn.SetWriteDeadline(time.Now().Add(writeTimeout))
if err := c.conn.WriteMessage(msg); err != nil {
c.logger.Printf("failed to write message: %v", err)
break
}
}
if err := c.Close(); err != nil && !errors.Is(err, net.ErrClosed) {
c.logger.Printf("failed to close connection: %v", err)
} else {
c.logger.Debugf("connection closed")
}
}()
c.logger.Debugf("new connection")
return c
}
func (c *conn) isClosed() bool {
return c.closed.Load()
}
// Close closes the connection. It is safe to call from any goroutine.
func (c *conn) Close() error {
if c.closed.Swap(true) {
return net.ErrClosed
}
err := c.conn.Close()
close(c.closedCh)
return err
}
// Read reads an incoming message. It must be called from a single goroutine
// at a time.
//
// io.EOF is returned when there are no more messages to read.
func (c *conn) ReadMessage() (*irc.Message, error) {
msg, err := c.conn.ReadMessage()
if errors.Is(err, net.ErrClosed) {
return nil, io.EOF
} else if err != nil {
return nil, err
}
c.logger.Debugf("received: %v", msg)
return msg, nil
}
// SendMessage queues a new outgoing message. It is safe to call from any
// goroutine.
//
// If the connection is closed before the message is sent, SendMessage silently
// drops the message.
func (c *conn) SendMessage(ctx context.Context, msg *irc.Message) {
if c.closed.Load() {
return
}
select {
case c.outgoingCh <- msg:
// Success
case <-c.closedCh:
// Ignore
case <-ctx.Done():
c.logger.Printf("failed to send message: %v", ctx.Err())
}
}
// Shutdown gracefully closes the connection, flushing any pending message.
func (c *conn) Shutdown(ctx context.Context) {
if c.closed.Load() {
return
}
select {
case c.outgoingCh <- nil:
// Success
case <-c.closedCh:
// Ignore
case <-ctx.Done():
c.logger.Printf("failed to shutdown connection: %v", ctx.Err())
// Forcibly close the connection
if err := c.Close(); err != nil && !errors.Is(err, net.ErrClosed) {
c.logger.Printf("failed to close connection: %v", err)
}
}
}
func (c *conn) RemoteAddr() net.Addr {
return c.conn.RemoteAddr()
}
func (c *conn) LocalAddr() net.Addr {
return c.conn.LocalAddr()
}
// NewContext returns a copy of the parent context with a new Done channel. The
// returned context's Done channel is closed when the connection is closed,
// when the returned cancel function is called, or when the parent context's
// Done channel is closed, whichever happens first.
//
// Canceling this context releases resources associated with it, so code should
// call cancel as soon as the operations running in this Context complete.
func (c *conn) NewContext(parent context.Context) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(parent)
go func() {
defer cancel()
select {
case <-ctx.Done():
// The parent context has been cancelled, or the caller has called
// cancel()
case <-c.closedCh:
// The connection has been closed
}
}()
return ctx, cancel
}
|