mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 05:11:19 -04:00
Merge branch 'deadlock'
This commit is contained in:
commit
6399ef9d4f
1 changed files with 34 additions and 27 deletions
|
@ -26,6 +26,9 @@ const defaultBrokerURL = "https://snowflake-broker.bamsoftware.com/"
|
||||||
const defaultRelayURL = "wss://snowflake.bamsoftware.com/"
|
const defaultRelayURL = "wss://snowflake.bamsoftware.com/"
|
||||||
const defaultSTUNURL = "stun:stun.l.google.com:19302"
|
const defaultSTUNURL = "stun:stun.l.google.com:19302"
|
||||||
const pollInterval = 5 * time.Second
|
const pollInterval = 5 * time.Second
|
||||||
|
//amount of time after sending an SDP answer before the proxy assumes the
|
||||||
|
//client is not going to connect
|
||||||
|
const dataChannelTimeout = time.Minute
|
||||||
|
|
||||||
var brokerURL *url.URL
|
var brokerURL *url.URL
|
||||||
var relayURL string
|
var relayURL string
|
||||||
|
@ -259,8 +262,6 @@ func datachannelHandler(conn *webRTCConn, remoteAddr net.Addr) {
|
||||||
// Installs an OnDataChannel callback that creates a webRTCConn and passes it to
|
// Installs an OnDataChannel callback that creates a webRTCConn and passes it to
|
||||||
// datachannelHandler.
|
// datachannelHandler.
|
||||||
func makePeerConnectionFromOffer(sdp *webrtc.SessionDescription, config *webrtc.Configuration) (*webrtc.PeerConnection, error) {
|
func makePeerConnectionFromOffer(sdp *webrtc.SessionDescription, config *webrtc.Configuration) (*webrtc.PeerConnection, error) {
|
||||||
errChan := make(chan error)
|
|
||||||
answerChan := make(chan struct{})
|
|
||||||
|
|
||||||
pc, err := webrtc.NewPeerConnection(config)
|
pc, err := webrtc.NewPeerConnection(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -269,9 +270,6 @@ func makePeerConnectionFromOffer(sdp *webrtc.SessionDescription, config *webrtc.
|
||||||
pc.OnNegotiationNeeded = func() {
|
pc.OnNegotiationNeeded = func() {
|
||||||
panic("OnNegotiationNeeded")
|
panic("OnNegotiationNeeded")
|
||||||
}
|
}
|
||||||
pc.OnIceComplete = func() {
|
|
||||||
answerChan <- struct{}{}
|
|
||||||
}
|
|
||||||
pc.OnDataChannel = func(dc *webrtc.DataChannel) {
|
pc.OnDataChannel = func(dc *webrtc.DataChannel) {
|
||||||
log.Println("OnDataChannel")
|
log.Println("OnDataChannel")
|
||||||
|
|
||||||
|
@ -310,31 +308,40 @@ func makePeerConnectionFromOffer(sdp *webrtc.SessionDescription, config *webrtc.
|
||||||
}
|
}
|
||||||
log.Println("sdp offer successfully received.")
|
log.Println("sdp offer successfully received.")
|
||||||
|
|
||||||
go func() {
|
log.Println("Generating answer...")
|
||||||
log.Println("Generating answer...")
|
answer, err := pc.CreateAnswer()
|
||||||
answer, err := pc.CreateAnswer() // blocking
|
// blocks on ICE gathering. we need to add a timeout if needed
|
||||||
if err != nil {
|
// not putting this in a separate go routine, because we need
|
||||||
errChan <- err
|
// SetLocalDescription(answer) to be called before sendAnswer
|
||||||
return
|
if err != nil {
|
||||||
}
|
|
||||||
err = pc.SetLocalDescription(answer)
|
|
||||||
if err != nil {
|
|
||||||
errChan <- err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Wait until answer is ready.
|
|
||||||
select {
|
|
||||||
case err = <-errChan:
|
|
||||||
pc.Destroy()
|
pc.Destroy()
|
||||||
return nil, err
|
return nil, err
|
||||||
case _, ok := <-answerChan:
|
|
||||||
if !ok {
|
|
||||||
pc.Destroy()
|
|
||||||
return nil, fmt.Errorf("Failed gathering ICE candidates.")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if answer == nil {
|
||||||
|
pc.Destroy()
|
||||||
|
return nil, fmt.Errorf("Failed gathering ICE candidates.")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = pc.SetLocalDescription(answer)
|
||||||
|
if err != nil {
|
||||||
|
pc.Destroy()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set a timeout on peerconnection. If the connection state has not
|
||||||
|
// advanced to PeerConnectionStateConnected in this time,
|
||||||
|
// destroy the peer connection and return the token.
|
||||||
|
go func() {
|
||||||
|
<-time.After(dataChannelTimeout)
|
||||||
|
if pc.ConnectionState() != webrtc.PeerConnectionStateConnected {
|
||||||
|
log.Println("Timed out waiting for client to open data cannel.")
|
||||||
|
pc.Destroy()
|
||||||
|
retToken()
|
||||||
|
}
|
||||||
|
|
||||||
|
}()
|
||||||
|
|
||||||
return pc, nil
|
return pc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue