2020-04-06 12:07:16 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2020-04-08 23:39:40 +00:00
|
|
|
"github.com/cbeuw/Cloak/internal/common"
|
2020-04-06 12:07:16 +00:00
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
mux "github.com/cbeuw/Cloak/internal/multiplex"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-10-15 22:02:51 +00:00
|
|
|
// On different invocations to MakeSession, authInfo.SessionId MUST be different
|
|
|
|
func MakeSession(connConfig RemoteConnConfig, authInfo AuthInfo, dialer common.Dialer) *mux.Session {
|
2020-04-06 12:07:16 +00:00
|
|
|
log.Info("Attempting to start a new session")
|
|
|
|
|
2020-10-15 20:32:38 +00:00
|
|
|
connsCh := make(chan net.Conn, connConfig.NumConn)
|
2020-04-06 12:07:16 +00:00
|
|
|
var _sessionKey atomic.Value
|
|
|
|
var wg sync.WaitGroup
|
2020-10-15 20:32:38 +00:00
|
|
|
for i := 0; i < connConfig.NumConn; i++ {
|
2020-04-06 12:07:16 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
makeconn:
|
2020-04-08 23:34:02 +00:00
|
|
|
remoteConn, err := dialer.Dial("tcp", connConfig.RemoteAddr)
|
2020-04-06 12:07:16 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to establish new connections to remote: %v", err)
|
|
|
|
// TODO increase the interval if failed multiple times
|
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
goto makeconn
|
|
|
|
}
|
2020-04-08 19:53:09 +00:00
|
|
|
|
|
|
|
transportConn := connConfig.TransportMaker()
|
|
|
|
sk, err := transportConn.Handshake(remoteConn, authInfo)
|
2020-04-06 12:07:16 +00:00
|
|
|
if err != nil {
|
2020-04-08 19:53:09 +00:00
|
|
|
transportConn.Close()
|
2020-04-06 12:07:16 +00:00
|
|
|
log.Errorf("Failed to prepare connection to remote: %v", err)
|
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
goto makeconn
|
|
|
|
}
|
2020-10-15 22:02:51 +00:00
|
|
|
// sessionKey given by each connection should be identical
|
2020-04-06 12:07:16 +00:00
|
|
|
_sessionKey.Store(sk)
|
2020-04-08 19:53:09 +00:00
|
|
|
connsCh <- transportConn
|
2020-04-06 12:07:16 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
log.Debug("All underlying connections established")
|
|
|
|
|
2020-04-07 20:15:28 +00:00
|
|
|
sessionKey := _sessionKey.Load().([32]byte)
|
2020-04-08 19:53:09 +00:00
|
|
|
obfuscator, err := mux.MakeObfuscator(authInfo.EncryptionMethod, sessionKey)
|
2020-04-06 12:07:16 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-04-08 14:07:35 +00:00
|
|
|
seshConfig := mux.SessionConfig{
|
2020-10-15 20:32:38 +00:00
|
|
|
Singleplex: connConfig.Singleplex,
|
2020-04-09 15:37:46 +00:00
|
|
|
Obfuscator: obfuscator,
|
|
|
|
Valve: nil,
|
|
|
|
Unordered: authInfo.Unordered,
|
|
|
|
MaxFrameSize: appDataMaxLength,
|
2020-04-06 12:07:16 +00:00
|
|
|
}
|
|
|
|
sesh := mux.MakeSession(authInfo.SessionId, seshConfig)
|
|
|
|
|
2020-10-15 20:32:38 +00:00
|
|
|
for i := 0; i < connConfig.NumConn; i++ {
|
2020-04-06 12:07:16 +00:00
|
|
|
conn := <-connsCh
|
|
|
|
sesh.AddConnection(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Session %v established", authInfo.SessionId)
|
|
|
|
return sesh
|
|
|
|
}
|