Skip to content

Commit

Permalink
replace utils module with direct generate calls (#148)
Browse files Browse the repository at this point in the history
The `utils` module uses a convoluted mechanism for filling byte arrays
with random data. The `generate` function can be used directly, making
the `utils` module obsolete.
  • Loading branch information
etan-status committed Jul 24, 2023
1 parent 2c3ae31 commit f8ed9b4
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 62 deletions.
5 changes: 3 additions & 2 deletions tests/extensions/testextflow.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## nim-websock
## Copyright (c) 2021 Status Research & Development GmbH
## Copyright (c) 2021-2023 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
Expand Down Expand Up @@ -118,10 +118,11 @@ suite "Encode frame extensions flow":
frame.opcode == Opcode.Binary

suite "Decode frame extensions flow":
let rng = HmacDrbgContext.new()
var
address: TransportAddress
server: StreamServer
maskKey = genMaskKey(HmacDrbgContext.new())
maskKey = MaskKey.random(rng[])
transport: StreamTransport
reader: AsyncStreamReader
frame: Frame
Expand Down
4 changes: 2 additions & 2 deletions tests/helpers.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## nim-websock
## Copyright (c) 2021 Status Research & Development GmbH
## Copyright (c) 2021-2023 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
Expand Down Expand Up @@ -88,7 +88,7 @@ proc connectClient*(
onPing: ControlCb = nil,
onPong: ControlCb = nil,
onClose: CloseCb = nil,
rng: Rng = nil): Future[WSSession] {.async.} =
rng = HmacDrbgContext.new()): Future[WSSession] {.async.} =
let secure = when defined secure: true else: false
return await WebSocket.connect(
host = address,
Expand Down
3 changes: 1 addition & 2 deletions tests/testframes.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## nim-websock
## Copyright (c) 2021 Status Research & Development GmbH
## Copyright (c) 2021-2023 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
Expand All @@ -12,7 +12,6 @@ import
pkg/chronos/unittest2/asynctests

include ../websock/frame
include ../websock/utils

# TODO: Fix Test.

Expand Down
7 changes: 4 additions & 3 deletions tests/testwebsockets.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## nim-websock
## Copyright (c) 2021-2022 Status Research & Development GmbH
## Copyright (c) 2021-2023 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
Expand Down Expand Up @@ -733,6 +733,7 @@ suite "Test Closing":

suite "Test Payload":
setup:
let rng = HmacDrbgContext.new()
var
server: HttpServer

Expand Down Expand Up @@ -837,7 +838,7 @@ suite "Test Payload":
address = initTAddress("127.0.0.1:8888"),
frameSize = maxFrameSize)

let maskKey = genMaskKey(HmacDrbgContext.new())
let maskKey = MaskKey.random(rng[])
await session.stream.writer.write(
(await Frame(
fin: false,
Expand Down Expand Up @@ -897,7 +898,7 @@ suite "Test Payload":
pong = true
)

let maskKey = genMaskKey(HmacDrbgContext.new())
let maskKey = MaskKey.random(rng[])
await session.stream.writer.write(
(await Frame(
fin: false,
Expand Down
9 changes: 5 additions & 4 deletions websock/session.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## nim-websock
## Copyright (c) 2021-2022 Status Research & Development GmbH
## Copyright (c) 2021-2023 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
Expand All @@ -11,7 +11,7 @@

import std/strformat
import pkg/[chronos, chronicles, stew/byteutils, stew/endians2]
import ./types, ./frame, ./utils, ./utf8dfa, ./http
import ./types, ./frame, ./utf8dfa, ./http

import pkg/chronos/streams/asyncstream

Expand Down Expand Up @@ -114,8 +114,9 @@ proc nonCancellableSend(

trace "Sending data to remote"

let maskKey = if ws.masked:
genMaskKey(ws.rng)
let maskKey =
if ws.masked:
MaskKey.random(ws.rng[])
else:
default(MaskKey)

Expand Down
12 changes: 8 additions & 4 deletions websock/types.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## nim-websock
## Copyright (c) 2021-2022 Status Research & Development GmbH
## Copyright (c) 2021-2023 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
Expand All @@ -13,11 +13,11 @@ import std/deques
import pkg/[chronos,
chronos/streams/tlsstream,
chronos/apps/http/httptable,
bearssl/rand,
httputils,
stew/results]
import ./utils

export deques
export deques, rand

const
SHA1DigestSize* = 20
Expand Down Expand Up @@ -55,6 +55,7 @@ type
HeaderFlags* = set[HeaderFlag]

MaskKey* = array[4, char]
WebSecKey* = array[16, byte]

Frame* = ref object
fin*: bool ## Indicates that this is the final fragment in a message.
Expand Down Expand Up @@ -89,7 +90,7 @@ type
masked*: bool # send masked packets
binary*: bool # is payload binary?
flags*: set[TLSFlags]
rng*: Rng
rng*: ref HmacDrbgContext
frameSize*: int # max frame buffer size
onPing*: ControlCb
onPong*: ControlCb
Expand Down Expand Up @@ -212,3 +213,6 @@ method encode*(self: Ext, frame: Frame): Future[Frame] {.base, async.} =

method toHttpOptions*(self: Ext): string {.base, gcsafe.} =
raiseAssert "Not implemented!"

func random*(T: typedesc[MaskKey|WebSecKey], rng: var HmacDrbgContext): T =
rng.generate(result)
37 changes: 0 additions & 37 deletions websock/utils.nim

This file was deleted.

15 changes: 7 additions & 8 deletions websock/websock.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import pkg/[chronos,
stew/base10,
nimcrypto/sha]

import ./utils, ./frame, ./session, /types, ./http, ./extensions/extutils
import ./frame, ./session, /types, ./http, ./extensions/extutils

export utils, session, frame, types, http, httptable
export session, frame, types, http, httptable

logScope:
topics = "websock ws-server"
Expand Down Expand Up @@ -117,11 +117,10 @@ proc connect*(
onPing: ControlCb = nil,
onPong: ControlCb = nil,
onClose: CloseCb = nil,
rng: Rng = nil): Future[WSSession] {.async.} =
rng = HmacDrbgContext.new()): Future[WSSession] {.async.} =

let
rng = if isNil(rng): HmacDrbgContext.new() else: rng
key = Base64Pad.encode(genWebSecKey(rng))
key = Base64Pad.encode(WebSecKey.random(rng[]))
hostname = if hostName.len > 0: hostName else: $host

let client = if secure:
Expand Down Expand Up @@ -216,7 +215,7 @@ proc connect*(
onPing: ControlCb = nil,
onPong: ControlCb = nil,
onClose: CloseCb = nil,
rng: Rng = nil): Future[WSSession]
rng = HmacDrbgContext.new()): Future[WSSession]
{.raises: [Defect, WSWrongUriSchemeError].} =
## Create a new websockets client
## using a Uri
Expand Down Expand Up @@ -359,12 +358,12 @@ proc new*(
onPing: ControlCb = nil,
onPong: ControlCb = nil,
onClose: CloseCb = nil,
rng: Rng = nil): WSServer =
rng = HmacDrbgContext.new()): WSServer =

return WSServer(
protocols: @protos,
masked: false,
rng: if isNil(rng): HmacDrbgContext.new() else: rng,
rng: rng,
frameSize: frameSize,
factories: @factories,
onPing: onPing,
Expand Down

0 comments on commit f8ed9b4

Please sign in to comment.