iptable 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ec29d57d27a6e6476d7e7b45ce0b00e2a6e2d18b
4
+ data.tar.gz: 741f015325d6734dfa087f872923ab23763c4672
5
+ SHA512:
6
+ metadata.gz: a7b8932b08f65e35b209a39cbbf44d5e10129e2ad60135d3b87c7bf8c0bf2217601aed294d953257481e2c0fbea17436c3030ce27b10e3128bbc1eeba0aceeb1
7
+ data.tar.gz: 9649433b362c790c7496dbc0a2d07a4fbdebb95cd462eb9697a014fa610aed75e3da84c5625a9457bf2b4a99d699c2af1cbcee86e6034f07296a773152c23cdb
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << "test"
5
+ t.test_files = FileList['test/test*.rb']
6
+ t.verbose = true
7
+ end
data/iptable.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'iptable'
3
+ s.version = '0.0.1'
4
+ s.date = '2014-11-05'
5
+ s.summary = "IP Table"
6
+ s.description = "manipulate iptables"
7
+ s.authors = ["towski"]
8
+ s.email = 'towski@gmail.com'
9
+ s.homepage = 'http://rubygems.org/gems/iptable'
10
+ s.license = 'MIT'
11
+ s.files = `git ls-files`.split("\n")
12
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
14
+ s.require_paths = ["lib"]
15
+
16
+ #s.add_dependency('rails', '>= 3.0.7')
17
+ #s.add_dependency('render_anywhere')
18
+ end
data/lib/iptable/ip.rb ADDED
@@ -0,0 +1,64 @@
1
+ module IP
2
+ CHAIN_RE = /Chain ([a-zA-Z_]+) (\(policy ACCEPT ([0-9]+) packets, ([0-9]+) bytes\)){0,1}/
3
+ RULE_RE = /([0-9]+)\s+([0-9]+)\s+([a-zA-Z_]*)\s+([a-z]*)\s+--\s+\*\s+\*\s+([0-9\.\/]+)\s+([0-9\.\/]+)\s*(tcp (dpt|spt):([0-9]+)){0,1}/
4
+
5
+ class Table
6
+ attr_reader :chains
7
+
8
+ def initialize(load_iptables = true)
9
+ @chains = {}
10
+ load_chains if load_iptables
11
+ end
12
+
13
+ def refresh
14
+ @chains = {}
15
+ load_chains
16
+ end
17
+
18
+ def load_chains
19
+ IO.popen("/sbin/iptables -L -n -v -x") do |output|
20
+ output.readlines.each do |line|
21
+ next if match_chain(line)
22
+ @current_chain.match_rule(line) if @current_chain
23
+ end
24
+ end
25
+ end
26
+
27
+ def match_chain(line)
28
+ if match = line.match(CHAIN_RE)
29
+ name = match[1]
30
+ @current_chain = @chains[name] = Chain.new(name)
31
+ return true
32
+ end
33
+ false
34
+ end
35
+ end
36
+
37
+ class Chain
38
+ attr_reader :rules
39
+
40
+ def initialize(name)
41
+ @name = name
42
+ @rules = []
43
+ end
44
+
45
+ def add_rule(*args)
46
+ @rules << Rule.new(args)
47
+ end
48
+
49
+ def match_rule(string)
50
+ if match = string.match(RULE_RE)
51
+ add_rule match[1, -1]
52
+ end
53
+ end
54
+ end
55
+
56
+ class Rule
57
+ attr_accessor :chain, :target
58
+
59
+ def initialize(*args)
60
+ @chain = nil
61
+ @target = nil
62
+ end
63
+ end
64
+ end
data/lib/iptable.rb ADDED
@@ -0,0 +1,5 @@
1
+ unless Process.uid == 0
2
+ raise "the iptables gem must be run as root. try 'rvmsudo ruby ...'"
3
+ end
4
+
5
+ require 'iptable/ip'
data/test/test.rb ADDED
@@ -0,0 +1,25 @@
1
+ gem 'mocha'
2
+ require 'minitest/autorun'
3
+ require 'iptable'
4
+ require 'mocha/mini_test'
5
+
6
+ class Tests < Minitest::Test
7
+ def test_rule_matching
8
+ chain = IP::Chain.new "hey"
9
+ str = "607939 956613034 TRAFFIC_ACCT_OUT all -- * * 0.0.0.0/0 0.0.0.0/0 "
10
+ chain.match_rule str
11
+ assert chain.rules.size == 1
12
+ end
13
+
14
+ def test_match_chain
15
+ IP::Table.any_instance.expects(:load_chains)
16
+ table = IP::Table.new
17
+ assert table.match_chain "Chain OUTPUT (policy ACCEPT 607201 packets, 960137939 bytes) "
18
+ end
19
+
20
+ def test_match_chain_without_policy
21
+ IP::Table.any_instance.expects(:load_chains)
22
+ table = IP::Table.new
23
+ assert table.match_chain "Chain TRAFFIC_ACCT (0 references)"
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iptable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - towski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: manipulate iptables
14
+ email: towski@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - Rakefile
20
+ - iptable.gemspec
21
+ - lib/iptable.rb
22
+ - lib/iptable/ip.rb
23
+ - test/test.rb
24
+ homepage: http://rubygems.org/gems/iptable
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.2.2
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: IP Table
48
+ test_files: []