2020-07-04 22:31:17 +02:00
|
|
|
require "option_parser"
|
|
|
|
require "ipc"
|
|
|
|
require "socket"
|
|
|
|
require "./colors"
|
|
|
|
|
2020-08-24 16:57:02 +02:00
|
|
|
require "colorize"
|
|
|
|
|
2020-07-04 22:31:17 +02:00
|
|
|
require "json"
|
|
|
|
|
|
|
|
require "socket"
|
|
|
|
require "http/server"
|
|
|
|
require "base64"
|
|
|
|
require "digest"
|
|
|
|
require "./utils"
|
|
|
|
|
2020-07-10 16:22:31 +02:00
|
|
|
require "./instance_storage.cr"
|
|
|
|
|
2020-07-04 22:31:17 +02:00
|
|
|
# All modifications to standard libraries go there.
|
|
|
|
require "./lib_modifications.cr"
|
|
|
|
|
2020-08-24 16:57:02 +02:00
|
|
|
class Context
|
2020-07-09 09:13:57 +02:00
|
|
|
# service instance parameters
|
|
|
|
# they can be changed via the cli
|
|
|
|
class_property service_name : String = "websocket"
|
|
|
|
class_property host : String = "0.0.0.0"
|
|
|
|
class_property port_to_listen : UInt16 = 1234
|
|
|
|
class_property timer_delay : Int32 = 30_000 # 30 seconds
|
|
|
|
class_property verbosity : Int32 = 1
|
|
|
|
end
|
2020-07-04 22:31:17 +02:00
|
|
|
|
2020-08-24 16:57:02 +02:00
|
|
|
class Log
|
|
|
|
def self.debug(message)
|
|
|
|
STDOUT << ":: ".colorize(:green) << message.colorize(:white) << "\n" if ::Context.verbosity > 2
|
|
|
|
end
|
|
|
|
def self.info(message)
|
|
|
|
STDOUT << ":: ".colorize(:blue) << message.colorize(:white) << "\n" if ::Context.verbosity > 1
|
|
|
|
end
|
|
|
|
def self.warning(message)
|
|
|
|
STDERR << "?? ".colorize(:yellow) << message.colorize(:yellow) << "\n" if ::Context.verbosity > 0
|
|
|
|
end
|
|
|
|
def self.error(message)
|
|
|
|
STDERR << "!! ".colorize(:red) << message.colorize(:red) << "\n" if ::Context.verbosity > 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-04 22:31:17 +02:00
|
|
|
OptionParser.parse do |parser|
|
|
|
|
parser.on "-l host", "--l host", "IP address to listen on." do |h|
|
2020-08-24 16:57:02 +02:00
|
|
|
Context.host = h
|
2020-07-04 22:31:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
parser.on "-p port", "--port port", "Port to listen on." do |port|
|
2020-08-24 16:57:02 +02:00
|
|
|
Context.port_to_listen = port.to_u16
|
2020-07-04 22:31:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
parser.on "-s service-name", "--service-name service-name", "Service name." do |name|
|
2020-08-24 16:57:02 +02:00
|
|
|
Context.service_name = name
|
2020-07-04 22:31:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
parser.on "-t timer-delay", "--timer-delay timer-delay", "Timer delay (in seconds)" do |t|
|
2020-08-24 16:57:02 +02:00
|
|
|
Context.timer_delay = t.to_i32 * 1000 # stored in ms
|
2020-07-04 22:31:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
parser.on "-v verbosity-level", "--verbosity level", "Verbosity." do |opt|
|
2020-08-24 16:57:02 +02:00
|
|
|
Context.verbosity = opt.to_i
|
2020-07-04 22:31:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
parser.on "-h", "--help", "Show this help" do
|
|
|
|
puts parser
|
|
|
|
exit 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-09 09:13:57 +02:00
|
|
|
def sending_ping_messages
|
2020-07-10 16:22:31 +02:00
|
|
|
Context.context.fd_to_websocket.each do |fd, ws|
|
|
|
|
begin
|
|
|
|
ws.ping "hello from #{fd}"
|
|
|
|
rescue e
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.error "Exception: #{e}, already closed client #{fd}"
|
2020-07-10 16:22:31 +02:00
|
|
|
begin
|
|
|
|
Context.context.remove_fd fd
|
|
|
|
rescue e
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.error "Cannot remove #{fd} from clients: #{e}"
|
2020-07-10 16:22:31 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-07-04 22:31:17 +02:00
|
|
|
end
|
|
|
|
|
2020-07-09 09:13:57 +02:00
|
|
|
def ws_http_upgrade(client)
|
2020-07-04 22:31:17 +02:00
|
|
|
request = HTTP::Request.from_io client
|
|
|
|
|
|
|
|
if request.nil?
|
|
|
|
raise "#REQUEST IS NIL"
|
|
|
|
end
|
|
|
|
|
|
|
|
if request.is_a? HTTP::Status && request == HTTP::Status::BAD_REQUEST
|
|
|
|
raise "BAD REQUEST DAZE~"
|
|
|
|
end
|
|
|
|
|
|
|
|
if request.is_a? HTTP::Status
|
|
|
|
raise "Not bad request but still pretty bad: #{request.to_s}"
|
|
|
|
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"
|
2020-08-24 16:57:02 +02:00
|
|
|
# Log.debug response_key
|
2020-07-04 22:31:17 +02:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# requested service, fd
|
2020-08-21 03:00:04 +02:00
|
|
|
req_service = request.path.lchop.sub "ws/", ""
|
2020-07-04 22:31:17 +02:00
|
|
|
|
|
|
|
if req_service.ends_with? ".JSON"
|
2020-07-10 16:22:31 +02:00
|
|
|
Context.context.is_json[client.fd] = true
|
2020-07-04 22:31:17 +02:00
|
|
|
req_service = req_service.gsub /.JSON$/, ""
|
|
|
|
else
|
2020-07-10 16:22:31 +02:00
|
|
|
Context.context.is_json[client.fd] = false
|
2020-07-04 22:31:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# puts "#{headers_header}\n#{headers.to_s}\r\n"
|
|
|
|
client.send "#{headers_header}\n#{headers.to_s}\r\n"
|
|
|
|
|
|
|
|
wsclient = WebSocket.new client
|
|
|
|
wsclient.on_pong do |m|
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.debug "pong #{m}"
|
2020-07-04 22:31:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# registering the client into storing structures to avoid being garbage collected
|
2020-07-11 16:08:46 +02:00
|
|
|
Context.context.fd_to_socket[client.fd] = client
|
2020-07-10 16:22:31 +02:00
|
|
|
Context.context.fd_to_websocket[client.fd] = wsclient
|
2020-07-04 22:31:17 +02:00
|
|
|
|
2020-08-23 02:51:49 +02:00
|
|
|
return req_service, request
|
2020-07-11 16:08:46 +02:00
|
|
|
rescue e
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.error "Exception in ws_http_upgrade: #{e}"
|
2020-07-11 16:08:46 +02:00
|
|
|
raise "DROP IT"
|
2020-07-10 16:22:31 +02:00
|
|
|
end
|
2020-07-09 09:13:57 +02:00
|
|
|
|
|
|
|
def handle_new_clients(service, server)
|
|
|
|
# TODO: this is a lot of C-like notation, should be changed.
|
|
|
|
# In the client accept function
|
|
|
|
# 1. accept TCP/WS connection
|
|
|
|
client = server.accept
|
|
|
|
|
|
|
|
# 2. upgrade HTTP connections and get the service name in the URI path.
|
2020-08-23 02:51:49 +02:00
|
|
|
req_service, request = ws_http_upgrade client
|
2020-07-09 09:13:57 +02:00
|
|
|
|
|
|
|
# 3. connect to the service via ipc_connection_switched
|
2020-08-23 02:51:49 +02:00
|
|
|
serverfd : Int32 = 0
|
2020-07-11 16:08:46 +02:00
|
|
|
# LibIPC.ipc_connection_switched service.pointer, req_service, client.fd, Pointer(Libc::Int).null
|
2020-08-23 02:51:49 +02:00
|
|
|
spointer = pointerof(serverfd)
|
|
|
|
if req_service == "tracker"
|
|
|
|
req_service = "tracking"
|
|
|
|
end
|
|
|
|
LibIPC.ipc_connection_switched service.pointer, req_service, client.fd, spointer
|
|
|
|
if serverfd == 0
|
|
|
|
raise "Could not connect to #{req_service}"
|
|
|
|
end
|
2020-07-09 09:13:57 +02:00
|
|
|
|
2020-08-23 02:51:49 +02:00
|
|
|
# 4. change client callbacks via ipc_switching_callbacks
|
|
|
|
# only the client callbacks are changed, since the associated server is a simple libipc service
|
2020-08-03 00:28:23 +02:00
|
|
|
proc_cb_in = ->ws_cb_in(Int32, Pointer(LibIPC::Message), Int16*)
|
2020-07-11 16:08:46 +02:00
|
|
|
proc_cb_out = ->ws_cb_out(Int32, Pointer(LibIPC::Message))
|
2020-07-09 09:13:57 +02:00
|
|
|
|
2020-07-10 16:22:31 +02:00
|
|
|
LibIPC.ipc_switching_callbacks service.pointer, client.fd, proc_cb_in, proc_cb_out
|
2020-07-09 09:13:57 +02:00
|
|
|
|
2020-08-23 02:51:49 +02:00
|
|
|
# 5. In case the requested service is trackingd,
|
|
|
|
# the websocketd has to provide the IP address as the first message.
|
|
|
|
if req_service == "tracking"
|
|
|
|
# The proxy is configured to provide the X-Real-IP in the headers.
|
|
|
|
# If not found, we use the client address from the socket.
|
|
|
|
real_ip_address = request.headers["X-Real-IP"] || client.remote_address.address
|
|
|
|
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.info "trackingd - sending the IP address #{real_ip_address} to fd #{serverfd}"
|
2020-08-23 02:51:49 +02:00
|
|
|
# sfd = Context.context.switchtable[client.fd]
|
|
|
|
# message = IPC::Message.from_json(JSON).to_packet
|
|
|
|
# => JSON has to include these attributes: mtype, utype, payload
|
|
|
|
# message = IPC::Message.new mtype, utype, payload
|
|
|
|
# remote_address = client.remote_address.address
|
|
|
|
message = IPC::Message.new serverfd, 1, 1.to_u8, "{\"ipaddress\": \"#{real_ip_address}\"}"
|
|
|
|
service.send message
|
|
|
|
# serv = WrappedTCPFileDescriptor.new(fd: serverfd, family: Socket::Family::INET)
|
|
|
|
# serv.send message.to_packet
|
|
|
|
end
|
|
|
|
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.debug "new client: #{client.fd}"
|
2020-07-09 09:13:57 +02:00
|
|
|
rescue e
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.error "Exception in handle_new_client: #{e}"
|
2020-07-09 09:13:57 +02:00
|
|
|
unless client.nil?
|
2020-07-11 16:08:46 +02:00
|
|
|
client.close
|
2020-07-04 22:31:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-11 16:08:46 +02:00
|
|
|
def ws_cb_out(fd : Int32, pm : Pointer(LibIPC::Message))
|
2020-08-24 16:57:02 +02:00
|
|
|
# Log.info "OUT fd is #{fd}"
|
2020-07-10 16:22:31 +02:00
|
|
|
wsclient = Context.context.fd_to_websocket[fd]
|
|
|
|
|
|
|
|
message = IPC::Message.new pm
|
2020-07-11 16:08:46 +02:00
|
|
|
# pp! message
|
2020-07-10 16:22:31 +02:00
|
|
|
|
|
|
|
if Context.context.is_json[fd]
|
|
|
|
buf = message.to_json
|
|
|
|
else
|
2020-07-11 16:08:46 +02:00
|
|
|
buf = message.to_packet
|
2020-07-10 16:22:31 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
wsclient.send buf
|
|
|
|
|
2020-07-09 09:13:57 +02:00
|
|
|
return LibIPC::IPCCB::NoError
|
2020-07-10 16:22:31 +02:00
|
|
|
rescue e
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.error "Exception during message transfer: #{e}"
|
2020-07-10 16:22:31 +02:00
|
|
|
Context.context.remove_fd fd
|
|
|
|
return LibIPC::IPCCB::Error
|
2020-07-04 22:31:17 +02:00
|
|
|
end
|
|
|
|
|
2020-08-03 00:28:23 +02:00
|
|
|
def ws_cb_in(fd : Int32, pm : LibIPC::Message*, more_to_read : Int16*)
|
2020-08-24 16:57:02 +02:00
|
|
|
# Log.debug "IN fd is #{fd}"
|
2020-07-10 16:22:31 +02:00
|
|
|
|
|
|
|
wsclient = Context.context.fd_to_websocket[fd]
|
|
|
|
|
2020-07-11 16:08:46 +02:00
|
|
|
message = nil
|
|
|
|
begin
|
|
|
|
message = wsclient.run_once
|
|
|
|
rescue e
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.error "run_once FAILED: #{e}"
|
2020-07-12 21:09:06 +02:00
|
|
|
Context.context.remove_fd fd
|
2020-07-11 16:08:46 +02:00
|
|
|
return LibIPC::IPCCB::Error
|
|
|
|
end
|
2020-07-10 16:22:31 +02:00
|
|
|
|
2020-08-03 00:28:23 +02:00
|
|
|
if wsclient.ws.io.empty?
|
|
|
|
more_to_read[0] = 0
|
|
|
|
else
|
|
|
|
more_to_read[0] = 1
|
|
|
|
end
|
|
|
|
|
2020-07-10 16:22:31 +02:00
|
|
|
if wsclient.closed?
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.info "client closed the connection"
|
2020-07-12 21:09:06 +02:00
|
|
|
Context.context.remove_fd fd
|
2020-07-10 16:22:31 +02:00
|
|
|
return LibIPC::IPCCB::Closing
|
|
|
|
end
|
|
|
|
|
|
|
|
if message.nil?
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.error "Reveiced a nil message"
|
2020-07-12 21:09:06 +02:00
|
|
|
Context.context.remove_fd fd
|
2020-07-10 16:22:31 +02:00
|
|
|
return LibIPC::IPCCB::Closing
|
|
|
|
end
|
|
|
|
|
|
|
|
case message
|
|
|
|
when String
|
2020-07-11 16:08:46 +02:00
|
|
|
if Context.context.is_json[fd]
|
|
|
|
ipc_message = IPC::Message.from_json(message)
|
|
|
|
ipc_message.copy_to_message_pointer pm
|
|
|
|
return LibIPC::IPCCB::NoError
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# every other option should be dropped
|
|
|
|
case message
|
|
|
|
when WebSocket::Error
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.error "An error occured"
|
2020-07-12 21:09:06 +02:00
|
|
|
Context.context.remove_fd fd
|
2020-07-11 16:08:46 +02:00
|
|
|
return LibIPC::IPCCB::Error
|
|
|
|
when WebSocket::Ping
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.debug "Received a ping message"
|
2020-07-12 21:09:06 +02:00
|
|
|
Context.context.remove_fd fd
|
2020-07-11 16:08:46 +02:00
|
|
|
return LibIPC::IPCCB::Ignore
|
|
|
|
when WebSocket::Pong
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.debug "Received a pong message"
|
2020-07-11 16:08:46 +02:00
|
|
|
return LibIPC::IPCCB::Ignore
|
|
|
|
when WebSocket::Close
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.debug "Received a close message"
|
2020-07-12 21:09:06 +02:00
|
|
|
Context.context.remove_fd fd
|
2020-07-11 16:08:46 +02:00
|
|
|
return LibIPC::IPCCB::Closing
|
|
|
|
when WebSocket::NotFinal
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.warning "Received only part of a message: NOT IMPLEMENTED"
|
2020-07-12 21:09:06 +02:00
|
|
|
Context.context.remove_fd fd
|
2020-07-11 16:08:46 +02:00
|
|
|
return LibIPC::IPCCB::Error
|
|
|
|
when Bytes
|
|
|
|
# TODO: when receiving a binary message
|
|
|
|
# we should test the format and maybe its content
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.error "Received a binary message: NOT IMPLEMENTED, YET"
|
2020-07-12 21:09:06 +02:00
|
|
|
Context.context.remove_fd fd
|
2020-07-11 16:08:46 +02:00
|
|
|
return LibIPC::IPCCB::Error
|
|
|
|
else
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.error "Received a websocket message with unknown type"
|
2020-07-11 16:08:46 +02:00
|
|
|
end
|
2020-07-10 16:22:31 +02:00
|
|
|
end
|
|
|
|
|
2020-07-11 16:08:46 +02:00
|
|
|
return LibIPC::IPCCB::Error
|
2020-07-10 16:22:31 +02:00
|
|
|
|
|
|
|
rescue e
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.error "Exception (receiving a message) #{e}"
|
2020-07-11 16:08:46 +02:00
|
|
|
# tcp = WrappedTCPFileDescriptor.new(fd: fd, family: Socket::Family::INET)
|
|
|
|
# tcp.close
|
2020-07-10 16:22:31 +02:00
|
|
|
Context.context.remove_fd fd
|
|
|
|
return LibIPC::IPCCB::Error
|
2020-07-04 22:31:17 +02:00
|
|
|
end
|
|
|
|
|
2020-07-10 16:22:31 +02:00
|
|
|
class Context
|
|
|
|
class_property context = InstanceStorage.new
|
|
|
|
end
|
2020-07-04 22:31:17 +02:00
|
|
|
|
2020-07-09 09:13:57 +02:00
|
|
|
def main
|
|
|
|
# by default, listen on any IP address
|
2020-08-24 16:57:02 +02:00
|
|
|
server = TCPServer.new(Context.host, Context.port_to_listen)
|
|
|
|
service = IPC::Server.new Context.service_name
|
2020-07-09 09:13:57 +02:00
|
|
|
service << server.fd
|
2020-07-20 22:52:38 +02:00
|
|
|
|
2020-07-09 09:13:57 +02:00
|
|
|
# Every few seconds, the service should trigger the timer
|
|
|
|
# Allowing the sending of Ping messages to clients
|
2020-08-24 16:57:02 +02:00
|
|
|
service.base_timer = Context.timer_delay
|
|
|
|
service.timer = Context.timer_delay
|
2020-07-09 09:13:57 +02:00
|
|
|
|
|
|
|
service.loop do |event|
|
2020-08-24 16:57:02 +02:00
|
|
|
# Log.info "current state of the context:"
|
2020-07-22 10:14:27 +02:00
|
|
|
# service.pp
|
2020-07-09 09:13:57 +02:00
|
|
|
case event
|
|
|
|
when IPC::Event::Timer
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.debug "IPC::Event::Timer"
|
2020-07-10 16:22:31 +02:00
|
|
|
sending_ping_messages
|
2020-07-20 22:52:38 +02:00
|
|
|
|
2020-07-09 09:13:57 +02:00
|
|
|
when IPC::Event::Connection
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.debug "IPC::Event::Connection: fd #{event.fd}"
|
2020-08-03 00:38:29 +02:00
|
|
|
|
2020-07-09 09:13:57 +02:00
|
|
|
when IPC::Event::Disconnection
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.debug "IPC::Event::Disconnection: fd #{event.fd}"
|
2020-07-10 16:22:31 +02:00
|
|
|
Context.context.remove_fd event.fd
|
2020-08-03 00:38:29 +02:00
|
|
|
|
2020-07-09 09:13:57 +02:00
|
|
|
when IPC::Event::ExtraSocket
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.debug "IPC::Event::ExtraSocket: fd #{event.fd}"
|
2020-07-09 09:13:57 +02:00
|
|
|
if server.fd != event.fd
|
|
|
|
raise "Error: the only extra socket should be the TCP/WS server"
|
2020-07-04 22:31:17 +02:00
|
|
|
end
|
2020-07-09 09:13:57 +02:00
|
|
|
handle_new_clients(service, server)
|
2020-07-04 22:31:17 +02:00
|
|
|
|
2020-07-09 09:13:57 +02:00
|
|
|
when IPC::Event::Switch
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.debug "IPC::Event::Switch: fd #{event.fd}"
|
2020-07-10 16:22:31 +02:00
|
|
|
# raise "Not implemented."
|
2020-07-04 22:31:17 +02:00
|
|
|
|
2020-07-09 09:13:57 +02:00
|
|
|
when IPC::Event::MessageSent
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.debug "IPC::Event::MessageSent: fd #{event.fd}"
|
2020-07-04 22:31:17 +02:00
|
|
|
|
2020-07-09 09:13:57 +02:00
|
|
|
when IPC::Event::MessageReceived
|
2020-08-24 16:57:02 +02:00
|
|
|
Log.debug "IPC::Event::Message: fd #{event.fd}"
|
2020-07-04 22:31:17 +02:00
|
|
|
|
2020-07-09 09:13:57 +02:00
|
|
|
raise "Not implemented."
|
|
|
|
end
|
2020-07-04 22:31:17 +02:00
|
|
|
end
|
|
|
|
end
|
2020-07-09 09:13:57 +02:00
|
|
|
|
|
|
|
main
|