tile38/internal/client/conn.go

323 lines
8.6 KiB
Go
Raw Normal View History

2016-03-05 02:08:16 +03:00
package client
import (
"bufio"
"bytes"
"crypto/sha1"
"encoding/base64"
"encoding/binary"
"errors"
"io"
"net"
"net/url"
"strconv"
"strings"
"time"
)
// LiveJSON is the value returned when a connection goes "live".
const LiveJSON = `{"ok":true,"live":true}`
// MaxMessageSize is maximum accepted message size
2016-03-22 04:32:41 +03:00
const MaxMessageSize = 0x1FFFFFFF // 536,870,911 bytes
2016-03-05 02:08:16 +03:00
2016-04-03 05:16:36 +03:00
// Proto is the protocol value.
2016-03-05 02:08:16 +03:00
type Proto int
const (
2016-04-03 05:16:36 +03:00
Native Proto = 0 // native protocol
Telnet Proto = 1 // telnet protocol
HTTP Proto = 2 // http protocol
WebSocket Proto = 3 // websocket protocol
2016-03-05 02:08:16 +03:00
)
// Conn represents a connection to a tile38 server.
type Conn struct {
c net.Conn
rd *bufio.Reader
pool *Pool
detached bool
}
// Dial connects to a tile38 server.
func Dial(addr string) (*Conn, error) {
c, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
return &Conn{c: c, rd: bufio.NewReader(c)}, nil
}
// DialTimeout connects to a tile38 server with a timeout.
func DialTimeout(addr string, timeout time.Duration) (*Conn, error) {
c, err := net.DialTimeout("tcp", addr, timeout)
if err != nil {
return nil, err
}
return &Conn{c: c, rd: bufio.NewReader(c)}, nil
}
// Close will close a connection.
func (conn *Conn) Close() error {
if conn.pool == nil {
if !conn.detached {
conn.Do("QUIT")
}
return conn.c.Close()
}
return conn.pool.put(conn)
}
2016-04-03 05:16:36 +03:00
// SetDeadline sets the connection deadline for reads and writes.
2016-03-05 02:08:16 +03:00
func (conn *Conn) SetDeadline(t time.Time) error {
return conn.c.SetDeadline(t)
}
2016-04-03 05:16:36 +03:00
// SetDeadline sets the connection deadline for reads.
2016-03-05 02:08:16 +03:00
func (conn *Conn) SetReadDeadline(t time.Time) error {
return conn.c.SetReadDeadline(t)
}
2016-04-03 05:16:36 +03:00
// SetDeadline sets the connection deadline for writes.
2016-03-05 02:08:16 +03:00
func (conn *Conn) SetWriteDeadline(t time.Time) error {
return conn.c.SetWriteDeadline(t)
}
// Do sends a command to the server and returns the received reply.
func (conn *Conn) Do(command string) ([]byte, error) {
if err := WriteMessage(conn.c, []byte(command)); err != nil {
conn.pool = nil
return nil, err
}
2016-03-08 18:35:43 +03:00
message, _, _, err := ReadMessage(conn.rd, nil)
2016-03-05 02:08:16 +03:00
if err != nil {
conn.pool = nil
return nil, err
}
if string(message) == LiveJSON {
conn.pool = nil // detach from pool
}
return message, nil
}
// ReadMessage returns the next message. Used when reading live connections
func (conn *Conn) ReadMessage() (message []byte, err error) {
2016-03-08 18:35:43 +03:00
message, _, _, err = readMessage(conn.c, conn.rd)
2016-03-05 02:08:16 +03:00
if err != nil {
conn.pool = nil
return message, err
}
return message, nil
}
// Reader returns the underlying reader.
func (conn *Conn) Reader() io.Reader {
conn.pool = nil // Remove from the pool because once the reader is called
conn.detached = true // we will assume that this connection is detached.
return conn.rd
}
// WriteMessage write a message to an io.Writer
func WriteMessage(w io.Writer, message []byte) error {
h := []byte("$" + strconv.FormatUint(uint64(len(message)), 10) + " ")
b := make([]byte, len(h)+len(message)+2)
copy(b, h)
copy(b[len(h):], message)
b[len(b)-2] = '\r'
b[len(b)-1] = '\n'
_, err := w.Write(b)
return err
}
2016-04-03 05:16:36 +03:00
// WriteHTTP writes an http message to the connection and closes the connection.
2016-03-05 02:08:16 +03:00
func WriteHTTP(conn net.Conn, data []byte) error {
var buf bytes.Buffer
buf.WriteString("HTTP/1.1 200 OK\r\n")
buf.WriteString("Content-Length: " + strconv.FormatInt(int64(len(data))+1, 10) + "\r\n")
buf.WriteString("Content-Type: application/json\r\n")
buf.WriteString("Connection: close\r\n")
buf.WriteString("\r\n")
buf.Write(data)
buf.WriteByte('\n')
_, err := conn.Write(buf.Bytes())
return err
}
2016-04-03 05:16:36 +03:00
// WriteWebSocket writes a websocket message.
2016-03-05 02:08:16 +03:00
func WriteWebSocket(conn net.Conn, data []byte) error {
var msg []byte
buf := make([]byte, 10+len(data))
buf[0] = 129 // FIN + TEXT
if len(data) <= 125 {
buf[1] = byte(len(data))
copy(buf[2:], data)
msg = buf[:2+len(data)]
} else if len(data) <= 0xFFFF {
buf[1] = 126
binary.BigEndian.PutUint16(buf[2:], uint16(len(data)))
copy(buf[4:], data)
msg = buf[:4+len(data)]
} else {
buf[1] = 127
binary.BigEndian.PutUint64(buf[2:], uint64(len(data)))
copy(buf[10:], data)
msg = buf[:10+len(data)]
}
_, err := conn.Write(msg)
return err
}
// ReadMessage reads the next message from a bufio.Reader.
2016-03-08 18:35:43 +03:00
func readMessage(wr io.Writer, rd *bufio.Reader) (message []byte, proto Proto, auth string, err error) {
2016-03-05 02:08:16 +03:00
h, err := rd.Peek(1)
if err != nil {
2016-03-08 18:35:43 +03:00
return nil, proto, auth, err
2016-03-05 02:08:16 +03:00
}
switch h[0] {
case '$':
return readProtoMessage(rd)
}
message, proto, err = readTelnetMessage(rd)
if err != nil {
2016-03-08 18:35:43 +03:00
return nil, proto, auth, err
2016-03-05 02:08:16 +03:00
}
if len(message) > 6 && string(message[len(message)-9:len(message)-2]) == " HTTP/1" {
return readHTTPMessage(string(message), wr, rd)
}
2016-03-08 18:35:43 +03:00
return message, proto, auth, nil
2016-03-05 02:08:16 +03:00
}
2016-04-03 05:16:36 +03:00
// ReadMessage read the next message from a bufio Reader.
2016-03-08 18:35:43 +03:00
func ReadMessage(rd *bufio.Reader, wr io.Writer) (message []byte, proto Proto, auth string, err error) {
2016-03-05 02:08:16 +03:00
return readMessage(wr, rd)
}
2016-03-08 18:35:43 +03:00
func readProtoMessage(rd *bufio.Reader) (message []byte, proto Proto, auth string, err error) {
2016-03-05 02:08:16 +03:00
b, err := rd.ReadBytes(' ')
if err != nil {
2016-03-08 18:35:43 +03:00
return nil, Native, auth, err
2016-03-05 02:08:16 +03:00
}
if len(b) > 0 && b[0] != '$' {
2016-03-08 18:35:43 +03:00
return nil, Native, auth, errors.New("not a proto message")
2016-03-05 02:08:16 +03:00
}
n, err := strconv.ParseUint(string(b[1:len(b)-1]), 10, 32)
if err != nil {
2016-03-08 18:35:43 +03:00
return nil, Native, auth, errors.New("invalid size")
2016-03-05 02:08:16 +03:00
}
if n > MaxMessageSize {
2016-03-08 18:35:43 +03:00
return nil, Native, auth, errors.New("message too big")
2016-03-05 02:08:16 +03:00
}
b = make([]byte, int(n)+2)
if _, err := io.ReadFull(rd, b); err != nil {
2016-03-08 18:35:43 +03:00
return nil, Native, auth, err
2016-03-05 02:08:16 +03:00
}
if b[len(b)-2] != '\r' || b[len(b)-1] != '\n' {
2016-03-08 18:35:43 +03:00
return nil, Native, auth, errors.New("expecting crlf suffix")
2016-03-05 02:08:16 +03:00
}
2016-03-08 18:35:43 +03:00
return b[:len(b)-2], Native, auth, nil
2016-03-05 02:08:16 +03:00
}
func readTelnetMessage(rd *bufio.Reader) (command []byte, proto Proto, err error) {
line, err := rd.ReadBytes('\n')
if err != nil {
return nil, Telnet, err
}
if len(line) > 1 && line[len(line)-2] == '\r' {
line = line[:len(line)-2]
} else {
line = line[:len(line)-1]
}
return line, Telnet, nil
}
2016-03-08 18:35:43 +03:00
func readHTTPMessage(line string, wr io.Writer, rd *bufio.Reader) (command []byte, proto Proto, auth string, err error) {
proto = HTTP
2016-03-05 02:08:16 +03:00
parts := strings.Split(line, " ")
if len(parts) != 3 {
2016-03-08 18:35:43 +03:00
err = errors.New("invalid HTTP request")
return
2016-03-05 02:08:16 +03:00
}
method := parts[0]
path := parts[1]
if len(path) == 0 || path[0] != '/' {
2016-03-08 18:35:43 +03:00
err = errors.New("invalid HTTP request")
return
2016-03-05 02:08:16 +03:00
}
path, err = url.QueryUnescape(path[1:])
if err != nil {
2016-03-08 18:35:43 +03:00
err = errors.New("invalid HTTP request")
return
2016-03-05 02:08:16 +03:00
}
if method != "GET" && method != "POST" {
2016-03-08 18:35:43 +03:00
err = errors.New("invalid HTTP method")
return
2016-03-05 02:08:16 +03:00
}
contentLength := 0
websocket := false
websocketVersion := 0
websocketKey := ""
for {
2016-03-08 18:35:43 +03:00
var b []byte
b, _, err = readTelnetMessage(rd) // read a header line
2016-03-05 02:08:16 +03:00
if err != nil {
2016-03-08 18:35:43 +03:00
return
2016-03-05 02:08:16 +03:00
}
header := string(b)
if header == "" {
break // end of headers
}
2016-03-08 18:35:43 +03:00
if header[0] == 'a' || header[0] == 'A' {
if strings.HasPrefix(strings.ToLower(header), "authorization:") {
auth = strings.TrimSpace(header[len("authorization:"):])
}
} else if header[0] == 'u' || header[0] == 'U' {
2016-03-05 02:08:16 +03:00
if strings.HasPrefix(strings.ToLower(header), "upgrade:") && strings.ToLower(strings.TrimSpace(header[len("upgrade:"):])) == "websocket" {
websocket = true
}
} else if header[0] == 's' || header[0] == 'S' {
if strings.HasPrefix(strings.ToLower(header), "sec-websocket-version:") {
2016-03-08 18:35:43 +03:00
var n uint64
n, err = strconv.ParseUint(strings.TrimSpace(header[len("sec-websocket-version:"):]), 10, 64)
2016-03-05 02:08:16 +03:00
if err != nil {
2016-03-08 18:35:43 +03:00
return
2016-03-05 02:08:16 +03:00
}
websocketVersion = int(n)
} else if strings.HasPrefix(strings.ToLower(header), "sec-websocket-key:") {
websocketKey = strings.TrimSpace(header[len("sec-websocket-key:"):])
}
} else if header[0] == 'c' || header[0] == 'C' {
if strings.HasPrefix(strings.ToLower(header), "content-length:") {
2016-03-08 18:35:43 +03:00
var n uint64
n, err = strconv.ParseUint(strings.TrimSpace(header[len("content-length:"):]), 10, 64)
2016-03-05 02:08:16 +03:00
if err != nil {
2016-03-08 18:35:43 +03:00
return
2016-03-05 02:08:16 +03:00
}
contentLength = int(n)
}
}
}
if websocket && websocketVersion >= 13 && websocketKey != "" {
2016-03-08 18:35:43 +03:00
proto = WebSocket
2016-03-05 02:08:16 +03:00
if wr == nil {
2016-03-08 18:35:43 +03:00
err = errors.New("connection is nil")
return
2016-03-05 02:08:16 +03:00
}
sum := sha1.Sum([]byte(websocketKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))
accept := base64.StdEncoding.EncodeToString(sum[:])
wshead := "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: " + accept + "\r\n\r\n"
2016-03-08 18:35:43 +03:00
if _, err = wr.Write([]byte(wshead)); err != nil {
return
2016-03-05 02:08:16 +03:00
}
} else if contentLength > 0 {
2016-03-08 18:35:43 +03:00
proto = HTTP
2016-03-05 02:08:16 +03:00
buf := make([]byte, contentLength)
2016-03-08 18:35:43 +03:00
if _, err = io.ReadFull(rd, buf); err != nil {
return
2016-03-05 02:08:16 +03:00
}
path += string(buf)
}
2016-03-08 18:35:43 +03:00
command = []byte(path)
return
2016-03-05 02:08:16 +03:00
}