mirror of https://github.com/cbeuw/Cloak
Refactor out Dialer
This commit is contained in:
parent
8cf7b60f4e
commit
86095ba5e6
|
|
@ -6,6 +6,7 @@ import (
|
|||
"encoding/base64"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/cbeuw/Cloak/internal/client"
|
||||
|
|
@ -129,7 +130,6 @@ func main() {
|
|||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
remoteConfig.Protector = protector
|
||||
|
||||
var adminUID []byte
|
||||
if b64AdminUID != "" {
|
||||
|
|
@ -141,13 +141,15 @@ func main() {
|
|||
|
||||
var seshMaker func() *mux.Session
|
||||
|
||||
d := &net.Dialer{Control: protector, KeepAlive: remoteConfig.KeepAlive}
|
||||
|
||||
if adminUID != nil {
|
||||
log.Infof("API base is %v", localConfig.LocalAddr)
|
||||
authInfo.UID = adminUID
|
||||
remoteConfig.NumConn = 1
|
||||
|
||||
seshMaker = func() *mux.Session {
|
||||
return client.MakeSession(remoteConfig, authInfo, true)
|
||||
return client.MakeSession(remoteConfig, authInfo, d, true)
|
||||
}
|
||||
} else {
|
||||
var network string
|
||||
|
|
@ -158,7 +160,7 @@ func main() {
|
|||
}
|
||||
log.Infof("Listening on %v %v for %v client", network, localConfig.LocalAddr, authInfo.ProxyMethod)
|
||||
seshMaker = func() *mux.Session {
|
||||
return client.MakeSession(remoteConfig, authInfo, false)
|
||||
return client.MakeSession(remoteConfig, authInfo, d, false)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ import (
|
|||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func MakeSession(connConfig remoteConnConfig, authInfo authInfo, isAdmin bool) *mux.Session {
|
||||
func MakeSession(connConfig remoteConnConfig, authInfo authInfo, dialer util.Dialer, 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
|
||||
|
|
@ -24,7 +24,6 @@ func MakeSession(connConfig remoteConnConfig, authInfo authInfo, isAdmin bool) *
|
|||
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
|
||||
|
|
@ -32,7 +31,7 @@ func MakeSession(connConfig remoteConnConfig, authInfo authInfo, isAdmin bool) *
|
|||
wg.Add(1)
|
||||
go func() {
|
||||
makeconn:
|
||||
remoteConn, err := d.Dial("tcp", connConfig.RemoteAddr)
|
||||
remoteConn, err := dialer.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
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import (
|
|||
"io/ioutil"
|
||||
"net"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/cbeuw/Cloak/internal/ecdh"
|
||||
|
|
@ -41,7 +40,6 @@ type rawConfig struct {
|
|||
type remoteConnConfig struct {
|
||||
NumConn int
|
||||
KeepAlive time.Duration
|
||||
Protector func(string, string, syscall.RawConn) error
|
||||
RemoteAddr string
|
||||
TransportMaker func() Transport
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ func DispatchConnection(conn net.Conn, sta *State) {
|
|||
if redirPort == "" {
|
||||
_, redirPort, _ = net.SplitHostPort(conn.LocalAddr().String())
|
||||
}
|
||||
webConn, err := net.Dial("tcp", net.JoinHostPort(sta.RedirHost.String(), redirPort))
|
||||
webConn, err := sta.RedirDialer.Dial("tcp", net.JoinHostPort(sta.RedirHost.String(), redirPort))
|
||||
if err != nil {
|
||||
log.Errorf("Making connection to redirection server: %v", err)
|
||||
return
|
||||
|
|
@ -165,8 +165,7 @@ func DispatchConnection(conn net.Conn, sta *State) {
|
|||
}
|
||||
}
|
||||
proxyAddr := sta.ProxyBook[ci.ProxyMethod]
|
||||
d := net.Dialer{KeepAlive: sta.KeepAlive}
|
||||
localConn, err := d.Dial(proxyAddr.Network(), proxyAddr.String())
|
||||
localConn, err := sta.ProxyDialer.Dial(proxyAddr.Network(), proxyAddr.String())
|
||||
if err != nil {
|
||||
log.Errorf("Failed to connect to %v: %v", ci.ProxyMethod, err)
|
||||
user.CloseSession(ci.SessionId, "Failed to connect to proxy server")
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"github.com/cbeuw/Cloak/internal/server/usermanager"
|
||||
"github.com/cbeuw/Cloak/internal/util"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"strings"
|
||||
|
|
@ -30,19 +31,22 @@ type rawConfig struct {
|
|||
|
||||
// State type stores the global state of the program
|
||||
type State struct {
|
||||
BindAddr []net.Addr
|
||||
ProxyBook map[string]net.Addr
|
||||
BindAddr []net.Addr
|
||||
ProxyBook map[string]net.Addr
|
||||
ProxyDialer util.Dialer
|
||||
|
||||
Now func() time.Time
|
||||
AdminUID []byte
|
||||
Timeout time.Duration
|
||||
KeepAlive time.Duration
|
||||
Now func() time.Time
|
||||
AdminUID []byte
|
||||
Timeout time.Duration
|
||||
//KeepAlive time.Duration
|
||||
|
||||
BypassUID map[[16]byte]struct{}
|
||||
staticPv crypto.PrivateKey
|
||||
|
||||
RedirHost net.Addr
|
||||
RedirPort string
|
||||
// TODO: this doesn't have to be a net.Addr; resolution is done in Dial automatically
|
||||
RedirHost net.Addr
|
||||
RedirPort string
|
||||
RedirDialer util.Dialer
|
||||
|
||||
usedRandomM sync.RWMutex
|
||||
usedRandom map[[32]byte]int64
|
||||
|
|
@ -176,9 +180,9 @@ func (sta *State) ParseConfig(conf string) (err error) {
|
|||
}
|
||||
|
||||
if preParse.KeepAlive <= 0 {
|
||||
sta.KeepAlive = -1
|
||||
sta.ProxyDialer = &net.Dialer{KeepAlive: -1}
|
||||
} else {
|
||||
sta.KeepAlive = time.Duration(preParse.KeepAlive) * time.Second
|
||||
sta.ProxyDialer = &net.Dialer{KeepAlive: time.Duration(preParse.KeepAlive) * time.Second}
|
||||
}
|
||||
|
||||
sta.RedirHost, sta.RedirPort, err = parseRedirAddr(preParse.RedirAddr)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
package util
|
||||
|
||||
import "net"
|
||||
|
||||
type Dialer interface {
|
||||
Dial(network, address string) (net.Conn, error)
|
||||
}
|
||||
Loading…
Reference in New Issue