-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(tlstool): add 8:4:rest segmenter
See #622
- Loading branch information
1 parent
24aa4b0
commit 6865ead
Showing
7 changed files
with
433 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package segmenter | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net" | ||
"time" | ||
) | ||
|
||
type FakeDialer struct { | ||
Conn net.Conn | ||
Err error | ||
} | ||
|
||
func (d FakeDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) { | ||
time.Sleep(10 * time.Microsecond) | ||
return d.Conn, d.Err | ||
} | ||
|
||
type FakeConn struct { | ||
ReadError error | ||
ReadData []byte | ||
SetDeadlineError error | ||
SetReadDeadlineError error | ||
SetWriteDeadlineError error | ||
WriteData [][]byte | ||
WriteError error | ||
} | ||
|
||
func (c *FakeConn) Read(b []byte) (int, error) { | ||
if len(c.ReadData) > 0 { | ||
n := copy(b, c.ReadData) | ||
c.ReadData = c.ReadData[n:] | ||
return n, nil | ||
} | ||
if c.ReadError != nil { | ||
return 0, c.ReadError | ||
} | ||
return 0, io.EOF | ||
} | ||
|
||
func (c *FakeConn) Write(b []byte) (n int, err error) { | ||
if c.WriteError != nil { | ||
return 0, c.WriteError | ||
} | ||
c.WriteData = append(c.WriteData, b) | ||
n = len(b) | ||
return | ||
} | ||
|
||
func (*FakeConn) Close() (err error) { | ||
return | ||
} | ||
|
||
func (*FakeConn) LocalAddr() net.Addr { | ||
return &net.TCPAddr{} | ||
} | ||
|
||
func (*FakeConn) RemoteAddr() net.Addr { | ||
return &net.TCPAddr{} | ||
} | ||
|
||
func (c *FakeConn) SetDeadline(t time.Time) (err error) { | ||
return c.SetDeadlineError | ||
} | ||
|
||
func (c *FakeConn) SetReadDeadline(t time.Time) (err error) { | ||
return c.SetReadDeadlineError | ||
} | ||
|
||
func (c *FakeConn) SetWriteDeadline(t time.Time) (err error) { | ||
return c.SetWriteDeadlineError | ||
} |
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,70 @@ | ||
// Package segmenter contains code that splits TCP data in three | ||
// segments as described by Kevin Boch in https://youtu.be/ksojSRFLbBM?t=1140. | ||
package segmenter | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"time" | ||
|
||
"github.com/ooni/probe-engine/netx" | ||
) | ||
|
||
// Dialer creates connections where we split TCP data in three | ||
// segments as mentioned above. We optionally also delay writing | ||
// each segment by a specific number of milliseconds. | ||
type Dialer struct { | ||
netx.Dialer | ||
Delay int64 | ||
} | ||
|
||
// DialContext implements netx.Dialer.DialContext. | ||
func (d Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) { | ||
conn, err := d.Dialer.DialContext(ctx, network, address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return Conn{Conn: conn, Delay: d.Delay}, nil | ||
} | ||
|
||
// Conn is the net.Conn generated by patternsplitter.Dialer. | ||
// | ||
// Caveat | ||
// | ||
// The connection will keep splitting segments forever. This behaviour | ||
// is fine as long as we're just checking whether the TLS handshake works. | ||
type Conn struct { | ||
net.Conn | ||
BeforeSecondWrite func() // for testing | ||
BeforeThirdWrite func() // for testing | ||
Delay int64 | ||
} | ||
|
||
// Write implements net.Conn.Write. | ||
func (c Conn) Write(b []byte) (int, error) { | ||
const ( | ||
first = 8 | ||
second = 12 | ||
) | ||
if len(b) > second { | ||
if _, err := c.Conn.Write(b[:first]); err != nil { | ||
return 0, err | ||
} | ||
<-time.After(time.Duration(c.Delay) * time.Millisecond) | ||
if c.BeforeSecondWrite != nil { | ||
c.BeforeSecondWrite() | ||
} | ||
if _, err := c.Conn.Write(b[first:second]); err != nil { | ||
return 0, err | ||
} | ||
<-time.After(time.Duration(c.Delay) * time.Millisecond) | ||
if c.BeforeThirdWrite != nil { | ||
c.BeforeThirdWrite() | ||
} | ||
if _, err := c.Conn.Write(b[second:]); err != nil { | ||
return 0, err | ||
} | ||
return len(b), nil | ||
} | ||
return c.Conn.Write(b) | ||
} |
Oops, something went wrong.