Obsolete
/
ipcd
Archived
3
0
Fork 0

websocketd: v0.1 (no authentication)

master
Philippe PITTOLI 2019-08-01 00:44:38 +02:00
parent da8ca176a5
commit 15db08db0b
7 changed files with 491 additions and 13 deletions

View File

@ -10,25 +10,25 @@ description: |
dependencies: dependencies:
ipc: ipc:
git: https://git.karchnu.fr/JunkOS/ipc.cr git: https://git.karchnu.fr/JunkOS/ipc.cr
branch: v0.2 branch: master
targets: targets:
pongc: pongc:
main: src/pongc.cr main: src/pongc.cr
pongd: pongd:
main: src/pongd.cr main: src/pongd.cr
networkd: networkd:
main: src/main.cr main: src/main.cr
tcpd: tcpd:
main: src/tcpd.cr main: src/tcpd.cr
tcp: tcp:
main: src/tcp.cr main: src/tcp.cr
# webipc: websocketd:
# main: src/webipcd.cr main: src/websocketd.cr
# websocketclient: websocketc:
# main: src/websocket-client.cr main: src/websocketc.cr
# websocketserver:
# main: src/websocket-server.cr
license: ISC license: ISC

View File

@ -2,20 +2,20 @@ require "ipc"
client = IPC::Client.new("pong") client = IPC::Client.new("pong")
# client.send(LibIPC::MessageType::Data, 42, "salut ça va ?") # client.send(42, "salut ça va ?")
client.send(42.to_u8, "salut ça va ?") client.send(42.to_u8, "salut ça va ?")
# # client.send(LibIPC::MessageType::Data, 42, "salut ça va ?") # # client.send(42, "salut ça va ?")
# m = client.read # m = client.read
# #
# puts "message received: #{m.to_s}" # puts "message received: #{m.to_s}"
# #
# sleep 1 # sleep 1
# #
# # client.send(LibIPC::MessageType::Data, 42, "salut ça va ?") # # client.send(42, "salut ça va ?")
# client.send(42.to_u8, "autre truc") # client.send(42.to_u8, "autre truc")
# # client.send(LibIPC::MessageType::Data, 42, "salut ça va ?") # # client.send(42, "salut ça va ?")
# m = client.read # m = client.read
# #
# puts "message received: #{m.to_s}" # puts "message received: #{m.to_s}"

View File

@ -8,8 +8,8 @@ IPC::Service.new ("pong") do |event|
when IPC::Event::Disconnection when IPC::Event::Disconnection
puts "#{CBLUE}IPC::Event::Disconnection#{CRESET}, client: #{event.connection.fd}" puts "#{CBLUE}IPC::Event::Disconnection#{CRESET}, client: #{event.connection.fd}"
when IPC::Event::Message when IPC::Event::Message
puts "#{CGREEN}IPC::Event::Message#{CRESET}" puts "#{CGREEN}IPC::Event::Message#{CRESET}, client: #{event.connection.fd}"
puts event.message.to_s # puts event.message.to_s
event.connection.send event.message event.connection.send event.message
end end
end end

31
src/utils.cr Normal file
View File

@ -0,0 +1,31 @@
require "io/hexdump"
def to_message (user_type : Int, message : String)
payload = Bytes.new (6 + message.size)
# true start
payload[0] = 1.to_u8
payload[1] = 0.to_u8
payload[2] = 0.to_u8
payload[3] = 0.to_u8
payload[4] = message.size.to_u8
# second part: user message
payload[5] = user_type.to_u8
(payload + 6).copy_from message.to_slice
return payload
end
def print_hexa(message : String, aroundmsg : String)
puts "#{aroundmsg} [["
m = IO::Memory.new(message)
io = IO::Hexdump.new(m, output: STDERR, read: true)
buffer = Bytes.new 4000
io.read (buffer) # reading = should print
puts "]] #{aroundmsg}"
end
# str = to_message(42, "coucou")
# print_hexa str, "message sent"
# ws.send str

80
src/websocketc.cr Normal file
View File

@ -0,0 +1,80 @@
require "http/web_socket"
require "option_parser"
require "./colors"
require "./utils"
require "./ws"
uri = "ws://localhost:1234/pong"
OptionParser.parse! do |parser|
parser.on "-u uri", "--uri uri", "URI" do |opturi|
uri = opturi
end
parser.on "-h", "--help", "Show this help" do
puts parser
exit 0
end
end
ws = WebSocket.new(URI.parse(uri))
# HTTP::WebSocket.new(URI.parse("wss://websocket.example.com/chat")) # Creates a new WebSocket with TLS to `websocket.example.com`
# HTTP::WebSocket.new(URI.parse("http://websocket.example.com:8080/chat")) # Creates a new WebSocket to `websocket.example.com` on port `8080`
# HTTP::WebSocket.new(URI.parse("ws://websocket.example.com/chat"), # Creates a new WebSocket to `websocket.example.com` with an Authorization header
# HTTP::Headers{"Authorization" => "Bearer authtoken"})
m = ws.read
if m.nil?
puts "oh no"
exit
end
# puts "message: #{String.new(m)}"
# puts "sending pong"
ws.send "pong".to_slice
m = ws.read
if m.nil?
puts "oh no"
exit
end
# puts "message: #{String.new(m)}"
# puts "sending coucou"
tosend = to_message 2, "coucou"
# print_hexa(String.new(tosend), "#{CBLUE}Sending message hexa#{CRESET}")
ws.send tosend
m = ws.read
if m.nil?
puts "oh no"
exit
end
puts "message: #{String.new(m)}"
# ws.on_message do |message|
# if message == "are we websocket yet?"
# puts "sending pong"
# ws.send "pong\n"
# elsif message.chomp == "OK"
# puts "sending coucou"
# m = to_message 2, "coucou"
# print_hexa(String.new(m), "#{CBLUE}Sending message hexa#{CRESET}")
# ws.send m
# else
# print_hexa message, "#{CORANGE}Receving a message#{CRESET}"
# end
# end
ws.on_close do |socket|
puts "socket is closing"
# socket.close
end
# ws.run
ws.close
ws.read

329
src/websocketd.cr Normal file
View File

@ -0,0 +1,329 @@
require "option_parser"
require "ipc"
require "socket"
require "./colors"
require "./ws"
require "socket"
require "http"
require "http/server"
require "base64"
require "digest"
require "./utils"
class IPC::Connection
def initialize(fd : Int32)
external_connection = LibIPC::Connection.new
external_connection.fd = fd
initialize(external_connection)
end
end
class IPC::Message
def to_buffer
to_message @type, String.new(@payload)
end
end
class WrappedTCPFileDescriptor < TCPSocket
# do not close the connection when garbage collected!!
def finalize
# puts "WrappedTCPFileDescriptor garbage collection!!"
# super
end
end
service_name = "websocket"
port_to_listen = 1234
OptionParser.parse! do |parser|
parser.on "-p port", "--port port", "Port to listen on." do |port|
port_to_listen = port.to_u16
end
parser.on "-s service-name", "--service-name service-name", "Service name." do |name|
service_name = name
end
parser.on "-h", "--help", "Show this help" do
puts parser
exit 0
end
end
class InstanceStorage
property service : IPC::SwitchingService
property fdlist : Hash(Int32,String)
property socklist : Hash(Int32, TCPSocket)
property wssocklist : Hash(Int32, WebSocket)
property switchtable : Hash(Int32, Int32)
property connectionlist : Hash(Int32, IPC::Connection)
def initialize (@service : IPC::SwitchingService)
# fdlist_client = [] of TCPSocket
@fdlist = Hash(Int32,String).new
@socklist = Hash(Int32, TCPSocket).new
@wssocklist = Hash(Int32, WebSocket).new
@switchtable = Hash(Int32, Int32).new
@connectionlist = Hash(Int32, IPC::Connection).new
end
def remove_fd (fdclient : Int32)
# 1. closing both the client and the service
fdservice = @switchtable[fdclient]
# puts "1. fds to close: #{fdclient} and #{fdservice}"
tcpfdc = @socklist[fdclient]
service = @connectionlist[fdservice]
# 2. closing the TCP connections
# puts "2.1 closing fd #{fdclient}"
tcpfdc.close unless tcpfdc.closed?
# puts "2.2 closing fd #{fdservice}"
service.close
# 3. removing the client and the service fds from the loop check
# puts "3.1. removing #{fdclient} from @service"
@service.remove_fd (fdclient)
# puts "3.2. removing #{fdservice} from @service"
@service.remove_fd (fdservice)
# 4. removing the service IPC::Connection from connectionlist
# puts "4. removing fdservice from @connectionlist"
@connectionlist.select do |k, v|
k != fdservice
end
# 5. removing both the client and the service from the switchtable
# puts "5. removing both fd from @switchable"
@switchtable.select do |fdc, fds|
fdc != fdclient && fds != fdclient
end
# 6. removing the client and the service from fdlist
# puts "6. removing both fd from fdlist"
@fdlist.select do |fd,v| fd != fdclient end
@fdlist.select do |fd,v| fd != fdservice end
end
end
server = TCPServer.new("localhost", port_to_listen)
service = IPC::SwitchingService.new service_name
service << server.fd
context = InstanceStorage.new service
def websocket_client_connection(client, context : InstanceStorage)
request = HTTP::Request.from_io client
# pp! request
if request.nil?
puts "#{CRED}eEQUEST IS NIL#{CRESET}"
return
end
if request.is_a? HTTP::Request::BadRequest
puts "#{CRED}BAD REQUEST DAZE~#{CRESET}"
return
end
# FIXME: check they actually wanted to upgrade to websocket
key = request.headers["Sec-WebSocket-Key"]
response_key = Digest::SHA1.base64digest key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
# puts response_key
# HTTP inside bru
headers_header = "HTTP/1.1 101 Switching Protocols"
headers = HTTP::Headers {
"Upgrade" => "websocket",
"Connection" => "Upgrade",
"Sec-WebSocket-Accept" => response_key
}
headers = headers.map { |key, value| "#{key}: #{value[0]}\r\n" }.join
# puts "#{headers_header}\n#{headers.to_s}\r\n"
client.send "#{headers_header}\n#{headers.to_s}\r\n"
wsclient = WebSocket.new client
wsclient.send "are we websocket yet?"
# the client is still not connected to a service
context.fdlist[client.fd] = "not connected"
# listen to the client's file descriptor
context.service << client.fd
# puts "#{CBLUE}new client: #{client.fd}#{CRESET}"
# registering the client into storing structures to avoid being garbage collected
context.socklist[client.fd] = client
context.wssocklist[client.fd] = wsclient
end
def closing_client (fdclient : Int, context : InstanceStorage)
# puts "closing the client #{fdclient}"
context.remove_fd fdclient
end
# first message from the client: requested service name
# 1. extracting the requested service from the message
# 2. connection to the service
# 3. listening on the service fd
# 4. bounding both file descriptors (through switchtable hash)
# 5. indicating that the client is connected (in fdlist)
# 6. sending an ack to the client
def websocket_connection_procedure (message : String, clientfd : Int32, context : InstanceStorage)
begin
# 1. reading the service name from the received message
requested_service = message
# 2. establishing a connection to the service
newservice = IPC::Connection.new requested_service
context.connectionlist[newservice.fd] = newservice
# 3. listening on the service fd
context.service << newservice.fd
# cannot perform automatic switching due to websockets headers
# service.switch.add fdclient, newservice.fd
# 4. bounding the client and the service fd
context.switchtable[clientfd] = newservice.fd
context.switchtable[newservice.fd] = clientfd
# 5. the client is then connected, send it a message
context.fdlist[clientfd] = "connected"
context.fdlist[newservice.fd] = "service"
# 6. ack
wsclient = context.wssocklist[clientfd]
wsclient.send "OK\n"
rescue e
puts "#{CRED}Exception during connection to the service:#{CRESET} #{e}"
closing_client clientfd, context
end
end
def websocket_switching_procedure (activefd : Int, context : InstanceStorage)
begin
if context.fdlist[activefd] == "connected"
# The client is a WebSocket on top of a TCP connection
client = context.socklist[activefd]
wsclient = context.wssocklist[activefd]
message = wsclient.read
if wsclient.closed?
# puts "#{CBLUE}client is closed#{CRESET}"
closing_client client.fd, context
return
end
if message.nil?
puts "#{CRED}message is nil#{CRESET}"
closing_client client.fd, context
return
end
# puts "switching: client to service"
# print_hexa(String.new(message), "#{CBLUE}Received message hexa#{CRESET}")
# client => service
fdservice = context.switchtable[activefd]
# XXX: this is not a TCP fd, but since behind the scene this is compatible, I'm hacking a bit
serv = WrappedTCPFileDescriptor.new(fd: fdservice, family: Socket::Family::INET)
#serv = context.connectionlist[fdservice]
serv.send message
elsif context.fdlist[activefd] == "service"
# puts "switching: service to client"
# service => client
fdclient = context.switchtable[activefd]
wsclient = context.wssocklist[fdclient]
serv = context.connectionlist[activefd]
message = serv.read
# puts "received message from service: #{message.to_s}"
buf = message.to_buffer
# print_hexa String.new(buf), "\033[31m Message to send to the client \033[00m "
wsclient.send buf
else
raise "cannot understand if the fd is a client or a service"
end
rescue e
puts "#{CRED}Exception during message transfer:#{CRESET} #{e}"
if context.fdlist[activefd] == "service"
clientfd = context.switchtable[activefd]
closing_client clientfd, context
elsif context.fdlist[activefd] == "service"
closing_client activefd, context
end
end
end
service.loop do |event|
case event
when IPC::Event::Connection
puts "#{CBLUE}IPC::Event::Connection: #{event.connection.fd}#{CRESET}"
when IPC::Event::Disconnection
puts "#{CBLUE}IPC::Event::Disconnection: #{event.connection.fd}#{CRESET}"
when IPC::Event::ExtraSocket
puts "#{CBLUE}IPC::Event::ExtraSocket#{CRESET}"
# 1. accept new websocket clients
if server.fd == event.connection.fd
client = server.accept
websocket_client_connection client, context
puts "#{CBLUE}new client:#{CRESET} #{client.fd}"
next
end
# 2. active fd != server fd
# is this a service or a client?
activefd = event.connection.fd
if context.fdlist[activefd] == "not connected"
# since it's an external communication
# we have to read the message here, it's not handled by default in libipc
wsclient = context.wssocklist[activefd]
message = wsclient.read
if message.nil?
puts "#{CBLUE}disconnection of client #{event.connection.fd}#{CRESET}"
closing_client activefd, context
next
end
# requested service, fd
websocket_connection_procedure String.new(message), activefd, context
elsif context.fdlist[activefd] == "connected"
# the service is sending a message
websocket_switching_procedure activefd, context
elsif context.fdlist[activefd] == "service"
# the service is sending a message
websocket_switching_procedure activefd, context
else
raise "should not happen: client not recorded in fdlist"
end
when IPC::Event::Switch
puts "\033[36mIPC::Event::Switch#{CRESET}: from fd #{event.connection.fd}"
raise "Not implemented."
# IPC::Event::Message has to be the last entry
# because ExtraSocket and Switch inherit from Message class
when IPC::Event::Message
puts "#{CBLUE}IPC::Event::Message#{CRESET}: #{event.connection.fd}"
raise "Not implemented."
end
end

38
src/ws.cr Normal file
View File

@ -0,0 +1,38 @@
require "http"
require "http/web_socket/protocol"
class WebSocket < HTTP::WebSocket
getter? closed = false
def read
size = 0
begin
info = @ws.receive(@buffer)
size = info.size
rescue IO::EOFError
close
return nil
end
case info.opcode
when Protocol::Opcode::TEXT
return @buffer[0..size-1]
when Protocol::Opcode::BINARY
return @buffer[0..size-1]
when Protocol::Opcode::CLOSE
begin
close
rescue e
puts "\033[31mwebsocket failed to close properly\033[00m #{e}"
end
return nil
end
end
def finalize
# puts "WrappedTCPFileDescriptor garbage collection!!"
# super
end
end