fidius-common 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ doc/
6
+ .yardoc/
data/.yardopts ADDED
@@ -0,0 +1,6 @@
1
+ --title 'FIDIUS Common libraries'
2
+ --private
3
+ --protected
4
+ --readme 'README.md'
5
+ lib/**/*.rb -
6
+ LICENSE CREDITS.md
data/CREDITS.md ADDED
@@ -0,0 +1,5 @@
1
+ Third-party tools
2
+
3
+ * `lib/fidius-common/ip-helper.rb`: `get_my_ip` methods
4
+ * Credit goes to Coderr
5
+ * https://coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in fidiuscommon.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,57 @@
1
+ The Simplified BSD License
2
+
3
+ Copyright (C) 2010-2011 FIDIUS Intrusion Detection with Intelligent
4
+ User Support (FIDIUS). All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are
8
+ met:
9
+
10
+ 1. Redistributions of source code must retain the above copyright
11
+ notice, this list of conditions and the following disclaimer.
12
+
13
+ 2. Redistributions in binary form must reproduce the above copyright
14
+ notice, this list of conditions and the following disclaimer in the
15
+ documentation and/or other materials provided with the distribution.
16
+
17
+ THIS SOFTWARE IS PROVIDED BY FIDIUS ``AS IS'' AND ANY EXPRESS OR
18
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ DISCLAIMED. IN NO EVENT SHALL FIDIUS OR CONTRIBUTORS BE LIABLE FOR ANY
21
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
+ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25
+ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26
+ IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
+ POSSIBILITY OF SUCH DAMAGE.
28
+
29
+ The views and conclusions contained in the software and documentation
30
+ are those of the authors and should not be interpreted as representing
31
+ official policies, either expressed or implied, of FIDIUS.
32
+
33
+
34
+ *OR*
35
+
36
+ GNU GENERAL PUBLIC LICENSE
37
+ Version 2, June 1991
38
+
39
+ Copyright (C) 2010-2011 FIDIUS Intrusion Detection with Intelligent
40
+ User Suppport.
41
+
42
+ This program is free software; you can redistribute it and/or modify
43
+ it under the terms of the GNU General Public License as published by
44
+ the Free Software Foundation; either version 2 of the License, or
45
+ (at your option) any later version.
46
+
47
+ This program is distributed in the hope that it will be useful,
48
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
49
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50
+ GNU General Public License for more details.
51
+
52
+ You should have received a copy of the GNU General Public License along
53
+ with this program; if not, write to the Free Software Foundation, Inc.,
54
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
55
+
56
+ A digital copy is also available for download here:
57
+ http://www.gnu.org/licenses/gpl-2.0.txt.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # FIDIUS Common libraries
2
+
3
+ This RubyGem is a small collection of some commonly used methods and
4
+ module extensions of the FIDIUS project. These methods are meant to run
5
+ standalone (i.e. without setting up a FIDIUS C&C server).
6
+
7
+
8
+ ## Installation
9
+
10
+ Simply install this package with RubyGems:
11
+
12
+ $ gem install fidius-common
13
+
14
+
15
+ ## Example of use
16
+
17
+ You can use this library as any other RubyGem:
18
+
19
+ require 'rubygems' if RUBY_VERSION < '1.9'
20
+ require 'fidius-common'
21
+
22
+ For an overview of the provided methods, have a look into the
23
+ [API documentation](http://rubydoc.info/gems/fidius-common/frames).
24
+ Currently, there is no special sorting, but that may change in future.
25
+
26
+
27
+ ## Authors and Contact
28
+
29
+ The common library was written by
30
+
31
+ * FIDIUS Intrusion Detection with Intelligent User Support
32
+ <grp-fidius+fidicom@tzi.de>, <http://fidius.me>
33
+ * in particular:
34
+ * Dominik Menke <dmke+fidicom@tzi.de>
35
+ * Bernd Katzmarski <bkatzm+fidicom@tzi.de>
36
+
37
+ If you have any questions, remarks, suggestion, improvements,
38
+ etc. feel free to drop a line at the addresses given above.
39
+ You might also join `#fidius` on Freenode or use the contact
40
+ form on our [website](http://fidius.me/en/contact).
41
+
42
+
43
+ ## License
44
+
45
+ Simplified BSD License and GNU GPLv2. See also the file LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ require 'rake/testtask'
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "test"
8
+ t.test_files = FileList['test/test*.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ begin
13
+ require 'yard'
14
+
15
+ YARD::Rake::YardocTask.new(:doc) do |t|
16
+ t.files = ['lib/**/*.rb']
17
+ static_files = 'LICENSE,CREDITS.md'
18
+ t.options += [
19
+ '--title', 'FIDIUS Common libraries',
20
+ '--private', # include private methods
21
+ '--protected', # include protected methods
22
+ '--files', static_files,
23
+ '--readme', 'README.md'
24
+ ]
25
+ end
26
+ rescue LoadError
27
+ puts 'YARD not installed (gem install yard), http://yardoc.org'
28
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "fidius-common/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "fidius-common"
7
+ s.version = FIDIUS::Common::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Dominik Menke", "Bernd Katzmarski"]
10
+ s.email = ["dmke+fidicom@tzi.org", "bkatzm+fidicom@tzi.org"]
11
+ s.homepage = ""
12
+ s.summary = "Common useful libraries and methods for the FIDIUS C&C server, the FIDIUS 'architecture' and maybe other components."
13
+ s.description = s.summary + "\n\nThese gem and its classes/modules/methods are meant to run stand-alone, e.g. in non-FIDIUS components."
14
+
15
+ s.rubyforge_project = ""
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_runtime_dependency 'active_support', '>= 3.0.0'
23
+
24
+ s.rdoc_options << '--title' << s.name <<
25
+ '--main' << 'README.md' << '--show-hash' <<
26
+ `git ls-files -- lib/*`.split("\n") <<
27
+ 'README.md' << 'LICENSE' << 'CREDITS.md'
28
+ end
@@ -0,0 +1,32 @@
1
+ require 'ipaddr' # Used to gain a single address from an IP range.
2
+ require 'socket' # Used to determine this host's IP address.
3
+
4
+ module FIDIUS
5
+ module Common
6
+
7
+ # Returns the IP address of that interface, which would connect to
8
+ # an address of the given +iprange+.
9
+ #
10
+ # Thanks to Coderrr (https://coderrr.wordpress.com) for this
11
+ # implementation.
12
+ #
13
+ # @see https://coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/
14
+ #
15
+ # @param [String] iprange The target IP range, e.g. +127.0.0.0/8+
16
+ # or +127.0.0.1+.
17
+ # @return [String] Your (local) IP address.
18
+ def self.get_my_ip(iprange)
19
+ # backup setting
20
+ orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true
21
+ UDPSocket.open do |s|
22
+ # udp is stateless, so there is no real connect
23
+ s.connect IPAddr.new(iprange).to_s, 1
24
+ s.addr.last
25
+ end
26
+ ensure
27
+ # restore backup
28
+ Socket.do_not_reverse_lookup = orig
29
+ end
30
+
31
+ end # module Common
32
+ end # FIDIUS
@@ -0,0 +1,114 @@
1
+ # These changes are necessary to extend json encoding & decoding
2
+ # for ruby-symbols. Tricky parts are symbols in Arrays or Hashes.
3
+
4
+ require 'active_support'
5
+ require 'active_support/json'
6
+
7
+ # Part of the JSON Symbol Addon.
8
+ # @see FIDIUS::Common.install_json_symbol_addon
9
+ class Symbol
10
+ # Encodes symbols as +String+, prefixed with ":".
11
+ #
12
+ # @param [Hash] options Ingnored.
13
+ # @return [String] The converted +Symbol+.
14
+ def as_json(options = nil)
15
+ ":" + to_s
16
+ end
17
+ end
18
+
19
+ # Part of the JSON Symbol Addon.
20
+ # @see FIDIUS::Common.install_json_symbol_addon
21
+ class Hash
22
+ # JSON-encodes +self+.
23
+ #
24
+ # @param [Hash] options An options-hash. Keys maybe +:only+, +:exept+
25
+ # or +:encoder+.
26
+ # @return [String] The JSON-encoded string of +self+.
27
+ def as_json(options = nil)
28
+ # create a subset of the hash by applying :only or :except
29
+ subset = if options
30
+ if attrs = options[:only]
31
+ slice(*Array.wrap(attrs))
32
+ elsif attrs = options[:except]
33
+ except(*Array.wrap(attrs))
34
+ else
35
+ self
36
+ end
37
+ else
38
+ self
39
+ end
40
+
41
+ # use encoder as a proxy to call as_json on all values in the subset, to protect from circular references
42
+ encoder = options && options[:encoder] || ActiveSupport::JSON::Encoding::Encoder.new(options)
43
+
44
+ # FIDIUS changed here to from k.to_s to k.as_json to decode strings with : as symbols
45
+ pairs = subset.map do |k, v|
46
+ [k.as_json, encoder.as_json(v)]
47
+ end
48
+ result = if self.is_a?(ActiveSupport::OrderedHash)
49
+ ActiveSupport::OrderedHash.new
50
+ else
51
+ Hash.new
52
+ end
53
+ pairs.inject(result) do |hash, pair|
54
+ hash[pair.first] = pair.last; hash
55
+ end
56
+ end
57
+
58
+ # Converts each key to a symbol, if the key is a +String+ and its
59
+ # first character is a +:+.
60
+ #
61
+ # @return [Hash] The converted hash.
62
+ def symbolize_keys_if_needed
63
+ to_add = Hash.new
64
+ self.each_pair do |k,v|
65
+ if v.respond_to?("symbolize_keys_if_needed")
66
+ v.symbolize_keys_if_needed
67
+ end
68
+ if k[0] == ":"
69
+ self.delete(k)
70
+ to_add[k[1..-1].to_sym] = v
71
+ end
72
+ end
73
+ to_add.each_pair do |k,v|
74
+ self[k] = v
75
+ end
76
+ end
77
+ end
78
+
79
+ # Part of the JSON Symbol Addon.
80
+ # @see FIDIUS::Common.install_json_symbol_addon
81
+ class Array
82
+ # [1,":aa",:asa].symbolize_keys_if_needed
83
+ # => [1, :aa, :asa]
84
+ def symbolize_keys_if_needed
85
+ self.each_with_index do |e,i|
86
+ e.symbolize_keys_if_needed if e.respond_to?("symbolize_keys_if_needed")
87
+ if self[i].to_s[0] == ":"
88
+ self[i] = e[1..-1].to_sym
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ # Part of the JSON Symbol Addon.
95
+ # @see FIDIUS::Common.install_json_symbol_addon
96
+ # @see http://rubyonrails.org
97
+ # @see http://api.rubyonrails.org
98
+ module ActiveSupport
99
+ # ActiveSupports JSON module
100
+ # @see http://api.rubyonrails.org/classes/ActiveSupport/JSON.html
101
+ module JSON
102
+ # Parses a JSON string or IO and convert it into an object. Any
103
+ # strings of that object will be converted, if they are prefixed
104
+ # with a +:+.
105
+ #
106
+ # @param [String, IO] json Any JSON formatted object.
107
+ # @return [Object] The converted object.
108
+ def self.decode(json)
109
+ set_default_backend unless defined?(@backend)
110
+ result = @backend.decode(json)
111
+ result.symbolize_keys_if_needed
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,8 @@
1
+ module FIDIUS
2
+ module Common
3
+
4
+ # Current version number
5
+ VERSION = "0.0.1"
6
+
7
+ end # module Common
8
+ end # module FIDIUS
@@ -0,0 +1,29 @@
1
+ require 'fidius-common/ip-helper'
2
+
3
+ # FIDIUS Intrusion Detection with Intelligent User Support.
4
+ # @see http://fidius.me
5
+ module FIDIUS
6
+ # Common used methods and libraries. You might use these also in a
7
+ # non-FIDIUS context, since there are no other FIDIUS-related
8
+ # dependencies.
9
+ module Common
10
+
11
+ autoload :VERSION, './fidius-common/version'
12
+
13
+ # Since this addon changes behaviour of +to_json+, you have to
14
+ # require it explicitly either with this helper method or manually
15
+ # with
16
+ #
17
+ # require 'fidius-common/json_symbol_addon'
18
+ #
19
+ # Important note: Both variants will also load and require
20
+ # +active_support+.
21
+ #
22
+ # @return [Boolean] true if the addon was loaded successfully or
23
+ # false if the addon is already loaded.
24
+ def self.install_json_symbol_addon
25
+ require 'fidius-common/json_symbol_addon'
26
+ end
27
+
28
+ end # module Common
29
+ end # module FIDIUS
@@ -0,0 +1,25 @@
1
+ require 'fidius-common/ip-helper'
2
+ require 'test/unit'
3
+
4
+ class IPHelperTest < Test::Unit::TestCase
5
+ def test_get_localhost
6
+ ip = FIDIUS::Common.get_my_ip '127.0.0.1'
7
+ assert_equal '127.0.0.1', ip
8
+ end
9
+
10
+ def test_get_invalid_host
11
+ assert_raise ArgumentError do
12
+ FIDIUS::Common.get_my_ip '300.0.0.0'
13
+ end
14
+ end
15
+
16
+ def test_get_google_dns
17
+ begin
18
+ # run `ifconfig` to determine possible addresses?
19
+ ip = FIDIUS::Common.get_my_ip '8.8.8.8'
20
+ assert_not_nil ip
21
+ rescue Errno::ENETUNREACH
22
+ puts "Skipped get_google_dns, since network is not reachable"
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ require 'fidius-common/json_symbol_addon'
2
+ require 'test/unit'
3
+
4
+
5
+ class JSONSymbolAddonTest < Test::Unit::TestCase
6
+ SIMPLE_HASH_SAMPLE = { :a => 1, :b => 2 }
7
+ SIMPLE_ARRAY_SAMPLE = [ :a, 1, :b, 2 ]
8
+
9
+ def test_simple_hash
10
+ json = JSON.parse(SIMPLE_HASH_SAMPLE.to_json)
11
+ assert_not_equal SIMPLE_HASH_SAMPLE, json
12
+ assert_equal SIMPLE_HASH_SAMPLE, json.symbolize_keys_if_needed
13
+ end
14
+
15
+ def test_simple_array
16
+ json = JSON.parse(SIMPLE_ARRAY_SAMPLE.to_json)
17
+ assert_not_equal SIMPLE_ARRAY_SAMPLE, json
18
+ assert_equal SIMPLE_ARRAY_SAMPLE, json.symbolize_keys_if_needed
19
+ end
20
+
21
+ def test_complex_sample
22
+ sample = {
23
+ :array => SIMPLE_ARRAY_SAMPLE,
24
+ :hash => SIMPLE_HASH_SAMPLE
25
+ }
26
+ json = JSON.parse(sample.to_json)
27
+ assert_not_equal sample, json
28
+ assert_equal sample, json.symbolize_keys_if_needed
29
+ end
30
+
31
+ end # class JSONSymbolAddonTest
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fidius-common
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Dominik Menke
13
+ - Bernd Katzmarski
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-17 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: active_support
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ segments:
30
+ - 3
31
+ - 0
32
+ - 0
33
+ version: 3.0.0
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ description: |-
37
+ Common useful libraries and methods for the FIDIUS C&C server, the FIDIUS 'architecture' and maybe other components.
38
+
39
+ These gem and its classes/modules/methods are meant to run stand-alone, e.g. in non-FIDIUS components.
40
+ email:
41
+ - dmke+fidicom@tzi.org
42
+ - bkatzm+fidicom@tzi.org
43
+ executables: []
44
+
45
+ extensions: []
46
+
47
+ extra_rdoc_files: []
48
+
49
+ files:
50
+ - .gitignore
51
+ - .yardopts
52
+ - CREDITS.md
53
+ - Gemfile
54
+ - LICENSE
55
+ - README.md
56
+ - Rakefile
57
+ - fidius-common.gemspec
58
+ - lib/fidius-common.rb
59
+ - lib/fidius-common/ip-helper.rb
60
+ - lib/fidius-common/json_symbol_addon.rb
61
+ - lib/fidius-common/version.rb
62
+ - test/test_iphelper.rb
63
+ - test/test_json_addon.rb
64
+ has_rdoc: true
65
+ homepage: ""
66
+ licenses: []
67
+
68
+ post_install_message:
69
+ rdoc_options:
70
+ - --title
71
+ - fidius-common
72
+ - --main
73
+ - README.md
74
+ - --show-hash
75
+ - - lib/fidius-common.rb
76
+ - lib/fidius-common/ip-helper.rb
77
+ - lib/fidius-common/json_symbol_addon.rb
78
+ - lib/fidius-common/version.rb
79
+ - README.md
80
+ - LICENSE
81
+ - CREDITS.md
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ requirements: []
101
+
102
+ rubyforge_project: ""
103
+ rubygems_version: 1.3.7
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Common useful libraries and methods for the FIDIUS C&C server, the FIDIUS 'architecture' and maybe other components.
107
+ test_files: []
108
+