mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 05:11:19 -04:00
add paddable connection
This commit is contained in:
parent
9e45772177
commit
53172a588b
1 changed files with 44 additions and 0 deletions
44
common/packetpadding/conn.go
Normal file
44
common/packetpadding/conn.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package packetpadding
|
||||
|
||||
import "io"
|
||||
|
||||
type ReadWriteCloserPreservesBoundary interface {
|
||||
io.ReadWriteCloser
|
||||
MessageBoundaryPreserved()
|
||||
}
|
||||
|
||||
type PaddableConnection interface {
|
||||
ReadWriteCloserPreservesBoundary
|
||||
}
|
||||
|
||||
func NewPaddableConnection(rwc ReadWriteCloserPreservesBoundary, padding PacketPaddingContainer) PaddableConnection {
|
||||
return &paddableConnection{
|
||||
ReadWriteCloserPreservesBoundary: rwc,
|
||||
padding: padding,
|
||||
}
|
||||
}
|
||||
|
||||
type paddableConnection struct {
|
||||
ReadWriteCloserPreservesBoundary
|
||||
padding PacketPaddingContainer
|
||||
}
|
||||
|
||||
func (c *paddableConnection) Write(p []byte) (n int, err error) {
|
||||
dataLen := len(p)
|
||||
if _, err = c.ReadWriteCloserPreservesBoundary.Write(c.padding.Pack(p, 0)); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return dataLen, nil
|
||||
}
|
||||
|
||||
func (c *paddableConnection) Read(p []byte) (n int, err error) {
|
||||
if n, err = c.ReadWriteCloserPreservesBoundary.Read(p); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
payload, _ := c.padding.Unpack(p[:n])
|
||||
if payload != nil {
|
||||
copy(p, payload)
|
||||
}
|
||||
return len(payload), nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue