move endWebRTC() mostly into webRTCConn.Close()

This commit is contained in:
Serene Han 2016-02-26 14:35:15 -08:00
parent f3e0abe0e1
commit 5edac195f6
2 changed files with 42 additions and 21 deletions

View file

@ -46,6 +46,8 @@ func copyLoop(a, b net.Conn) {
wg.Done() wg.Done()
}() }()
wg.Wait() wg.Wait()
// a.Close()
// b.Close()
log.Println("copy loop ended") log.Println("copy loop ended")
} }
@ -79,16 +81,8 @@ func endWebRTC() {
if nil == webrtcRemote { if nil == webrtcRemote {
return return
} }
if nil != webrtcRemote.snowflake { webrtcRemote.Close()
s := webrtcRemote.snowflake webrtcRemote = nil
webrtcRemote.snowflake = nil
log.Printf("WebRTC: closing DataChannel")
s.Close()
}
if nil != webrtcRemote.pc {
log.Printf("WebRTC: closing PeerConnection")
webrtcRemote.pc.Close()
}
} }
// Establish a WebRTC channel for SOCKS connections. // Establish a WebRTC channel for SOCKS connections.
@ -116,6 +110,7 @@ func handler(conn *pt.SocksConn) error {
// TODO: Make SOCKS acceptance more independent from WebRTC so they can // TODO: Make SOCKS acceptance more independent from WebRTC so they can
// be more easily interchanged. // be more easily interchanged.
copyLoop(conn, remote) copyLoop(conn, remote)
// <-remote.endChannel
log.Println("----END---") log.Println("----END---")
return nil return nil
} }
@ -123,8 +118,9 @@ func handler(conn *pt.SocksConn) error {
func acceptLoop(ln *pt.SocksListener) error { func acceptLoop(ln *pt.SocksListener) error {
defer ln.Close() defer ln.Close()
for { for {
log.Println("SOCKS listening...") log.Println("SOCKS listening...", ln)
conn, err := ln.AcceptSocks() conn, err := ln.AcceptSocks()
log.Println("accepting", conn, err)
if err != nil { if err != nil {
if e, ok := err.(net.Error); ok && e.Temporary() { if e, ok := err.(net.Error); ok && e.Temporary() {
continue continue

View file

@ -19,6 +19,7 @@ type webRTCConn struct {
offerChannel chan *webrtc.SessionDescription offerChannel chan *webrtc.SessionDescription
answerChannel chan *webrtc.SessionDescription answerChannel chan *webrtc.SessionDescription
errorChannel chan error errorChannel chan error
endChannel chan struct{}
recvPipe *io.PipeReader recvPipe *io.PipeReader
writePipe *io.PipeWriter writePipe *io.PipeWriter
buffer bytes.Buffer buffer bytes.Buffer
@ -45,8 +46,25 @@ func (c *webRTCConn) Write(b []byte) (int, error) {
} }
func (c *webRTCConn) Close() error { func (c *webRTCConn) Close() error {
// Data channel closed implicitly? var err error = nil
return c.pc.Close() log.Printf("WebRTC: Closing")
if nil != c.snowflake {
s := c.snowflake
c.snowflake = nil
log.Printf("WebRTC: closing DataChannel")
s.Close()
}
if nil != c.pc {
log.Printf("WebRTC: closing PeerConnection")
err = c.pc.Close()
c.pc = nil
}
close(c.offerChannel)
close(c.answerChannel)
close(c.errorChannel)
// c.writePipe.Close()
// c.recvPipe.Close()
return err
} }
func (c *webRTCConn) LocalAddr() net.Addr { func (c *webRTCConn) LocalAddr() net.Addr {
@ -77,6 +95,7 @@ func NewWebRTCConnection(config *webrtc.Configuration,
connection.offerChannel = make(chan *webrtc.SessionDescription, 1) connection.offerChannel = make(chan *webrtc.SessionDescription, 1)
connection.answerChannel = make(chan *webrtc.SessionDescription, 1) connection.answerChannel = make(chan *webrtc.SessionDescription, 1)
connection.errorChannel = make(chan error, 1) connection.errorChannel = make(chan error, 1)
connection.endChannel = make(chan struct{}, 1)
connection.reset = make(chan struct{}, 1) connection.reset = make(chan struct{}, 1)
// Log every few seconds. // Log every few seconds.
@ -100,13 +119,13 @@ func (c *webRTCConn) ConnectLoop() {
// PeerConnection won't need to be re-prepared each time. // PeerConnection won't need to be re-prepared each time.
c.preparePeerConnection() c.preparePeerConnection()
err := c.establishDataChannel() err := c.establishDataChannel()
if err == nil { if err != nil {
log.Println("WebRTC: Could not establish DataChannel.")
} else {
c.sendOffer() c.sendOffer()
c.receiveAnswer() c.receiveAnswer()
<-c.reset <-c.reset
log.Println(" --- snowflake connection reset ---") log.Println(" --- snowflake connection reset ---")
} else {
log.Println("WebRTC: Could not establish DataChannel.")
} }
} }
} }
@ -172,7 +191,7 @@ func (c *webRTCConn) establishDataChannel() error {
if nil != c.snowflake { if nil != c.snowflake {
panic("PeerConnection snowflake already exists.") panic("PeerConnection snowflake already exists.")
} }
// Flush the buffer if necessary. // Flush buffered outgoing SOCKS data if necessary.
if c.buffer.Len() > 0 { if c.buffer.Len() > 0 {
dc.Send(c.buffer.Bytes()) dc.Send(c.buffer.Bytes())
log.Println("Flushed", c.buffer.Len(), "bytes.") log.Println("Flushed", c.buffer.Len(), "bytes.")
@ -186,10 +205,16 @@ func (c *webRTCConn) establishDataChannel() error {
// Future writes will go to the buffer until a new DataChannel is available. // Future writes will go to the buffer until a new DataChannel is available.
log.Println("WebRTC: DataChannel.OnClose") log.Println("WebRTC: DataChannel.OnClose")
// Only reset if this OnClose was triggered remotely. // Only reset if this OnClose was triggered remotely.
if nil != c.snowflake { if nil == c.snowflake {
c.snowflake = nil panic("Should not have nil snowflake before closing. ")
c.Reset()
} }
c.snowflake = nil
// TODO: Need a way to update the circuit so that when a new WebRTC
// data channel is available, the relay actually recognizes the new
// snowflake?
c.Reset()
// c.Close()
// c.endChannel <- struct{}{}
} }
dc.OnMessage = func(msg []byte) { dc.OnMessage = func(msg []byte) {
// log.Println("ONMESSAGE: ", len(msg)) // log.Println("ONMESSAGE: ", len(msg))
@ -214,7 +239,6 @@ func (c *webRTCConn) establishDataChannel() error {
// Block until an offer is available, then send it to either // Block until an offer is available, then send it to either
// the Broker or signal pipe. // the Broker or signal pipe.
func (c *webRTCConn) sendOffer() error { func (c *webRTCConn) sendOffer() error {
log.Println("sendOffer...")
select { select {
case offer := <-c.offerChannel: case offer := <-c.offerChannel:
if "" == brokerURL { if "" == brokerURL {
@ -254,6 +278,7 @@ func (c *webRTCConn) receiveAnswer() {
log.Printf("Received Answer:\n\n%s\n", answer.Sdp) log.Printf("Received Answer:\n\n%s\n", answer.Sdp)
err := c.pc.SetRemoteDescription(answer) err := c.pc.SetRemoteDescription(answer)
if nil != err { if nil != err {
log.Printf("webrtc: Unable to SetRemoteDescription.")
c.errorChannel <- err c.errorChannel <- err
} }
}() }()