mirror of https://github.com/cbeuw/Cloak
Merge branch 'fix-blocking-make-session' into notsure2
This commit is contained in:
commit
77f9f6c9a4
|
|
@ -184,12 +184,12 @@ func main() {
|
||||||
return net.ListenUDP("udp", udpAddr)
|
return net.ListenUDP("udp", udpAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
client.RouteUDP(acceptor, localConfig.Timeout, seshMaker)
|
client.RouteUDP(acceptor, localConfig.Timeout, remoteConfig.Singleplex, seshMaker)
|
||||||
} else {
|
} else {
|
||||||
listener, err := net.Listen("tcp", localConfig.LocalAddr)
|
listener, err := net.Listen("tcp", localConfig.LocalAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
client.RouteTCP(listener, localConfig.Timeout, seshMaker)
|
client.RouteTCP(listener, localConfig.Timeout, remoteConfig.Singleplex, seshMaker)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,5 @@
|
||||||
"RedirAddr": "204.79.197.200:443",
|
"RedirAddr": "204.79.197.200:443",
|
||||||
"PrivateKey": "EN5aPEpNBO+vw+BtFQY2OnK9bQU7rvEj5qmnmgwEtUc=",
|
"PrivateKey": "EN5aPEpNBO+vw+BtFQY2OnK9bQU7rvEj5qmnmgwEtUc=",
|
||||||
"AdminUID": "5nneblJy6lniPJfr81LuYQ==",
|
"AdminUID": "5nneblJy6lniPJfr81LuYQ==",
|
||||||
"DatabasePath": "userinfo.db",
|
"DatabasePath": "userinfo.db"
|
||||||
"StreamTimeout": 300
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,8 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RouteUDP(bindFunc func() (*net.UDPConn, error), streamTimeout time.Duration, newSeshFunc func() *mux.Session) {
|
func RouteUDP(bindFunc func() (*net.UDPConn, error), streamTimeout time.Duration, singleplex bool, newSeshFunc func() *mux.Session) {
|
||||||
var sesh *mux.Session
|
var multiplexSession *mux.Session
|
||||||
localConn, err := bindFunc()
|
localConn, err := bindFunc()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|
@ -27,17 +27,24 @@ func RouteUDP(bindFunc func() (*net.UDPConn, error), streamTimeout time.Duration
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if sesh == nil || sesh.IsClosed() || sesh.Singleplex {
|
if !singleplex && (multiplexSession == nil || multiplexSession.IsClosed()) {
|
||||||
sesh = newSeshFunc()
|
multiplexSession = newSeshFunc()
|
||||||
}
|
}
|
||||||
|
|
||||||
stream, ok := streams[addr.String()]
|
stream, ok := streams[addr.String()]
|
||||||
if !ok {
|
if !ok {
|
||||||
stream, err = sesh.OpenStream()
|
var session *mux.Session
|
||||||
|
if multiplexSession != nil {
|
||||||
|
session = multiplexSession
|
||||||
|
} else {
|
||||||
|
session = newSeshFunc()
|
||||||
|
}
|
||||||
|
|
||||||
|
stream, err = session.OpenStream()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to open stream: %v", err)
|
log.Errorf("Failed to open stream: %v", err)
|
||||||
if sesh.Singleplex {
|
if session.Singleplex {
|
||||||
sesh.Close()
|
session.Close()
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
@ -74,18 +81,25 @@ func RouteUDP(bindFunc func() (*net.UDPConn, error), streamTimeout time.Duration
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RouteTCP(listener net.Listener, streamTimeout time.Duration, newSeshFunc func() *mux.Session) {
|
func RouteTCP(listener net.Listener, streamTimeout time.Duration, singleplex bool, newSeshFunc func() *mux.Session) {
|
||||||
var sesh *mux.Session
|
var multiplexSession *mux.Session
|
||||||
for {
|
for {
|
||||||
localConn, err := listener.Accept()
|
localConn, err := listener.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if sesh == nil || sesh.IsClosed() || sesh.Singleplex {
|
if !singleplex && (multiplexSession == nil || multiplexSession.IsClosed()) {
|
||||||
sesh = newSeshFunc()
|
multiplexSession = newSeshFunc()
|
||||||
}
|
}
|
||||||
go func(sesh *mux.Session, localConn net.Conn, timeout time.Duration) {
|
go func(multiplexSession *mux.Session, newSingleplexSeshFunc func() *mux.Session, localConn net.Conn, timeout time.Duration) {
|
||||||
|
var session *mux.Session
|
||||||
|
if multiplexSession != nil {
|
||||||
|
session = multiplexSession
|
||||||
|
} else {
|
||||||
|
session = newSingleplexSeshFunc()
|
||||||
|
}
|
||||||
|
|
||||||
data := make([]byte, 10240)
|
data := make([]byte, 10240)
|
||||||
_ = localConn.SetReadDeadline(time.Now().Add(streamTimeout))
|
_ = localConn.SetReadDeadline(time.Now().Add(streamTimeout))
|
||||||
i, err := io.ReadAtLeast(localConn, data, 1)
|
i, err := io.ReadAtLeast(localConn, data, 1)
|
||||||
|
|
@ -97,12 +111,12 @@ func RouteTCP(listener net.Listener, streamTimeout time.Duration, newSeshFunc fu
|
||||||
var zeroTime time.Time
|
var zeroTime time.Time
|
||||||
_ = localConn.SetReadDeadline(zeroTime)
|
_ = localConn.SetReadDeadline(zeroTime)
|
||||||
|
|
||||||
stream, err := sesh.OpenStream()
|
stream, err := session.OpenStream()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to open stream: %v", err)
|
log.Errorf("Failed to open stream: %v", err)
|
||||||
localConn.Close()
|
localConn.Close()
|
||||||
if sesh.Singleplex {
|
if session.Singleplex {
|
||||||
sesh.Close()
|
session.Close()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -123,7 +137,6 @@ func RouteTCP(listener net.Listener, streamTimeout time.Duration, newSeshFunc fu
|
||||||
if _, err = common.Copy(stream, localConn); err != nil {
|
if _, err = common.Copy(stream, localConn); err != nil {
|
||||||
log.Tracef("copying proxy client to stream: %v", err)
|
log.Tracef("copying proxy client to stream: %v", err)
|
||||||
}
|
}
|
||||||
}(sesh, localConn, streamTimeout)
|
}(multiplexSession, newSeshFunc, localConn, streamTimeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -206,12 +206,12 @@ func establishSession(lcc client.LocalConnConfig, rcc client.RemoteConnConfig, a
|
||||||
addrCh <- conn.LocalAddr().(*net.UDPAddr)
|
addrCh <- conn.LocalAddr().(*net.UDPAddr)
|
||||||
return conn, err
|
return conn, err
|
||||||
}
|
}
|
||||||
go client.RouteUDP(acceptor, lcc.Timeout, clientSeshMaker)
|
go client.RouteUDP(acceptor, lcc.Timeout, rcc.Singleplex, clientSeshMaker)
|
||||||
proxyToCkClientD = mDialer
|
proxyToCkClientD = mDialer
|
||||||
} else {
|
} else {
|
||||||
var proxyToCkClientL *connutil.PipeListener
|
var proxyToCkClientL *connutil.PipeListener
|
||||||
proxyToCkClientD, proxyToCkClientL = connutil.DialerListener(10 * 1024)
|
proxyToCkClientD, proxyToCkClientL = connutil.DialerListener(10 * 1024)
|
||||||
go client.RouteTCP(proxyToCkClientL, lcc.Timeout, clientSeshMaker)
|
go client.RouteTCP(proxyToCkClientL, lcc.Timeout, rcc.Singleplex, clientSeshMaker)
|
||||||
}
|
}
|
||||||
|
|
||||||
// set up server
|
// set up server
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue