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"
|
|
|
|
"errors"
|
2017-03-13 14:53:21 +01:00
|
|
|
"fmt"
|
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
|
|
|
errNotTLS = errors.New("Not a TLS connection")
|
|
|
|
errNoPeerCerts = errors.New("Client did not provide a certificate")
|
|
|
|
handshakeTimeout, _ = time.ParseDuration("5s")
|
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 {
|
2016-06-15 13:21:45 +02:00
|
|
|
Closed bool
|
|
|
|
conn net.Conn
|
|
|
|
reader *bufio.Reader
|
2017-03-13 14:53:21 +01:00
|
|
|
|
2017-03-13 23:12:39 +01:00
|
|
|
MaxSendQBytes uint64
|
|
|
|
|
2017-03-13 14:53:21 +01:00
|
|
|
lineToSendExists chan bool
|
|
|
|
linesToSend []string
|
|
|
|
linesToSendMutex sync.Mutex
|
2014-02-14 04:37:16 +01:00
|
|
|
}
|
|
|
|
|
2016-06-15 13:21:45 +02:00
|
|
|
// NewSocket returns a new Socket.
|
2017-03-13 23:12:39 +01:00
|
|
|
func NewSocket(conn net.Conn, maxSendQBytes uint64) Socket {
|
2016-06-15 13:21:45 +02:00
|
|
|
return Socket{
|
2017-03-13 14:53:21 +01:00
|
|
|
conn: conn,
|
|
|
|
reader: bufio.NewReader(conn),
|
2017-03-13 23:12:39 +01:00
|
|
|
MaxSendQBytes: maxSendQBytes,
|
2017-03-13 14:53:21 +01:00
|
|
|
lineToSendExists: make(chan bool),
|
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() {
|
2016-06-15 13:21:45 +02:00
|
|
|
socket.Closed = true
|
2017-03-23 03:07:23 +01:00
|
|
|
|
2017-04-17 13:02:34 +02:00
|
|
|
// force close loop to happen
|
2017-03-23 03:07:23 +01:00
|
|
|
go socket.fillLineToSendExists()
|
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) {
|
|
|
|
if socket.Closed {
|
|
|
|
return "", io.EOF
|
2014-04-15 18:07:25 +02:00
|
|
|
}
|
|
|
|
|
2016-06-15 13:21:45 +02:00
|
|
|
lineBytes, err := socket.reader.ReadBytes('\n')
|
2014-02-20 22:03:33 +01:00
|
|
|
|
2016-06-15 13:21:45 +02:00
|
|
|
// convert bytes to string
|
|
|
|
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
|
|
|
|
2016-06-15 13:21:45 +02:00
|
|
|
return strings.TrimRight(line, "\r\n"), nil
|
|
|
|
}
|
2014-02-20 20:15:42 +01:00
|
|
|
|
2016-06-15 13:21:45 +02:00
|
|
|
// Write sends the given string out of Socket.
|
|
|
|
func (socket *Socket) Write(data string) error {
|
|
|
|
if socket.Closed {
|
|
|
|
return io.EOF
|
2014-03-29 19:56:23 +01:00
|
|
|
}
|
2014-02-20 20:15:42 +01:00
|
|
|
|
2017-03-13 14:53:21 +01:00
|
|
|
socket.linesToSendMutex.Lock()
|
|
|
|
socket.linesToSend = append(socket.linesToSend, data)
|
|
|
|
socket.linesToSendMutex.Unlock()
|
|
|
|
go socket.fillLineToSendExists()
|
|
|
|
|
2016-06-15 13:21:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-13 14:53:21 +01:00
|
|
|
// fillLineToSendExists only exists because you can't goroutine single statements.
|
|
|
|
func (socket *Socket) fillLineToSendExists() {
|
|
|
|
socket.lineToSendExists <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunSocketWriter starts writing messages to the outgoing socket.
|
|
|
|
func (socket *Socket) RunSocketWriter() {
|
|
|
|
var errOut bool
|
|
|
|
for {
|
|
|
|
// wait for new lines
|
|
|
|
select {
|
|
|
|
case <-socket.lineToSendExists:
|
|
|
|
socket.linesToSendMutex.Lock()
|
|
|
|
|
2017-04-17 13:02:34 +02:00
|
|
|
// check if we're closed
|
|
|
|
if socket.Closed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-03-13 23:12:39 +01:00
|
|
|
// check sendq
|
|
|
|
var sendQBytes uint64
|
|
|
|
for _, line := range socket.linesToSend {
|
|
|
|
sendQBytes += uint64(len(line))
|
|
|
|
if socket.MaxSendQBytes < sendQBytes {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if socket.MaxSendQBytes < sendQBytes {
|
|
|
|
socket.conn.Write([]byte("\r\nERROR :SendQ Exceeded\r\n"))
|
2017-03-23 03:12:39 +01:00
|
|
|
fmt.Println("SendQ exceeded, disconnected client")
|
2017-03-13 23:12:39 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-03-13 14:53:21 +01:00
|
|
|
// get data
|
|
|
|
data := socket.linesToSend[0]
|
|
|
|
if len(socket.linesToSend) > 1 {
|
|
|
|
socket.linesToSend = socket.linesToSend[1:]
|
|
|
|
} else {
|
|
|
|
socket.linesToSend = []string{}
|
|
|
|
}
|
|
|
|
|
2017-03-24 12:54:22 +01:00
|
|
|
socket.linesToSendMutex.Unlock()
|
|
|
|
|
2017-03-13 14:53:21 +01:00
|
|
|
// write data
|
2017-03-23 03:07:23 +01:00
|
|
|
if 0 < len(data) {
|
|
|
|
_, err := socket.conn.Write([]byte(data))
|
|
|
|
if err != nil {
|
|
|
|
errOut = true
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we're closed
|
|
|
|
if socket.Closed {
|
2017-03-13 14:53:21 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if errOut {
|
|
|
|
// error out, bad stuff happened
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//TODO(dan): empty socket.lineToSendExists queue
|
|
|
|
socket.conn.Close()
|
|
|
|
if !socket.Closed {
|
|
|
|
socket.Closed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 13:21:45 +02:00
|
|
|
// WriteLine writes the given line out of Socket.
|
|
|
|
func (socket *Socket) WriteLine(line string) error {
|
|
|
|
return socket.Write(line + "\r\n")
|
2014-02-14 04:37:16 +01:00
|
|
|
}
|