forked from taboola/goreplay
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Middleware for custom request rewrite logic #162
Merged
Merged
Changes from 7 commits
Commits
Show all changes
69 commits
Select commit
Hold shift + click to select a range
208faec
Initial support for input modifiers
buger dcb8a1d
Merge branch 'master' into input-modifier
buger 4f03f81
Working modifier implementation with examples
buger 674ffc6
Start adding tests for traffic modifier
buger 98f941b
Add support for complex commands
buger 3e86456
Add more debug
buger 8db14e3
Merge branch 'master' into input-modifier
buger e64aac9
Rename input modifier to middleware
buger f812120
Support only 1 middleware and rewrite tests
buger 5e6f7e0
First middleware tests! :dance:
buger 5cc4228
Merge branch 'master' into input-modifier
buger d62eccb
Make raw message expiration configurable to improve tests time
buger a9be181
Merge branch 'master' into input-modifier
buger 3585419
Fix tests
buger 9ab8b3c
Add header to request and response
buger 5667cd3
Merge branch 'master' into input-modifier
buger 5098e38
Fix tests, now properly receive responses
buger 0009555
Apply fmt
buger 8046468
Fix tests
buger 2990fab
Track original responses
buger 491541c
Response object should be separate message
buger bb319b5
Add request ID and token modifier middleware
buger 70e7d8d
Reduce delay of raw input
buger 56f0a3f
Remove too verbose output
buger 4f2341b
Remove more debug
buger 41c1421
Simulate some real-life server by adding small response delay
buger 1a36374
Add some comments
buger a18079f
Improving docs
buger ddd5190
Improve docs
buger 879ed48
Doc changes
buger 0d44572
Some fixes
buger d109110
Ignore all release files
buger 211be0c
Add request time and round-trip response time
buger 69424bf
Fix hound
buger d96e339
Handle HTTP error codes
buger ea94dd0
fmt changes
buger 3e763c4
Unify all input/output plugins
buger a971d8a
Change file format to be text based
buger 7790b33
Refactor protocol scanner
buger 8da4e54
Tcp communication should use same protocol as file based
buger ae35f88
Remove magic numbers
buger d6c776f
Verbose output for travis
buger b53fba0
Fix formatting issues
buger 7cff781
Remove verbose flag
buger c4037e0
Increase travis timeout time
buger 78456d6
More timeout
buger a6e29c6
Close connection
buger 5d2f6ec
Fixes
buger e54f77f
Try to return all responses
buger 6cb4c26
Some debugging and race fixes
buger 9550b31
Fix plugin registration
buger 33cdcd4
Use httptest package and properly close servers
buger 6974a6e
Ensure that raw input gets closed
buger 3c45595
More race fixes
buger 81d3508
Enable race testing for travis
buger 76d5e22
Enable packet debugging
buger 9309fb8
Reduce debugging
buger 6533b50
Travis should use 1.5
buger 16d7f23
New fixes
buger cbc27bd
More debugging
buger 02bf7a8
Rollback to 1.4.2 in travis
buger d5a1eb0
Increase input raw timeout
buger f3ea30c
Add example of java echo middleware
buger f0b96af
Add protection for malformed requests
buger 929e027
small formatting change
buger 3c81723
Increase timeout for large payload
buger fe91a20
fix race
buger 43262f3
Increase timeouts
buger abde6ec
less verbose output
buger 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 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,16 @@ | ||
#!/usr/bin/env ruby | ||
# encoding: utf-8 | ||
while data = STDIN.gets | ||
next unless data | ||
data = data.chomp | ||
|
||
decoded = [data].pack("H*") | ||
encoded = decoded.unpack("H*").first | ||
|
||
STDOUT.puts encoded | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra blank line detected. |
||
STDERR.puts "[DEBUG] Original data: #{data}" | ||
STDERR.puts "[DEBUG] Decoded request: #{decoded}" | ||
STDERR.puts "[DEBUG] Encoded data: #{encoded}" | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Final newline missing. |
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,10 @@ | ||
#!/usr/bin/env bash | ||
while read line; do | ||
decoded=$(echo "$line" | xxd -r -p) | ||
encoded=$(echo "$decoded" | xxd -p | tr -d "\\n") | ||
echo "$encoded" | ||
|
||
>&2 echo "[DEBUG] Original data: $line" | ||
>&2 echo "[DEBUG] Decoded request: $decoded" | ||
>&2 echo "[DEBUG] Encoded data: $encoded" | ||
done; |
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
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,98 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"encoding/hex" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
type TrafficModifier struct { | ||
plugin interface{} | ||
command string | ||
|
||
data chan []byte | ||
|
||
Stdin io.Writer | ||
Stdout io.Reader | ||
} | ||
|
||
func NewTrafficModifier(plugin interface{}, command string) io.Reader { | ||
m := new(TrafficModifier) | ||
m.plugin = plugin | ||
m.command = command | ||
m.data = make(chan []byte) | ||
|
||
commands := strings.Split(command, " ") | ||
cmd := exec.Command(commands[0], commands[1:]...) | ||
|
||
m.Stdout, _ = cmd.StdoutPipe() | ||
m.Stdin, _ = cmd.StdinPipe() | ||
cmd.Stderr = os.Stderr | ||
|
||
go m.copy(m.Stdin, m.plugin.(io.Reader)) | ||
go m.read(m.Stdout) | ||
|
||
go func() { | ||
err := cmd.Start() | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
}() | ||
|
||
defer cmd.Wait() | ||
|
||
return m | ||
} | ||
|
||
func (m *TrafficModifier) copy(to io.Writer, from io.Reader) { | ||
buf := make([]byte, 5*1024*1024) | ||
dst := make([]byte, len(buf)*2) | ||
|
||
for { | ||
nr, _ := from.Read(buf) | ||
if nr > 0 && len(buf) > nr { | ||
hex.Encode(dst, buf[0:nr]) | ||
to.Write(dst[0 : nr*2]) | ||
to.Write([]byte("\r\n")) | ||
} | ||
} | ||
} | ||
|
||
func (m *TrafficModifier) read(from io.Reader) { | ||
buf := make([]byte, 5*1024*1024) | ||
|
||
scanner := bufio.NewScanner(from) | ||
|
||
for scanner.Scan() { | ||
bytes := scanner.Bytes() | ||
hex.Decode(buf, bytes) | ||
|
||
Debug("Received:", buf[0:len(bytes)/2]) | ||
|
||
m.data <- buf[0 : len(bytes)/2] | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
fmt.Fprintln(os.Stderr, "Traffic modifier command failed:", err) | ||
} | ||
|
||
return | ||
} | ||
|
||
func (m *TrafficModifier) Read(data []byte) (int, error) { | ||
Debug("Trying to read channel!") | ||
buf := <-m.data | ||
copy(data, buf) | ||
|
||
return len(buf), nil | ||
} | ||
|
||
func (m *TrafficModifier) String() string { | ||
return fmt.Sprintf("Modifying traffic for %s using '%s' command", m.plugin, m.command) | ||
} |
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.
Use 2 (not 4) spaces for indentation.