1 | package soju
|
---|
2 |
|
---|
3 | import (
|
---|
4 | "context"
|
---|
5 | "fmt"
|
---|
6 | "io"
|
---|
7 | "net"
|
---|
8 | "sync"
|
---|
9 | "time"
|
---|
10 |
|
---|
11 | "gopkg.in/irc.v3"
|
---|
12 | "nhooyr.io/websocket"
|
---|
13 | )
|
---|
14 |
|
---|
15 | // ircConn is a generic IRC connection. It's similar to net.Conn but focuses on
|
---|
16 | // reading and writing IRC messages.
|
---|
17 | type ircConn interface {
|
---|
18 | ReadMessage() (*irc.Message, error)
|
---|
19 | WriteMessage(*irc.Message) error
|
---|
20 | Close() error
|
---|
21 | SetReadDeadline(time.Time) error
|
---|
22 | SetWriteDeadline(time.Time) error
|
---|
23 | RemoteAddr() net.Addr
|
---|
24 | }
|
---|
25 |
|
---|
26 | func newNetIRCConn(c net.Conn) ircConn {
|
---|
27 | type netConn net.Conn
|
---|
28 | return struct {
|
---|
29 | *irc.Conn
|
---|
30 | netConn
|
---|
31 | }{irc.NewConn(c), c}
|
---|
32 | }
|
---|
33 |
|
---|
34 | type websocketIRCConn struct {
|
---|
35 | conn *websocket.Conn
|
---|
36 | readDeadline, writeDeadline time.Time
|
---|
37 | remoteAddr string
|
---|
38 | }
|
---|
39 |
|
---|
40 | func newWebsocketIRCConn(c *websocket.Conn, remoteAddr string) ircConn {
|
---|
41 | return websocketIRCConn{conn: c, remoteAddr: remoteAddr}
|
---|
42 | }
|
---|
43 |
|
---|
44 | func (wic websocketIRCConn) ReadMessage() (*irc.Message, error) {
|
---|
45 | ctx := context.Background()
|
---|
46 | if !wic.readDeadline.IsZero() {
|
---|
47 | var cancel context.CancelFunc
|
---|
48 | ctx, cancel = context.WithDeadline(ctx, wic.readDeadline)
|
---|
49 | defer cancel()
|
---|
50 | }
|
---|
51 | _, b, err := wic.conn.Read(ctx)
|
---|
52 | if err != nil {
|
---|
53 | switch websocket.CloseStatus(err) {
|
---|
54 | case websocket.StatusNormalClosure, websocket.StatusGoingAway:
|
---|
55 | return nil, io.EOF
|
---|
56 | default:
|
---|
57 | return nil, err
|
---|
58 | }
|
---|
59 | }
|
---|
60 | return irc.ParseMessage(string(b))
|
---|
61 | }
|
---|
62 |
|
---|
63 | func (wic websocketIRCConn) WriteMessage(msg *irc.Message) error {
|
---|
64 | b := []byte(msg.String())
|
---|
65 | ctx := context.Background()
|
---|
66 | if !wic.writeDeadline.IsZero() {
|
---|
67 | var cancel context.CancelFunc
|
---|
68 | ctx, cancel = context.WithDeadline(ctx, wic.writeDeadline)
|
---|
69 | defer cancel()
|
---|
70 | }
|
---|
71 | return wic.conn.Write(ctx, websocket.MessageText, b)
|
---|
72 | }
|
---|
73 |
|
---|
74 | func (wic websocketIRCConn) Close() error {
|
---|
75 | return wic.conn.Close(websocket.StatusNormalClosure, "")
|
---|
76 | }
|
---|
77 |
|
---|
78 | func (wic websocketIRCConn) SetReadDeadline(t time.Time) error {
|
---|
79 | wic.readDeadline = t
|
---|
80 | return nil
|
---|
81 | }
|
---|
82 |
|
---|
83 | func (wic websocketIRCConn) SetWriteDeadline(t time.Time) error {
|
---|
84 | wic.writeDeadline = t
|
---|
85 | return nil
|
---|
86 | }
|
---|
87 |
|
---|
88 | func (wic websocketIRCConn) RemoteAddr() net.Addr {
|
---|
89 | return websocketAddr(wic.remoteAddr)
|
---|
90 | }
|
---|
91 |
|
---|
92 | type websocketAddr string
|
---|
93 |
|
---|
94 | func (websocketAddr) Network() string {
|
---|
95 | return "ws"
|
---|
96 | }
|
---|
97 |
|
---|
98 | func (wa websocketAddr) String() string {
|
---|
99 | return string(wa)
|
---|
100 | }
|
---|
101 |
|
---|
102 | type conn struct {
|
---|
103 | conn ircConn
|
---|
104 | srv *Server
|
---|
105 | logger Logger
|
---|
106 |
|
---|
107 | lock sync.Mutex
|
---|
108 | outgoing chan<- *irc.Message
|
---|
109 | closed bool
|
---|
110 | }
|
---|
111 |
|
---|
112 | func newConn(srv *Server, ic ircConn, logger Logger) *conn {
|
---|
113 | outgoing := make(chan *irc.Message, 64)
|
---|
114 | c := &conn{
|
---|
115 | conn: ic,
|
---|
116 | srv: srv,
|
---|
117 | outgoing: outgoing,
|
---|
118 | logger: logger,
|
---|
119 | }
|
---|
120 |
|
---|
121 | go func() {
|
---|
122 | for msg := range outgoing {
|
---|
123 | if c.srv.Debug {
|
---|
124 | c.logger.Printf("sent: %v", msg)
|
---|
125 | }
|
---|
126 | c.conn.SetWriteDeadline(time.Now().Add(writeTimeout))
|
---|
127 | if err := c.conn.WriteMessage(msg); err != nil {
|
---|
128 | c.logger.Printf("failed to write message: %v", err)
|
---|
129 | break
|
---|
130 | }
|
---|
131 | }
|
---|
132 | if err := c.conn.Close(); err != nil {
|
---|
133 | c.logger.Printf("failed to close connection: %v", err)
|
---|
134 | } else {
|
---|
135 | c.logger.Printf("connection closed")
|
---|
136 | }
|
---|
137 | // Drain the outgoing channel to prevent SendMessage from blocking
|
---|
138 | for range outgoing {
|
---|
139 | // This space is intentionally left blank
|
---|
140 | }
|
---|
141 | }()
|
---|
142 |
|
---|
143 | c.logger.Printf("new connection")
|
---|
144 | return c
|
---|
145 | }
|
---|
146 |
|
---|
147 | func (c *conn) isClosed() bool {
|
---|
148 | c.lock.Lock()
|
---|
149 | defer c.lock.Unlock()
|
---|
150 | return c.closed
|
---|
151 | }
|
---|
152 |
|
---|
153 | // Close closes the connection. It is safe to call from any goroutine.
|
---|
154 | func (c *conn) Close() error {
|
---|
155 | c.lock.Lock()
|
---|
156 | defer c.lock.Unlock()
|
---|
157 |
|
---|
158 | if c.closed {
|
---|
159 | return fmt.Errorf("connection already closed")
|
---|
160 | }
|
---|
161 |
|
---|
162 | err := c.conn.Close()
|
---|
163 | c.closed = true
|
---|
164 | close(c.outgoing)
|
---|
165 | return err
|
---|
166 | }
|
---|
167 |
|
---|
168 | func (c *conn) ReadMessage() (*irc.Message, error) {
|
---|
169 | msg, err := c.conn.ReadMessage()
|
---|
170 | if err != nil {
|
---|
171 | return nil, err
|
---|
172 | }
|
---|
173 |
|
---|
174 | if c.srv.Debug {
|
---|
175 | c.logger.Printf("received: %v", msg)
|
---|
176 | }
|
---|
177 |
|
---|
178 | return msg, nil
|
---|
179 | }
|
---|
180 |
|
---|
181 | // SendMessage queues a new outgoing message. It is safe to call from any
|
---|
182 | // goroutine.
|
---|
183 | //
|
---|
184 | // If the connection is closed before the message is sent, SendMessage silently
|
---|
185 | // drops the message.
|
---|
186 | func (c *conn) SendMessage(msg *irc.Message) {
|
---|
187 | c.lock.Lock()
|
---|
188 | defer c.lock.Unlock()
|
---|
189 |
|
---|
190 | if c.closed {
|
---|
191 | return
|
---|
192 | }
|
---|
193 | c.outgoing <- msg
|
---|
194 | }
|
---|