ircd/irc/socket.go

195 lines
4.3 KiB
Go
Raw Normal View History

// Copyright (c) 2012-2014 Jeremy Latt
2017-03-27 12:15:02 +00:00
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
2014-02-14 03:37:16 +00:00
package irc
import (
"bufio"
"crypto/sha256"
"crypto/tls"
"encoding/hex"
2018-03-15 23:11:29 +00:00
"errors"
"io"
2014-02-14 03:37:16 +00:00
"net"
2016-06-15 11:21:45 +00:00
"strings"
"sync"
2016-10-22 10:53:36 +00:00
"time"
2014-02-14 03:37:16 +00:00
)
var (
2016-10-22 10:53:36 +00:00
handshakeTimeout, _ = time.ParseDuration("5s")
2018-03-15 23:11:29 +00:00
errSendQExceeded = errors.New("SendQ exceeded")
)
2016-06-15 11:21:45 +00:00
// Socket represents an IRC socket.
2014-02-14 03:37:16 +00:00
type Socket struct {
2018-03-15 23:11:29 +00:00
sync.Mutex
2016-06-15 11:21:45 +00:00
conn net.Conn
reader *bufio.Reader
2018-03-18 01:32:12 +00:00
maxSendQBytes int
2017-04-18 12:26:01 +00:00
2018-03-15 23:11:29 +00:00
// coordination system for asynchronous writes
buffer []byte
lineToSendExists chan bool
2018-03-15 23:11:29 +00:00
closed bool
sendQExceeded bool
finalData string // what to send when we die
2014-02-14 03:37:16 +00:00
}
2016-06-15 11:21:45 +00:00
// NewSocket returns a new Socket.
2018-03-18 01:32:12 +00:00
func NewSocket(conn net.Conn, maxReadQBytes int, maxSendQBytes int) Socket {
2016-06-15 11:21:45 +00:00
return Socket{
conn: conn,
2018-03-18 01:32:12 +00:00
reader: bufio.NewReaderSize(conn, maxReadQBytes),
maxSendQBytes: maxSendQBytes,
2018-03-15 23:11:29 +00:00
lineToSendExists: make(chan bool, 1),
2014-02-14 03:37:16 +00:00
}
}
2016-06-15 11:21:45 +00:00
// Close stops a Socket from being able to send/receive any more data.
2014-02-14 03:37:16 +00:00
func (socket *Socket) Close() {
2018-03-16 22:16:04 +00:00
socket.Lock()
socket.closed = true
socket.Unlock()
socket.wakeWriter()
2014-02-14 03:37:16 +00:00
}
// CertFP returns the fingerprint of the certificate provided by the client.
func (socket *Socket) CertFP() (string, error) {
var tlsConn, isTLS = socket.conn.(*tls.Conn)
if !isTLS {
2016-09-07 11:32:58 +00:00
return "", errNotTLS
}
2016-10-22 10:53:36 +00:00
// ensure handehake is performed, and timeout after a few seconds
tlsConn.SetDeadline(time.Now().Add(handshakeTimeout))
err := tlsConn.Handshake()
tlsConn.SetDeadline(time.Time{})
if err != nil {
return "", err
}
2016-09-07 11:32:58 +00:00
peerCerts := tlsConn.ConnectionState().PeerCertificates
if len(peerCerts) < 1 {
return "", errNoPeerCerts
}
rawCert := sha256.Sum256(peerCerts[0].Raw)
fingerprint := hex.EncodeToString(rawCert[:])
return fingerprint, nil
}
2016-06-15 11:21:45 +00:00
// Read returns a single IRC line from a Socket.
func (socket *Socket) Read() (string, error) {
2017-04-18 12:26:01 +00:00
if socket.IsClosed() {
2016-06-15 11:21:45 +00:00
return "", io.EOF
}
2018-03-18 01:32:12 +00:00
lineBytes, isPrefix, err := socket.reader.ReadLine()
if isPrefix {
return "", errReadQ
}
2016-06-15 11:21:45 +00:00
// convert bytes to string
2018-03-18 01:32:12 +00:00
line := string(lineBytes)
2014-02-14 03:37:16 +00:00
2016-06-15 11:21:45 +00:00
// read last message properly (such as ERROR/QUIT/etc), just fail next reads/writes
if err == io.EOF {
socket.Close()
}
2016-06-15 11:21:45 +00:00
if err == io.EOF && strings.TrimSpace(line) != "" {
// don't do anything
} else if err != nil {
return "", err
2014-02-18 17:45:10 +00:00
}
2014-02-18 07:58:02 +00:00
2018-03-18 01:32:12 +00:00
return line, nil
2016-06-15 11:21:45 +00:00
}
2014-02-20 19:15:42 +00:00
2016-06-15 11:21:45 +00:00
// Write sends the given string out of Socket.
2018-03-15 23:11:29 +00:00
func (socket *Socket) Write(data string) (err error) {
socket.Lock()
if socket.closed {
err = io.EOF
2018-03-18 01:32:12 +00:00
} else if len(data)+len(socket.buffer) > socket.maxSendQBytes {
2018-03-15 23:11:29 +00:00
socket.sendQExceeded = true
err = errSendQExceeded
} else {
socket.buffer = append(socket.buffer, data...)
}
socket.Unlock()
2016-06-15 11:21:45 +00:00
2018-03-16 22:16:04 +00:00
socket.wakeWriter()
return
}
// wakeWriter wakes up the goroutine that actually performs the write, without blocking
func (socket *Socket) wakeWriter() {
// nonblocking send to the channel, no-op if it's full
select {
case socket.lineToSendExists <- true:
default:
}
}
2017-04-18 12:26:01 +00:00
// SetFinalData sets the final data to send when the SocketWriter closes.
func (socket *Socket) SetFinalData(data string) {
2018-03-15 23:11:29 +00:00
socket.Lock()
defer socket.Unlock()
2017-04-18 12:26:01 +00:00
socket.finalData = data
}
// IsClosed returns whether the socket is closed.
func (socket *Socket) IsClosed() bool {
2018-03-15 23:11:29 +00:00
socket.Lock()
defer socket.Unlock()
2017-04-18 12:26:01 +00:00
return socket.closed
}
// RunSocketWriter starts writing messages to the outgoing socket.
func (socket *Socket) RunSocketWriter() {
2018-03-15 23:11:29 +00:00
localBuffer := make([]byte, 0)
shouldStop := false
for !shouldStop {
// wait for new lines
select {
case <-socket.lineToSendExists:
2018-03-15 23:11:29 +00:00
// retrieve the buffered data, clear the buffer
socket.Lock()
localBuffer = append(localBuffer, socket.buffer...)
socket.buffer = socket.buffer[:0]
socket.Unlock()
_, err := socket.conn.Write(localBuffer)
localBuffer = localBuffer[:0]
socket.Lock()
shouldStop = (err != nil) || socket.closed || socket.sendQExceeded
socket.Unlock()
}
}
2017-04-18 12:26:01 +00:00
2018-03-15 23:11:29 +00:00
// mark the socket closed (if someone hasn't already), then write error lines
socket.Lock()
socket.closed = true
finalData := socket.finalData
if socket.sendQExceeded {
finalData = "\r\nERROR :SendQ Exceeded\r\n"
}
socket.Unlock()
if finalData != "" {
socket.conn.Write([]byte(finalData))
}
2017-04-18 12:26:01 +00:00
// close the connection
socket.conn.Close()
2014-02-14 03:37:16 +00:00
}