mirror of https://github.com/cbeuw/Cloak
Refactor client config
parent
402cfc9e25
commit
a163f066a6
@ -0,0 +1,88 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
mux "github.com/cbeuw/Cloak/internal/multiplex"
|
||||||
|
"github.com/cbeuw/Cloak/internal/util"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
type remoteConnConfig struct {
|
||||||
|
NumConn int
|
||||||
|
KeepAlive time.Duration
|
||||||
|
Protector func(string, string, syscall.RawConn) error
|
||||||
|
RemoteAddr string
|
||||||
|
Transport Transport
|
||||||
|
}
|
||||||
|
|
||||||
|
func MakeSession(connConfig *remoteConnConfig, authInfo *authInfo, isAdmin bool) *mux.Session {
|
||||||
|
log.Info("Attempting to start a new session")
|
||||||
|
if !isAdmin {
|
||||||
|
// sessionID is usergenerated. There shouldn't be a security concern because the scope of
|
||||||
|
// sessionID is limited to its UID.
|
||||||
|
quad := make([]byte, 4)
|
||||||
|
util.CryptoRandRead(quad)
|
||||||
|
authInfo.SessionId = binary.BigEndian.Uint32(quad)
|
||||||
|
} else {
|
||||||
|
authInfo.SessionId = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
d := net.Dialer{Control: connConfig.Protector, KeepAlive: connConfig.KeepAlive}
|
||||||
|
connsCh := make(chan net.Conn, connConfig.NumConn)
|
||||||
|
var _sessionKey atomic.Value
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for i := 0; i < connConfig.NumConn; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
makeconn:
|
||||||
|
remoteConn, err := d.Dial("tcp", connConfig.RemoteAddr)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
var sk []byte
|
||||||
|
remoteConn, sk, err = connConfig.Transport.PrepareConnection(authInfo, remoteConn)
|
||||||
|
if err != nil {
|
||||||
|
remoteConn.Close()
|
||||||
|
log.Errorf("Failed to prepare connection to remote: %v", err)
|
||||||
|
time.Sleep(time.Second * 3)
|
||||||
|
goto makeconn
|
||||||
|
}
|
||||||
|
_sessionKey.Store(sk)
|
||||||
|
connsCh <- remoteConn
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
log.Debug("All underlying connections established")
|
||||||
|
|
||||||
|
sessionKey := _sessionKey.Load().([]byte)
|
||||||
|
obfuscator, err := mux.GenerateObfs(authInfo.EncryptionMethod, sessionKey, connConfig.Transport.HasRecordLayer())
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
seshConfig := &mux.SessionConfig{
|
||||||
|
Obfuscator: obfuscator,
|
||||||
|
Valve: nil,
|
||||||
|
UnitRead: connConfig.Transport.UnitReadFunc(),
|
||||||
|
Unordered: authInfo.Unordered,
|
||||||
|
}
|
||||||
|
sesh := mux.MakeSession(authInfo.SessionId, seshConfig)
|
||||||
|
|
||||||
|
for i := 0; i < connConfig.NumConn; i++ {
|
||||||
|
conn := <-connsCh
|
||||||
|
sesh.AddConnection(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infof("Session %v established", authInfo.SessionId)
|
||||||
|
return sesh
|
||||||
|
}
|
@ -0,0 +1,144 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
mux "github.com/cbeuw/Cloak/internal/multiplex"
|
||||||
|
"github.com/cbeuw/Cloak/internal/util"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RouteUDP(localConfig *localConnConfig, newSeshFunc func() *mux.Session) {
|
||||||
|
var sesh *mux.Session
|
||||||
|
localUDPAddr, err := net.ResolveUDPAddr("udp", localConfig.LocalAddr)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
start:
|
||||||
|
localConn, err := net.ListenUDP("udp", localUDPAddr)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
var otherEnd atomic.Value
|
||||||
|
data := make([]byte, 10240)
|
||||||
|
i, oe, err := localConn.ReadFromUDP(data)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Failed to read first packet from proxy client: %v", err)
|
||||||
|
localConn.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
otherEnd.Store(oe)
|
||||||
|
|
||||||
|
if sesh == nil || sesh.IsClosed() {
|
||||||
|
sesh = newSeshFunc()
|
||||||
|
}
|
||||||
|
log.Debugf("proxy local address %v", otherEnd.Load().(*net.UDPAddr).String())
|
||||||
|
stream, err := sesh.OpenStream()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Failed to open stream: %v", err)
|
||||||
|
localConn.Close()
|
||||||
|
//localConnWrite.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = stream.Write(data[:i])
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Failed to write to stream: %v", err)
|
||||||
|
localConn.Close()
|
||||||
|
//localConnWrite.Close()
|
||||||
|
stream.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// stream to proxy
|
||||||
|
go func() {
|
||||||
|
buf := make([]byte, 16380)
|
||||||
|
for {
|
||||||
|
i, err := io.ReadAtLeast(stream, buf, 1)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
localConn.Close()
|
||||||
|
stream.Close()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
_, err = localConn.WriteToUDP(buf[:i], otherEnd.Load().(*net.UDPAddr))
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
localConn.Close()
|
||||||
|
stream.Close()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// proxy to stream
|
||||||
|
buf := make([]byte, 16380)
|
||||||
|
if localConfig.Timeout != 0 {
|
||||||
|
localConn.SetReadDeadline(time.Now().Add(localConfig.Timeout))
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
if localConfig.Timeout != 0 {
|
||||||
|
localConn.SetReadDeadline(time.Now().Add(localConfig.Timeout))
|
||||||
|
}
|
||||||
|
i, oe, err := localConn.ReadFromUDP(buf)
|
||||||
|
if err != nil {
|
||||||
|
localConn.Close()
|
||||||
|
stream.Close()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
otherEnd.Store(oe)
|
||||||
|
_, err = stream.Write(buf[:i])
|
||||||
|
if err != nil {
|
||||||
|
localConn.Close()
|
||||||
|
stream.Close()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
goto start
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func RouteTCP(localConfig *localConnConfig, newSeshFunc func() *mux.Session) {
|
||||||
|
tcpListener, err := net.Listen("tcp", localConfig.LocalAddr)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
var sesh *mux.Session
|
||||||
|
for {
|
||||||
|
localConn, err := tcpListener.Accept()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if sesh == nil || sesh.IsClosed() {
|
||||||
|
sesh = newSeshFunc()
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
data := make([]byte, 10240)
|
||||||
|
i, err := io.ReadAtLeast(localConn, data, 1)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Failed to read first packet from proxy client: %v", err)
|
||||||
|
localConn.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
stream, err := sesh.OpenStream()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Failed to open stream: %v", err)
|
||||||
|
localConn.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = stream.Write(data[:i])
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Failed to write to stream: %v", err)
|
||||||
|
localConn.Close()
|
||||||
|
stream.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go util.Pipe(localConn, stream, 0)
|
||||||
|
util.Pipe(stream, localConn, localConfig.Timeout)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue