2020-04-08 20:44:30 +03:00
|
|
|
module websocket
|
2020-04-08 15:22:31 +03:00
|
|
|
|
2020-04-26 14:49:31 +03:00
|
|
|
import net
|
|
|
|
import net.urllib
|
|
|
|
import encoding.base64
|
2020-07-22 18:36:24 +03:00
|
|
|
import encoding.utf8
|
2020-04-26 14:49:31 +03:00
|
|
|
import eventbus
|
|
|
|
import sync
|
2020-07-22 17:31:22 +03:00
|
|
|
import log
|
2020-04-08 15:22:31 +03:00
|
|
|
|
|
|
|
pub struct Client {
|
2020-05-26 13:50:37 +03:00
|
|
|
retry int
|
|
|
|
eb &eventbus.EventBus
|
|
|
|
is_ssl bool
|
2020-07-22 17:31:22 +03:00
|
|
|
mut:
|
2020-05-26 13:50:37 +03:00
|
|
|
// subprotocol_len int
|
|
|
|
// cwebsocket_subprotocol *subprotocol;
|
|
|
|
// cwebsocket_subprotocol *subprotocols[];
|
2020-07-04 13:44:25 +03:00
|
|
|
mtx &sync.Mutex = sync.new_mutex()
|
2020-06-01 18:24:38 +03:00
|
|
|
write_lock &sync.Mutex = sync.new_mutex()
|
2020-05-26 13:50:37 +03:00
|
|
|
state State
|
|
|
|
socket net.Socket
|
|
|
|
flags []Flag
|
|
|
|
sslctx &C.SSL_CTX
|
|
|
|
ssl &C.SSL
|
|
|
|
fragments []Fragment
|
|
|
|
pub mut:
|
2020-07-22 17:31:22 +03:00
|
|
|
log log.Log = log.Log{
|
|
|
|
output_label: 'ws'
|
|
|
|
}
|
2020-05-26 13:50:37 +03:00
|
|
|
uri string
|
2020-04-08 15:22:31 +03:00
|
|
|
subscriber &eventbus.Subscriber
|
2020-06-01 20:37:24 +03:00
|
|
|
nonce_size int = 18 // you can try 16 too
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Fragment {
|
|
|
|
data voidptr
|
2020-05-26 13:50:37 +03:00
|
|
|
len u64
|
2020-04-08 15:22:31 +03:00
|
|
|
code OPCode
|
|
|
|
}
|
|
|
|
|
2020-04-08 22:19:01 +03:00
|
|
|
pub struct Message {
|
2020-05-26 13:50:37 +03:00
|
|
|
pub:
|
|
|
|
opcode OPCode
|
|
|
|
payload voidptr
|
2020-04-08 15:22:31 +03:00
|
|
|
payload_len int
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum OPCode {
|
2020-04-23 02:16:16 +03:00
|
|
|
continuation = 0x00
|
|
|
|
text_frame = 0x01
|
|
|
|
binary_frame = 0x02
|
|
|
|
close = 0x08
|
|
|
|
ping = 0x09
|
2020-04-08 15:22:31 +03:00
|
|
|
pong = 0x0A
|
|
|
|
}
|
|
|
|
|
|
|
|
enum State {
|
2020-04-23 02:16:16 +03:00
|
|
|
connecting = 0
|
|
|
|
connected
|
|
|
|
open
|
2020-05-26 13:50:37 +03:00
|
|
|
closing
|
2020-04-23 02:16:16 +03:00
|
|
|
closed
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Uri {
|
2020-05-26 13:50:37 +03:00
|
|
|
mut:
|
|
|
|
hostname string
|
|
|
|
port string
|
|
|
|
resource string
|
2020-04-08 15:22:31 +03:00
|
|
|
querystring string
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Flag {
|
2020-04-23 02:16:16 +03:00
|
|
|
has_accept
|
|
|
|
has_connection
|
2020-04-08 15:22:31 +03:00
|
|
|
has_upgrade
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Frame {
|
2020-05-26 13:50:37 +03:00
|
|
|
mut:
|
|
|
|
fin bool
|
|
|
|
rsv1 bool
|
|
|
|
rsv2 bool
|
|
|
|
rsv3 bool
|
|
|
|
opcode OPCode
|
|
|
|
mask bool
|
2020-04-08 15:22:31 +03:00
|
|
|
payload_len u64
|
|
|
|
masking_key [4]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(uri string) &Client {
|
|
|
|
eb := eventbus.new()
|
|
|
|
ws := &Client{
|
|
|
|
uri: uri
|
|
|
|
state: .closed
|
2020-05-26 13:50:37 +03:00
|
|
|
eb: eb
|
2020-04-08 15:22:31 +03:00
|
|
|
subscriber: eb.subscriber
|
2020-05-26 13:50:37 +03:00
|
|
|
is_ssl: uri.starts_with('wss')
|
|
|
|
ssl: 0
|
|
|
|
sslctx: 0
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
return ws
|
|
|
|
}
|
2020-05-26 13:50:37 +03:00
|
|
|
|
2020-04-08 15:22:31 +03:00
|
|
|
fn (ws &Client) parse_uri() &Uri {
|
2020-05-26 13:50:37 +03:00
|
|
|
u := urllib.parse(ws.uri) or {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
v := u.request_uri().split('?')
|
2020-07-22 17:35:30 +03:00
|
|
|
mut port := u.port()
|
|
|
|
//Check if port is empty and check protocol to get the port, secure by default
|
|
|
|
if port == '' {
|
|
|
|
if ws.uri.contains('://') {
|
|
|
|
port = if ws.uri.split('://')[0] == 'ws' { '80' } else { '443' }
|
|
|
|
} else {
|
|
|
|
port = '443'
|
|
|
|
}
|
|
|
|
}
|
2020-05-26 13:50:37 +03:00
|
|
|
querystring := if v.len > 1 { '?' + v[1] } else { '' }
|
|
|
|
return &Uri{
|
2020-04-08 15:22:31 +03:00
|
|
|
hostname: u.hostname()
|
2020-07-22 17:35:30 +03:00
|
|
|
port: port
|
2020-04-08 15:22:31 +03:00
|
|
|
resource: v[0]
|
|
|
|
querystring: querystring
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut ws Client) connect() int {
|
2020-04-08 22:19:01 +03:00
|
|
|
match ws.state {
|
|
|
|
.connected {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.fatal('connect: websocket already connected')
|
2020-04-08 22:19:01 +03:00
|
|
|
}
|
|
|
|
.connecting {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.fatal('connect: websocket already connecting')
|
2020-04-08 22:19:01 +03:00
|
|
|
}
|
|
|
|
.open {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.fatal('connect: websocket already open')
|
2020-05-26 13:50:37 +03:00
|
|
|
}
|
|
|
|
else {
|
2020-04-08 22:19:01 +03:00
|
|
|
// do nothing
|
|
|
|
}
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
2020-07-04 13:44:25 +03:00
|
|
|
ws.mtx.m_lock()
|
2020-04-08 15:22:31 +03:00
|
|
|
ws.state = .connecting
|
2020-07-04 13:44:25 +03:00
|
|
|
ws.mtx.unlock()
|
2020-04-08 15:22:31 +03:00
|
|
|
uri := ws.parse_uri()
|
2020-06-01 20:31:45 +03:00
|
|
|
nonce := get_nonce(ws.nonce_size)
|
2020-04-08 15:22:31 +03:00
|
|
|
seckey := base64.encode(nonce)
|
|
|
|
ai_family := C.AF_INET
|
|
|
|
ai_socktype := C.SOCK_STREAM
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.debug('handshake header:')
|
|
|
|
handshake := 'GET $uri.resource$uri.querystring HTTP/1.1\r\nHost: $uri.hostname:$uri.port\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: $seckey\r\nSec-WebSocket-Version: 13\r\n\r\n'
|
|
|
|
ws.log.debug(handshake)
|
2020-04-08 15:22:31 +03:00
|
|
|
socket := net.new_socket(ai_family, ai_socktype, 0) or {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.fatal(err)
|
2020-04-08 15:22:31 +03:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
ws.socket = socket
|
|
|
|
ws.socket.connect(uri.hostname, uri.port.int()) or {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.fatal(err)
|
2020-04-08 15:22:31 +03:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
optval := 1
|
|
|
|
ws.socket.setsockopt(C.SOL_SOCKET, C.SO_KEEPALIVE, &optval) or {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.fatal(err)
|
2020-04-08 15:22:31 +03:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if ws.is_ssl {
|
|
|
|
ws.connect_ssl()
|
|
|
|
}
|
2020-07-04 13:44:25 +03:00
|
|
|
ws.mtx.m_lock()
|
2020-04-08 15:22:31 +03:00
|
|
|
ws.state = .connected
|
2020-07-04 13:44:25 +03:00
|
|
|
ws.mtx.unlock()
|
2020-04-08 15:22:31 +03:00
|
|
|
res := ws.write_to_server(handshake.str, handshake.len)
|
|
|
|
if res <= 0 {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.fatal('Handshake failed.')
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
ws.read_handshake(seckey)
|
2020-07-04 13:44:25 +03:00
|
|
|
ws.mtx.m_lock()
|
2020-04-08 15:22:31 +03:00
|
|
|
ws.state = .open
|
2020-07-04 13:44:25 +03:00
|
|
|
ws.mtx.unlock()
|
2020-04-08 15:22:31 +03:00
|
|
|
ws.send_open_event()
|
|
|
|
unsafe {
|
|
|
|
handshake.free()
|
|
|
|
nonce.free()
|
|
|
|
free(uri)
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-05-26 13:50:37 +03:00
|
|
|
pub fn (mut ws Client) close(code int, message string) {
|
2020-04-08 15:22:31 +03:00
|
|
|
if ws.state != .closed && ws.socket.sockfd > 1 {
|
2020-07-04 13:44:25 +03:00
|
|
|
ws.mtx.m_lock()
|
2020-04-08 15:22:31 +03:00
|
|
|
ws.state = .closing
|
2020-07-04 13:44:25 +03:00
|
|
|
ws.mtx.unlock()
|
2020-04-08 15:22:31 +03:00
|
|
|
mut code32 := 0
|
|
|
|
if code > 0 {
|
2020-05-26 13:50:37 +03:00
|
|
|
code_ := C.htons(code)
|
2020-04-08 15:22:31 +03:00
|
|
|
message_len := message.len + 2
|
|
|
|
mut close_frame := [`0`].repeat(message_len)
|
2020-05-27 06:42:48 +03:00
|
|
|
close_frame[0] = byte(code_ & 0xFF)
|
|
|
|
close_frame[1] = byte(code_ >> 8)
|
2020-04-08 15:22:31 +03:00
|
|
|
code32 = (close_frame[0] << 8) + close_frame[1]
|
2020-05-26 13:50:37 +03:00
|
|
|
for i in 0 .. message.len {
|
|
|
|
close_frame[i + 2] = message[i]
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
2020-05-26 13:50:37 +03:00
|
|
|
ws.send_control_frame(.close, 'CLOSE', close_frame)
|
2020-04-08 15:22:31 +03:00
|
|
|
} else {
|
2020-05-26 13:50:37 +03:00
|
|
|
ws.send_control_frame(.close, 'CLOSE', [])
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
2020-05-26 13:50:37 +03:00
|
|
|
if ws.ssl != 0 {
|
2020-04-24 08:32:51 +03:00
|
|
|
C.SSL_shutdown(ws.ssl)
|
|
|
|
C.SSL_free(ws.ssl)
|
2020-05-26 13:50:37 +03:00
|
|
|
if ws.sslctx != 0 {
|
2020-04-24 08:32:51 +03:00
|
|
|
C.SSL_CTX_free(ws.sslctx)
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if C.shutdown(ws.socket.sockfd, C.SHUT_WR) == -1 {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.error('Unabled to shutdown websocket.')
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
mut buf := [`0`]
|
|
|
|
for ws.read_from_server(buf.data, 1) > 0 {
|
|
|
|
buf[0] = `\0`
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
buf.free()
|
|
|
|
}
|
|
|
|
if C.close(ws.socket.sockfd) == -1 {
|
2020-05-26 13:50:37 +03:00
|
|
|
// ws.send_close_event()(websocket, 1011, strerror(C.errno));
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ws.fragments = []
|
|
|
|
ws.send_close_event()
|
2020-07-04 13:44:25 +03:00
|
|
|
ws.mtx.m_lock()
|
2020-04-08 15:22:31 +03:00
|
|
|
ws.state = .closed
|
2020-07-04 13:44:25 +03:00
|
|
|
ws.mtx.unlock()
|
2020-05-26 13:50:37 +03:00
|
|
|
// TODO impl autoreconnect
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 18:42:57 +03:00
|
|
|
// write will send the payload to the websocket server. NB: *it will not free the payload*, the caller is responsible for that.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut ws Client) write(payload byteptr, payload_len int, code OPCode) int {
|
2020-05-26 13:50:37 +03:00
|
|
|
mut bytes_written := -1
|
2020-04-08 15:22:31 +03:00
|
|
|
if ws.state != .open {
|
2020-05-26 13:50:37 +03:00
|
|
|
ws.send_error_event('WebSocket closed. Cannot write.')
|
2020-04-08 15:22:31 +03:00
|
|
|
return -1
|
|
|
|
}
|
2020-05-26 13:50:37 +03:00
|
|
|
header_len := 6 + if payload_len > 125 { 2 } else { 0 } + if payload_len > 0xffff { 6 } else { 0 }
|
|
|
|
frame_len := header_len + payload_len
|
|
|
|
mut frame_buf := [`0`].repeat(frame_len)
|
2020-07-22 17:31:22 +03:00
|
|
|
fbdata := byteptr(frame_buf.data)
|
2020-04-08 15:22:31 +03:00
|
|
|
masking_key := create_masking_key()
|
|
|
|
mut header := [`0`].repeat(header_len)
|
2020-05-27 06:42:48 +03:00
|
|
|
header[0] = byte(code) | 0x80
|
2020-04-08 15:22:31 +03:00
|
|
|
if payload_len <= 125 {
|
2020-05-27 06:42:48 +03:00
|
|
|
header[1] = byte(payload_len | 0x80)
|
2020-04-08 15:22:31 +03:00
|
|
|
header[2] = masking_key[0]
|
|
|
|
header[3] = masking_key[1]
|
|
|
|
header[4] = masking_key[2]
|
|
|
|
header[5] = masking_key[3]
|
|
|
|
} else if payload_len > 125 && payload_len <= 0xffff {
|
|
|
|
len16 := C.htons(payload_len)
|
|
|
|
header[1] = (126 | 0x80)
|
2020-07-03 19:10:10 +03:00
|
|
|
unsafe {
|
|
|
|
C.memcpy(header.data + 2, &len16, 2)
|
|
|
|
}
|
2020-04-08 15:22:31 +03:00
|
|
|
header[4] = masking_key[0]
|
|
|
|
header[5] = masking_key[1]
|
|
|
|
header[6] = masking_key[2]
|
|
|
|
header[7] = masking_key[3]
|
|
|
|
} else if payload_len > 0xffff && payload_len <= 0xffffffffffffffff { // 65535 && 18446744073709551615
|
|
|
|
len64 := htonl64(u64(payload_len))
|
|
|
|
header[1] = (127 | 0x80)
|
2020-07-03 19:10:10 +03:00
|
|
|
unsafe {
|
|
|
|
C.memcpy(header.data + 2, len64, 8)
|
|
|
|
}
|
2020-04-08 15:22:31 +03:00
|
|
|
header[10] = masking_key[0]
|
|
|
|
header[11] = masking_key[1]
|
|
|
|
header[12] = masking_key[2]
|
|
|
|
header[13] = masking_key[3]
|
|
|
|
} else {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.fatal('write: frame too large')
|
2020-05-26 13:50:37 +03:00
|
|
|
ws.close(1009, 'frame too large')
|
2020-04-08 15:22:31 +03:00
|
|
|
goto free_data
|
|
|
|
return -1
|
|
|
|
}
|
2020-07-22 17:31:22 +03:00
|
|
|
unsafe {
|
2020-07-03 19:10:10 +03:00
|
|
|
C.memcpy(fbdata, header.data, header_len)
|
|
|
|
C.memcpy(fbdata + header_len, payload, payload_len)
|
|
|
|
}
|
2020-05-26 13:50:37 +03:00
|
|
|
for i in 0 .. payload_len {
|
|
|
|
frame_buf[header_len + i] ^= masking_key[i % 4] & 0xff
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
2020-05-26 13:50:37 +03:00
|
|
|
bytes_written = ws.write_to_server(fbdata, frame_len)
|
2020-04-08 15:22:31 +03:00
|
|
|
if bytes_written == -1 {
|
|
|
|
err := string(byteptr(C.strerror(C.errno)))
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.error('write: there was an error writing data: $err')
|
2020-05-26 13:50:37 +03:00
|
|
|
ws.send_error_event('Error writing data')
|
2020-04-08 15:22:31 +03:00
|
|
|
goto free_data
|
|
|
|
return -1
|
|
|
|
}
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.debug('write: $bytes_written bytes written.')
|
2020-04-08 15:22:31 +03:00
|
|
|
free_data:
|
2020-04-23 02:16:16 +03:00
|
|
|
unsafe {
|
2020-04-08 15:22:31 +03:00
|
|
|
frame_buf.free()
|
|
|
|
header.free()
|
|
|
|
masking_key.free()
|
|
|
|
}
|
|
|
|
return bytes_written
|
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut ws Client) listen() {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.info('Starting listener...')
|
2020-04-08 15:22:31 +03:00
|
|
|
for ws.state == .open {
|
|
|
|
ws.read()
|
|
|
|
}
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.info('Listener stopped as websocket was closed.')
|
2020-04-23 02:16:16 +03:00
|
|
|
}
|
2020-04-08 15:22:31 +03:00
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut ws Client) read() int {
|
2020-04-08 15:22:31 +03:00
|
|
|
mut bytes_read := u64(0)
|
|
|
|
initial_buffer := u64(256)
|
|
|
|
mut header_len := 2
|
|
|
|
header_len_offset := 2
|
|
|
|
extended_payload16_end_byte := 4
|
|
|
|
extended_payload64_end_byte := 10
|
|
|
|
mut payload_len := u64(0)
|
2020-05-26 13:50:37 +03:00
|
|
|
mut data := C.calloc(initial_buffer, 1) // [`0`].repeat(int(max_buffer))
|
2020-04-08 15:22:31 +03:00
|
|
|
mut frame := Frame{}
|
|
|
|
mut frame_size := u64(header_len)
|
|
|
|
for bytes_read < frame_size && ws.state == .open {
|
2020-07-03 19:10:10 +03:00
|
|
|
mut byt := 0
|
|
|
|
unsafe {
|
|
|
|
byt = ws.read_from_server(data + int(bytes_read), 1)
|
|
|
|
}
|
2020-04-08 15:22:31 +03:00
|
|
|
match byt {
|
|
|
|
0 {
|
2020-05-26 13:50:37 +03:00
|
|
|
error := 'server closed the connection.'
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.error('read: $error')
|
2020-04-08 15:22:31 +03:00
|
|
|
ws.send_error_event(error)
|
|
|
|
ws.close(1006, error)
|
|
|
|
goto free_data
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
-1 {
|
|
|
|
err := string(byteptr(C.strerror(C.errno)))
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.error('read: error reading frame. $err')
|
2020-05-26 13:50:37 +03:00
|
|
|
ws.send_error_event('error reading frame')
|
2020-04-08 15:22:31 +03:00
|
|
|
goto free_data
|
|
|
|
return -1
|
2020-05-26 13:50:37 +03:00
|
|
|
}
|
|
|
|
else {
|
2020-04-08 15:22:31 +03:00
|
|
|
bytes_read++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if bytes_read == u64(header_len_offset) {
|
2020-07-22 20:28:53 +03:00
|
|
|
data0 := unsafe {data[0]}
|
|
|
|
frame.fin = (data0 & 0x80) == 0x80
|
|
|
|
frame.rsv1 = (data0 & 0x40) == 0x40
|
|
|
|
frame.rsv2 = (data0 & 0x20) == 0x20
|
|
|
|
frame.rsv3 = (data0 & 0x10) == 0x10
|
|
|
|
frame.opcode = OPCode(int(data0 & 0x7F))
|
|
|
|
frame.mask = (unsafe {data[1]} & 0x80) == 0x80
|
|
|
|
frame.payload_len = u64(unsafe {data[1]} & 0x7F)
|
2020-05-26 13:50:37 +03:00
|
|
|
// masking key
|
2020-04-08 15:22:31 +03:00
|
|
|
if frame.mask {
|
2020-07-22 20:28:53 +03:00
|
|
|
frame.masking_key[0] = unsafe {data[2]}
|
|
|
|
frame.masking_key[1] = unsafe {data[3]}
|
|
|
|
frame.masking_key[2] = unsafe {data[4]}
|
|
|
|
frame.masking_key[3] = unsafe {data[5]}
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
payload_len = frame.payload_len
|
|
|
|
frame_size = u64(header_len) + payload_len
|
|
|
|
}
|
|
|
|
if frame.payload_len == u64(126) && bytes_read == u64(extended_payload16_end_byte) {
|
|
|
|
header_len += 2
|
|
|
|
mut extended_payload_len := 0
|
2020-07-22 20:28:53 +03:00
|
|
|
extended_payload_len |= unsafe {data[2]} << 8
|
|
|
|
extended_payload_len |= unsafe {data[3]} << 0
|
2020-05-26 13:50:37 +03:00
|
|
|
// masking key
|
2020-04-08 15:22:31 +03:00
|
|
|
if frame.mask {
|
2020-07-22 20:28:53 +03:00
|
|
|
frame.masking_key[0] = unsafe {data[4]}
|
|
|
|
frame.masking_key[1] = unsafe {data[5]}
|
|
|
|
frame.masking_key[2] = unsafe {data[6]}
|
|
|
|
frame.masking_key[3] = unsafe {data[7]}
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
payload_len = u64(extended_payload_len)
|
|
|
|
frame_size = u64(header_len) + payload_len
|
|
|
|
if frame_size > initial_buffer {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.debug('reallocating: $frame_size')
|
2020-07-11 13:41:37 +03:00
|
|
|
data = v_realloc(data, u32(frame_size))
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
} else if frame.payload_len == u64(127) && bytes_read == u64(extended_payload64_end_byte) {
|
2020-05-26 13:50:37 +03:00
|
|
|
header_len += 8 // TODO Not sure...
|
2020-04-08 15:22:31 +03:00
|
|
|
mut extended_payload_len := u64(0)
|
2020-07-22 20:28:53 +03:00
|
|
|
extended_payload_len |= u64(unsafe {data[2]}) << 56
|
|
|
|
extended_payload_len |= u64(unsafe {data[3]}) << 48
|
|
|
|
extended_payload_len |= u64(unsafe {data[4]}) << 40
|
|
|
|
extended_payload_len |= u64(unsafe {data[5]}) << 32
|
|
|
|
extended_payload_len |= u64(unsafe {data[6]}) << 24
|
|
|
|
extended_payload_len |= u64(unsafe {data[7]}) << 16
|
|
|
|
extended_payload_len |= u64(unsafe {data[8]}) << 8
|
|
|
|
extended_payload_len |= u64(unsafe {data[9]}) << 0
|
2020-05-26 13:50:37 +03:00
|
|
|
// masking key
|
2020-04-08 15:22:31 +03:00
|
|
|
if frame.mask {
|
2020-07-22 20:28:53 +03:00
|
|
|
frame.masking_key[0] = unsafe {data[10]}
|
|
|
|
frame.masking_key[1] = unsafe {data[11]}
|
|
|
|
frame.masking_key[2] = unsafe {data[12]}
|
|
|
|
frame.masking_key[3] = unsafe {data[13]}
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
payload_len = extended_payload_len
|
|
|
|
frame_size = u64(header_len) + payload_len
|
|
|
|
if frame_size > initial_buffer {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.debug('reallocating: $frame_size')
|
2020-07-11 13:41:37 +03:00
|
|
|
data = v_realloc(data, u32(frame_size)) // TODO u64 => u32
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// unmask the payload
|
|
|
|
if frame.mask {
|
2020-05-26 13:50:37 +03:00
|
|
|
for i in 0 .. payload_len {
|
2020-07-22 20:28:53 +03:00
|
|
|
unsafe {
|
|
|
|
data[header_len + i] ^= frame.masking_key[i % 4] & 0xff
|
|
|
|
}
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ws.fragments.len > 0 && frame.opcode in [.text_frame, .binary_frame] {
|
2020-05-26 13:50:37 +03:00
|
|
|
ws.close(0, '')
|
2020-04-08 15:22:31 +03:00
|
|
|
goto free_data
|
|
|
|
return -1
|
2020-04-23 02:16:16 +03:00
|
|
|
} else if frame.opcode in [.text_frame, .binary_frame] {
|
2020-04-08 15:22:31 +03:00
|
|
|
data_node:
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.debug('read: recieved text_frame or binary_frame')
|
2020-06-01 22:15:59 +03:00
|
|
|
mut payload := malloc(int(sizeof(byte) * u32(payload_len) + 1))
|
2020-05-26 13:50:37 +03:00
|
|
|
if payload == 0 {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.fatal('out of memory')
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
C.memcpy(payload, &data[header_len], payload_len)
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
if frame.fin {
|
|
|
|
if ws.fragments.len > 0 {
|
2020-05-26 13:50:37 +03:00
|
|
|
// join fragments
|
2020-04-08 15:22:31 +03:00
|
|
|
ws.fragments << Fragment{
|
|
|
|
data: payload
|
|
|
|
len: payload_len
|
|
|
|
}
|
2020-04-26 17:25:54 +03:00
|
|
|
mut frags := []Fragment{}
|
2020-04-08 15:22:31 +03:00
|
|
|
mut size := u64(0)
|
|
|
|
for f in ws.fragments {
|
|
|
|
if f.len > 0 {
|
|
|
|
frags << f
|
|
|
|
size += f.len
|
|
|
|
}
|
|
|
|
}
|
2020-06-01 22:15:59 +03:00
|
|
|
mut pl := malloc(int(sizeof(byte) * u32(size)))
|
2020-05-26 13:50:37 +03:00
|
|
|
if pl == 0 {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.fatal('out of memory')
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
mut by := 0
|
2020-05-26 13:50:37 +03:00
|
|
|
for f in frags {
|
2020-07-03 19:10:10 +03:00
|
|
|
unsafe {
|
|
|
|
C.memcpy(pl + by, f.data, f.len)
|
|
|
|
}
|
2020-04-08 15:22:31 +03:00
|
|
|
by += int(f.len)
|
2020-05-26 13:50:37 +03:00
|
|
|
unsafe {
|
|
|
|
free(f.data)
|
|
|
|
}
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
payload = pl
|
|
|
|
frame.opcode = ws.fragments[0].code
|
|
|
|
payload_len = size
|
2020-05-26 13:50:37 +03:00
|
|
|
// clear the fragments
|
2020-04-08 15:22:31 +03:00
|
|
|
unsafe {
|
|
|
|
ws.fragments.free()
|
|
|
|
}
|
|
|
|
ws.fragments = []
|
|
|
|
}
|
2020-07-22 20:28:53 +03:00
|
|
|
unsafe {
|
|
|
|
payload[payload_len] = `\0`
|
|
|
|
}
|
2020-04-08 15:22:31 +03:00
|
|
|
if frame.opcode == .text_frame && payload_len > 0 {
|
2020-07-22 18:36:24 +03:00
|
|
|
if !utf8.validate(payload, int(payload_len)) {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.error('malformed utf8 payload')
|
2020-07-22 20:28:53 +03:00
|
|
|
ws.send_error_event('Received malformed utf8.')
|
2020-05-26 13:50:37 +03:00
|
|
|
ws.close(1007, 'malformed utf8 payload')
|
2020-04-08 15:22:31 +03:00
|
|
|
goto free_data
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
2020-07-05 18:44:33 +03:00
|
|
|
message := &Message{
|
2020-04-08 15:22:31 +03:00
|
|
|
opcode: frame.opcode
|
|
|
|
payload: payload
|
|
|
|
payload_len: int(payload_len)
|
|
|
|
}
|
|
|
|
ws.send_message_event(message)
|
|
|
|
} else {
|
2020-05-26 13:50:37 +03:00
|
|
|
// fragment start.
|
2020-04-08 15:22:31 +03:00
|
|
|
ws.fragments << Fragment{
|
|
|
|
data: payload
|
|
|
|
len: payload_len
|
|
|
|
code: frame.opcode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
free(data)
|
|
|
|
}
|
|
|
|
return int(bytes_read)
|
2020-05-26 13:50:37 +03:00
|
|
|
} else if frame.opcode == .continuation {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.debug('read: continuation')
|
2020-04-08 15:22:31 +03:00
|
|
|
if ws.fragments.len <= 0 {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.error('Nothing to continue.')
|
2020-05-26 13:50:37 +03:00
|
|
|
ws.close(1002, 'nothing to continue')
|
2020-04-08 15:22:31 +03:00
|
|
|
goto free_data
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
goto data_node
|
2020-04-23 02:16:16 +03:00
|
|
|
return 0
|
2020-05-26 13:50:37 +03:00
|
|
|
} else if frame.opcode == .ping {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.debug('read: ping')
|
2020-04-08 15:22:31 +03:00
|
|
|
if !frame.fin {
|
2020-05-26 13:50:37 +03:00
|
|
|
ws.close(1002, 'control message must not be fragmented')
|
2020-04-08 15:22:31 +03:00
|
|
|
goto free_data
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if frame.payload_len > 125 {
|
2020-05-26 13:50:37 +03:00
|
|
|
ws.close(1002, 'control frames must not exceed 125 bytes')
|
2020-04-08 15:22:31 +03:00
|
|
|
goto free_data
|
|
|
|
return -1
|
|
|
|
}
|
2020-04-26 14:49:31 +03:00
|
|
|
mut payload := []byte{}
|
2020-04-08 15:22:31 +03:00
|
|
|
if payload_len > 0 {
|
|
|
|
payload = [`0`].repeat(int(payload_len))
|
2020-07-22 17:31:22 +03:00
|
|
|
unsafe {
|
|
|
|
C.memcpy(payload.data, &data[header_len], payload_len)
|
|
|
|
}
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
free(data)
|
|
|
|
}
|
2020-05-26 13:50:37 +03:00
|
|
|
return ws.send_control_frame(.pong, 'PONG', payload)
|
|
|
|
} else if frame.opcode == .pong {
|
2020-04-08 15:22:31 +03:00
|
|
|
if !frame.fin {
|
2020-05-26 13:50:37 +03:00
|
|
|
ws.close(1002, 'control message must not be fragmented')
|
2020-04-08 15:22:31 +03:00
|
|
|
goto free_data
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
free(data)
|
|
|
|
}
|
2020-05-26 13:50:37 +03:00
|
|
|
// got pong
|
2020-04-08 15:22:31 +03:00
|
|
|
return 0
|
2020-05-26 13:50:37 +03:00
|
|
|
} else if frame.opcode == .close {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.debug('read: close')
|
2020-04-08 15:22:31 +03:00
|
|
|
if frame.payload_len > 125 {
|
2020-05-26 13:50:37 +03:00
|
|
|
ws.close(1002, 'control frames must not exceed 125 bytes')
|
2020-04-08 15:22:31 +03:00
|
|
|
goto free_data
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
mut code := 0
|
2020-05-26 13:50:37 +03:00
|
|
|
mut reason := ''
|
2020-04-08 15:22:31 +03:00
|
|
|
if payload_len > 2 {
|
2020-07-22 20:28:53 +03:00
|
|
|
code = (int(unsafe {data[header_len]}) << 8) + int(unsafe {data[header_len + 1]})
|
2020-04-08 15:22:31 +03:00
|
|
|
header_len += 2
|
|
|
|
payload_len -= 2
|
2020-07-22 20:28:53 +03:00
|
|
|
reason = unsafe {string(&data[header_len])}
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.info('Closing with reason: $reason & code: $code')
|
2020-07-22 18:36:24 +03:00
|
|
|
if reason.len > 1 && !utf8.validate(reason.str, reason.len) {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.error('malformed utf8 payload')
|
2020-05-26 13:50:37 +03:00
|
|
|
ws.send_error_event('Recieved malformed utf8.')
|
|
|
|
ws.close(1007, 'malformed utf8 payload')
|
2020-04-08 15:22:31 +03:00
|
|
|
goto free_data
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
2020-05-26 13:50:37 +03:00
|
|
|
unsafe {
|
2020-04-08 15:22:31 +03:00
|
|
|
free(data)
|
|
|
|
}
|
|
|
|
ws.close(code, reason)
|
|
|
|
return 0
|
|
|
|
}
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.error('read: Recieved unsupported opcode: $frame.opcode fin: $frame.fin uri: $ws.uri')
|
|
|
|
ws.send_error_event('Recieved unsupported opcode: $frame.opcode')
|
2020-05-26 13:50:37 +03:00
|
|
|
ws.close(1002, 'Unsupported opcode')
|
2020-04-08 15:22:31 +03:00
|
|
|
free_data:
|
2020-04-23 02:16:16 +03:00
|
|
|
unsafe {
|
2020-04-08 15:22:31 +03:00
|
|
|
free(data)
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut ws Client) send_control_frame(code OPCode, frame_typ string, payload []byte) int {
|
2020-05-26 13:50:37 +03:00
|
|
|
mut bytes_written := -1
|
2020-04-08 15:22:31 +03:00
|
|
|
if ws.socket.sockfd <= 0 {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.error('No socket opened.')
|
2020-06-07 16:19:09 +03:00
|
|
|
unsafe {
|
2020-05-26 13:50:37 +03:00
|
|
|
payload.free()
|
|
|
|
}
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.fatal('send_control_frame: error sending $frame_typ control frame.')
|
2020-04-08 15:22:31 +03:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
header_len := 6
|
|
|
|
frame_len := header_len + payload.len
|
|
|
|
mut control_frame := [`0`].repeat(frame_len)
|
|
|
|
masking_key := create_masking_key()
|
2020-05-27 06:42:48 +03:00
|
|
|
control_frame[0] = byte(code | 0x80)
|
|
|
|
control_frame[1] = byte(payload.len | 0x80)
|
2020-04-08 15:22:31 +03:00
|
|
|
control_frame[2] = masking_key[0]
|
|
|
|
control_frame[3] = masking_key[1]
|
|
|
|
control_frame[4] = masking_key[2]
|
|
|
|
control_frame[5] = masking_key[3]
|
|
|
|
if code == .close {
|
|
|
|
if payload.len > 2 {
|
|
|
|
mut parsed_payload := [`0`].repeat(payload.len + 1)
|
2020-07-22 17:31:22 +03:00
|
|
|
unsafe {
|
|
|
|
C.memcpy(parsed_payload.data, &payload[0], payload.len)
|
|
|
|
}
|
2020-04-08 15:22:31 +03:00
|
|
|
parsed_payload[payload.len] = `\0`
|
2020-05-26 13:50:37 +03:00
|
|
|
for i in 0 .. payload.len {
|
|
|
|
control_frame[6 + i] = (parsed_payload[i] ^ masking_key[i % 4]) & 0xff
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
parsed_payload.free()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-05-26 13:50:37 +03:00
|
|
|
for i in 0 .. payload.len {
|
2020-04-08 15:22:31 +03:00
|
|
|
control_frame[header_len + i] = (payload[i] ^ masking_key[i % 4]) & 0xff
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bytes_written = ws.write_to_server(control_frame.data, frame_len)
|
|
|
|
free_data:
|
|
|
|
unsafe {
|
|
|
|
control_frame.free()
|
|
|
|
payload.free()
|
|
|
|
masking_key.free()
|
|
|
|
}
|
|
|
|
match bytes_written {
|
|
|
|
0 {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.debug('send_control_frame: remote host closed the connection.')
|
2020-04-08 15:22:31 +03:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
-1 {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.error('send_control_frame: error sending $frame_typ control frame.')
|
2020-04-08 15:22:31 +03:00
|
|
|
return -1
|
2020-05-26 13:50:37 +03:00
|
|
|
}
|
|
|
|
else {
|
2020-07-22 17:31:22 +03:00
|
|
|
ws.log.debug('send_control_frame: wrote $bytes_written byte $frame_typ frame.')
|
2020-04-08 15:22:31 +03:00
|
|
|
return bytes_written
|
|
|
|
}
|
|
|
|
}
|
2020-05-26 13:50:37 +03:00
|
|
|
}
|