ooni-probe-cli/internal/engine/experiment/tlstool/internal/writer.go
Simone Basso d57c78bc71
chore: merge probe-engine into probe-cli (#201)
This is how I did it:

1. `git clone https://github.com/ooni/probe-engine internal/engine`

2. ```
(cd internal/engine && git describe --tags)
v0.23.0
```

3. `nvim go.mod` (merging `go.mod` with `internal/engine/go.mod`

4. `rm -rf internal/.git internal/engine/go.{mod,sum}`

5. `git add internal/engine`

6. `find . -type f -name \*.go -exec sed -i 's@/ooni/probe-engine@/ooni/probe-cli/v3/internal/engine@g' {} \;`

7. `go build ./...` (passes)

8. `go test -race ./...` (temporary failure on RiseupVPN)

9. `go mod tidy`

10. this commit message

Once this piece of work is done, we can build a new version of `ooniprobe` that
is using `internal/engine` directly. We need to do more work to ensure all the
other functionality in `probe-engine` (e.g. making mobile packages) are still WAI.

Part of https://github.com/ooni/probe/issues/1335
2021-02-02 12:05:47 +01:00

58 lines
1.4 KiB
Go

package internal
import (
"net"
"time"
)
// SleeperWriter is a net.Conn that optionally sleeps for the
// specified delay before posting each write.
type SleeperWriter struct {
net.Conn
Delay time.Duration
}
func (c SleeperWriter) Write(b []byte) (int, error) {
<-time.After(c.Delay)
return c.Conn.Write(b)
}
// SplitterWriter is a writer that splits every outgoing buffer
// according to the rules specified by the Splitter.
//
// Caveat
//
// The TLS ClientHello may be retransmitted if the server is
// requesting us to restart the negotiation. Therefore, it is
// not safe to just run the splitting once. Since this code
// is meant to investigate TLS blocking, that's fine.
type SplitterWriter struct {
net.Conn
Splitter func([]byte) [][]byte
}
// Write implements net.Conn.Write
func (c SplitterWriter) Write(b []byte) (int, error) {
if c.Splitter != nil {
return Writev(c.Conn, c.Splitter(b))
}
return c.Conn.Write(b)
}
// Writev writes all the vectors inside datalist using the specified
// conn. Returns either an error or the number of bytes sent. Note
// that this function skips any empty entry in datalist.
func Writev(conn net.Conn, datalist [][]byte) (int, error) {
var total int
for _, data := range datalist {
if len(data) > 0 {
count, err := conn.Write(data)
if err != nil {
return 0, err
}
total += count
}
}
return total, nil
}