2016-06-15 13:50:56 +02:00
|
|
|
// Copyright (c) 2012-2014 Jeremy Latt
|
2017-03-27 14:15:02 +02:00
|
|
|
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
|
2016-06-15 13:50:56 +02:00
|
|
|
// released under the MIT license
|
|
|
|
|
2014-02-14 04:37:16 +01:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2016-09-05 05:53:39 +02:00
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/hex"
|
2018-03-16 00:11:29 +01:00
|
|
|
"errors"
|
2014-02-20 22:03:33 +01:00
|
|
|
"io"
|
2014-02-14 04:37:16 +01:00
|
|
|
"net"
|
2016-06-15 13:21:45 +02:00
|
|
|
"strings"
|
2017-03-13 14:53:21 +01:00
|
|
|
"sync"
|
2016-10-22 12:53:36 +02:00
|
|
|
"time"
|
2014-02-14 04:37:16 +01:00
|
|
|
)
|
|
|
|
|
2016-09-05 05:53:39 +02:00
|
|
|
var (
|
2016-10-22 12:53:36 +02:00
|
|
|
handshakeTimeout, _ = time.ParseDuration("5s")
|
2018-03-16 00:11:29 +01:00
|
|
|
errSendQExceeded = errors.New("SendQ exceeded")
|
2016-09-05 05:53:39 +02:00
|
|
|
)
|
|
|
|
|
2016-06-15 13:21:45 +02:00
|
|
|
// Socket represents an IRC socket.
|
2014-02-14 04:37:16 +01:00
|
|
|
type Socket struct {
|
2018-03-16 00:11:29 +01:00
|
|
|
sync.Mutex
|
|
|
|
|
2016-06-15 13:21:45 +02:00
|
|
|
conn net.Conn
|
|
|
|
reader *bufio.Reader
|
2017-03-13 14:53:21 +01:00
|
|
|
|
2018-03-18 02:32:12 +01:00
|
|
|
maxSendQBytes int
|
2017-04-18 14:26:01 +02:00
|
|
|
|
2018-04-13 21:47:57 +02:00
|
|
|
// this is a trylock enforcing that only one goroutine can write to `conn` at a time
|
|
|
|
writerSlotOpen chan bool
|
2018-03-16 00:11:29 +01:00
|
|
|
|
2018-04-13 21:47:57 +02:00
|
|
|
buffer []byte
|
2018-03-16 00:11:29 +01:00
|
|
|
closed bool
|
|
|
|
sendQExceeded bool
|
|
|
|
finalData string // what to send when we die
|
2018-04-13 21:47:57 +02:00
|
|
|
finalized bool
|
2014-02-14 04:37:16 +01:00
|
|
|
}
|
|
|
|
|
2016-06-15 13:21:45 +02:00
|
|
|
// NewSocket returns a new Socket.
|
2018-04-15 07:21:32 +02:00
|
|
|
func NewSocket(conn net.Conn, maxReadQBytes int, maxSendQBytes int) *Socket {
|
2018-04-13 21:47:57 +02:00
|
|
|
result := Socket{
|
|
|
|
conn: conn,
|
|
|
|
reader: bufio.NewReaderSize(conn, maxReadQBytes),
|
|
|
|
maxSendQBytes: maxSendQBytes,
|
|
|
|
writerSlotOpen: make(chan bool, 1),
|
2014-02-14 04:37:16 +01:00
|
|
|
}
|
2018-04-13 21:47:57 +02:00
|
|
|
result.writerSlotOpen <- true
|
2018-04-15 07:21:32 +02:00
|
|
|
return &result
|
2014-02-14 04:37:16 +01:00
|
|
|
}
|
|
|
|
|
2016-06-15 13:21:45 +02:00
|
|
|
// Close stops a Socket from being able to send/receive any more data.
|
2014-02-14 04:37:16 +01:00
|
|
|
func (socket *Socket) Close() {
|
2018-03-16 23:16:04 +01:00
|
|
|
socket.Lock()
|
|
|
|
socket.closed = true
|
|
|
|
socket.Unlock()
|
|
|
|
|
2018-04-16 01:05:22 +02:00
|
|
|
socket.wakeWriter()
|
2014-02-14 04:37:16 +01:00
|
|
|
}
|
|
|
|
|
2016-09-05 05:53:39 +02: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 13:32:58 +02:00
|
|
|
return "", errNotTLS
|
2016-09-05 05:53:39 +02:00
|
|
|
}
|
|
|
|
|
2016-10-22 12:53:36 +02: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 13:32:58 +02:00
|
|
|
|
2016-09-05 05:53:39 +02: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 13:21:45 +02:00
|
|
|
// Read returns a single IRC line from a Socket.
|
|
|
|
func (socket *Socket) Read() (string, error) {
|
2017-04-18 14:26:01 +02:00
|
|
|
if socket.IsClosed() {
|
2016-06-15 13:21:45 +02:00
|
|
|
return "", io.EOF
|
2014-04-15 18:07:25 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 02:32:12 +01:00
|
|
|
lineBytes, isPrefix, err := socket.reader.ReadLine()
|
|
|
|
if isPrefix {
|
|
|
|
return "", errReadQ
|
|
|
|
}
|
2014-02-20 22:03:33 +01:00
|
|
|
|
2016-06-15 13:21:45 +02:00
|
|
|
// convert bytes to string
|
2018-03-18 02:32:12 +01:00
|
|
|
line := string(lineBytes)
|
2014-02-14 04:37:16 +01:00
|
|
|
|
2016-06-15 13:21:45 +02:00
|
|
|
// read last message properly (such as ERROR/QUIT/etc), just fail next reads/writes
|
|
|
|
if err == io.EOF {
|
|
|
|
socket.Close()
|
2014-04-15 18:07:25 +02:00
|
|
|
}
|
|
|
|
|
2016-06-15 13:21:45 +02:00
|
|
|
if err == io.EOF && strings.TrimSpace(line) != "" {
|
|
|
|
// don't do anything
|
|
|
|
} else if err != nil {
|
|
|
|
return "", err
|
2014-02-18 18:45:10 +01:00
|
|
|
}
|
2014-02-18 08:58:02 +01:00
|
|
|
|
2018-03-18 02:32:12 +01:00
|
|
|
return line, nil
|
2016-06-15 13:21:45 +02:00
|
|
|
}
|
2014-02-20 20:15:42 +01:00
|
|
|
|
2018-04-13 21:47:57 +02:00
|
|
|
// Write sends the given string out of Socket. Requirements:
|
|
|
|
// 1. MUST NOT block for macroscopic amounts of time
|
|
|
|
// 2. MUST NOT reorder messages
|
|
|
|
// 3. MUST provide mutual exclusion for socket.conn.Write
|
|
|
|
// 4. SHOULD NOT tie up additional goroutines, beyond the one blocked on socket.conn.Write
|
2018-03-16 00:11:29 +01:00
|
|
|
func (socket *Socket) Write(data string) (err error) {
|
|
|
|
socket.Lock()
|
|
|
|
if socket.closed {
|
|
|
|
err = io.EOF
|
2018-03-18 02:32:12 +01:00
|
|
|
} else if len(data)+len(socket.buffer) > socket.maxSendQBytes {
|
2018-03-16 00:11:29 +01:00
|
|
|
socket.sendQExceeded = true
|
|
|
|
err = errSendQExceeded
|
|
|
|
} else {
|
|
|
|
socket.buffer = append(socket.buffer, data...)
|
|
|
|
}
|
2018-03-16 17:39:11 +01:00
|
|
|
socket.Unlock()
|
2016-06-15 13:21:45 +02:00
|
|
|
|
2018-04-16 01:05:22 +02:00
|
|
|
socket.wakeWriter()
|
2018-03-16 23:16:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-16 01:05:22 +02:00
|
|
|
// wakeWriter starts the goroutine that actually performs the write, without blocking
|
|
|
|
func (socket *Socket) wakeWriter() {
|
|
|
|
// attempt to acquire the trylock
|
|
|
|
select {
|
|
|
|
case <-socket.writerSlotOpen:
|
|
|
|
// acquired the trylock; send() will release it
|
|
|
|
go socket.send()
|
|
|
|
default:
|
|
|
|
// failed to acquire; the holder will check for more data after releasing it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 14:26:01 +02:00
|
|
|
// SetFinalData sets the final data to send when the SocketWriter closes.
|
|
|
|
func (socket *Socket) SetFinalData(data string) {
|
2018-03-16 00:11:29 +01:00
|
|
|
socket.Lock()
|
|
|
|
defer socket.Unlock()
|
2017-04-18 14:26:01 +02:00
|
|
|
socket.finalData = data
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsClosed returns whether the socket is closed.
|
|
|
|
func (socket *Socket) IsClosed() bool {
|
2018-03-16 00:11:29 +01:00
|
|
|
socket.Lock()
|
|
|
|
defer socket.Unlock()
|
2017-04-18 14:26:01 +02:00
|
|
|
return socket.closed
|
|
|
|
}
|
|
|
|
|
2018-04-13 21:47:57 +02:00
|
|
|
// is there data to write?
|
|
|
|
func (socket *Socket) readyToWrite() bool {
|
|
|
|
socket.Lock()
|
|
|
|
defer socket.Unlock()
|
|
|
|
// on the first time observing socket.closed, we still have to write socket.finalData
|
|
|
|
return !socket.finalized && (len(socket.buffer) > 0 || socket.closed || socket.sendQExceeded)
|
|
|
|
}
|
|
|
|
|
|
|
|
// send actually writes messages to socket.Conn; it may block
|
|
|
|
func (socket *Socket) send() {
|
2018-04-15 07:21:32 +02:00
|
|
|
for {
|
2018-04-16 01:05:22 +02:00
|
|
|
// we are holding the trylock: actually do the write
|
|
|
|
socket.performWrite()
|
|
|
|
// surrender the trylock, avoiding a race where a write comes in after we've
|
|
|
|
// checked readyToWrite() and it returned false, but while we still hold the trylock:
|
|
|
|
socket.writerSlotOpen <- true
|
|
|
|
// check if more data came in while we held the trylock:
|
|
|
|
if !socket.readyToWrite() {
|
|
|
|
return
|
|
|
|
}
|
2017-03-13 14:53:21 +01:00
|
|
|
select {
|
2018-04-13 21:47:57 +02:00
|
|
|
case <-socket.writerSlotOpen:
|
2018-04-16 01:05:22 +02:00
|
|
|
// got the trylock, loop back around and write
|
2018-04-13 21:47:57 +02:00
|
|
|
default:
|
2018-04-16 01:05:22 +02:00
|
|
|
// failed to acquire; exit and wait for the holder to observe readyToWrite()
|
|
|
|
// after releasing it
|
2018-04-13 21:47:57 +02:00
|
|
|
return
|
2017-03-13 14:53:21 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-13 21:47:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// write the contents of the buffer, then see if we need to close
|
|
|
|
func (socket *Socket) performWrite() {
|
|
|
|
// retrieve the buffered data, clear the buffer
|
|
|
|
socket.Lock()
|
|
|
|
buffer := socket.buffer
|
|
|
|
socket.buffer = nil
|
|
|
|
socket.Unlock()
|
|
|
|
|
|
|
|
_, err := socket.conn.Write(buffer)
|
|
|
|
|
|
|
|
socket.Lock()
|
|
|
|
shouldClose := (err != nil) || socket.closed || socket.sendQExceeded
|
|
|
|
socket.Unlock()
|
|
|
|
|
|
|
|
if !shouldClose {
|
|
|
|
return
|
|
|
|
}
|
2017-04-18 14:26:01 +02:00
|
|
|
|
2018-03-16 00:11:29 +01:00
|
|
|
// mark the socket closed (if someone hasn't already), then write error lines
|
|
|
|
socket.Lock()
|
|
|
|
socket.closed = true
|
2018-04-13 21:47:57 +02:00
|
|
|
socket.finalized = true
|
2018-03-16 00:11:29 +01:00
|
|
|
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:29:00 +02:00
|
|
|
}
|
2017-04-18 14:26:01 +02:00
|
|
|
|
|
|
|
// close the connection
|
2017-04-18 12:29:00 +02:00
|
|
|
socket.conn.Close()
|
2014-02-14 04:37:16 +01:00
|
|
|
}
|