Remove Snowflake interface, use *WebRTCPeer directly.

The other interfaces in client/lib/interfaces.go exist for the purpose
of running tests, but not Snowflake. Existing code would not have worked
with other types anyway, because it does unchecked .(*WebRTCPeer)
conversions.
This commit is contained in:
David Fifield 2020-04-24 14:21:08 -06:00
parent d9b076c32e
commit 76732155e7
5 changed files with 15 additions and 28 deletions

View file

@ -9,16 +9,9 @@ type Connector interface {
Connect() error
}
// Interface for a single remote WebRTC peer.
// In the Client context, "Snowflake" refers to the remote browser proxy.
type Snowflake interface {
io.ReadWriteCloser
Connector
}
// Interface for catching Snowflakes. (aka the remote dialer)
type Tongue interface {
Catch() (Snowflake, error)
Catch() (*WebRTCPeer, error)
}
// Interface for collecting some number of Snowflakes, for passing along
@ -26,10 +19,10 @@ type Tongue interface {
type SnowflakeCollector interface {
// Add a Snowflake to the collection.
// Implementation should decide how to connect and maintain the webRTCConn.
Collect() (Snowflake, error)
Collect() (*WebRTCPeer, error)
// Remove and return the most available Snowflake from the collection.
Pop() Snowflake
Pop() *WebRTCPeer
// Signal when the collector has stopped collecting.
Melted() <-chan struct{}

View file

@ -52,7 +52,7 @@ func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
type FakeDialer struct{}
func (w FakeDialer) Catch() (Snowflake, error) {
func (w FakeDialer) Catch() (*WebRTCPeer, error) {
fmt.Println("Caught a dummy snowflake.")
return &WebRTCPeer{}, nil
}
@ -70,9 +70,9 @@ func (f FakeSocksConn) Grant(addr *net.TCPAddr) error { return nil }
type FakePeers struct{ toRelease *WebRTCPeer }
func (f FakePeers) Collect() (Snowflake, error) { return &WebRTCPeer{}, nil }
func (f FakePeers) Pop() Snowflake { return nil }
func (f FakePeers) Melted() <-chan struct{} { return nil }
func (f FakePeers) Collect() (*WebRTCPeer, error) { return &WebRTCPeer{}, nil }
func (f FakePeers) Pop() *WebRTCPeer { return nil }
func (f FakePeers) Melted() <-chan struct{} { return nil }
const sampleSDP = `"v=0\r\no=- 4358805017720277108 2 IN IP4 8.8.8.8\r\ns=-\r\nt=0 0\r\na=group:BUNDLE data\r\na=msid-semantic: WMS\r\nm=application 56688 DTLS/SCTP 5000\r\nc=IN IP4 8.8.8.8\r\na=candidate:3769337065 1 udp 2122260223 8.8.8.8 56688 typ host generation 0 network-id 1 network-cost 50\r\na=candidate:2921887769 1 tcp 1518280447 8.8.8.8 35441 typ host tcptype passive generation 0 network-id 1 network-cost 50\r\na=ice-ufrag:aMAZ\r\na=ice-pwd:jcHb08Jjgrazp2dzjdrvPPvV\r\na=ice-options:trickle\r\na=fingerprint:sha-256 C8:88:EE:B9:E7:02:2E:21:37:ED:7A:D1:EB:2B:A3:15:A2:3B:5B:1C:3D:D4:D5:1F:06:CF:52:40:03:F8:DD:66\r\na=setup:actpass\r\na=mid:data\r\na=sctpmap:5000 webrtc-datachannel 1024\r\n"`

View file

@ -22,7 +22,7 @@ type Peers struct {
Tongue
BytesLogger BytesLogger
snowflakeChan chan Snowflake
snowflakeChan chan *WebRTCPeer
activePeers *list.List
capacity int
@ -33,14 +33,14 @@ type Peers struct {
func NewPeers(max int) *Peers {
p := &Peers{capacity: max}
// Use buffered go channel to pass snowflakes onwards to the SOCKS handler.
p.snowflakeChan = make(chan Snowflake, max)
p.snowflakeChan = make(chan *WebRTCPeer, max)
p.activePeers = list.New()
p.melt = make(chan struct{})
return p
}
// As part of |SnowflakeCollector| interface.
func (p *Peers) Collect() (Snowflake, error) {
func (p *Peers) Collect() (*WebRTCPeer, error) {
cnt := p.Count()
s := fmt.Sprintf("Currently at [%d/%d]", cnt, p.capacity)
if cnt >= p.capacity {
@ -64,21 +64,18 @@ func (p *Peers) Collect() (Snowflake, error) {
// Pop blocks until an available, valid snowflake appears. Returns nil after End
// has been called.
//
// Part of |SnowflakeCollector| interface.
func (p *Peers) Pop() Snowflake {
func (p *Peers) Pop() *WebRTCPeer {
for {
snowflake, ok := <-p.snowflakeChan
if !ok {
return nil
}
conn := snowflake.(*WebRTCPeer)
if conn.closed {
if snowflake.closed {
continue
}
// Set to use the same rate-limited traffic logger to keep consistency.
conn.BytesLogger = p.BytesLogger
return conn
snowflake.BytesLogger = p.BytesLogger
return snowflake
}
}

View file

@ -147,7 +147,7 @@ func NewWebRTCDialer(broker *BrokerChannel, iceServers []webrtc.ICEServer) *WebR
}
// Initialize a WebRTC Connection by signaling through the broker.
func (w WebRTCDialer) Catch() (Snowflake, error) {
func (w WebRTCDialer) Catch() (*WebRTCPeer, error) {
// TODO: [#25591] Fetch ICE server information from Broker.
// TODO: [#25596] Consider TURN servers here too.
connection := NewWebRTCPeer(w.webrtcConfig, w.BrokerChannel)

View file

@ -14,8 +14,6 @@ import (
)
// Remote WebRTC peer.
// Implements the |Snowflake| interface, which includes
// |io.ReadWriter| and |Connector|.
//
// Handles preparation of go-webrtc PeerConnection. Only ever has
// one DataChannel.
@ -87,7 +85,6 @@ func (c *WebRTCPeer) Write(b []byte) (int, error) {
return len(b), nil
}
// As part of |Snowflake|
func (c *WebRTCPeer) Close() error {
c.once.Do(func() {
c.closed = true