Skip to content

Instantly share code, notes, and snippets.

@syusui-s
Last active June 16, 2020 11:56
Show Gist options
  • Select an option

  • Save syusui-s/fe799c3da793e9385d1d1341f5fb604e to your computer and use it in GitHub Desktop.

Select an option

Save syusui-s/fe799c3da793e9385d1d1341f5fb604e to your computer and use it in GitHub Desktop.
# curl 'http://ws.example.com/ws' \
# -H 'Pragma: no-cache' \
# -H 'Origin: http://ws.example.com/' \
# -H 'Accept-Language: ja-JP,ja;q=0.9,en;q=0.8' \
# -H 'Sec-WebSocket-Key: BASE64_NONCE' \
# -H 'Upgrade: websocket' \
# -H 'Sec-WebSocket-Version: 13' \
# --compressed \
# --output ~/out
#
# Notice: 'Sec-Websocket-Accept' response header will not be verified.
WebSocketOpcode = {
0 => :Continuation,
1 => :Text,
2 => :Binary,
8 => :ConnectionClose,
9 => :Ping,
10 => :Pong,
}
WebSocketPacket = Struct.new(:fin, :rsv1, :rsv2, :rsv3, :opcode, :mask, :payload_length, :masking_key, :payload) do
class << self
def read(io)
first_octet, second_octet = io.read(2).bytes
fin = (first_octet & 0b1000_0000) != 0
rsv1 = (first_octet & 0b0100_0000) != 0
rsv2 = (first_octet & 0b0010_0000) != 0
rsv3 = (first_octet & 0b0001_0000) != 0
opcode = (first_octet & 0b0000_1111)
mask = (second_octet & 0b1000_0000) != 0
payload_length = (second_octet & 0b0111_1111)
case payload_length
when 126
payload_length = bytes_to_int(io.read(2).bytes)
when 127
payload_length = bytes_to_int(io.read(8).bytes)
end
masking_key = nil
if mask
masking_key = bytes_to_int(io.read(4).bytes)
end
payload = io.read(payload_length)
self.new(fin, rsv1, rsv2, rsv3, opcode, mask, payload_length, masking_key, payload)
end
private
# Network byte order
def bytes_to_int(bytes)
bytes.reduce(0) {|accum, b| (accum << 8) + b }
end
end
end
#file = ARGV[0]
file = File.expand_path("~/out")
File.open(file, "r") do |input|
until input.eof?
pkt = WebSocketPacket.read(input)
File.open("~/outws.mp4", "a") do |out|
out.write(pkt.payload)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment