-
Notifications
You must be signed in to change notification settings - Fork 0
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
26 changed files
with
272 additions
and
64 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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
## [Unreleased] | ||
|
||
## [0.1.0] - 2024-09-29 | ||
## [1.0.0] - 2024-09-29 | ||
|
||
- Initial release |
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
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "cloudfront_viewer_address/version" | ||
require_relative "cloudfront_viewer_address/remote_ip" | ||
|
||
require_relative "cloudfront_viewer_address/railtie" if defined?(Rails::Railtie) | ||
|
||
module ActionPack | ||
module CloudfrontViewerAddress | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "remote_ip" | ||
|
||
module ActionPack | ||
module CloudfrontViewerAddress | ||
class Railtie < ::Rails::Railtie | ||
initializer "actionpack_cloudfront_viewer_address.configure_rails_initialization" do |app| | ||
app.config.middleware.insert_after ActionDispatch::RemoteIp, ActionPack::CloudfrontViewerAddress::RemoteIp | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require "action_dispatch" | ||
|
||
module ActionPack | ||
module CloudfrontViewerAddress | ||
class RemoteIp | ||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
req = ::ActionDispatch::Request.new(env) | ||
if env["HTTP_CLOUDFRONT_VIEWER_ADDRESS"].present? | ||
# IPv4 "HTTP_CLOUDFRONT_VIEWER_ADDRESS" = "1.1.1.1:3000" | ||
# IPV6 "HTTP_CLOUDFRONT_VIEWER_ADDRESS" = "0000:0000:0000:0000:0000:0000:0000:0000:3000" | ||
req.remote_ip = req.env["HTTP_CLOUDFRONT_VIEWER_ADDRESS"].sub(/:\d+\z/, "") # remove `port(:0000)` part | ||
end | ||
app.call(req.env) | ||
end | ||
|
||
private | ||
|
||
attr_reader :app | ||
end | ||
end | ||
end |
4 changes: 2 additions & 2 deletions
4
...pack/cloudfront_viewer_address/version.rb → ...pack/cloudfront_viewer_address/version.rb
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,7 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module Actionpack | ||
module ActionPack | ||
module CloudfrontViewerAddress | ||
VERSION = "0.1.0" | ||
VERSION = "1.0.0" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "cloudfront_viewer_address/version" | ||
|
||
module Actionpack | ||
module CloudfrontViewerAddress | ||
class Error < StandardError; end | ||
# Your code goes here... | ||
end | ||
end | ||
require "action_pack/cloudfront_viewer_address" |
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
spec/action_pack/cloudfront_viewer_address/remote_ip_spec.rb
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe ActionPack::CloudfrontViewerAddress::RemoteIp do | ||
context "when CloudFront-Viewer-Address(IPv4 Address + Port) specified" do | ||
it "action_dispatch.remote_ip = IPv4 Address" do | ||
remote_ip = nil | ||
described_class | ||
.new(->(env) { remote_ip = env["action_dispatch.remote_ip"] }) | ||
.call({ "action_dispatch.remote_ip" => "0.0.0.0", "HTTP_CLOUDFRONT_VIEWER_ADDRESS" => "1.1.1.1:3000" }) | ||
expect(remote_ip).to eq "1.1.1.1" | ||
end | ||
end | ||
|
||
context "when CloudFront-Viewer-Address(IPv6 Address + Port) specified" do | ||
it "action_dispatch.remote_ip = IPv6 Address" do | ||
remote_ip = nil | ||
described_class | ||
.new(->(env) { remote_ip = env["action_dispatch.remote_ip"] }) | ||
.call({ "action_dispatch.remote_ip" => "0.0.0.0", "HTTP_CLOUDFRONT_VIEWER_ADDRESS" => "1111:1111:1111:1111:1111:1111:1111:1111:3000" }) | ||
expect(remote_ip).to eq "1111:1111:1111:1111:1111:1111:1111:1111" | ||
end | ||
end | ||
|
||
context "when CloudFront-Viewer-Address unspecified" do | ||
it "action_dispatch.remote_ip is unchanged" do | ||
remote_ip = nil | ||
described_class | ||
.new(->(env) { remote_ip = env["action_dispatch.remote_ip"] }) | ||
.call({ "action_dispatch.remote_ip" => "0.0.0.0" }) | ||
expect(remote_ip).to eq "0.0.0.0" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe ActionPack::CloudfrontViewerAddress do | ||
it "has a version number" do | ||
expect(ActionPack::CloudfrontViewerAddress::VERSION).not_to be_nil | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class RemoteIpController < ActionController::API | ||
def show | ||
render plain: request.remote_ip | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "config/environment" | ||
|
||
run Rails.application | ||
Rails.application.load_server |
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,14 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "boot" | ||
|
||
require "rails" | ||
require "action_controller/railtie" | ||
|
||
require "actionpack/cloudfront_viewer_address" | ||
|
||
module Dummy | ||
class Application < Rails::Application | ||
config.eager_load = true | ||
end | ||
end |
Oops, something went wrong.