2020-08-22 01:50:38 +03:00
|
|
|
module websocket
|
|
|
|
|
|
|
|
import rand
|
|
|
|
import crypto.sha1
|
|
|
|
import encoding.base64
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// htonl64 converts payload length to header bits
|
2020-08-22 01:50:38 +03:00
|
|
|
fn htonl64(payload_len u64) []byte {
|
|
|
|
mut ret := []byte{len: 8}
|
|
|
|
ret[0] = byte(((payload_len & (u64(0xff) << 56)) >> 56) & 0xff)
|
|
|
|
ret[1] = byte(((payload_len & (u64(0xff) << 48)) >> 48) & 0xff)
|
|
|
|
ret[2] = byte(((payload_len & (u64(0xff) << 40)) >> 40) & 0xff)
|
|
|
|
ret[3] = byte(((payload_len & (u64(0xff) << 32)) >> 32) & 0xff)
|
|
|
|
ret[4] = byte(((payload_len & (u64(0xff) << 24)) >> 24) & 0xff)
|
|
|
|
ret[5] = byte(((payload_len & (u64(0xff) << 16)) >> 16) & 0xff)
|
|
|
|
ret[6] = byte(((payload_len & (u64(0xff) << 8)) >> 8) & 0xff)
|
|
|
|
ret[7] = byte(((payload_len & (u64(0xff) << 0)) >> 0) & 0xff)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// create_masking_key returs a new masking key to use when masking websocket messages
|
2020-08-22 01:50:38 +03:00
|
|
|
fn create_masking_key() []byte {
|
|
|
|
mask_bit := byte(rand.intn(255))
|
|
|
|
buf := []byte{len: 4, init: `0`}
|
2020-12-28 08:25:46 +03:00
|
|
|
unsafe { C.memcpy(buf.data, &mask_bit, 4) }
|
2020-08-22 01:50:38 +03:00
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2020-12-02 06:02:53 +03:00
|
|
|
// create_key_challenge_response creates a key challange response from security key
|
2020-08-22 01:50:38 +03:00
|
|
|
fn create_key_challenge_response(seckey string) ?string {
|
|
|
|
if seckey.len == 0 {
|
|
|
|
return error('unexpected seckey lengt zero')
|
|
|
|
}
|
|
|
|
guid := '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
|
|
|
|
sha1buf := seckey + guid
|
|
|
|
shabytes := sha1buf.bytes()
|
|
|
|
hash := sha1.sum(shabytes)
|
|
|
|
b64 := base64.encode(tos(hash.data, hash.len))
|
|
|
|
unsafe {
|
|
|
|
hash.free()
|
|
|
|
shabytes.free()
|
|
|
|
}
|
|
|
|
return b64
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// get_nonce creates a randomized array used in handshake process
|
2020-08-22 01:50:38 +03:00
|
|
|
fn get_nonce(nonce_size int) string {
|
|
|
|
mut nonce := []byte{len: nonce_size, cap: nonce_size}
|
|
|
|
alphanum := '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz'
|
|
|
|
for i in 0 .. nonce_size {
|
|
|
|
nonce[i] = alphanum[rand.intn(alphanum.len)]
|
|
|
|
}
|
|
|
|
return tos(nonce.data, nonce.len).clone()
|
|
|
|
}
|