improve client interface specificity and composability which eliminates much unnecessary code

This commit is contained in:
Serene Han 2016-06-13 11:05:26 -07:00
parent 02562ba750
commit 4ca0a3aa0a
6 changed files with 100 additions and 104 deletions

View file

@ -47,7 +47,7 @@ func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
type FakeDialer struct{} type FakeDialer struct{}
func (w FakeDialer) Catch() (*webRTCConn, error) { func (w FakeDialer) Catch() (Snowflake, error) {
fmt.Println("Caught a dummy snowflake.") fmt.Println("Caught a dummy snowflake.")
return &webRTCConn{}, nil return &webRTCConn{}, nil
} }
@ -66,56 +66,9 @@ func (f FakeSocksConn) Grant(addr *net.TCPAddr) error { return nil }
type FakePeers struct{ toRelease *webRTCConn } type FakePeers struct{ toRelease *webRTCConn }
func (f FakePeers) Collect() error { return nil } func (f FakePeers) Collect() error { return nil }
func (f FakePeers) Pop() *webRTCConn { return nil } func (f FakePeers) Pop() Snowflake { return nil }
func TestSnowflakeClient(t *testing.T) { func TestSnowflakeClient(t *testing.T) {
SkipConvey("WebRTC ConnectLoop", t, func() {
Convey("WebRTC ConnectLoop continues until capacity of 1.\n", func() {
snowflakes := NewPeers(1)
snowflakes.Tongue = FakeDialer{}
go ConnectLoop(snowflakes)
// <-snowflakes.maxedChan
So(snowflakes.Count(), ShouldEqual, 1)
r := <-snowflakes.snowflakeChan
So(r, ShouldNotBeNil)
So(snowflakes.Count(), ShouldEqual, 0)
})
Convey("WebRTC ConnectLoop continues until capacity of 3.\n", func() {
snowflakes := NewPeers(3)
snowflakes.Tongue = FakeDialer{}
go ConnectLoop(snowflakes)
// <-snowflakes.maxedChan
So(snowflakes.Count(), ShouldEqual, 3)
<-snowflakes.snowflakeChan
<-snowflakes.snowflakeChan
<-snowflakes.snowflakeChan
So(snowflakes.Count(), ShouldEqual, 0)
})
Convey("WebRTC ConnectLoop continues filling when Snowflakes disconnect.\n", func() {
snowflakes := NewPeers(3)
snowflakes.Tongue = FakeDialer{}
go ConnectLoop(snowflakes)
// <-snowflakes.maxedChan
So(snowflakes.Count(), ShouldEqual, 3)
r := <-snowflakes.snowflakeChan
So(snowflakes.Count(), ShouldEqual, 2)
r.Close()
// <-snowflakes.maxedChan
So(snowflakes.Count(), ShouldEqual, 3)
<-snowflakes.snowflakeChan
<-snowflakes.snowflakeChan
<-snowflakes.snowflakeChan
So(snowflakes.Count(), ShouldEqual, 0)
})
})
Convey("Peers", t, func() { Convey("Peers", t, func() {
Convey("Can construct", func() { Convey("Can construct", func() {
@ -183,6 +136,17 @@ func TestSnowflakeClient(t *testing.T) {
So(p.Count(), ShouldEqual, 2) So(p.Count(), ShouldEqual, 2)
}) })
Convey("End Closes all peers.", func() {
cnt := 5
p := NewPeers(cnt)
for i := 0; i < cnt; i++ {
p.activePeers.PushBack(&webRTCConn{})
}
So(p.Count(), ShouldEqual, cnt)
p.End()
So(p.Count(), ShouldEqual, 0)
})
}) })
Convey("Snowflake", t, func() { Convey("Snowflake", t, func() {
@ -253,6 +217,29 @@ func TestSnowflakeClient(t *testing.T) {
}) })
}) })
Convey("Dialers", t, func() {
Convey("Can construct WebRTCDialer.", func() {
broker := &BrokerChannel{Host: "test"}
d := NewWebRTCDialer(broker, nil)
So(d, ShouldNotBeNil)
So(d.BrokerChannel, ShouldNotBeNil)
So(d.BrokerChannel.Host, ShouldEqual, "test")
})
Convey("WebRTCDialer cannot Catch a snowflake with nil broker.", func() {
d := NewWebRTCDialer(nil, nil)
conn, err := d.Catch()
So(conn, ShouldBeNil)
So(err, ShouldNotBeNil)
})
SkipConvey("WebRTCDialer can Catch a snowflake.", func() {
broker := &BrokerChannel{Host: "test"}
d := NewWebRTCDialer(broker, nil)
conn, err := d.Catch()
So(conn, ShouldBeNil)
So(err, ShouldNotBeNil)
})
})
Convey("Rendezvous", t, func() { Convey("Rendezvous", t, func() {
webrtc.SetLoggingVerbosity(0) webrtc.SetLoggingVerbosity(0)
transport := &MockTransport{http.StatusOK} transport := &MockTransport{http.StatusOK}

View file

@ -1,13 +1,31 @@
// In the Client context, "Snowflake" refers to a remote browser proxy.
package main package main
import ( import (
"io"
"net" "net"
) )
type Connector interface {
Connect() error
}
type Resetter interface {
Reset()
WaitForReset()
}
// Interface for a single remote WebRTC peer.
// In the Client context, "Snowflake" refers to the remote browser proxy.
type Snowflake interface {
io.ReadWriter
Resetter
Connector
Close() error
}
// Interface for catching Snowflakes. (aka the remote dialer) // Interface for catching Snowflakes. (aka the remote dialer)
type Tongue interface { type Tongue interface {
Catch() (*webRTCConn, error) Catch() (Snowflake, error)
} }
// Interface for collecting some number of Snowflakes, for passing along // Interface for collecting some number of Snowflakes, for passing along
@ -19,7 +37,7 @@ type SnowflakeCollector interface {
Collect() error Collect() error
// Remove and return the most available Snowflake from the collection. // Remove and return the most available Snowflake from the collection.
Pop() *webRTCConn Pop() Snowflake
} }
// Interface to adapt to goptlib's SocksConn struct. // Interface to adapt to goptlib's SocksConn struct.

View file

@ -22,7 +22,7 @@ type Peers struct {
Tongue Tongue
BytesLogger BytesLogger
snowflakeChan chan *webRTCConn snowflakeChan chan Snowflake
activePeers *list.List activePeers *list.List
capacity int capacity int
} }
@ -31,7 +31,7 @@ type Peers struct {
func NewPeers(max int) *Peers { func NewPeers(max int) *Peers {
p := &Peers{capacity: max} p := &Peers{capacity: max}
// Use buffered go channel to pass snowflakes onwards to the SOCKS handler. // Use buffered go channel to pass snowflakes onwards to the SOCKS handler.
p.snowflakeChan = make(chan *webRTCConn, max) p.snowflakeChan = make(chan Snowflake, max)
p.activePeers = list.New() p.activePeers = list.New()
return p return p
} }
@ -59,7 +59,7 @@ func (p *Peers) Collect() error {
} }
// As part of |SnowflakeCollector| interface. // As part of |SnowflakeCollector| interface.
func (p *Peers) Pop() *webRTCConn { func (p *Peers) Pop() Snowflake {
// Blocks until an available snowflake appears. // Blocks until an available snowflake appears.
snowflake, ok := <-p.snowflakeChan snowflake, ok := <-p.snowflakeChan
@ -67,7 +67,7 @@ func (p *Peers) Pop() *webRTCConn {
return nil return nil
} }
// Set to use the same rate-limited traffic logger to keep consistency. // Set to use the same rate-limited traffic logger to keep consistency.
snowflake.BytesLogger = p.BytesLogger snowflake.(*webRTCConn).BytesLogger = p.BytesLogger
return snowflake return snowflake
} }

View file

@ -128,7 +128,7 @@ func NewWebRTCDialer(
} }
// Initialize a WebRTC Connection by signaling through the broker. // Initialize a WebRTC Connection by signaling through the broker.
func (w WebRTCDialer) Catch() (*webRTCConn, error) { func (w WebRTCDialer) Catch() (Snowflake, error) {
if nil == w.BrokerChannel { if nil == w.BrokerChannel {
return nil, errors.New("Cannot Dial WebRTC without a BrokerChannel.") return nil, errors.New("Cannot Dial WebRTC without a BrokerChannel.")
} }
@ -174,7 +174,7 @@ func NewCopyPasteDialer(iceServers IceServerList) *CopyPasteDialer {
} }
// Initialize a WebRTC connection via manual copy-paste. // Initialize a WebRTC connection via manual copy-paste.
func (d *CopyPasteDialer) Catch() (*webRTCConn, error) { func (d *CopyPasteDialer) Catch() (Snowflake, error) {
if nil == d.signal { if nil == d.signal {
return nil, errors.New("Cannot copy-paste dial without signal pipe.") return nil, errors.New("Cannot copy-paste dial without signal pipe.")
} }

View file

@ -26,21 +26,6 @@ const (
// ends, -1 is written. // ends, -1 is written.
var handlerChan = make(chan int) var handlerChan = make(chan int)
func copyLoop(a, b net.Conn) {
var wg sync.WaitGroup
wg.Add(2)
go func() {
io.Copy(b, a)
wg.Done()
}()
go func() {
io.Copy(a, b)
wg.Done()
}()
wg.Wait()
log.Println("copy loop ended")
}
// Maintain |SnowflakeCapacity| number of available WebRTC connections, to // Maintain |SnowflakeCapacity| number of available WebRTC connections, to
// transfer to the Tor SOCKS handler when needed. // transfer to the Tor SOCKS handler when needed.
func ConnectLoop(snowflakes SnowflakeCollector) { func ConnectLoop(snowflakes SnowflakeCollector) {
@ -104,11 +89,28 @@ func handler(socks SocksConnector, snowflakes SnowflakeCollector) error {
// When WebRTC resets, close the SOCKS connection, which induces new handler. // When WebRTC resets, close the SOCKS connection, which induces new handler.
// TODO: Double check this / fix it. // TODO: Double check this / fix it.
<-snowflake.reset snowflake.WaitForReset()
log.Println("---- Closed ---") log.Println("---- Closed ---")
return nil return nil
} }
// Exchanges bytes between two ReadWriters.
// (In this case, between a SOCKS and WebRTC connection.)
func copyLoop(a, b io.ReadWriter) {
var wg sync.WaitGroup
wg.Add(2)
go func() {
io.Copy(b, a)
wg.Done()
}()
go func() {
io.Copy(a, b)
wg.Done()
}()
wg.Wait()
log.Println("copy loop ended")
}
func main() { func main() {
webrtc.SetLoggingVerbosity(1) webrtc.SetLoggingVerbosity(1)
logFile, err := os.OpenFile("snowflake.log", logFile, err := os.OpenFile("snowflake.log",

View file

@ -3,15 +3,15 @@ package main
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt"
"github.com/keroserene/go-webrtc" "github.com/keroserene/go-webrtc"
"io" "io"
"log" "log"
"net"
"time" "time"
) )
// Remote WebRTC peer. Implements the |net.Conn| interface. // Remote WebRTC peer.
// Implements the |Snowflake| interface, which includes
// |io.ReadWriter|, |Resetter|, and |Connector|.
type webRTCConn struct { type webRTCConn struct {
config *webrtc.Configuration config *webrtc.Configuration
pc *webrtc.PeerConnection pc *webrtc.PeerConnection
@ -33,11 +33,14 @@ type webRTCConn struct {
BytesLogger BytesLogger
} }
// Read bytes from remote WebRTC.
// As part of |io.ReadWriter|
func (c *webRTCConn) Read(b []byte) (int, error) { func (c *webRTCConn) Read(b []byte) (int, error) {
return c.recvPipe.Read(b) return c.recvPipe.Read(b)
} }
// Writes bytes out to the snowflake proxy. // Writes bytes out to remote WebRTC.
// As part of |io.ReadWriter|
func (c *webRTCConn) Write(b []byte) (int, error) { func (c *webRTCConn) Write(b []byte) (int, error) {
c.BytesLogger.AddOutbound(len(b)) c.BytesLogger.AddOutbound(len(b))
if nil == c.snowflake { if nil == c.snowflake {
@ -49,6 +52,7 @@ func (c *webRTCConn) Write(b []byte) (int, error) {
return len(b), nil return len(b), nil
} }
// As part of |Snowflake|
func (c *webRTCConn) Close() error { func (c *webRTCConn) Close() error {
var err error = nil var err error = nil
log.Printf("WebRTC: Closing") log.Printf("WebRTC: Closing")
@ -67,25 +71,17 @@ func (c *webRTCConn) Close() error {
return err return err
} }
func (c *webRTCConn) LocalAddr() net.Addr { // As part of |Resetter|
return nil func (c *webRTCConn) Reset() {
go func() {
c.reset <- struct{}{}
log.Println("WebRTC resetting...")
}()
c.Close()
} }
func (c *webRTCConn) RemoteAddr() net.Addr { // As part of |Resetter|
return nil func (c *webRTCConn) WaitForReset() { <-c.reset }
}
func (c *webRTCConn) SetDeadline(t time.Time) error {
return fmt.Errorf("SetDeadline not implemented")
}
func (c *webRTCConn) SetReadDeadline(t time.Time) error {
return fmt.Errorf("SetReadDeadline not implemented")
}
func (c *webRTCConn) SetWriteDeadline(t time.Time) error {
return fmt.Errorf("SetWriteDeadline not implemented")
}
// Construct a WebRTC PeerConnection. // Construct a WebRTC PeerConnection.
func NewWebRTCConnection(config *webrtc.Configuration, func NewWebRTCConnection(config *webrtc.Configuration,
@ -108,6 +104,7 @@ func NewWebRTCConnection(config *webrtc.Configuration,
return connection return connection
} }
// As part of |Connector| interface.
func (c *webRTCConn) Connect() error { func (c *webRTCConn) Connect() error {
log.Printf("Establishing WebRTC connection #%d...", c.index) log.Printf("Establishing WebRTC connection #%d...", c.index)
// TODO: When go-webrtc is more stable, it's possible that a new // TODO: When go-webrtc is more stable, it's possible that a new
@ -287,14 +284,6 @@ func (c *webRTCConn) exchangeSDP() error {
return nil return nil
} }
func (c *webRTCConn) Reset() {
go func() {
c.reset <- struct{}{}
log.Println("WebRTC resetting...")
}()
c.Close()
}
func (c *webRTCConn) cleanup() { func (c *webRTCConn) cleanup() {
if nil != c.snowflake { if nil != c.snowflake {
log.Printf("WebRTC: closing DataChannel") log.Printf("WebRTC: closing DataChannel")