mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
webRTCConn has better seam with BytesLogger interface
This commit is contained in:
parent
6b8568cc6c
commit
b2c9fcac5e
4 changed files with 39 additions and 20 deletions
|
@ -109,11 +109,7 @@ func TestSnowflakeClient(t *testing.T) {
|
|||
})
|
||||
|
||||
Convey("WebRTC Connection", func() {
|
||||
c := new(webRTCConn)
|
||||
c.BytesInfo = &BytesInfo{
|
||||
inboundChan: make(chan int), outboundChan: make(chan int),
|
||||
inbound: 0, outbound: 0, inEvents: 0, outEvents: 0,
|
||||
}
|
||||
c := NewWebRTCConnection(nil, nil)
|
||||
So(c.buffer.Bytes(), ShouldEqual, nil)
|
||||
|
||||
Convey("Can construct a WebRTCConn", func() {
|
||||
|
|
|
@ -66,6 +66,7 @@ type SnowflakeChannel interface {
|
|||
// updated once multiplexed transport on a single circuit is available.
|
||||
type Peers struct {
|
||||
Tongue
|
||||
BytesLogger
|
||||
|
||||
snowflakeChan chan *webRTCConn
|
||||
current *webRTCConn
|
||||
|
@ -88,6 +89,7 @@ func (p *Peers) FindSnowflake() (*webRTCConn, error) {
|
|||
return nil, errors.New(s)
|
||||
}
|
||||
connection, err := p.Catch()
|
||||
connection.BytesLogger = p.BytesLogger
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -96,7 +98,11 @@ func (p *Peers) FindSnowflake() (*webRTCConn, error) {
|
|||
|
||||
// TODO: Needs fixing.
|
||||
func (p *Peers) Count() int {
|
||||
return len(p.snowflakeChan)
|
||||
count := 0
|
||||
if p.current != nil {
|
||||
count = 1
|
||||
}
|
||||
return count + len(p.snowflakeChan)
|
||||
}
|
||||
|
||||
// Close all remote peers.
|
||||
|
@ -257,6 +263,12 @@ func main() {
|
|||
remotes := NewPeers(SnowflakeCapacity)
|
||||
broker := NewBrokerChannel(brokerURL, frontDomain, CreateBrokerTransport())
|
||||
|
||||
remotes.BytesLogger = &BytesSyncLogger{
|
||||
inboundChan: make(chan int, 5), outboundChan: make(chan int, 5),
|
||||
inbound: 0, outbound: 0, inEvents: 0, outEvents: 0,
|
||||
}
|
||||
go remotes.BytesLogger.Log()
|
||||
|
||||
remotes.Tongue = WebRTCDialer{broker}
|
||||
go ConnectLoop(remotes)
|
||||
|
||||
|
|
|
@ -29,7 +29,22 @@ func (i *IceServerList) Set(s string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
type BytesInfo struct {
|
||||
type BytesLogger interface {
|
||||
Log()
|
||||
AddOutbound(int)
|
||||
AddInbound(int)
|
||||
}
|
||||
|
||||
// Default BytesLogger does nothing.
|
||||
type BytesNullLogger struct{}
|
||||
|
||||
func (b BytesNullLogger) Log() {}
|
||||
func (b BytesNullLogger) AddOutbound(amount int) {}
|
||||
func (b BytesNullLogger) AddInbound(amount int) {}
|
||||
|
||||
// BytesSyncLogger uses channels to safely log from multiple sources with output
|
||||
// occuring at reasonable intervals.
|
||||
type BytesSyncLogger struct {
|
||||
outboundChan chan int
|
||||
inboundChan chan int
|
||||
outbound int
|
||||
|
@ -39,7 +54,7 @@ type BytesInfo struct {
|
|||
isLogging bool
|
||||
}
|
||||
|
||||
func (b *BytesInfo) Log() {
|
||||
func (b *BytesSyncLogger) Log() {
|
||||
b.isLogging = true
|
||||
var amount int
|
||||
output := func() {
|
||||
|
@ -76,14 +91,14 @@ func (b *BytesInfo) Log() {
|
|||
}
|
||||
}
|
||||
|
||||
func (b *BytesInfo) AddOutbound(amount int) {
|
||||
func (b *BytesSyncLogger) AddOutbound(amount int) {
|
||||
if !b.isLogging {
|
||||
return
|
||||
}
|
||||
b.outboundChan <- amount
|
||||
}
|
||||
|
||||
func (b *BytesInfo) AddInbound(amount int) {
|
||||
func (b *BytesSyncLogger) AddInbound(amount int) {
|
||||
if !b.isLogging {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -29,7 +29,8 @@ type webRTCConn struct {
|
|||
|
||||
index int
|
||||
closed bool
|
||||
*BytesInfo
|
||||
|
||||
BytesLogger
|
||||
}
|
||||
|
||||
func (c *webRTCConn) Read(b []byte) (int, error) {
|
||||
|
@ -38,7 +39,7 @@ func (c *webRTCConn) Read(b []byte) (int, error) {
|
|||
|
||||
// Writes bytes out to the snowflake proxy.
|
||||
func (c *webRTCConn) Write(b []byte) (int, error) {
|
||||
c.BytesInfo.AddOutbound(len(b))
|
||||
c.BytesLogger.AddOutbound(len(b))
|
||||
if nil == c.snowflake {
|
||||
log.Printf("Buffered %d bytes --> WebRTC", len(b))
|
||||
c.buffer.Write(b)
|
||||
|
@ -99,13 +100,8 @@ func NewWebRTCConnection(config *webrtc.Configuration,
|
|||
connection.errorChannel = make(chan error, 1)
|
||||
connection.reset = make(chan struct{}, 1)
|
||||
|
||||
// TODO: Separate out.
|
||||
// Log every few seconds.
|
||||
connection.BytesInfo = &BytesInfo{
|
||||
inboundChan: make(chan int, 5), outboundChan: make(chan int, 5),
|
||||
inbound: 0, outbound: 0, inEvents: 0, outEvents: 0,
|
||||
}
|
||||
go connection.BytesInfo.Log()
|
||||
// Override with something that's not NullLogger to have real logging.
|
||||
connection.BytesLogger = &BytesNullLogger{}
|
||||
|
||||
// Pipes remain the same even when DataChannel gets switched.
|
||||
connection.recvPipe, connection.writePipe = io.Pipe()
|
||||
|
@ -224,7 +220,7 @@ func (c *webRTCConn) establishDataChannel() error {
|
|||
if len(msg) <= 0 {
|
||||
log.Println("0 length message---")
|
||||
}
|
||||
c.BytesInfo.AddInbound(len(msg))
|
||||
c.BytesLogger.AddInbound(len(msg))
|
||||
n, err := c.writePipe.Write(msg)
|
||||
if err != nil {
|
||||
// TODO: Maybe shouldn't actually close.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue