Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
83 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
require 'iptables/protocol/tcp' | ||
require 'iptables/protocol/udp' | ||
require 'iptables/rule' | ||
|
||
module IPTables | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module IPTables | ||
module Protocol | ||
module UDP | ||
attr_reader :source_port, :destination_port | ||
|
||
def self.extended(base) | ||
base.mod_opts[:protocol] = [] | ||
end | ||
|
||
def source_port=(value) | ||
value = value.to_i | ||
unless value > 0 | ||
raise "UDP source port must be a valid integer greater than 0" | ||
end | ||
|
||
@source_port = value | ||
@mod_opts[:protocol] << "--sport" << source_port | ||
end | ||
|
||
def destination_port=(value) | ||
value = value.to_i | ||
unless value > 0 | ||
raise "UDP destination port must be a valid integer greater than 0" | ||
end | ||
|
||
@destination_port = value | ||
@mod_opts[:protocol] << "--dport" << destination_port | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'spec_helper' | ||
|
||
describe IPTables::Rule do | ||
describe "when protocol is udp" do | ||
subject do | ||
rule = IPTables::Rule.new | ||
rule.protocol = :udp | ||
rule | ||
end | ||
|
||
its(:protocol) { should == :udp } | ||
|
||
it { should respond_to(:source_port).with(0).arguments } | ||
it { should respond_to(:source_port=).with(1).argument } | ||
it { should respond_to(:destination_port).with(0).arguments } | ||
it { should respond_to(:destination_port=).with(1).argument } | ||
|
||
context "and creating a rule with source_port" do | ||
subject do | ||
rule = IPTables::Rule.new | ||
rule.chain = :input | ||
rule.target = :accept | ||
rule.protocol = :udp | ||
rule.source_port = 80 | ||
rule | ||
end | ||
|
||
its(:source_port) { should == 80 } | ||
its(:to_iptables) { | ||
should == "-A INPUT -p udp --sport 80 -j ACCEPT" | ||
} | ||
end | ||
|
||
context "and creating a rule with destination_port" do | ||
subject do | ||
rule = IPTables::Rule.new | ||
rule.chain = :input | ||
rule.target = :accept | ||
rule.protocol = :udp | ||
rule.destination_port = 443 | ||
rule | ||
end | ||
|
||
its(:destination_port) { should == 443 } | ||
its(:to_iptables) { | ||
should == "-A INPUT -p udp --dport 443 -j ACCEPT" | ||
} | ||
end | ||
end | ||
end |