3
0
Fork 0

trackingd and the right IP addresses.

dev
Karchnu 2020-08-23 02:51:49 +02:00
parent 492a4dae34
commit 4ba0b805a2
1 changed files with 33 additions and 21 deletions

View File

@ -108,20 +108,6 @@ def ws_http_upgrade(client)
Context.context.is_json[client.fd] = false Context.context.is_json[client.fd] = false
end end
# TODO: if trackerd, send the IP address of the client
# if req_service == "tracker"
# puts "tracker - sending the IP address"
# puts "connection from #{client.remote_address}"
# 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 1, 1.to_u8, "{\"ipaddress\": \"#{remote_address}\"}"
# serv = WrappedTCPFileDescriptor.new(fd: sfd, family: Socket::Family::INET)
# serv.send message.to_packet
# end
# puts "#{headers_header}\n#{headers.to_s}\r\n" # puts "#{headers_header}\n#{headers.to_s}\r\n"
client.send "#{headers_header}\n#{headers.to_s}\r\n" client.send "#{headers_header}\n#{headers.to_s}\r\n"
@ -134,7 +120,7 @@ def ws_http_upgrade(client)
Context.context.fd_to_socket[client.fd] = client Context.context.fd_to_socket[client.fd] = client
Context.context.fd_to_websocket[client.fd] = wsclient Context.context.fd_to_websocket[client.fd] = wsclient
req_service return req_service, request
rescue e rescue e
puts "#{CRED}Exception in ws_http_upgrade#{CRESET}: #{CBLUE}#{e}#{CRESET}" puts "#{CRED}Exception in ws_http_upgrade#{CRESET}: #{CBLUE}#{e}#{CRESET}"
raise "DROP IT" raise "DROP IT"
@ -147,20 +133,46 @@ def handle_new_clients(service, server)
client = server.accept client = server.accept
# 2. upgrade HTTP connections and get the service name in the URI path. # 2. upgrade HTTP connections and get the service name in the URI path.
req_service = ws_http_upgrade client req_service, request = ws_http_upgrade client
# 3. connect to the service via ipc_connection_switched # 3. connect to the service via ipc_connection_switched
serverfd = 0 serverfd : Int32 = 0
# LibIPC.ipc_connection_switched service.pointer, req_service, client.fd, Pointer(Libc::Int).null # LibIPC.ipc_connection_switched service.pointer, req_service, client.fd, Pointer(Libc::Int).null
LibIPC.ipc_connection_switched service.pointer, req_service, client.fd, pointerof (serverfd) spointer = pointerof(serverfd)
if req_service == "tracker"
proc_cb_in = ->ws_cb_in(Int32, Pointer(LibIPC::Message), Int16*) req_service = "tracking"
proc_cb_out = ->ws_cb_out(Int32, Pointer(LibIPC::Message)) end
LibIPC.ipc_connection_switched service.pointer, req_service, client.fd, spointer
if serverfd == 0
raise "Could not connect to #{req_service}"
end
# 4. change client callbacks via ipc_switching_callbacks # 4. change client callbacks via ipc_switching_callbacks
# only the client callbacks are changed, since the associated server is a simple libipc service # only the client callbacks are changed, since the associated server is a simple libipc service
proc_cb_in = ->ws_cb_in(Int32, Pointer(LibIPC::Message), Int16*)
proc_cb_out = ->ws_cb_out(Int32, Pointer(LibIPC::Message))
LibIPC.ipc_switching_callbacks service.pointer, client.fd, proc_cb_in, proc_cb_out LibIPC.ipc_switching_callbacks service.pointer, client.fd, proc_cb_in, proc_cb_out
# 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
puts "trackingd - sending the IP address #{real_ip_address} to fd #{serverfd}"
# 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
info "#{CBLUE}new client:#{CRESET} #{client.fd}" info "#{CBLUE}new client:#{CRESET} #{client.fd}"
rescue e rescue e
puts "Exception in handle_new_client: #{CRED}#{e}#{CRESET}" puts "Exception in handle_new_client: #{CRED}#{e}#{CRESET}"