Skip to content

Commit

Permalink
Added Request#remote_addr, #remote_ip, and #remote_port` (closes #6
Browse files Browse the repository at this point in the history
…).
  • Loading branch information
postmodern committed Jul 6, 2024
1 parent deb7ee1 commit b2ace9b
Show file tree
Hide file tree
Showing 4 changed files with 334 additions and 183 deletions.
70 changes: 54 additions & 16 deletions lib/ronin/listener/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ module HTTP
#
class Request

# The remote address that sent the request.
#
# @return [Addrinfo]
attr_reader :remote_addr

# The HTTP request method.
#
# @return [String]
Expand Down Expand Up @@ -57,6 +62,9 @@ class Request
#
# Initializes the request.
#
# @param [Addrinfo] remote_addr
# The remote address that sent the request.
#
# @param [String] method
# The HTTP request method.
#
Expand All @@ -72,12 +80,36 @@ class Request
# @param [String, nil] body
# The optional body sent with the request.
#
def initialize(method: , path: , version: , headers:, body: nil)
@method = method
@path = path
@version = version
@headers = headers
@body = body
def initialize(remote_addr: ,
method: ,
path: ,
version: ,
headers:,
body: nil)
@remote_addr = remote_addr
@method = method
@path = path
@version = version
@headers = headers
@body = body
end

#
# The remote IP address that sent the request.
#
# @return [String]
#
def remote_ip
@remote_addr.ip_address
end

#
# The remote port that sent the request.
#
# @return [String]
#
def remote_port
@remote_addr.ip_port
end

#
Expand All @@ -91,11 +123,13 @@ def initialize(method: , path: , version: , headers:, body: nil)
#
def ==(other)
self.class == other.class &&
@method == other.method &&
@path == other.path &&
@version == other.version &&
@headers == other.headers &&
@body == other.body
remote_ip == other.remote_ip &&
remote_port == other.remote_port &&
@method == other.method &&
@path == other.path &&
@version == other.version &&
@headers == other.headers &&
@body == other.body
end

#
Expand All @@ -122,11 +156,13 @@ def to_s
#
def to_h
{
method: @method,
path: @path,
version: @version,
headers: @headers,
body: @body
remote_ip: remote_ip,
remote_port: remote_port,
method: @method,
path: @path,
version: @version,
headers: @headers,
body: @body
}
end

Expand All @@ -139,6 +175,8 @@ def to_h
def to_csv
CSV.generate_line(
[
remote_ip,
remote_port,
@method,
@path,
@version,
Expand Down
11 changes: 6 additions & 5 deletions lib/ronin/listener/http/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,12 @@ def process(request)
if request.path == @root || request.path.start_with?(@root)
@callback.call(
Request.new(
method: request.method,
path: request.path,
version: request.version,
headers: request.headers,
body: request.body
remote_addr: request.remote_address,
method: request.method,
path: request.path,
version: request.version,
headers: request.headers,
body: request.body
)
)
end
Expand Down
89 changes: 69 additions & 20 deletions spec/request_spec.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
require 'spec_helper'
require 'ronin/listener/http/request'

require 'socket'

describe Ronin::Listener::HTTP::Request do
let(:method) { 'GET' }
let(:path) { '/' }
let(:version) { '1.1' }
let(:headers) { {'Host' => 'host1.com'} }
let(:body) { "foo bar" }
let(:remote_ip) { '192.168.1.1' }
let(:remote_port) { 1234 }
let(:remote_addr) { Addrinfo.tcp(remote_ip,remote_port) }
let(:method) { 'GET' }
let(:path) { '/' }
let(:version) { '1.1' }
let(:headers) { {'Host' => 'host1.com'} }
let(:body) { "foo bar" }

subject do
described_class.new(
method: method,
path: path,
version: version,
headers: headers,
body: body
remote_addr: remote_addr,
method: method,
path: path,
version: version,
headers: headers,
body: body
)
end

describe "#initialize" do
it "must set #remote_addr" do
expect(subject.remote_addr).to eq(remote_addr)
end

it "must set #method" do
expect(subject.method).to eq(method)
end
Expand All @@ -40,16 +50,29 @@
end
end

describe "#remote_ip" do
it "must return the remote IP address String" do
expect(subject.remote_ip).to eq(remote_ip)
end
end

describe "#remote_port" do
it "must return the remote port" do
expect(subject.remote_port).to eq(remote_port)
end
end

describe "#==" do
context "when given a #{described_class}" do
context "and all attributes are the same" do
let(:other) do
described_class.new(
method: method,
path: path,
version: version,
headers: headers,
body: body
remote_addr: remote_addr,
method: method,
path: path,
version: version,
headers: headers,
body: body
)
end

Expand All @@ -58,9 +81,27 @@
end
end

context "but the #remote_addr is different" do
let(:other) do
described_class.new(
remote_addr: Addrinfo.tcp('192.168.1.42',2345),
method: method,
path: path,
version: version,
headers: headers,
body: body
)
end

it "must return false" do
expect(subject == other).to be(false)
end
end

context "but the #method is different" do
let(:other) do
described_class.new(
remote_addr: remote_addr,
method: 'POST',
path: path,
version: version,
Expand All @@ -77,6 +118,7 @@
context "but the #path is different" do
let(:other) do
described_class.new(
remote_addr: remote_addr,
method: method,
path: '/different',
version: version,
Expand All @@ -93,6 +135,7 @@
context "but the #path is different" do
let(:other) do
described_class.new(
remote_addr: remote_addr,
method: method,
path: path,
version: '1.0',
Expand All @@ -109,6 +152,7 @@
context "but the #path is different" do
let(:other) do
described_class.new(
remote_addr: remote_addr,
method: method,
path: path,
version: version,
Expand All @@ -125,6 +169,7 @@
context "but the #path is different" do
let(:other) do
described_class.new(
remote_addr: remote_addr,
method: method,
path: path,
version: version,
Expand Down Expand Up @@ -165,11 +210,13 @@
it "must return a Hash containing #method, #path, #version, #headers, and #body" do
expect(subject.to_h).to eq(
{
method: method,
path: path,
version: version,
headers: headers,
body: body
remote_ip: remote_ip,
remote_port: remote_port,
method: method,
path: path,
version: version,
headers: headers,
body: body
}
)
end
Expand All @@ -180,6 +227,8 @@
expect(subject.to_csv).to eq(
CSV.generate_line(
[
remote_ip,
remote_port,
method,
path,
version,
Expand Down
Loading

0 comments on commit b2ace9b

Please sign in to comment.