websocket: update to work with latest V
parent
998fc8fc07
commit
f3e3d7e0c5
|
@ -3,7 +3,7 @@ module websocket
|
||||||
fn (ws mut Client) read_handshake(seckey string){
|
fn (ws mut Client) read_handshake(seckey string){
|
||||||
l.d("reading handshake...")
|
l.d("reading handshake...")
|
||||||
mut bytes_read := 0
|
mut bytes_read := 0
|
||||||
max_buffer := 256
|
max_buffer := 1024
|
||||||
buffer_size := 1
|
buffer_size := 1
|
||||||
mut buffer := malloc(max_buffer)
|
mut buffer := malloc(max_buffer)
|
||||||
|
|
||||||
|
@ -37,21 +37,24 @@ fn (ws mut Client) handshake_handler(handshake_response, seckey string){
|
||||||
keys := lines[i].split(":")
|
keys := lines[i].split(":")
|
||||||
|
|
||||||
match keys[0] {
|
match keys[0] {
|
||||||
"Upgrade" {
|
"Upgrade", "upgrade" {
|
||||||
ws.flags << Flag.has_upgrade
|
ws.flags << .has_upgrade
|
||||||
}
|
}
|
||||||
"Connection" {
|
"Connection", "connection" {
|
||||||
ws.flags << Flag.has_connection
|
ws.flags << .has_connection
|
||||||
}
|
}
|
||||||
"Sec-WebSocket-Accept" {
|
"Sec-WebSocket-Accept", "sec-websocket-accept" {
|
||||||
l.d("comparing hashes")
|
l.d("comparing hashes")
|
||||||
response := create_key_challenge_response(seckey)
|
l.d("seckey: ${seckey}")
|
||||||
if keys[1].trim_space() != response {
|
challenge := create_key_challenge_response(seckey)
|
||||||
|
l.d("challenge: ${challenge}")
|
||||||
|
l.d("response: ${keys[1]}")
|
||||||
|
if keys[1].trim_space() != challenge {
|
||||||
l.e("handshake_handler: Sec-WebSocket-Accept header does not match computed sha1/base64 response.")
|
l.e("handshake_handler: Sec-WebSocket-Accept header does not match computed sha1/base64 response.")
|
||||||
}
|
}
|
||||||
ws.flags << Flag.has_accept
|
ws.flags << .has_accept
|
||||||
unsafe {
|
unsafe {
|
||||||
response.free()
|
challenge.free()
|
||||||
}
|
}
|
||||||
} else {}
|
} else {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,9 +10,9 @@ struct C.SSL
|
||||||
struct C.SSL_METHOD
|
struct C.SSL_METHOD
|
||||||
fn C.SSL_load_error_strings()
|
fn C.SSL_load_error_strings()
|
||||||
fn C.SSL_library_init()
|
fn C.SSL_library_init()
|
||||||
fn C.SSLv23_client_method() &SSL_METHOD
|
fn C.SSLv23_client_method() &C.SSL_METHOD
|
||||||
fn C.SSL_CTX_new() &SSL_CTX
|
fn C.SSL_CTX_new() &C.SSL_CTX
|
||||||
fn C.SSL_new() &SSL
|
fn C.SSL_new() &C.SSL
|
||||||
fn C.SSL_set_fd() int
|
fn C.SSL_set_fd() int
|
||||||
fn C.SSL_connect() int
|
fn C.SSL_connect() int
|
||||||
fn C.SSL_shutdown()
|
fn C.SSL_shutdown()
|
||||||
|
@ -26,21 +26,21 @@ fn (ws mut Client) connect_ssl(){
|
||||||
C.SSL_load_error_strings()
|
C.SSL_load_error_strings()
|
||||||
C.SSL_library_init()
|
C.SSL_library_init()
|
||||||
|
|
||||||
ws.sslctx = SSL_CTX_new(SSLv23_client_method())
|
ws.sslctx = C.SSL_CTX_new(C.SSLv23_client_method())
|
||||||
if ws.sslctx == C.NULL {
|
if ws.sslctx == C.NULL {
|
||||||
l.f("Couldn't get ssl context")
|
l.f("Couldn't get ssl context")
|
||||||
}
|
}
|
||||||
|
|
||||||
ws.ssl = SSL_new(ws.sslctx)
|
ws.ssl = C.SSL_new(ws.sslctx)
|
||||||
if ws.ssl == C.NULL {
|
if ws.ssl == C.NULL {
|
||||||
l.f("Couldn't create OpenSSL instance.")
|
l.f("Couldn't create OpenSSL instance.")
|
||||||
}
|
}
|
||||||
|
|
||||||
if SSL_set_fd(ws.ssl, ws.socket.sockfd) != 1 {
|
if C.SSL_set_fd(ws.ssl, ws.socket.sockfd) != 1 {
|
||||||
l.f("Couldn't assign ssl to socket.")
|
l.f("Couldn't assign ssl to socket.")
|
||||||
}
|
}
|
||||||
|
|
||||||
if SSL_connect(ws.ssl) != 1 {
|
if C.SSL_connect(ws.ssl) != 1 {
|
||||||
l.f("Couldn't connect using SSL.")
|
l.f("Couldn't connect using SSL.")
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -49,7 +49,7 @@ fn (s mut Utf8State) seq(r0 bool, r1 bool, is_tail bool) bool {
|
||||||
fn (s mut Utf8State) next_state (c byte) {
|
fn (s mut Utf8State) next_state (c byte) {
|
||||||
//sequence 1
|
//sequence 1
|
||||||
if s.index == 0 {
|
if s.index == 0 {
|
||||||
if ((c >= 0x00 + 1 && c <= 0x7F) || c == 0x00) {
|
if (c >= 0x00 + 1 && c <= 0x7F) || c == 0x00 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.index++
|
s.index++
|
||||||
|
|
|
@ -48,7 +48,7 @@ fn create_key_challenge_response(seckey string) string {
|
||||||
fn get_nonce() string {
|
fn get_nonce() string {
|
||||||
mut nonce := []byte
|
mut nonce := []byte
|
||||||
alphanum := "0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz"
|
alphanum := "0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz"
|
||||||
for i in 0..16 {
|
for i in 0..18 {
|
||||||
nonce << alphanum[rand.next(61)]
|
nonce << alphanum[rand.next(61)]
|
||||||
}
|
}
|
||||||
return string(byteptr(nonce.data))
|
return string(byteptr(nonce.data))
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
encoding.base64
|
encoding.base64
|
||||||
eventbus
|
eventbus
|
||||||
sync
|
sync
|
||||||
logger
|
net.websocket.logger
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -144,8 +144,10 @@ pub fn (ws mut Client) connect() int {
|
||||||
ai_family := C.AF_INET
|
ai_family := C.AF_INET
|
||||||
ai_socktype := C.SOCK_STREAM
|
ai_socktype := C.SOCK_STREAM
|
||||||
|
|
||||||
|
l.d("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"
|
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"
|
||||||
|
l.d(handshake)
|
||||||
|
|
||||||
socket := net.new_socket(ai_family, ai_socktype, 0) or {
|
socket := net.new_socket(ai_family, ai_socktype, 0) or {
|
||||||
l.f(err)
|
l.f(err)
|
||||||
return -1
|
return -1
|
||||||
|
@ -215,10 +217,10 @@ pub fn (ws mut Client) close(code int, message string){
|
||||||
}
|
}
|
||||||
|
|
||||||
if ws.ssl != C.NULL {
|
if ws.ssl != C.NULL {
|
||||||
SSL_shutdown(ws.ssl)
|
C.SSL_shutdown(ws.ssl)
|
||||||
SSL_free(ws.ssl)
|
C.SSL_free(ws.ssl)
|
||||||
if ws.sslctx != C.NULL {
|
if ws.sslctx != C.NULL {
|
||||||
SSL_CTX_free(ws.sslctx)
|
C.SSL_CTX_free(ws.sslctx)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if C.shutdown(ws.socket.sockfd, C.SHUT_WR) == -1 {
|
if C.shutdown(ws.socket.sockfd, C.SHUT_WR) == -1 {
|
||||||
|
|
Loading…
Reference in New Issue