mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 05:11:19 -04:00
Make preparePeerConnection a standalone function.
This commit is contained in:
parent
81d14ad33a
commit
8295c87fbe
1 changed files with 14 additions and 22 deletions
|
@ -19,7 +19,6 @@ import (
|
||||||
// one DataChannel.
|
// one DataChannel.
|
||||||
type WebRTCPeer struct {
|
type WebRTCPeer struct {
|
||||||
id string
|
id string
|
||||||
config *webrtc.Configuration
|
|
||||||
pc *webrtc.PeerConnection
|
pc *webrtc.PeerConnection
|
||||||
transport *webrtc.DataChannel
|
transport *webrtc.DataChannel
|
||||||
broker *BrokerChannel
|
broker *BrokerChannel
|
||||||
|
@ -48,7 +47,6 @@ func NewWebRTCPeer(config *webrtc.Configuration,
|
||||||
}
|
}
|
||||||
connection.id = "snowflake-" + hex.EncodeToString(buf[:])
|
connection.id = "snowflake-" + hex.EncodeToString(buf[:])
|
||||||
}
|
}
|
||||||
connection.config = config
|
|
||||||
connection.broker = broker
|
connection.broker = broker
|
||||||
|
|
||||||
// Override with something that's not NullLogger to have real logging.
|
// Override with something that's not NullLogger to have real logging.
|
||||||
|
@ -57,7 +55,7 @@ func NewWebRTCPeer(config *webrtc.Configuration,
|
||||||
// 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()
|
||||||
|
|
||||||
err := connection.connect()
|
err := connection.connect(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
connection.Close()
|
connection.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -115,11 +113,12 @@ func (c *WebRTCPeer) checkForStaleness() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WebRTCPeer) connect() error {
|
func (c *WebRTCPeer) connect(config *webrtc.Configuration) error {
|
||||||
log.Println(c.id, " connecting...")
|
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()
|
var err error
|
||||||
|
c.pc, err = preparePeerConnection(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -136,19 +135,13 @@ func (c *WebRTCPeer) connect() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create and prepare callbacks on a new WebRTC PeerConnection.
|
// preparePeerConnection creates a new WebRTC PeerConnection and returns it
|
||||||
func (c *WebRTCPeer) preparePeerConnection() error {
|
// after ICE candidate gathering is complete..
|
||||||
if nil != c.pc {
|
func preparePeerConnection(config *webrtc.Configuration) (*webrtc.PeerConnection, error) {
|
||||||
if err := c.pc.Close(); err != nil {
|
pc, err := webrtc.NewPeerConnection(*config)
|
||||||
log.Printf("c.pc.Close returned error: %v", err)
|
|
||||||
}
|
|
||||||
c.pc = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
pc, err := webrtc.NewPeerConnection(*c.config)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("NewPeerConnection ERROR: %s", err)
|
log.Printf("NewPeerConnection ERROR: %s", err)
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Prepare PeerConnection callbacks.
|
// Prepare PeerConnection callbacks.
|
||||||
offerChannel := make(chan struct{})
|
offerChannel := make(chan struct{})
|
||||||
|
@ -161,27 +154,26 @@ func (c *WebRTCPeer) preparePeerConnection() error {
|
||||||
log.Printf("WebRTC: Got ICE candidate: %s", candidate.String())
|
log.Printf("WebRTC: Got ICE candidate: %s", candidate.String())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
c.pc = pc
|
|
||||||
|
|
||||||
offer, err := pc.CreateOffer(nil)
|
offer, err := pc.CreateOffer(nil)
|
||||||
// TODO: Potentially timeout and retry if ICE isn't working.
|
// TODO: Potentially timeout and retry if ICE isn't working.
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to prepare offer", err)
|
log.Println("Failed to prepare offer", err)
|
||||||
c.Close()
|
pc.Close()
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
log.Println("WebRTC: Created offer")
|
log.Println("WebRTC: Created offer")
|
||||||
err = pc.SetLocalDescription(offer)
|
err = pc.SetLocalDescription(offer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to prepare offer", err)
|
log.Println("Failed to prepare offer", err)
|
||||||
c.Close()
|
pc.Close()
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
log.Println("WebRTC: Set local description")
|
log.Println("WebRTC: Set local description")
|
||||||
|
|
||||||
<-offerChannel // Wait for ICE candidate gathering to complete.
|
<-offerChannel // Wait for ICE candidate gathering to complete.
|
||||||
log.Println("WebRTC: PeerConnection created.")
|
log.Println("WebRTC: PeerConnection created.")
|
||||||
return nil
|
return pc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a WebRTC DataChannel locally.
|
// Create a WebRTC DataChannel locally.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue