websocketd updated to new libipc API

master
Karchnu 2020-08-23 17:30:54 +02:00
parent 6355596d6d
commit 5839ba98b4
1 changed files with 118 additions and 108 deletions

View File

@ -14,34 +14,48 @@ require "./utils"
# All modifications to standard libraries go there. # All modifications to standard libraries go there.
require "./lib_modifications.cr" require "./lib_modifications.cr"
# service instance parameters class Context
# they can be changed via the cli # service instance parameters
service_name = "websocket" # they can be changed via the cli
host = "0.0.0.0" class_property service_name = "websocket"
port_to_listen = 1234 class_property host = "0.0.0.0"
timer_delay = 30.to_f64 class_property port_to_listen : UInt16 = 1234
class_property timer_delay : Int32 = 30_000.to_i32
class_property verbosity = 1
end
def debug(message : String)
puts "#{CGREEN}#{message}#{CRESET}"
end
def info(message : String)
puts "#{CBLUE}#{message}#{CRESET}"
end
def danger(message : String)
puts "#{CORANGE}#{message}#{CRESET}"
end
verbosity = 1
OptionParser.parse do |parser| OptionParser.parse do |parser|
parser.on "-l host", "--l host", "IP address to listen on." do |h| parser.on "-l host", "--l host", "IP address to listen on." do |h|
host = h Context.host = h
end end
parser.on "-p port", "--port port", "Port to listen on." do |port| parser.on "-p port", "--port port", "Port to listen on." do |port|
port_to_listen = port.to_u16 Context.port_to_listen = port.to_u16
end end
parser.on "-s service-name", "--service-name service-name", "Service name." do |name| parser.on "-s service-name", "--service-name service-name", "Service name." do |name|
service_name = name Context.service_name = name
end end
parser.on "-t timer-delay", "--timer-delay timer-delay", "Timer delay (in seconds)" do |t| parser.on "-t timer-delay", "--timer-delay timer-delay", "Timer delay (in seconds)" do |t|
timer_delay = t.to_f64 Context.timer_delay = t.to_i32 * 1000
end end
parser.on "-v verbosity-level", "--verbosity level", "Verbosity." do |opt| parser.on "-v verbosity-level", "--verbosity level", "Verbosity." do |opt|
verbosity = opt.to_i Context.verbosity = opt.to_i
end end
parser.on "-h", "--help", "Show this help" do parser.on "-h", "--help", "Show this help" do
@ -51,7 +65,7 @@ OptionParser.parse do |parser|
end end
# Link between fd and TCPSocket, WebSocket and IPC::Connection instances. # Link between fd and TCPSocket, WebSocket and IPC::Client instances.
class InstanceStorage class InstanceStorage
property service : IPC::SwitchingService property service : IPC::SwitchingService
property switchtable : Hash(Int32, Int32) property switchtable : Hash(Int32, Int32)
@ -59,7 +73,7 @@ class InstanceStorage
property is_json : Hash(Int32, Bool) property is_json : Hash(Int32, Bool)
property fd_to_tcpsocket : Hash(Int32, TCPSocket) property fd_to_tcpsocket : Hash(Int32, TCPSocket)
property fd_to_websocket : Hash(Int32, WebSocket) property fd_to_websocket : Hash(Int32, WebSocket)
property fd_to_ipcconnection : Hash(Int32, IPC::Connection) property fd_to_ipcclient : Hash(Int32, IPC::Client)
def initialize (@service : IPC::SwitchingService) def initialize (@service : IPC::SwitchingService)
# fdlist_client = [] of TCPSocket # fdlist_client = [] of TCPSocket
@ -68,11 +82,11 @@ class InstanceStorage
@is_json = Hash(Int32,Bool).new @is_json = Hash(Int32,Bool).new
@fd_to_tcpsocket = Hash(Int32, TCPSocket).new @fd_to_tcpsocket = Hash(Int32, TCPSocket).new
@fd_to_websocket = Hash(Int32, WebSocket).new @fd_to_websocket = Hash(Int32, WebSocket).new
@fd_to_ipcconnection = Hash(Int32, IPC::Connection).new @fd_to_ipcclient = Hash(Int32, IPC::Client).new
end end
def remove_fd (fdclient : Int32) def remove_fd (fdclient : Int32)
puts "#{CBLUE}closing the client:#{CRESET} #{fdclient}" info "closing the client:#{CRESET} #{fdclient}"
# 1. closing both the client and the service # 1. closing both the client and the service
fdservice = @switchtable[fdclient]? fdservice = @switchtable[fdclient]?
tcpfdc = @fd_to_tcpsocket[fdclient] tcpfdc = @fd_to_tcpsocket[fdclient]
@ -97,10 +111,10 @@ class InstanceStorage
end end
unless fdservice.nil? unless fdservice.nil?
service = @fd_to_ipcconnection[fdservice] service = @fd_to_ipcclient[fdservice]
service.close service.close
@service.remove_fd (fdservice) @service.remove_fd (fdservice)
@fd_to_ipcconnection = @fd_to_ipcconnection.select do |k, v| @fd_to_ipcclient = @fd_to_ipcclient.select do |k, v|
k != fdservice k != fdservice
end end
@ -109,15 +123,16 @@ class InstanceStorage
end end
end end
class Context
class_property service = IPC::SwitchingService.new service_name
class_property context = InstanceStorage.new service
end
# by default, listen on any IP address # by default, listen on any IP address
server = TCPServer.new(host, port_to_listen) server = TCPServer.new(Context.host, Context.port_to_listen)
service = IPC::SwitchingService.new service_name Context.service << server.fd
service << server.fd
context = InstanceStorage.new service
def websocket_client_connection(client)
def websocket_client_connection(client, context : InstanceStorage)
request = HTTP::Request.from_io client request = HTTP::Request.from_io client
# pp! request # pp! request
@ -150,7 +165,7 @@ def websocket_client_connection(client, context : InstanceStorage)
headers = headers.map { |key, value| "#{key}: #{value[0]}\r\n" }.join headers = headers.map { |key, value| "#{key}: #{value[0]}\r\n" }.join
# requested service, fd # requested service, fd
req_service = request.path.lchop req_service = request.path.lchop.sub "ws/", ""
if req_service.empty? if req_service.empty?
client.close client.close
return return
@ -160,24 +175,30 @@ def websocket_client_connection(client, context : InstanceStorage)
# To that end, the client may send the name of the service it wants to reach with the prefix ".JSON". # To that end, the client may send the name of the service it wants to reach with the prefix ".JSON".
if req_service.ends_with? ".JSON" if req_service.ends_with? ".JSON"
context.is_json[client.fd] = true Context.context.is_json[client.fd] = true
req_service = req_service.gsub /.JSON$/, "" req_service = req_service.gsub /.JSON$/, ""
else else
context.is_json[client.fd] = false Context.context.is_json[client.fd] = false
end end
websocket_connection_procedure req_service, client.fd, context # Hack to preserve the daemon naming convention.
# TODO: if trackerd, send the IP address of the client
if req_service == "tracker" if req_service == "tracker"
puts "tracker - sending the IP address" req_service = "tracking"
puts "connection from #{client.remote_address}" end
sfd = context.switchtable[client.fd]
websocket_connection_procedure req_service, client.fd
# If the requested service is trackingd, send the IP address of the client
if req_service == "tracking"
real_ip_address = request.headers["X-Real-IP"] || client.remote_address.address
sfd = Context.context.switchtable[client.fd]
info "trackingd - sending the IP address #{real_ip_address} to fd #{sfd}"
# message = IPC::Message.from_json(JSON).to_packet # message = IPC::Message.from_json(JSON).to_packet
# => JSON has to include these attributes: mtype, utype, payload # => JSON has to include these attributes: mtype, utype, payload
# message = IPC::Message.new mtype, utype, payload # message = IPC::Message.new mtype, utype, payload
remote_address = client.remote_address.address remote_address = client.remote_address.address
message = IPC::Message.new 1, 1.to_u8, "{\"ipaddress\": \"#{remote_address}\"}" message = IPC::Message.new sfd, 1, 1.to_u8, "{\"ipaddress\": \"#{remote_address}\"}"
serv = WrappedTCPFileDescriptor.new(fd: sfd, family: Socket::Family::INET) serv = WrappedTCPFileDescriptor.new(fd: sfd, family: Socket::Family::INET)
serv.send message.to_packet serv.send message.to_packet
end end
@ -187,22 +208,21 @@ def websocket_client_connection(client, context : InstanceStorage)
wsclient = WebSocket.new client wsclient = WebSocket.new client
wsclient.on_pong do |m| wsclient.on_pong do |m|
puts "pong #{m}" debug "pong #{m}"
end end
context.is_client[client.fd] = true Context.context.is_client[client.fd] = true
# listen to the client's file descriptor # listen to the client's file descriptor
context.service << client.fd Context.context.service << client.fd
# puts "#{CBLUE}new client: #{client.fd}#{CRESET}" # puts "#{CBLUE}new client: #{client.fd}#{CRESET}"
# registering the client into storing structures to avoid being garbage collected # registering the client into storing structures to avoid being garbage collected
context.fd_to_tcpsocket[client.fd] = client Context.context.fd_to_tcpsocket[client.fd] = client
context.fd_to_websocket[client.fd] = wsclient Context.context.fd_to_websocket[client.fd] = wsclient
end end
def closing_client (fdclient : Int)
def closing_client (fdclient : Int, context : InstanceStorage) Context.context.remove_fd fdclient
context.remove_fd fdclient
end end
# first message from the client: requested service name # first message from the client: requested service name
@ -210,44 +230,45 @@ end
# 2. listening on the service fd # 2. listening on the service fd
# 3. bounding both file descriptors (through switchtable hash) # 3. bounding both file descriptors (through switchtable hash)
# 4. indicating that the client is connected (in is_client) # 4. indicating that the client is connected (in is_client)
def websocket_connection_procedure (requested_service : String, clientfd : Int32, context : InstanceStorage) def websocket_connection_procedure (requested_service : String, clientfd : Int32)
begin begin
# 2. establishing a connection to the service # 1. establishing a connection to the service
newservice = IPC::Connection.new requested_service newservice = IPC::Client.new requested_service
context.fd_to_ipcconnection[newservice.fd] = newservice new_service_fd = newservice.fd.not_nil!
Context.context.fd_to_ipcclient[new_service_fd] = newservice
# 3. listening on the client fd and the service fd # 2. listening on the client fd and the service fd
context.service << newservice.fd Context.context.service << new_service_fd
# cannot perform automatic switching due to websockets headers # cannot perform automatic switching due to websockets headers
# future version of the libipc lib should include some workaround, probably # future version of the libipc lib should include some workaround, probably
# service.switch.add fdclient, newservice.fd # service.switch.add fdclient, newservice.fd
# 4. bounding the client and the service fd # 3. bounding the client and the service fd
context.switchtable[clientfd] = newservice.fd Context.context.switchtable[clientfd] = new_service_fd
context.switchtable[newservice.fd] = clientfd Context.context.switchtable[new_service_fd] = clientfd
# 5. the client is then connected, send it a message # 4. the client is then connected, send it a message
context.is_client[clientfd] = true Context.context.is_client[clientfd] = true
context.is_client[newservice.fd] = false Context.context.is_client[new_service_fd] = false
rescue e rescue e
puts "#{CRED}Exception during connection to the service:#{CRESET} #{e}" puts "#{CRED}Exception during connection to the service:#{CRESET} #{e}"
context.remove_fd clientfd Context.context.remove_fd clientfd
end end
end end
def websocket_switching_procedure (activefd : Int, context : InstanceStorage) def websocket_switching_procedure (activefd : Int)
begin begin
if context.is_client[activefd] if Context.context.is_client[activefd]
# The client is a WebSocket on top of a TCP connection # The client is a WebSocket on top of a TCP connection
client = context.fd_to_tcpsocket[activefd] client = Context.context.fd_to_tcpsocket[activefd]
wsclient = context.fd_to_websocket[activefd] wsclient = Context.context.fd_to_websocket[activefd]
loop do loop do
begin begin
message = wsclient.run_once message = wsclient.run_once
rescue e rescue e
puts "#{CRED}Exception (receiving a message)#{CRESET} #{e}" puts "#{CRED}Exception (receiving a message)#{CRESET} #{e}"
context.remove_fd activefd Context.context.remove_fd activefd
return return
end end
@ -256,20 +277,20 @@ def websocket_switching_procedure (activefd : Int, context : InstanceStorage)
if wsclient.closed? if wsclient.closed?
# puts "#{CBLUE}client is closed#{CRESET}" # puts "#{CBLUE}client is closed#{CRESET}"
context.remove_fd client.fd Context.context.remove_fd client.fd
return return
end end
if message.nil? if message.nil?
puts "#{CRED}message is nil#{CRESET}" puts "#{CRED}message is nil#{CRESET}"
context.remove_fd client.fd Context.context.remove_fd client.fd
return return
end end
case message case message
when WebSocket::Error when WebSocket::Error
puts "#{CRED}An error occured#{CRESET}" puts "#{CRED}An error occured#{CRESET}"
context.remove_fd client.fd Context.context.remove_fd client.fd
return return
when WebSocket::Ping when WebSocket::Ping
# puts "#{CBLUE}Received a ping message#{CRESET}" # puts "#{CBLUE}Received a ping message#{CRESET}"
@ -281,7 +302,7 @@ def websocket_switching_procedure (activefd : Int, context : InstanceStorage)
break break
when WebSocket::Close when WebSocket::Close
# puts "#{CBLUE}Received a close message#{CRESET}" # puts "#{CBLUE}Received a close message#{CRESET}"
context.remove_fd client.fd Context.context.remove_fd client.fd
return return
when WebSocket::NotFinal when WebSocket::NotFinal
# puts "#{CBLUE}Received only part of a message#{CRESET}" # puts "#{CBLUE}Received only part of a message#{CRESET}"
@ -293,18 +314,18 @@ def websocket_switching_procedure (activefd : Int, context : InstanceStorage)
# puts "#{CBLUE}Received a binary message#{CRESET}" # puts "#{CBLUE}Received a binary message#{CRESET}"
end end
if context.is_json[activefd] && message.is_a?(String) if Context.context.is_json[activefd] && message.is_a?(String)
message = IPC::Message.from_json(message).to_packet message = IPC::Message.from_json(message).to_packet
end end
# client => service # client => service
fdservice = context.switchtable[activefd] fdservice = Context.context.switchtable[activefd]
# XXX: this is not a TCP fd, but since behind the scene this is compatible, I'm hacking a bit # XXX: this is not a TCP fd, but since behind the scene this is compatible, I'm hacking a bit
# Also, I changed the "finalize" method of the TCPFileDescriptor class not to close the socket # Also, I changed the "finalize" method of the TCPFileDescriptor class not to close the socket
# when the object is GC. # when the object is GC.
serv = WrappedTCPFileDescriptor.new(fd: fdservice, family: Socket::Family::INET) serv = WrappedTCPFileDescriptor.new(fd: fdservice, family: Socket::Family::INET)
#serv = context.fd_to_ipcconnection[fdservice] #serv = Context.context.fd_to_ipcclient[fdservice]
serv.send message serv.send message
break unless still_something_to_read break unless still_something_to_read
@ -312,13 +333,13 @@ def websocket_switching_procedure (activefd : Int, context : InstanceStorage)
end # loop over the remaining messages to read on the websocket end # loop over the remaining messages to read on the websocket
else else
# service => client # service => client
fdclient = context.switchtable[activefd] fdclient = Context.context.switchtable[activefd]
wsclient = context.fd_to_websocket[fdclient] wsclient = Context.context.fd_to_websocket[fdclient]
serv = context.fd_to_ipcconnection[activefd] serv = Context.context.fd_to_ipcclient[activefd]
message = serv.read message = serv.read
if context.is_json[fdclient] if Context.context.is_json[fdclient]
buf = message.to_json buf = message.to_json
else else
buf = message.to_packet buf = message.to_packet
@ -328,23 +349,23 @@ def websocket_switching_procedure (activefd : Int, context : InstanceStorage)
end end
rescue e rescue e
puts "#{CRED}Exception during message transfer:#{CRESET} #{e}" puts "#{CRED}Exception during message transfer:#{CRESET} #{e}"
if context.is_client[activefd] if Context.context.is_client[activefd]
closing_client activefd, context closing_client activefd
else else
clientfd = context.switchtable[activefd] clientfd = Context.context.switchtable[activefd]
closing_client clientfd, context closing_client clientfd
end end
end end
end end
def sending_ping_messages(context : InstanceStorage) def sending_ping_messages
context.fd_to_websocket.each do |fd, ws| Context.context.fd_to_websocket.each do |fd, ws|
begin begin
ws.ping "hello from #{fd}" ws.ping "hello from #{fd}"
rescue e rescue e
puts "#{CRED}Exception: #{e}#{CRESET}, already closed client #{fd}" puts "#{CRED}Exception: #{e}#{CRESET}, already closed client #{fd}"
begin begin
context.remove_fd fd Context.context.remove_fd fd
rescue e rescue e
puts "#{CRED}Cannot remove #{fd} from clients: #{e}#{CRESET}" puts "#{CRED}Cannot remove #{fd} from clients: #{e}#{CRESET}"
end end
@ -354,59 +375,48 @@ end
# Every few seconds, the service should trigger the timer # Every few seconds, the service should trigger the timer
# Allowing the sending of Ping messages to clients # Allowing the sending of Ping messages to clients
service.base_timer = timer_delay Context.service.base_timer = Context.timer_delay
service.loop do |event| Context.service.loop do |event|
case event case event
when IPC::Event::Timer when IPC::Event::Timer
if verbosity > 0 info "IPC::Event::Timer"
puts "#{CORANGE}IPC::Event::Timer#{CRESET}" sending_ping_messages
end
sending_ping_messages context
when IPC::Event::Connection when IPC::Event::Connection
if verbosity > 0 info "IPC::Event::Connection: #{event.fd}"
puts "#{CBLUE}IPC::Event::Connection#{CRESET}: #{event.connection.fd}"
end
when IPC::Event::Disconnection when IPC::Event::Disconnection
if verbosity > 0 info "IPC::Event::Disconnection: #{event.fd}"
puts "#{CBLUE}IPC::Event::Disconnection#{CRESET}: #{event.connection.fd}"
end
when IPC::Event::ExtraSocket when IPC::Event::ExtraSocket
if verbosity > 0 info "IPC::Event::ExtraSocket: #{event.fd}"
puts "#{CBLUE}IPC::Event::ExtraSocket#{CRESET}: #{event.connection.fd}"
end
# 1. accept new websocket clients # 1. accept new websocket clients
if server.fd == event.connection.fd if server.fd == event.fd
client = server.accept client = server.accept
begin begin
websocket_client_connection client, context websocket_client_connection client
puts "#{CBLUE}new client:#{CRESET} #{client.fd}" unless verbosity == 0 info "new client: #{client.fd}"
rescue e rescue e
puts "Exception: #{CRED}#{e}#{CRESET}" danger "Exception: #{e}"
client.close client.close
end end
next next
end end
# 2. active fd != server fd # 2. active fd != server fd
activefd = event.connection.fd activefd = event.fd
websocket_switching_procedure activefd, context websocket_switching_procedure activefd
when IPC::Event::Switch when IPC::Event::Switch
if verbosity > 0 puts "IPC::Event::Switch: from fd #{event.fd}"
puts "\033[36mIPC::Event::Switch#{CRESET}: from fd #{event.connection.fd}"
end
raise "Not implemented." raise "Not implemented."
# IPC::Event::Message has to be the last entry # IPC::Event::Message has to be the last entry
# because ExtraSocket and Switch inherit from Message class # because ExtraSocket and Switch inherit from Message class
when IPC::Event::Message when IPC::Event::MessageSent
if verbosity > 0 debug "IPC::Event::MessageSent: #{event.fd}"
puts "#{CBLUE}IPC::Event::Message#{CRESET}: #{event.connection.fd}"
end
when IPC::Event::MessageReceived
info "IPC::Event::MessageReceived: #{event.fd}"
raise "Not implemented." raise "Not implemented."
end end
end end