-
Notifications
You must be signed in to change notification settings - Fork 0
Correct outbound validation. #391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TreyE
wants to merge
1
commit into
trunk
Choose a base branch
from
fix_outbound_validation_issues
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Binary file not shown.
Binary file not shown.
This file contains hidden or 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 @@ | ||
require_relative "rex_port/errors" | ||
require_relative "rex_port/child_config" | ||
require_relative "rex_port/child" | ||
|
||
module RexPort | ||
end |
This file contains hidden or 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,107 @@ | ||
module RexPort | ||
class Child | ||
attr_reader :pid, :reader, :writer, :error_reader, :child_config | ||
|
||
def initialize(child_config, boot = false) | ||
@child_config = child_config | ||
boot! if boot | ||
end | ||
|
||
def request(message) | ||
tell(message) | ||
listen | ||
end | ||
|
||
def reboot! | ||
kill! | ||
boot! | ||
end | ||
|
||
def boot! | ||
@pid, @reader, @writer, @error_reader = @child_config.boot! | ||
check_status = check_process_death | ||
unless check_status.empty? | ||
raise Errors::StartupError.new(check_status.last) | ||
end | ||
end | ||
|
||
def kill! | ||
@writer.close | ||
@reader.close | ||
@error_reader.close | ||
begin | ||
Process.kill(9, @pid) | ||
Process.waitpid(@pid) | ||
rescue Errno::ECHILD, Errno::ESRCH | ||
end | ||
end | ||
|
||
protected | ||
|
||
def tell(message) | ||
check_status = check_process_death | ||
unless check_status.empty? | ||
raise Errors::DiedBeforeListeningError.new(check_status.last) | ||
end | ||
packet_size = [message.bytesize].pack("l>*") | ||
@writer.write(packet_size) | ||
@writer.write(message) | ||
@writer.flush | ||
end | ||
|
||
def listen | ||
readable = nil | ||
if @child_config.timeout > 0 | ||
readable = IO.select([@reader, @error_reader], [], [@error_reader], @child_config.timeout) | ||
else | ||
readable = IO.select([@reader, @error_reader], [], [@error_reader]) | ||
end | ||
unless readable | ||
reboot! | ||
raise Errors::ResponseTimeoutError, "process timeout!" | ||
end | ||
|
||
first_readable_array = readable.detect { |item| !item.empty? } | ||
if first_readable_array.first.fileno == @error_reader.fileno | ||
read = try_read_nonblock(first_readable_array.first) | ||
reboot! | ||
raise Errors::ResponseReadError, read | ||
end | ||
|
||
packet_response_size = @reader.read(4) | ||
if packet_response_size.nil? || packet_response_size.bytesize < 4 | ||
check_result = check_process_death | ||
reboot! | ||
raise Errors::ResponseReadError, "process crashed:\n#{check_result.last}" | ||
end | ||
read_size = packet_response_size.unpack("L>*") | ||
read_buff = @reader.read(read_size.first) | ||
check_result = check_process_death | ||
unless check_result.empty? | ||
reboot! | ||
raise Errors::ResponseReadError, "process crashed:\n#{check_result.last}" | ||
end | ||
read_buff | ||
end | ||
|
||
def check_process_death | ||
pid_status = nil | ||
begin | ||
pid_status = Process.waitpid(@pid, Process::WNOHANG) | ||
return [] unless pid_status | ||
rescue Errno::ECHILD | ||
pid_status = -1 | ||
end | ||
read_death_data = try_read_nonblock(@error_reader) | ||
[pid_status, read_death_data] | ||
end | ||
|
||
def try_read_nonblock(io) | ||
begin | ||
io.read_nonblock(2**16) | ||
rescue EOFError | ||
"" | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or 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,37 @@ | ||
module RexPort | ||
class ChildConfig | ||
attr_reader :chdir, :timeout, :command | ||
|
||
def initialize(command, working_dir, timeout = -1) | ||
@chdir = working_dir | ||
@command = command | ||
@timeout = timeout | ||
end | ||
|
||
def boot! | ||
error_reader, errors_from_validator = IO.pipe(binmode: true) | ||
reader, write_from_validator = IO.pipe(binmode: true) | ||
read_into_validator, writer = IO.pipe(binmode: true) | ||
pid = nil | ||
begin | ||
pid = Process.spawn( | ||
@command, | ||
chdir: @chdir, | ||
:in => read_into_validator, | ||
:out => write_from_validator, | ||
:err => errors_from_validator, | ||
error_reader.fileno => :close, | ||
reader.fileno => :close, | ||
writer.fileno => :close | ||
) | ||
rescue StandardError => e | ||
raise Errors::StartupError.new(e) | ||
end | ||
Process.detach(pid) | ||
write_from_validator.close | ||
read_into_validator.close | ||
errors_from_validator.close | ||
[pid, reader, writer, error_reader] | ||
end | ||
end | ||
end |
This file contains hidden or 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 @@ | ||
module RexPort | ||
module Errors | ||
class StartupError < RuntimeError; end | ||
|
||
class DiedBeforeListeningError < RuntimeError; end | ||
|
||
class ResponseTimeoutError < RuntimeError; end | ||
|
||
class ResponseReadError < RuntimeError; end | ||
end | ||
end |
This file contains hidden or 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,10 @@ | ||
Gem::Specification.new do |s| | ||
s.name = 'rex_port' | ||
s.version = '0.1.0' | ||
s.summary = "Communicate with external programs via ports." | ||
s.description = "Use the port protocol to communicate with external programs via child processes." | ||
s.authors = ["Trey Evans"] | ||
s.require_paths = ["lib"] | ||
s.email = 'lewis.r.evans@gmail.com' | ||
s.license = 'MIT' | ||
end |
This file contains hidden or 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 @@ | ||
exit(0) |
This file contains hidden or 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,12 @@ | ||
while (true) do | ||
msg_length_bytes = STDIN.read(4) | ||
msg_length = msg_length_bytes.unpack("L>*") | ||
msg = STDIN.read(msg_length.first) | ||
|
||
response = msg + " PONG!" | ||
response_size_bytes = [response.bytesize].pack("L>*") | ||
STDOUT.write(response_size_bytes) | ||
STDOUT.flush | ||
STDOUT.write(response) | ||
STDOUT.flush | ||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this pattern of having gems internally in a project has proved to be problematic in enroll, I know this is an internal gem, but shouldn't be better to have it own its repo? it will make it easier to track and to re-use