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