mirror of https://github.com/cbeuw/Cloak
Faster reconnection
This commit is contained in:
parent
17d024ac16
commit
9793537034
|
|
@ -11,7 +11,6 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/cbeuw/Cloak/internal/client"
|
"github.com/cbeuw/Cloak/internal/client"
|
||||||
|
|
@ -167,6 +166,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
start:
|
start:
|
||||||
|
log.Println("Attemtping to start a new session")
|
||||||
var UNLIMITED int64 = 1e12
|
var UNLIMITED int64 = 1e12
|
||||||
valve := mux.MakeValve(1e12, 1e12, &UNLIMITED, &UNLIMITED)
|
valve := mux.MakeValve(1e12, 1e12, &UNLIMITED, &UNLIMITED)
|
||||||
obfs := mux.MakeObfs(sta.UID)
|
obfs := mux.MakeObfs(sta.UID)
|
||||||
|
|
@ -190,10 +190,9 @@ start:
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
var broken uint32
|
|
||||||
for {
|
for {
|
||||||
if atomic.LoadUint32(&broken) == 1 {
|
if sesh.IsBroken() {
|
||||||
goto retry
|
goto start
|
||||||
}
|
}
|
||||||
ssConn, err := listener.Accept()
|
ssConn, err := listener.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -210,9 +209,6 @@ start:
|
||||||
}
|
}
|
||||||
stream, err := sesh.OpenStream()
|
stream, err := sesh.OpenStream()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == mux.ErrBrokenSession {
|
|
||||||
atomic.StoreUint32(&broken, 1)
|
|
||||||
}
|
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
ssConn.Close()
|
ssConn.Close()
|
||||||
return
|
return
|
||||||
|
|
@ -228,9 +224,5 @@ start:
|
||||||
pipe(stream, ssConn)
|
pipe(stream, ssConn)
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
retry:
|
|
||||||
time.Sleep(time.Second * 3)
|
|
||||||
log.Println("Reconnecting")
|
|
||||||
goto start
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ type Session struct {
|
||||||
// For accepting new streams
|
// For accepting new streams
|
||||||
acceptCh chan *Stream
|
acceptCh chan *Stream
|
||||||
|
|
||||||
|
broken uint32
|
||||||
die chan struct{}
|
die chan struct{}
|
||||||
suicide sync.Once
|
suicide sync.Once
|
||||||
}
|
}
|
||||||
|
|
@ -121,6 +122,7 @@ func (sesh *Session) getStream(id uint32, closingFrame bool) *Stream {
|
||||||
func (sesh *Session) Close() error {
|
func (sesh *Session) Close() error {
|
||||||
// Because closing a closed channel causes panic
|
// Because closing a closed channel causes panic
|
||||||
sesh.suicide.Do(func() { close(sesh.die) })
|
sesh.suicide.Do(func() { close(sesh.die) })
|
||||||
|
atomic.StoreUint32(&sesh.broken, 1)
|
||||||
sesh.streamsM.Lock()
|
sesh.streamsM.Lock()
|
||||||
for id, stream := range sesh.streams {
|
for id, stream := range sesh.streams {
|
||||||
// If we call stream.Close() here, streamsM will result in a deadlock
|
// If we call stream.Close() here, streamsM will result in a deadlock
|
||||||
|
|
@ -136,3 +138,7 @@ func (sesh *Session) Close() error {
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sesh *Session) IsBroken() bool {
|
||||||
|
return atomic.LoadUint32(&sesh.broken) == 1
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue