Skip to content

Commit

Permalink
Renamed Server#add_rule to just #rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed May 28, 2024
1 parent 31505b3 commit 9c71950
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,28 @@ DNS nameserver.
require 'ronin/dns/proxy'

Ronin::DNS::Proxy.run('127.0.0.1', 2346) do |server|
server.add_rule :A, 'example.com', '10.0.0.1'
server.add_rule :AAAA, 'example.com', 'dead:beef::1'
server.rule :A, 'example.com', '10.0.0.1'
server.rule :AAAA, 'example.com', 'dead:beef::1'

# return multiple values
server.add_rule :A, 'ftp.example.com', ['10.0.0.42', '10.0.0.43']
server.rule :A, 'ftp.example.com', ['10.0.0.42', '10.0.0.43']

# match a query using a regex
server.add_rule :TXT, /^spf\./, "v=spf1 include:10.0.0.1 ~all"
server.rule :TXT, /^spf\./, "v=spf1 include:10.0.0.1 ~all"

# return an error for a valid hostname
server.add_rule :A, 'updates.example.com', :ServFail
server.rule :A, 'updates.example.com', :ServFail

# define a dynamic rule
server.add_rule(:CNAME, /^www\./) do |type,name,transaction|
server.rule(:CNAME, /^www\./) do |type,name,transaction|
# append '.hax' to the domain name
names = name.split('.').push('hax')

transaction.respond!(names)
end

# return MX records
server.add_rule(:MX, 'example.com') do |type,name,transaction|
server.rule(:MX, 'example.com') do |type,name,transaction|
transaction.respond!(10, Resolv::DNS::Name.create('email.evil.com' ))
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/ronin/dns/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ module Proxy
# require 'ronin/dns/proxy'
#
# Ronin::DNS::Proxy.run('127.0.0.1', 2346) do |server|
# server.add_rule :A, 'example.com', '10.0.0.1'
# server.add_rule :AAAA, 'example.com', 'dead:beef::1'
# server.rule :A, 'example.com', '10.0.0.1'
# server.rule :AAAA, 'example.com', 'dead:beef::1'
# end
#
def self.run(host,port,**kwargs,&block)
Expand Down
22 changes: 11 additions & 11 deletions lib/ronin/dns/proxy/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ class Server < Async::DNS::Server
#
# @example Initializes a new DNS proxy server:
# server = Ronin::DNS::Proxy.new('127.0.0.1', 2346)
# server.add_rule :A, 'example.com', '10.0.0.1'
# server.add_rule :AAAA, 'example.com', 'dead:beef::1'
# server.rule :A, 'example.com', '10.0.0.1'
# server.rule :AAAA, 'example.com', 'dead:beef::1'
#
# @example Initializing a new DNS proxy server with a block:
# server = Ronin::DNS::Proxy.new('127.0.0.1', 2346) do |server|
# server.add_rule :A, 'example.com', '10.0.0.1'
# server.add_rule :AAAA, 'example.com', 'dead:beef::1'
# server.rule :A, 'example.com', '10.0.0.1'
# server.rule :AAAA, 'example.com', 'dead:beef::1'
# end
#
# @api public
Expand All @@ -101,7 +101,7 @@ def initialize(host,port, nameservers: Ronin::Support::Network::DNS.nameservers,

if rules
rules.each do |(record_type,name,result)|
add_rule(record_type,name,result)
rule(record_type,name,result)
end
end

Expand Down Expand Up @@ -143,19 +143,19 @@ def initialize(host,port, nameservers: Ronin::Support::Network::DNS.nameservers,
# The DNS query transaction object.
#
# @example override the IP address for a domain:
# server.add_rule :A, 'example.com', '10.0.0.42'
# server.rule :A, 'example.com', '10.0.0.42'
#
# @example return multiple IP addresses:
# server.add_rule :A, 'example.com', ['10.0.0.42', '10.0.0.43']
# server.rule :A, 'example.com', ['10.0.0.42', '10.0.0.43']
#
# @example return an error for the given hostname:
# server.add_rule :A, 'updates.example.com', :ServFail
# server.rule :A, 'updates.example.com', :ServFail
#
# @example match a query using a regex:
# server.add_rule :TXT, /^spf\./, "v=spf1 include:10.0.0.1 ~all"
# server.rule :TXT, /^spf\./, "v=spf1 include:10.0.0.1 ~all"
#
# @example define a dynamic rule:
# server.add_rule(:CNAME, /^www\./) do |type,name,transaction|
# server.rule(:CNAME, /^www\./) do |type,name,transaction|
# # append '.hax' to the domain name
# names = name.split('.').push('hax')
#
Expand All @@ -164,7 +164,7 @@ def initialize(host,port, nameservers: Ronin::Support::Network::DNS.nameservers,
#
# @api public
#
def add_rule(record_type,name,result=nil,&block)
def rule(record_type,name,result=nil,&block)
unless (result || block)
raise(ArgumentError,"must specify a result value or a block")
end
Expand Down
16 changes: 8 additions & 8 deletions spec/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@
end
end

describe "#add_rule" do
describe "#rule" do
let(:record_type) { :A }
let(:record_name) { 'example.com' }
let(:record_result) { '10.0.0.1' }

context "when type, name, and result arguments are given" do
before do
subject.add_rule :TXT, 'foo.example.com', '1.2.3.4'
subject.add_rule record_type, record_name, record_result
subject.rule :TXT, 'foo.example.com', '1.2.3.4'
subject.rule record_type, record_name, record_result
end

it "must append a new Ronin::DNS::Proxy::Rule object to #rules with the type, name, and result arguments" do
Expand All @@ -86,8 +86,8 @@
end

before do
subject.add_rule :TXT, 'foo.example.com', '1.2.3.4'
subject.add_rule(record_type,record_name,&block)
subject.rule :TXT, 'foo.example.com', '1.2.3.4'
subject.rule(record_type,record_name,&block)
end

it "must set the rule's result to the given block" do
Expand All @@ -101,7 +101,7 @@
context "and no block is given" do
it do
expect {
subject.add_rule(record_type,record_name)
subject.rule(record_type,record_name)
}.to raise_error(ArgumentError,"must specify a result value or a block")
end
end
Expand All @@ -119,7 +119,7 @@
let(:rule_result) { '10.0.0.1' }

before do
subject.add_rule rule_type, rule_name, rule_result
subject.rule rule_type, rule_name, rule_result
end

it "must return the result from the rule" do
Expand All @@ -131,7 +131,7 @@

context "when the query does not match any rule" do
before do
subject.add_rule :A, 'will.not.match.example.com', '1.2.3.4'
subject.rule :A, 'will.not.match.example.com', '1.2.3.4'
end

it "must pass through the query to the upstream resolver" do
Expand Down

0 comments on commit 9c71950

Please sign in to comment.