-
Notifications
You must be signed in to change notification settings - Fork 0
Tap/raw recorder #1
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
krowd3v
wants to merge
4
commits into
master
Choose a base branch
from
tap/raw-recorder
base: master
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f7c9380
handler/forward/local: raw byte recorder
krowd3v 66559ca
listener/grpc: maxStreams + unix sockets; raw recorder: sid prefix
krowd3v c560cf1
handler/forward/local: add route + service to raw record prefix
krowd3v c8b203c
handler/forward/local: keep raw recorder minimal
krowd3v 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package local | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/hex" | ||
| "net" | ||
| "time" | ||
|
|
||
| "github.com/go-gost/core/recorder" | ||
| ) | ||
|
|
||
| type recorderConn struct { | ||
| net.Conn | ||
| recorder recorder.RecorderObject | ||
| } | ||
|
|
||
| func (c *recorderConn) record(ctx context.Context, direction byte, b []byte) { | ||
| if len(b) == 0 || c.recorder.Recorder == nil { | ||
| return | ||
| } | ||
|
|
||
| var buf bytes.Buffer | ||
| if c.recorder.Options != nil && c.recorder.Options.Direction { | ||
| buf.WriteByte(direction) | ||
| } | ||
| if c.recorder.Options != nil && c.recorder.Options.TimestampFormat != "" { | ||
| buf.WriteString(time.Now().Format(c.recorder.Options.TimestampFormat)) | ||
| } | ||
| if buf.Len() > 0 { | ||
| buf.WriteByte('\n') | ||
| } | ||
|
|
||
| if c.recorder.Options != nil && c.recorder.Options.Hexdump { | ||
| buf.WriteString(hex.Dump(b)) | ||
| } else { | ||
| buf.Write(b) | ||
| } | ||
|
|
||
| _ = c.recorder.Recorder.Record(ctx, buf.Bytes()) | ||
| } | ||
|
|
||
| func (c *recorderConn) Read(b []byte) (n int, err error) { | ||
| n, err = c.Conn.Read(b) | ||
| c.record(context.Background(), '>', b[:n]) | ||
| return | ||
| } | ||
|
|
||
| func (c *recorderConn) Write(b []byte) (int, error) { | ||
| c.record(context.Background(), '<', b) | ||
| return c.Conn.Write(b) | ||
| } |
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,81 @@ | ||
| package local | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "net" | ||
| "testing" | ||
|
|
||
| "github.com/go-gost/core/recorder" | ||
| ) | ||
|
|
||
| type captureRecorder struct { | ||
| records [][]byte | ||
| err error | ||
| } | ||
|
|
||
| func (r *captureRecorder) Record(_ context.Context, b []byte, _ ...recorder.RecordOption) error { | ||
| r.records = append(r.records, append([]byte(nil), b...)) | ||
| return r.err | ||
| } | ||
|
|
||
| func TestRecorderConnRecordsPayload(t *testing.T) { | ||
| client, upstream := net.Pipe() | ||
| defer client.Close() | ||
| defer upstream.Close() | ||
|
|
||
| rec := &captureRecorder{} | ||
| conn := &recorderConn{ | ||
| Conn: client, | ||
| recorder: recorder.RecorderObject{ | ||
| Recorder: rec, | ||
| Options: &recorder.Options{Direction: true}, | ||
| }, | ||
| } | ||
|
|
||
| go func() { | ||
| buf := make([]byte, 7) | ||
| _, _ = upstream.Read(buf) | ||
| _, _ = upstream.Write([]byte("reply")) | ||
| }() | ||
|
|
||
| if _, err := conn.Write([]byte("payload")); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| buf := make([]byte, 5) | ||
| if _, err := conn.Read(buf); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| if got := string(rec.records[0]); got != "<\npayload" { | ||
| t.Fatalf("write record = %q, want %q", got, "<\npayload") | ||
| } | ||
| if got := string(rec.records[1]); got != ">\nreply" { | ||
| t.Fatalf("read record = %q, want %q", got, ">\nreply") | ||
| } | ||
| } | ||
|
|
||
| func TestRecorderConnRecordFailureDoesNotStopWrite(t *testing.T) { | ||
| client, upstream := net.Pipe() | ||
| defer client.Close() | ||
| defer upstream.Close() | ||
|
|
||
| conn := &recorderConn{ | ||
| Conn: client, | ||
| recorder: recorder.RecorderObject{ | ||
| Recorder: &captureRecorder{err: errors.New("sink down")}, | ||
| }, | ||
| } | ||
|
|
||
| done := make(chan struct{}) | ||
| go func() { | ||
| defer close(done) | ||
| buf := make([]byte, 7) | ||
| _, _ = upstream.Read(buf) | ||
| }() | ||
|
|
||
| if _, err := conn.Write([]byte("payload")); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| <-done | ||
| } |
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
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 loop no longer stops after finding
recorder.service.handler, so the last matching entry now overwrites earlier ones. Other handlers in this repo keep first-match behavior (breakon first match), so configs with multiple handler recorder entries will silently change which sink receives service metadata after this commit. That regression is hard to detect and can redirect records to the wrong backend.Useful? React with 👍 / 👎.