mqsink 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/mqsinkd +26 -0
- data/lib/mqsink/acl.rb +27 -0
- data/lib/mqsink/amqp.rb +48 -0
- data/lib/mqsink/server.rb +57 -0
- data/lib/mqsink.rb +9 -0
- metadata +51 -0
data/bin/mqsinkd
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'mqsink'
|
5
|
+
|
6
|
+
def local_boot
|
7
|
+
if not Dir.exists? "/etc/mqsink"
|
8
|
+
system("mkdir /etc/mqsink")
|
9
|
+
system("touch /etc/mqsink/acl.conf")
|
10
|
+
system("touch /etc/mqsink/classifiers.conf")
|
11
|
+
system("touch /etc/mqsink/mqsink.json")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
local_boot
|
16
|
+
jconfig = ARGV[0]
|
17
|
+
jconfig = '/etc/mqsink/mqsink.json' if not ARGV[0]
|
18
|
+
cfg_file = open(jconfig).readlines.join
|
19
|
+
puts "+ FATAL : the configuration file is empty!" if cfg_file == ''
|
20
|
+
exit 1 if cfg_file == ''
|
21
|
+
begin
|
22
|
+
cfg = JSON.parse cfg_file
|
23
|
+
debug = 0
|
24
|
+
debug = 1 if cfg['debug'] == "on"
|
25
|
+
sink = MQSink::Server.new cfg, debug
|
26
|
+
end
|
data/lib/mqsink/acl.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module MQSink
|
2
|
+
class ACL
|
3
|
+
def initialize clientip, acl
|
4
|
+
@clientip = clientip
|
5
|
+
@acl = acl
|
6
|
+
end
|
7
|
+
|
8
|
+
def dynamic_reconstruct acl, pool=[] # hacky removal of line breaks .. need to change it really #
|
9
|
+
acl.each do |line|
|
10
|
+
pool << line.chop
|
11
|
+
end
|
12
|
+
return pool
|
13
|
+
end
|
14
|
+
|
15
|
+
def lookup
|
16
|
+
pool = dynamic_reconstruct @acl
|
17
|
+
if pool.include?(@clientip)
|
18
|
+
return 0
|
19
|
+
elsif pool[0] == "0.0.0.0"
|
20
|
+
return 0
|
21
|
+
else
|
22
|
+
return 1
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
data/lib/mqsink/amqp.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
module MQSink
|
2
|
+
class AMQP
|
3
|
+
|
4
|
+
def initialize msg, remotehost, mode, mqserver, classifiers, static_queue, debug
|
5
|
+
@msg = msg
|
6
|
+
@remotehost = remotehost
|
7
|
+
@mode = mode
|
8
|
+
@mqserver = mqserver
|
9
|
+
@classifiers = classifiers
|
10
|
+
@static_queue = static_queue
|
11
|
+
@debug = debug
|
12
|
+
end
|
13
|
+
|
14
|
+
def publish
|
15
|
+
if @mode == 'xlate'
|
16
|
+
queue = @msg.split(" ")[0]
|
17
|
+
datag = @msg.split(" ")[1]
|
18
|
+
elsif @mode == 'classify'
|
19
|
+
File.open(@classifiers).each do |line|
|
20
|
+
if line.match /\[.*?\]/
|
21
|
+
queue, regex = line.split ' '
|
22
|
+
refine_regex = regex.split('[')[1].split(']')[0..-1].join
|
23
|
+
re = Regexp.new(refine_regex)
|
24
|
+
if @msg.match re
|
25
|
+
datag = @msg
|
26
|
+
else
|
27
|
+
return
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
elsif @mode == 'raw'
|
32
|
+
queue = @static_queue
|
33
|
+
datag = @msg
|
34
|
+
end
|
35
|
+
connection = Bunny.new(:hostname => @mqserver)
|
36
|
+
connection.start
|
37
|
+
ch = connection.create_channel
|
38
|
+
q = ch.queue(queue)
|
39
|
+
message = datag
|
40
|
+
mssg = { :timestamp => Time.now, :service => 'mqsink', :fwd_host => @remotehost, :payload => message }
|
41
|
+
mssg = JSON.dump mssg
|
42
|
+
puts "+ mqsink [debug]: outbound message to queue '#{queue}' -> #{mssg}" if @debug == 1
|
43
|
+
ch.default_exchange.publish(mssg, :routing_key => q.name)
|
44
|
+
connection.close
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module MQSink
|
2
|
+
class Server
|
3
|
+
|
4
|
+
def initialize cfg, debug
|
5
|
+
@localhost = cfg['localhost']
|
6
|
+
@localport = cfg['localport']
|
7
|
+
@firewall = cfg['firewall']
|
8
|
+
@fw_file = cfg['fw_file']
|
9
|
+
@mode = cfg['mode']
|
10
|
+
@mqserver = cfg['amqp_server']
|
11
|
+
@classifiers = cfg['classifiers']
|
12
|
+
@static_queue = cfg['static_queue']
|
13
|
+
@debug = debug
|
14
|
+
@acl = build_acl @fw_file
|
15
|
+
@acl_refresh = cfg['fw_refresh']
|
16
|
+
listen
|
17
|
+
end
|
18
|
+
|
19
|
+
def build_acl fw_file
|
20
|
+
puts "+ mqsink [debug]: rebuilding ACL .." if @debug == 1
|
21
|
+
@acl = open(fw_file).readlines
|
22
|
+
return @acl
|
23
|
+
end
|
24
|
+
|
25
|
+
def listen
|
26
|
+
@sock = UDPSocket.new
|
27
|
+
puts "+ mqsink [debug]: loading server .." if @debug == 1
|
28
|
+
@sock.bind @localhost, @localport
|
29
|
+
count = 1
|
30
|
+
while true do
|
31
|
+
r, w, e = IO.select([@sock], nil, nil)
|
32
|
+
if r[0]
|
33
|
+
buffer, sockaddr = @sock.recvfrom_nonblock(1500)
|
34
|
+
if @firewall == "on"
|
35
|
+
@acl = build_acl @fw_file if count == 5 and @acl_refresh == 'on'
|
36
|
+
count = 0 if count == 5 and @acl_refresh == 'on'
|
37
|
+
count += 1
|
38
|
+
puts "+ mqsink [debug]: verifying authorization for client -> #{sockaddr[3]} .." if @debug == 1
|
39
|
+
acl = MQSink::ACL.new sockaddr[3], @acl
|
40
|
+
lookup = acl.lookup
|
41
|
+
else # do not apply ACL when firewall is disabled #
|
42
|
+
lookup = 0
|
43
|
+
end
|
44
|
+
if lookup == 0
|
45
|
+
remotehost = Resolv.getname sockaddr[3]
|
46
|
+
puts "+ mqsink [debug]: buffer accepted -> #{buffer}" if @debug == 1
|
47
|
+
relay = MQSink::AMQP.new buffer, remotehost, @mode, @mqserver, @classifiers, @static_queue, @debug
|
48
|
+
relay.publish
|
49
|
+
else
|
50
|
+
puts "+ mqsink [debug]: client is not authorized .. ignoring buffer .." if @debug == 1
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
data/lib/mqsink.rb
ADDED
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mqsink
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Samer Abdel-Hafez
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-12-30 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: a smart and lightweight udp to amqp translator
|
15
|
+
email:
|
16
|
+
- sam@ip.fi
|
17
|
+
executables:
|
18
|
+
- mqsinkd
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- lib/mqsink.rb
|
23
|
+
- lib/mqsink/acl.rb
|
24
|
+
- lib/mqsink/amqp.rb
|
25
|
+
- lib/mqsink/server.rb
|
26
|
+
- bin/mqsinkd
|
27
|
+
homepage: http://github.com/nopedial/mqsink
|
28
|
+
licenses: []
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project: mqsink
|
47
|
+
rubygems_version: 1.8.23
|
48
|
+
signing_key:
|
49
|
+
specification_version: 3
|
50
|
+
summary: udp to amqp bridge
|
51
|
+
test_files: []
|