2013-03-10 23:14:31 +00:00
|
|
|
#!/usr/bin/env ruby
|
2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2018-10-01 20:00:26 +00:00
|
|
|
begin
|
|
|
|
load File.expand_path('../bin/spring', __dir__)
|
|
|
|
rescue LoadError => e
|
|
|
|
raise unless e.message.include?('spring')
|
|
|
|
end
|
2015-05-08 13:58:28 +00:00
|
|
|
|
2018-10-01 20:00:26 +00:00
|
|
|
dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
2015-08-18 08:50:12 +00:00
|
|
|
Dir.chdir dir
|
2017-04-27 07:34:40 +00:00
|
|
|
|
|
|
|
require 'bundler'
|
2018-10-01 20:00:26 +00:00
|
|
|
|
2015-08-18 08:50:12 +00:00
|
|
|
require File.join(dir, 'config', 'environment')
|
2018-10-01 20:00:26 +00:00
|
|
|
|
2017-05-08 14:05:38 +00:00
|
|
|
require 'eventmachine'
|
|
|
|
require 'em-websocket'
|
|
|
|
require 'json'
|
|
|
|
require 'fileutils'
|
|
|
|
require 'optparse'
|
|
|
|
require 'daemons'
|
2016-05-16 17:49:06 +00:00
|
|
|
|
2017-05-08 14:05:38 +00:00
|
|
|
def before_fork
|
|
|
|
# remember open file handles
|
|
|
|
@files_to_reopen = []
|
|
|
|
ObjectSpace.each_object(File) do |file|
|
2018-05-04 14:05:10 +00:00
|
|
|
@files_to_reopen << file if !file.closed?
|
2017-05-08 14:05:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_fork(dir)
|
|
|
|
Dir.chdir dir
|
|
|
|
|
|
|
|
# Re-open file handles
|
|
|
|
@files_to_reopen.each do |file|
|
|
|
|
file.reopen file.path, 'a+'
|
|
|
|
file.sync = true
|
|
|
|
end
|
|
|
|
|
2018-10-01 20:00:26 +00:00
|
|
|
# Spring redirects STDOUT and STDERR to /dev/null
|
|
|
|
# before we get here. This causes the `reopen` lines
|
|
|
|
# below to fail because the handles are already
|
|
|
|
# opened for write
|
|
|
|
if defined?(Spring)
|
|
|
|
$stdout.close
|
|
|
|
$stderr.close
|
|
|
|
end
|
|
|
|
|
|
|
|
$stdout.reopen("#{dir}/log/websocket-server_out.log", 'w').sync = true
|
|
|
|
$stderr.reopen("#{dir}/log/websocket-server_err.log", 'w').sync = true
|
2017-05-08 14:05:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
before_fork
|
|
|
|
|
2012-07-23 22:22:23 +00:00
|
|
|
# Look for -o with argument, and -I and -D boolean arguments
|
2012-08-06 06:29:39 +00:00
|
|
|
@options = {
|
2015-04-27 13:42:53 +00:00
|
|
|
p: 6042,
|
|
|
|
b: '0.0.0.0',
|
|
|
|
s: false,
|
|
|
|
v: false,
|
|
|
|
d: false,
|
|
|
|
k: '/path/to/server.key',
|
|
|
|
c: '/path/to/server.crt',
|
2018-10-01 20:00:26 +00:00
|
|
|
i: "#{dir}/tmp/pids/websocket.pid"
|
2012-07-23 22:22:23 +00:00
|
|
|
}
|
2013-03-10 23:14:31 +00:00
|
|
|
|
2012-08-02 09:30:30 +00:00
|
|
|
tls_options = {}
|
2012-07-23 22:22:23 +00:00
|
|
|
OptionParser.new do |opts|
|
2015-04-27 13:20:16 +00:00
|
|
|
opts.banner = 'Usage: websocket-server.rb start|stop [options]'
|
2012-07-23 22:22:23 +00:00
|
|
|
|
2015-04-27 13:20:16 +00:00
|
|
|
opts.on('-d', '--daemon', 'start as daemon') do |d|
|
2012-08-06 06:29:39 +00:00
|
|
|
@options[:d] = d
|
|
|
|
end
|
2015-04-27 13:20:16 +00:00
|
|
|
opts.on('-v', '--verbose', 'enable debug messages') do |d|
|
2013-03-10 23:14:31 +00:00
|
|
|
@options[:v] = d
|
|
|
|
end
|
2015-04-27 13:20:16 +00:00
|
|
|
opts.on('-p', '--port [OPT]', 'port of websocket server') do |p|
|
2012-08-06 06:29:39 +00:00
|
|
|
@options[:p] = p
|
2012-07-23 22:22:23 +00:00
|
|
|
end
|
2015-04-27 13:20:16 +00:00
|
|
|
opts.on('-b', '--bind [OPT]', 'bind address') do |b|
|
2018-03-15 18:21:04 +00:00
|
|
|
@options[:b] = IPAddr.new(b).to_s
|
2012-07-23 22:22:23 +00:00
|
|
|
end
|
2015-04-27 13:20:16 +00:00
|
|
|
opts.on('-s', '--secure', 'enable secure connections') do |s|
|
2012-08-06 06:29:39 +00:00
|
|
|
@options[:s] = s
|
2012-08-02 09:17:22 +00:00
|
|
|
end
|
2015-04-27 13:20:16 +00:00
|
|
|
opts.on('-i', '--pid [OPT]', 'pid, default is tmp/pids/websocket.pid') do |i|
|
2013-01-15 22:46:35 +00:00
|
|
|
@options[:i] = i
|
|
|
|
end
|
2015-04-27 13:20:16 +00:00
|
|
|
opts.on('-k', '--private-key [OPT]', '/path/to/server.key for secure connections') do |k|
|
2012-08-02 09:30:30 +00:00
|
|
|
tls_options[:private_key_file] = k
|
2012-08-02 09:17:22 +00:00
|
|
|
end
|
2015-04-27 13:20:16 +00:00
|
|
|
opts.on('-c', '--certificate [OPT]', '/path/to/server.crt for secure connections') do |c|
|
2012-08-02 09:30:30 +00:00
|
|
|
tls_options[:cert_chain_file] = c
|
2012-08-02 09:17:22 +00:00
|
|
|
end
|
2012-07-23 22:22:23 +00:00
|
|
|
end.parse!
|
|
|
|
|
2013-06-19 00:40:42 +00:00
|
|
|
if ARGV[0] != 'start' && ARGV[0] != 'stop'
|
2013-06-30 15:54:54 +00:00
|
|
|
puts "Usage: #{File.basename(__FILE__)} start|stop [options]"
|
2015-04-30 17:54:08 +00:00
|
|
|
exit
|
2013-06-19 00:40:42 +00:00
|
|
|
end
|
|
|
|
|
2013-03-10 23:14:31 +00:00
|
|
|
if ARGV[0] == 'stop'
|
2018-03-15 18:21:04 +00:00
|
|
|
pid = File.read(@options[:i]).to_i
|
|
|
|
puts "Stopping websocket server (pid: #{pid})"
|
2015-07-16 19:56:54 +00:00
|
|
|
|
2018-03-15 18:21:04 +00:00
|
|
|
# IMPORTANT: Use SIGTERM (15), not SIGKILL (9)
|
|
|
|
# Daemons.rb cleans up the PID file automatically on termination;
|
|
|
|
# SIGKILL ends the process immediately and bypasses cleanup.
|
|
|
|
# See https://major.io/2010/03/18/sigterm-vs-sigkill/ for more.
|
|
|
|
Process.kill(:SIGTERM, pid)
|
2013-03-10 23:14:31 +00:00
|
|
|
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
2015-11-24 02:33:40 +00:00
|
|
|
if ARGV[0] == 'start' && @options[:d]
|
2018-03-15 18:21:04 +00:00
|
|
|
puts "Starting websocket server on #{@options[:b]}:#{@options[:p]} (secure: #{@options[:s]}, pidfile: #{@options[:i]})"
|
2015-07-16 19:56:54 +00:00
|
|
|
|
2018-03-15 18:21:04 +00:00
|
|
|
# Use Daemons.rb's built-in facility for generating PID files
|
|
|
|
Daemons.daemonize(
|
|
|
|
app_name: File.basename(@options[:i], '.pid'),
|
|
|
|
dir_mode: :normal,
|
2018-10-01 20:00:26 +00:00
|
|
|
dir: File.dirname(@options[:i])
|
2018-03-15 18:21:04 +00:00
|
|
|
)
|
2013-03-10 23:14:31 +00:00
|
|
|
|
2017-05-08 14:05:38 +00:00
|
|
|
after_fork(dir)
|
2013-03-10 23:14:31 +00:00
|
|
|
end
|
2013-01-15 22:33:51 +00:00
|
|
|
|
2012-07-23 22:22:23 +00:00
|
|
|
@clients = {}
|
2016-08-20 19:29:22 +00:00
|
|
|
Rails.configuration.interface = 'websocket'
|
2017-10-01 12:25:52 +00:00
|
|
|
EventMachine.run do
|
2015-04-27 13:42:53 +00:00
|
|
|
EventMachine::WebSocket.start( host: @options[:b], port: @options[:p], secure: @options[:s], tls_options: tls_options ) do |ws|
|
2012-07-23 22:22:23 +00:00
|
|
|
|
|
|
|
# register client connection
|
2017-10-01 12:25:52 +00:00
|
|
|
ws.onopen do |handshake|
|
2016-01-05 08:29:15 +00:00
|
|
|
headers = handshake.headers
|
|
|
|
remote_ip = get_remote_ip(headers)
|
2013-06-22 17:00:58 +00:00
|
|
|
client_id = ws.object_id.to_s
|
2012-08-06 06:29:39 +00:00
|
|
|
log 'notice', 'Client connected.', client_id
|
2015-04-27 13:42:53 +00:00
|
|
|
Sessions.create( client_id, {}, { type: 'websocket' } )
|
2012-07-23 22:22:23 +00:00
|
|
|
|
|
|
|
if !@clients.include? client_id
|
|
|
|
@clients[client_id] = {
|
2015-05-10 20:53:15 +00:00
|
|
|
websocket: ws,
|
|
|
|
last_ping: Time.now.utc.to_i,
|
2015-04-27 13:42:53 +00:00
|
|
|
error_count: 0,
|
2016-01-05 08:29:15 +00:00
|
|
|
headers: headers,
|
2016-01-05 12:46:43 +00:00
|
|
|
remote_ip: remote_ip,
|
2012-08-06 06:29:39 +00:00
|
|
|
}
|
2012-07-23 22:22:23 +00:00
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2012-07-23 22:22:23 +00:00
|
|
|
|
|
|
|
# unregister client connection
|
2017-10-01 12:25:52 +00:00
|
|
|
ws.onclose do
|
2013-06-22 17:00:58 +00:00
|
|
|
client_id = ws.object_id.to_s
|
2012-08-06 06:29:39 +00:00
|
|
|
log 'notice', 'Client disconnected.', client_id
|
2012-08-04 13:35:55 +00:00
|
|
|
|
|
|
|
# removed from current client list
|
2012-07-23 22:22:23 +00:00
|
|
|
if @clients.include? client_id
|
|
|
|
@clients.delete client_id
|
|
|
|
end
|
2012-08-04 13:35:55 +00:00
|
|
|
|
2016-11-30 10:30:03 +00:00
|
|
|
Sessions.destroy(client_id)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2012-07-23 22:22:23 +00:00
|
|
|
|
|
|
|
# manage messages
|
2017-10-01 12:25:52 +00:00
|
|
|
ws.onmessage do |msg|
|
2012-07-23 22:22:23 +00:00
|
|
|
|
2013-06-22 17:00:58 +00:00
|
|
|
client_id = ws.object_id.to_s
|
2015-07-03 15:18:01 +00:00
|
|
|
log 'debug', "received: #{msg} ", client_id
|
2012-08-06 06:29:39 +00:00
|
|
|
begin
|
|
|
|
data = JSON.parse(msg)
|
|
|
|
rescue => e
|
2015-07-03 15:18:01 +00:00
|
|
|
log 'error', "can't parse message: #{msg}, #{e.inspect}", client_id
|
2012-08-06 06:29:39 +00:00
|
|
|
next
|
|
|
|
end
|
2012-07-23 22:22:23 +00:00
|
|
|
|
2015-09-06 11:41:51 +00:00
|
|
|
# check if connection not already exists
|
2012-08-07 05:33:47 +00:00
|
|
|
next if !@clients[client_id]
|
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
Sessions.touch(client_id) # rubocop:disable Rails/SkipsModelValidations
|
2015-11-10 14:01:04 +00:00
|
|
|
@clients[client_id][:last_ping] = Time.now.utc.to_i
|
|
|
|
|
2012-11-02 16:10:22 +00:00
|
|
|
# spool messages for new connects
|
|
|
|
if data['spool']
|
2015-12-09 13:09:37 +00:00
|
|
|
Sessions.spool_create(data)
|
2012-11-02 16:10:22 +00:00
|
|
|
end
|
|
|
|
|
2015-12-09 13:09:37 +00:00
|
|
|
if data['event']
|
|
|
|
log 'debug', "execute event '#{data['event']}'", client_id
|
|
|
|
message = Sessions::Event.run(
|
|
|
|
event: data['event'],
|
|
|
|
payload: data,
|
|
|
|
session: @clients[client_id][:session],
|
2016-01-05 12:46:43 +00:00
|
|
|
remote_ip: @clients[client_id][:remote_ip],
|
2015-12-09 13:09:37 +00:00
|
|
|
client_id: client_id,
|
|
|
|
clients: @clients,
|
|
|
|
options: @options,
|
|
|
|
)
|
2015-11-10 14:01:04 +00:00
|
|
|
if message
|
|
|
|
websocket_send(client_id, message)
|
|
|
|
end
|
2015-12-09 13:09:37 +00:00
|
|
|
else
|
|
|
|
log 'error', "unknown message '#{data.inspect}'", client_id
|
2012-08-04 13:35:55 +00:00
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2012-07-23 22:22:23 +00:00
|
|
|
end
|
|
|
|
|
2013-02-01 00:06:33 +00:00
|
|
|
# check unused connections
|
2017-10-01 12:25:52 +00:00
|
|
|
EventMachine.add_timer(0.5) do
|
2013-02-01 00:06:33 +00:00
|
|
|
check_unused_connections
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2013-02-01 00:06:33 +00:00
|
|
|
|
2012-11-27 00:59:46 +00:00
|
|
|
# check open unused connections, kick all connection without activitie in the last 2 minutes
|
2017-10-01 12:25:52 +00:00
|
|
|
EventMachine.add_periodic_timer(120) do
|
2012-12-24 13:53:50 +00:00
|
|
|
check_unused_connections
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2012-08-06 06:29:39 +00:00
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
EventMachine.add_periodic_timer(20) do
|
2012-11-26 05:04:44 +00:00
|
|
|
|
|
|
|
# websocket
|
2015-07-03 15:18:01 +00:00
|
|
|
log 'notice', "Status: websocket clients: #{@clients.size}"
|
2017-11-23 08:09:44 +00:00
|
|
|
@clients.each_key do |client_id|
|
2012-08-06 06:29:39 +00:00
|
|
|
log 'notice', 'working...', client_id
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2012-11-26 05:04:44 +00:00
|
|
|
|
|
|
|
# ajax
|
2013-08-21 18:35:22 +00:00
|
|
|
client_list = Sessions.list
|
2012-11-26 05:04:44 +00:00
|
|
|
clients = 0
|
2017-11-23 08:09:44 +00:00
|
|
|
client_list.each_value do |client|
|
2012-11-26 05:04:44 +00:00
|
|
|
next if client[:meta][:type] == 'websocket'
|
|
|
|
clients = clients + 1
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-07-03 15:18:01 +00:00
|
|
|
log 'notice', "Status: ajax clients: #{clients}"
|
2017-10-01 12:25:52 +00:00
|
|
|
client_list.each do |client_id, client|
|
2012-11-26 05:04:44 +00:00
|
|
|
next if client[:meta][:type] == 'websocket'
|
|
|
|
log 'notice', 'working...', client_id
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2012-11-26 05:04:44 +00:00
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2012-08-06 06:29:39 +00:00
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
EventMachine.add_periodic_timer(0.4) do
|
2016-01-16 10:05:04 +00:00
|
|
|
next if @clients.size.zero?
|
2015-05-12 08:46:07 +00:00
|
|
|
#log 'debug', 'checking for data to send...'
|
2017-10-01 12:25:52 +00:00
|
|
|
@clients.each do |client_id, client|
|
2012-08-07 05:33:47 +00:00
|
|
|
next if client[:disconnect]
|
2012-08-06 06:29:39 +00:00
|
|
|
log 'debug', 'checking for data...', client_id
|
2012-07-23 22:22:23 +00:00
|
|
|
begin
|
2017-06-23 14:39:40 +00:00
|
|
|
queue = Sessions.queue(client_id)
|
|
|
|
next if queue.blank?
|
|
|
|
log 'notice', 'send data to client', client_id
|
|
|
|
websocket_send(client_id, queue)
|
2012-08-03 22:46:05 +00:00
|
|
|
rescue => e
|
2012-08-06 06:29:39 +00:00
|
|
|
log 'error', 'problem:' + e.inspect, client_id
|
|
|
|
|
|
|
|
# disconnect client
|
2012-08-07 05:33:47 +00:00
|
|
|
client[:error_count] += 1
|
2013-06-22 17:00:58 +00:00
|
|
|
if client[:error_count] > 20
|
2012-08-07 05:33:47 +00:00
|
|
|
if @clients.include? client_id
|
|
|
|
@clients.delete client_id
|
|
|
|
end
|
2012-08-06 06:29:39 +00:00
|
|
|
end
|
2012-07-23 22:22:23 +00:00
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
|
|
|
end
|
2012-11-02 16:10:22 +00:00
|
|
|
|
2016-01-05 08:29:15 +00:00
|
|
|
def get_remote_ip(headers)
|
|
|
|
return headers['X-Forwarded-For'] if headers && headers['X-Forwarded-For']
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2015-05-12 11:31:32 +00:00
|
|
|
def websocket_send(client_id, data)
|
2016-01-15 17:22:57 +00:00
|
|
|
msg = if data.class != Array
|
|
|
|
"[#{data.to_json}]"
|
|
|
|
else
|
|
|
|
data.to_json
|
|
|
|
end
|
2015-05-12 08:46:07 +00:00
|
|
|
log 'debug', "send #{msg}", client_id
|
|
|
|
if !@clients[client_id]
|
|
|
|
log 'error', "no such @clients for #{client_id}", client_id
|
|
|
|
return
|
|
|
|
end
|
2015-05-12 11:31:32 +00:00
|
|
|
@clients[client_id][:websocket].send(msg)
|
2015-05-12 08:46:07 +00:00
|
|
|
end
|
|
|
|
|
2012-12-24 13:53:50 +00:00
|
|
|
def check_unused_connections
|
2015-04-27 13:20:16 +00:00
|
|
|
log 'notice', 'check unused idle connections...'
|
2012-12-24 13:53:50 +00:00
|
|
|
|
2015-02-25 22:17:34 +00:00
|
|
|
idle_time_in_sec = 4 * 60
|
2012-12-24 13:53:50 +00:00
|
|
|
|
2015-01-13 16:03:58 +00:00
|
|
|
# close unused web socket sessions
|
2017-10-01 12:25:52 +00:00
|
|
|
@clients.each do |client_id, client|
|
2012-12-24 13:53:50 +00:00
|
|
|
|
2015-05-10 20:53:15 +00:00
|
|
|
next if ( client[:last_ping].to_i + idle_time_in_sec ) >= Time.now.utc.to_i
|
2012-12-24 13:53:50 +00:00
|
|
|
|
2015-05-07 09:04:40 +00:00
|
|
|
log 'notice', 'closing idle websocket connection', client_id
|
2012-12-24 13:53:50 +00:00
|
|
|
|
2015-05-07 09:04:40 +00:00
|
|
|
# remember to not use this connection anymore
|
|
|
|
client[:disconnect] = true
|
|
|
|
|
|
|
|
# try to close regular
|
|
|
|
client[:websocket].close_websocket
|
|
|
|
|
|
|
|
# delete session from client list
|
|
|
|
sleep 0.3
|
|
|
|
@clients.delete(client_id)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2012-12-24 13:53:50 +00:00
|
|
|
|
2015-01-13 16:03:58 +00:00
|
|
|
# close unused ajax long polling sessions
|
2016-11-30 10:30:03 +00:00
|
|
|
clients = Sessions.destroy_idle_sessions(idle_time_in_sec)
|
2017-10-01 12:25:52 +00:00
|
|
|
clients.each do |client_id|
|
2015-04-27 13:20:16 +00:00
|
|
|
log 'notice', 'closing idle long polling connection', client_id
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2012-12-24 13:53:50 +00:00
|
|
|
end
|
|
|
|
|
2015-12-09 13:09:37 +00:00
|
|
|
def log(level, data, client_id = '-')
|
2013-03-10 23:14:31 +00:00
|
|
|
if !@options[:v]
|
2012-08-06 06:29:39 +00:00
|
|
|
return if level == 'debug'
|
|
|
|
end
|
2015-07-03 15:18:01 +00:00
|
|
|
puts "#{Time.now.utc.iso8601}:client(#{client_id}) #{data}"
|
2015-05-12 08:46:07 +00:00
|
|
|
#puts "#{Time.now.utc.iso8601}:#{ level }:client(#{ client_id }) #{ data }"
|
2012-08-03 22:46:05 +00:00
|
|
|
end
|
2012-07-23 22:22:23 +00:00
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|