websocket: add Client.nonce_size field

pull/5149/head
Delyan Angelov 2020-06-01 20:31:45 +03:00
parent 0c7bac4ba7
commit 328a235f94
2 changed files with 7 additions and 6 deletions

View File

@ -42,11 +42,11 @@ fn create_key_challenge_response(seckey string) string {
return b64 return b64
} }
fn get_nonce() string { fn get_nonce(nonce_size int) string {
mut nonce := []byte{} mut nonce := []byte{len: nonce_size, cap: nonce_size}
alphanum := '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz' alphanum := '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz'
for _ in 0 .. 18 { for i in 0 .. nonce_size {
nonce << alphanum[rand.next(61)] nonce[i] = alphanum[rand.next(61)]
} }
return string(byteptr(nonce.data)) return tos(nonce.data, nonce.len).clone()
} }

View File

@ -19,6 +19,7 @@ pub struct Client {
// cwebsocket_subprotocol *subprotocol; // cwebsocket_subprotocol *subprotocol;
// cwebsocket_subprotocol *subprotocols[]; // cwebsocket_subprotocol *subprotocols[];
mut: mut:
nonce_size int = 18 // you can try 16 too
lock &sync.Mutex = sync.new_mutex() lock &sync.Mutex = sync.new_mutex()
write_lock &sync.Mutex = sync.new_mutex() write_lock &sync.Mutex = sync.new_mutex()
state State state State
@ -137,7 +138,7 @@ pub fn (mut ws Client) connect() int {
ws.state = .connecting ws.state = .connecting
ws.lock.unlock() ws.lock.unlock()
uri := ws.parse_uri() uri := ws.parse_uri()
nonce := get_nonce() nonce := get_nonce(ws.nonce_size)
seckey := base64.encode(nonce) seckey := base64.encode(nonce)
ai_family := C.AF_INET ai_family := C.AF_INET
ai_socktype := C.SOCK_STREAM ai_socktype := C.SOCK_STREAM