mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
shutdown webrtc on interrupt in go client
This commit is contained in:
parent
17f30d1091
commit
30e7ba79ae
1 changed files with 26 additions and 4 deletions
|
@ -50,13 +50,15 @@ type webRTCConn struct {
|
||||||
dc *data.Channel
|
dc *data.Channel
|
||||||
recvPipe *io.PipeReader
|
recvPipe *io.PipeReader
|
||||||
}
|
}
|
||||||
|
var webrtcRemote *webRTCConn
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *webRTCConn) Write(b []byte) (int, error) {
|
func (c *webRTCConn) Write(b []byte) (int, error) {
|
||||||
log.Printf("webrtc Write %d %+q", len(b), string(b))
|
// log.Printf("webrtc Write %d %+q", len(b), string(b))
|
||||||
|
log.Printf("Write %d bytes --> WebRTC", len(b))
|
||||||
c.dc.Send(b)
|
c.dc.Send(b)
|
||||||
return len(b), nil
|
return len(b), nil
|
||||||
}
|
}
|
||||||
|
@ -97,6 +99,7 @@ func dialWebRTC(config *webrtc.Configuration) (*webRTCConn, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Triggered by CreateDataChannel.
|
||||||
pc.OnNegotiationNeeded = func() {
|
pc.OnNegotiationNeeded = func() {
|
||||||
log.Println("OnNegotiationNeeded")
|
log.Println("OnNegotiationNeeded")
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -116,6 +119,7 @@ func dialWebRTC(config *webrtc.Configuration) (*webRTCConn, error) {
|
||||||
log.Printf("OnIceCandidate %s", candidate.Serialize())
|
log.Printf("OnIceCandidate %s", candidate.Serialize())
|
||||||
// Allow candidates to accumulate until OnIceComplete.
|
// Allow candidates to accumulate until OnIceComplete.
|
||||||
}
|
}
|
||||||
|
// TODO: This may soon be deprecated, consider OnIceGatheringStateChange.
|
||||||
pc.OnIceComplete = func() {
|
pc.OnIceComplete = func() {
|
||||||
log.Printf("OnIceComplete")
|
log.Printf("OnIceComplete")
|
||||||
blobChan <- pc.LocalDescription().Serialize()
|
blobChan <- pc.LocalDescription().Serialize()
|
||||||
|
@ -142,7 +146,8 @@ func dialWebRTC(config *webrtc.Configuration) (*webRTCConn, error) {
|
||||||
close(openChan)
|
close(openChan)
|
||||||
}
|
}
|
||||||
dc.OnMessage = func(msg []byte) {
|
dc.OnMessage = func(msg []byte) {
|
||||||
log.Printf("OnMessage channel %d %+q", len(msg), msg)
|
// log.Printf("OnMessage channel %d %+q", len(msg), msg)
|
||||||
|
log.Printf("OnMessage <--- %d bytes", len(msg))
|
||||||
n, err := pw.Write(msg)
|
n, err := pw.Write(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pw.CloseWithError(err)
|
pw.CloseWithError(err)
|
||||||
|
@ -158,7 +163,7 @@ func dialWebRTC(config *webrtc.Configuration) (*webRTCConn, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
case offer := <-blobChan:
|
case offer := <-blobChan:
|
||||||
log.Printf("----------------")
|
log.Printf("----------------")
|
||||||
fmt.Fprintln(logFile, offer)
|
fmt.Fprintln(logFile, "\n" + offer + "\n")
|
||||||
log.Printf("----------------")
|
log.Printf("----------------")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -187,6 +192,21 @@ func dialWebRTC(config *webrtc.Configuration) (*webRTCConn, error) {
|
||||||
return &webRTCConn{pc: pc, dc: dc, recvPipe: pr}, nil
|
return &webRTCConn{pc: pc, dc: dc, recvPipe: pr}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func endWebRTC() {
|
||||||
|
log.Printf("WebRTC: interruped")
|
||||||
|
if nil == webrtcRemote {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if nil != webrtcRemote.dc {
|
||||||
|
log.Printf("WebRTC: closing DataChannel")
|
||||||
|
webrtcRemote.dc.Close()
|
||||||
|
}
|
||||||
|
if nil != webrtcRemote.pc {
|
||||||
|
log.Printf("WebRTC: closing PeerConnection")
|
||||||
|
webrtcRemote.pc.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func handler(conn *pt.SocksConn) error {
|
func handler(conn *pt.SocksConn) error {
|
||||||
handlerChan <- 1
|
handlerChan <- 1
|
||||||
defer func() {
|
defer func() {
|
||||||
|
@ -201,6 +221,7 @@ func handler(conn *pt.SocksConn) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer remote.Close()
|
defer remote.Close()
|
||||||
|
webrtcRemote = remote
|
||||||
|
|
||||||
err = conn.Grant(&net.TCPAddr{IP: net.IPv4zero, Port: 0})
|
err = conn.Grant(&net.TCPAddr{IP: net.IPv4zero, Port: 0})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -325,7 +346,8 @@ func main() {
|
||||||
ln.Close()
|
ln.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
if sig == syscall.SIGTERM {
|
if syscall.SIGTERM == sig || syscall.SIGINT == sig {
|
||||||
|
endWebRTC()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue