mirror of https://github.com/cbeuw/Cloak
Client auto-reconnect
This commit is contained in:
parent
23a06c6a52
commit
fd7d90e36a
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/cbeuw/Cloak/internal/client"
|
"github.com/cbeuw/Cloak/internal/client"
|
||||||
|
|
@ -22,7 +23,6 @@ import (
|
||||||
var version string
|
var version string
|
||||||
|
|
||||||
func pipe(dst io.ReadWriteCloser, src io.ReadWriteCloser) {
|
func pipe(dst io.ReadWriteCloser, src io.ReadWriteCloser) {
|
||||||
// TODO: auto reconnect
|
|
||||||
// The maximum size of TLS message will be 16396+12. 12 because of the stream header
|
// The maximum size of TLS message will be 16396+12. 12 because of the stream header
|
||||||
// 16408 is the max TLS message size on Firefox
|
// 16408 is the max TLS message size on Firefox
|
||||||
buf := make([]byte, 16396)
|
buf := make([]byte, 16396)
|
||||||
|
|
@ -139,12 +139,12 @@ func main() {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// sessionID is usergenerated. There shouldn't be a security concern because the scope of
|
// sessionID is usergenerated. There shouldn't be a security concern because the scope of
|
||||||
// sessionID is limited to its UID.
|
// sessionID is limited to its UID.
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
sessionID := rand.Uint32()
|
sessionID := rand.Uint32()
|
||||||
|
|
||||||
// opaque is used to generate the padding of session ticket
|
|
||||||
sta := client.InitState(localHost, localPort, remoteHost, remotePort, time.Now, sessionID)
|
sta := client.InitState(localHost, localPort, remoteHost, remotePort, time.Now, sessionID)
|
||||||
err := sta.ParseConfig(pluginOpts)
|
err := sta.ParseConfig(pluginOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -160,7 +160,13 @@ func main() {
|
||||||
if sta.TicketTimeHint == 0 {
|
if sta.TicketTimeHint == 0 {
|
||||||
log.Fatal("TicketTimeHint cannot be empty or 0")
|
log.Fatal("TicketTimeHint cannot be empty or 0")
|
||||||
}
|
}
|
||||||
|
listener, err := net.Listen("tcp", sta.SS_LOCAL_HOST+":"+sta.SS_LOCAL_PORT)
|
||||||
|
log.Println("Listening for ss on " + sta.SS_LOCAL_HOST + ":" + sta.SS_LOCAL_PORT)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
start:
|
||||||
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)
|
||||||
|
|
@ -171,10 +177,12 @@ func main() {
|
||||||
for i := 0; i < sta.NumConn; i++ {
|
for i := 0; i < sta.NumConn; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
|
makeconn:
|
||||||
conn, err := makeRemoteConn(sta)
|
conn, err := makeRemoteConn(sta)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to establish new connections to remote: %v\n", err)
|
log.Printf("Failed to establish new connections to remote: %v\n", err)
|
||||||
return
|
time.Sleep(time.Second * 3)
|
||||||
|
goto makeconn
|
||||||
}
|
}
|
||||||
sesh.AddConnection(conn)
|
sesh.AddConnection(conn)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
|
|
@ -182,12 +190,11 @@ func main() {
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
listener, err := net.Listen("tcp", sta.SS_LOCAL_HOST+":"+sta.SS_LOCAL_PORT)
|
var broken uint32
|
||||||
log.Println("Listening for ss on " + sta.SS_LOCAL_HOST + ":" + sta.SS_LOCAL_PORT)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
for {
|
for {
|
||||||
|
if atomic.LoadUint32(&broken) == 1 {
|
||||||
|
goto retry
|
||||||
|
}
|
||||||
ssConn, err := listener.Accept()
|
ssConn, err := listener.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
|
@ -203,6 +210,9 @@ func main() {
|
||||||
}
|
}
|
||||||
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
|
||||||
|
|
@ -218,5 +228,9 @@ func main() {
|
||||||
pipe(stream, ssConn)
|
pipe(stream, ssConn)
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
retry:
|
||||||
|
time.Sleep(time.Second * 3)
|
||||||
|
log.Println("Reconnecting")
|
||||||
|
goto start
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue