client multiplexes AND recovers correctly! (close #31)

- stabilize rest of WebRTCPeer
 - use uid for the datachannel label
 - store a consistent WebRTC config in the dialer
This commit is contained in:
Serene Han 2016-06-15 18:59:55 -07:00
parent 2bf0e5457e
commit e93c38f834
8 changed files with 50 additions and 35 deletions

View file

@ -23,6 +23,7 @@ before_install:
install: install:
- go get -u github.com/smartystreets/goconvey - go get -u github.com/smartystreets/goconvey
- go get -u github.com/keroserene/go-webrtc - go get -u github.com/keroserene/go-webrtc
- go get -u github.com/dchest/uniuri
- go get -u git.torproject.org/pluggable-transports/goptlib.git - go get -u git.torproject.org/pluggable-transports/goptlib.git
- pushd proxy - pushd proxy
- npm install - npm install

View file

@ -49,7 +49,7 @@ type FakeDialer struct{}
func (w FakeDialer) Catch() (Snowflake, error) { func (w FakeDialer) Catch() (Snowflake, error) {
fmt.Println("Caught a dummy snowflake.") fmt.Println("Caught a dummy snowflake.")
return &webRTCConn{}, nil return &WebRTCPeer{}, nil
} }
type FakeSocksConn struct { type FakeSocksConn struct {
@ -63,9 +63,9 @@ func (f FakeSocksConn) Reject() error {
} }
func (f FakeSocksConn) Grant(addr *net.TCPAddr) error { return nil } func (f FakeSocksConn) Grant(addr *net.TCPAddr) error { return nil }
type FakePeers struct{ toRelease *webRTCConn } type FakePeers struct{ toRelease *WebRTCPeer }
func (f FakePeers) Collect() (Snowflake, error) { return &webRTCConn{}, nil } func (f FakePeers) Collect() (Snowflake, error) { return &WebRTCPeer{}, nil }
func (f FakePeers) Pop() Snowflake { return nil } func (f FakePeers) Pop() Snowflake { return nil }
func (f FakePeers) Melted() <-chan struct{} { return nil } func (f FakePeers) Melted() <-chan struct{} { return nil }
@ -141,7 +141,7 @@ func TestSnowflakeClient(t *testing.T) {
cnt := 5 cnt := 5
p := NewPeers(cnt) p := NewPeers(cnt)
for i := 0; i < cnt; i++ { for i := 0; i < cnt; i++ {
p.activePeers.PushBack(&webRTCConn{}) p.activePeers.PushBack(&WebRTCPeer{})
} }
So(p.Count(), ShouldEqual, cnt) So(p.Count(), ShouldEqual, cnt)
p.End() p.End()

View file

@ -70,7 +70,7 @@ func (p *Peers) Pop() Snowflake {
var ok bool var ok bool
for nil == snowflake { for nil == snowflake {
snowflake, ok = <-p.snowflakeChan snowflake, ok = <-p.snowflakeChan
conn := snowflake.(*webRTCConn) conn := snowflake.(*WebRTCPeer)
if !ok { if !ok {
return nil return nil
} }
@ -79,7 +79,7 @@ func (p *Peers) Pop() Snowflake {
} }
} }
// 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.(*webRTCConn).BytesLogger = p.BytesLogger snowflake.(*WebRTCPeer).BytesLogger = p.BytesLogger
return snowflake return snowflake
} }
@ -99,7 +99,7 @@ func (p *Peers) Count() int {
func (p *Peers) purgeClosedPeers() { func (p *Peers) purgeClosedPeers() {
for e := p.activePeers.Front(); e != nil; { for e := p.activePeers.Front(); e != nil; {
next := e.Next() next := e.Next()
conn := e.Value.(*webRTCConn) conn := e.Value.(*WebRTCPeer)
// Purge those marked for deletion. // Purge those marked for deletion.
if conn.closed { if conn.closed {
p.activePeers.Remove(e) p.activePeers.Remove(e)
@ -115,7 +115,7 @@ func (p *Peers) End() {
cnt := p.Count() cnt := p.Count()
for e := p.activePeers.Front(); e != nil; { for e := p.activePeers.Front(); e != nil; {
next := e.Next() next := e.Next()
conn := e.Value.(*webRTCConn) conn := e.Value.(*WebRTCPeer)
conn.Close() conn.Close()
p.activePeers.Remove(e) p.activePeers.Remove(e)
e = next e = next

View file

@ -115,17 +115,19 @@ func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) (
// Implements the |Tongue| interface to catch snowflakes, using BrokerChannel. // Implements the |Tongue| interface to catch snowflakes, using BrokerChannel.
type WebRTCDialer struct { type WebRTCDialer struct {
*BrokerChannel *BrokerChannel
// webrtcConfig *webrtc.Configuration webrtcConfig *webrtc.Configuration
iceServers IceServerList
} }
func NewWebRTCDialer( func NewWebRTCDialer(
broker *BrokerChannel, iceServers IceServerList) *WebRTCDialer { broker *BrokerChannel, iceServers IceServerList) *WebRTCDialer {
config := webrtc.NewConfiguration(iceServers...)
if nil == config {
log.Println("Unable to prepare WebRTC configuration.")
return nil
}
return &WebRTCDialer{ return &WebRTCDialer{
BrokerChannel: broker, BrokerChannel: broker,
iceServers: iceServers, webrtcConfig: config,
// webrtcConfig: config,
} }
} }
@ -136,8 +138,7 @@ func (w WebRTCDialer) Catch() (Snowflake, error) {
} }
// TODO: [#3] Fetch ICE server information from Broker. // TODO: [#3] Fetch ICE server information from Broker.
// TODO: [#18] Consider TURN servers here too. // TODO: [#18] Consider TURN servers here too.
config := webrtc.NewConfiguration(w.iceServers...) connection := NewWebRTCConnection(w.webrtcConfig, w.BrokerChannel)
connection := NewWebRTCConnection(config, w.BrokerChannel)
err := connection.Connect() err := connection.Connect()
return connection, err return connection, err
} }
@ -149,7 +150,7 @@ func (w WebRTCDialer) Catch() (Snowflake, error) {
type CopyPasteDialer struct { type CopyPasteDialer struct {
webrtcConfig *webrtc.Configuration webrtcConfig *webrtc.Configuration
signal *os.File signal *os.File
current *webRTCConn current *WebRTCPeer
} }
func NewCopyPasteDialer(iceServers IceServerList) *CopyPasteDialer { func NewCopyPasteDialer(iceServers IceServerList) *CopyPasteDialer {

View file

@ -75,7 +75,6 @@ func handler(socks SocksConnector, snowflakes SnowflakeCollector) error {
handlerChan <- -1 handlerChan <- -1
}() }()
// Obtain an available WebRTC remote. May block. // Obtain an available WebRTC remote. May block.
log.Println("handler: awaiting Snowflake...")
snowflake := snowflakes.Pop() snowflake := snowflakes.Pop()
if nil == snowflake { if nil == snowflake {
socks.Reject() socks.Reject()
@ -148,6 +147,10 @@ func main() {
// Otherwise, use manual copy and pasting of SDP messages. // Otherwise, use manual copy and pasting of SDP messages.
snowflakes.Tongue = NewCopyPasteDialer(iceServers) snowflakes.Tongue = NewCopyPasteDialer(iceServers)
} }
if nil == snowflakes.Tongue {
log.Fatal("Unable to prepare rendezvous method.")
return
}
// Use a real logger to periodically output how much traffic is happening. // Use a real logger to periodically output how much traffic is happening.
snowflakes.BytesLogger = &BytesSyncLogger{ snowflakes.BytesLogger = &BytesSyncLogger{
inboundChan: make(chan int, 5), outboundChan: make(chan int, 5), inboundChan: make(chan int, 5), outboundChan: make(chan int, 5),
@ -212,4 +215,5 @@ func main() {
case sig = <-sigChan: case sig = <-sigChan:
} }
} }
log.Println("snowflake is done.")
} }

View file

@ -5,6 +5,6 @@ ClientTransportPlugin snowflake exec ./client \
-url https://snowflake-reg.appspot.com/ \ -url https://snowflake-reg.appspot.com/ \
-front www.google.com \ -front www.google.com \
-ice stun:stun.l.google.com:19302 \ -ice stun:stun.l.google.com:19302 \
-max 1 -max 4
Bridge snowflake 0.0.3.0:1 Bridge snowflake 0.0.3.0:1

View file

@ -2,10 +2,11 @@ package main
import ( import (
"fmt" "fmt"
"github.com/keroserene/go-webrtc"
"log" "log"
"strings" "strings"
"time" "time"
"github.com/keroserene/go-webrtc"
) )
const ( const (

View file

@ -3,16 +3,22 @@ package main
import ( import (
"bytes" "bytes"
"errors" "errors"
"github.com/keroserene/go-webrtc"
"io" "io"
"log" "log"
"time" "time"
"github.com/dchest/uniuri"
"github.com/keroserene/go-webrtc"
) )
// Remote WebRTC peer. // Remote WebRTC peer.
// Implements the |Snowflake| interface, which includes // Implements the |Snowflake| interface, which includes
// |io.ReadWriter|, |Resetter|, and |Connector|. // |io.ReadWriter|, |Resetter|, and |Connector|.
type webRTCConn struct { //
// Handles preparation of go-webrtc PeerConnection. Only ever has
// one DataChannel.
type WebRTCPeer struct {
id string
config *webrtc.Configuration config *webrtc.Configuration
pc *webrtc.PeerConnection pc *webrtc.PeerConnection
transport SnowflakeDataChannel // Holds the WebRTC DataChannel. transport SnowflakeDataChannel // Holds the WebRTC DataChannel.
@ -33,13 +39,13 @@ type webRTCConn struct {
// Read bytes from local SOCKS. // Read bytes from local SOCKS.
// As part of |io.ReadWriter| // As part of |io.ReadWriter|
func (c *webRTCConn) Read(b []byte) (int, error) { func (c *WebRTCPeer) Read(b []byte) (int, error) {
return c.recvPipe.Read(b) return c.recvPipe.Read(b)
} }
// Writes bytes out to remote WebRTC. // Writes bytes out to remote WebRTC.
// As part of |io.ReadWriter| // As part of |io.ReadWriter|
func (c *webRTCConn) Write(b []byte) (int, error) { func (c *WebRTCPeer) Write(b []byte) (int, error) {
c.BytesLogger.AddOutbound(len(b)) c.BytesLogger.AddOutbound(len(b))
if nil == c.transport { if nil == c.transport {
log.Printf("Buffered %d bytes --> WebRTC", len(b)) log.Printf("Buffered %d bytes --> WebRTC", len(b))
@ -51,7 +57,7 @@ func (c *webRTCConn) Write(b []byte) (int, error) {
} }
// As part of |Snowflake| // As part of |Snowflake|
func (c *webRTCConn) Close() error { func (c *WebRTCPeer) Close() error {
var err error = nil var err error = nil
log.Printf("WebRTC: Closing") log.Printf("WebRTC: Closing")
c.cleanup() c.cleanup()
@ -61,7 +67,7 @@ func (c *webRTCConn) Close() error {
} }
// As part of |Resetter| // As part of |Resetter|
func (c *webRTCConn) Reset() { func (c *WebRTCPeer) Reset() {
c.Close() c.Close()
go func() { go func() {
c.reset <- struct{}{} c.reset <- struct{}{}
@ -70,12 +76,13 @@ func (c *webRTCConn) Reset() {
} }
// As part of |Resetter| // As part of |Resetter|
func (c *webRTCConn) WaitForReset() { <-c.reset } func (c *WebRTCPeer) WaitForReset() { <-c.reset }
// Construct a WebRTC PeerConnection. // Construct a WebRTC PeerConnection.
func NewWebRTCConnection(config *webrtc.Configuration, func NewWebRTCConnection(config *webrtc.Configuration,
broker *BrokerChannel) *webRTCConn { broker *BrokerChannel) *WebRTCPeer {
connection := new(webRTCConn) connection := new(WebRTCPeer)
connection.id = "snowflake-" + uniuri.New()
connection.config = config connection.config = config
connection.broker = broker connection.broker = broker
connection.offerChannel = make(chan *webrtc.SessionDescription, 1) connection.offerChannel = make(chan *webrtc.SessionDescription, 1)
@ -94,7 +101,8 @@ func NewWebRTCConnection(config *webrtc.Configuration,
} }
// As part of |Connector| interface. // As part of |Connector| interface.
func (c *webRTCConn) Connect() error { func (c *WebRTCPeer) Connect() error {
log.Println(c.id, " connecting...")
// 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
// PeerConnection won't need to be re-prepared each time. // PeerConnection won't need to be re-prepared each time.
err := c.preparePeerConnection() err := c.preparePeerConnection()
@ -113,7 +121,7 @@ func (c *webRTCConn) Connect() error {
} }
// Create and prepare callbacks on a new WebRTC PeerConnection. // Create and prepare callbacks on a new WebRTC PeerConnection.
func (c *webRTCConn) preparePeerConnection() error { func (c *WebRTCPeer) preparePeerConnection() error {
if nil != c.pc { if nil != c.pc {
c.pc.Close() c.pc.Close()
c.pc = nil c.pc = nil
@ -161,11 +169,11 @@ func (c *webRTCConn) preparePeerConnection() error {
} }
// Create a WebRTC DataChannel locally. // Create a WebRTC DataChannel locally.
func (c *webRTCConn) establishDataChannel() error { func (c *WebRTCPeer) establishDataChannel() error {
if c.transport != nil { if c.transport != nil {
panic("Unexpected datachannel already exists!") panic("Unexpected datachannel already exists!")
} }
dc, err := c.pc.CreateDataChannel("snowflake", webrtc.Init{}) dc, err := c.pc.CreateDataChannel(c.id, webrtc.Init{})
// Triggers "OnNegotiationNeeded" on the PeerConnection, which will prepare // Triggers "OnNegotiationNeeded" on the PeerConnection, which will prepare
// an SDP offer while other goroutines operating on this struct handle the // an SDP offer while other goroutines operating on this struct handle the
// signaling. Eventually fires "OnOpen". // signaling. Eventually fires "OnOpen".
@ -220,7 +228,7 @@ func (c *webRTCConn) establishDataChannel() error {
return nil return nil
} }
func (c *webRTCConn) sendOfferToBroker() { func (c *WebRTCPeer) sendOfferToBroker() {
if nil == c.broker { if nil == c.broker {
return return
} }
@ -235,7 +243,7 @@ func (c *webRTCConn) sendOfferToBroker() {
// Block until an SDP offer is available, send it to either // Block until an SDP offer is available, send it to either
// the Broker or signal pipe, then await for the SDP answer. // the Broker or signal pipe, then await for the SDP answer.
func (c *webRTCConn) exchangeSDP() error { func (c *WebRTCPeer) exchangeSDP() error {
select { select {
case offer := <-c.offerChannel: case offer := <-c.offerChannel:
// Display for copy-paste when no broker available. // Display for copy-paste when no broker available.
@ -272,7 +280,7 @@ func (c *webRTCConn) exchangeSDP() error {
} }
// Close all channels and transports // Close all channels and transports
func (c *webRTCConn) cleanup() { func (c *WebRTCPeer) cleanup() {
if nil != c.offerChannel { if nil != c.offerChannel {
close(c.offerChannel) close(c.offerChannel)
} }