ergo/irc/socket.go

112 lines
2.2 KiB
Go
Raw Normal View History

// Copyright (c) 2012-2014 Jeremy Latt
// Copyright (c) 2016- Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
2014-02-14 04:37:16 +01:00
package irc
import (
"bufio"
"crypto/sha256"
"crypto/tls"
"encoding/hex"
"errors"
"io"
2014-02-14 04:37:16 +01:00
"net"
2016-06-15 13:21:45 +02:00
"strings"
2014-02-14 04:37:16 +01:00
)
var (
2016-09-07 13:32:58 +02:00
errNotTLS = errors.New("Not a TLS connection")
errNoPeerCerts = errors.New("Client did not provide a certificate")
)
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
2014-02-14 04:37:16 +01:00
}
2016-06-15 13:21:45 +02:00
// NewSocket returns a new Socket.
func NewSocket(conn net.Conn) Socket {
return Socket{
conn: conn,
reader: bufio.NewReader(conn),
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
if socket.Closed {
return
}
2016-06-15 13:21:45 +02:00
socket.Closed = true
2014-02-20 03:13:35 +01:00
socket.conn.Close()
2014-02-14 04:37:16 +01: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-07 13:32:58 +02:00
// ensure handehake is performed
tlsConn.Handshake()
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
}
2016-06-15 13:21:45 +02:00
lineBytes, err := socket.reader.ReadBytes('\n')
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()
}
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
2016-06-15 13:21:45 +02:00
// write data
_, err := socket.conn.Write([]byte(data))
2014-02-14 04:37:16 +01:00
if err != nil {
2016-06-15 13:21:45 +02:00
socket.Close()
return err
2014-02-14 04:37:16 +01:00
}
2016-06-15 13:21:45 +02:00
return nil
}
// 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
}