Refactor out Dialer

This commit is contained in:
Andy Wang 2020-04-09 00:34:02 +01:00
parent 8cf7b60f4e
commit 86095ba5e6
6 changed files with 30 additions and 21 deletions

View File

@ -6,6 +6,7 @@ import (
"encoding/base64" "encoding/base64"
"flag" "flag"
"fmt" "fmt"
"net"
"os" "os"
"github.com/cbeuw/Cloak/internal/client" "github.com/cbeuw/Cloak/internal/client"
@ -129,7 +130,6 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
remoteConfig.Protector = protector
var adminUID []byte var adminUID []byte
if b64AdminUID != "" { if b64AdminUID != "" {
@ -141,13 +141,15 @@ func main() {
var seshMaker func() *mux.Session var seshMaker func() *mux.Session
d := &net.Dialer{Control: protector, KeepAlive: remoteConfig.KeepAlive}
if adminUID != nil { if adminUID != nil {
log.Infof("API base is %v", localConfig.LocalAddr) log.Infof("API base is %v", localConfig.LocalAddr)
authInfo.UID = adminUID authInfo.UID = adminUID
remoteConfig.NumConn = 1 remoteConfig.NumConn = 1
seshMaker = func() *mux.Session { seshMaker = func() *mux.Session {
return client.MakeSession(remoteConfig, authInfo, true) return client.MakeSession(remoteConfig, authInfo, d, true)
} }
} else { } else {
var network string var network string
@ -158,7 +160,7 @@ func main() {
} }
log.Infof("Listening on %v %v for %v client", network, localConfig.LocalAddr, authInfo.ProxyMethod) log.Infof("Listening on %v %v for %v client", network, localConfig.LocalAddr, authInfo.ProxyMethod)
seshMaker = func() *mux.Session { seshMaker = func() *mux.Session {
return client.MakeSession(remoteConfig, authInfo, false) return client.MakeSession(remoteConfig, authInfo, d, false)
} }
} }

View File

@ -12,7 +12,7 @@ import (
log "github.com/sirupsen/logrus" 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") log.Info("Attempting to start a new session")
if !isAdmin { if !isAdmin {
// 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
@ -24,7 +24,6 @@ func MakeSession(connConfig remoteConnConfig, authInfo authInfo, isAdmin bool) *
authInfo.SessionId = 0 authInfo.SessionId = 0
} }
d := net.Dialer{Control: connConfig.Protector, KeepAlive: connConfig.KeepAlive}
connsCh := make(chan net.Conn, connConfig.NumConn) connsCh := make(chan net.Conn, connConfig.NumConn)
var _sessionKey atomic.Value var _sessionKey atomic.Value
var wg sync.WaitGroup var wg sync.WaitGroup
@ -32,7 +31,7 @@ func MakeSession(connConfig remoteConnConfig, authInfo authInfo, isAdmin bool) *
wg.Add(1) wg.Add(1)
go func() { go func() {
makeconn: makeconn:
remoteConn, err := d.Dial("tcp", connConfig.RemoteAddr) remoteConn, err := dialer.Dial("tcp", connConfig.RemoteAddr)
if err != nil { if err != nil {
log.Errorf("Failed to establish new connections to remote: %v", err) log.Errorf("Failed to establish new connections to remote: %v", err)
// TODO increase the interval if failed multiple times // TODO increase the interval if failed multiple times

View File

@ -7,7 +7,6 @@ import (
"io/ioutil" "io/ioutil"
"net" "net"
"strings" "strings"
"syscall"
"time" "time"
"github.com/cbeuw/Cloak/internal/ecdh" "github.com/cbeuw/Cloak/internal/ecdh"
@ -41,7 +40,6 @@ type rawConfig struct {
type remoteConnConfig struct { type remoteConnConfig struct {
NumConn int NumConn int
KeepAlive time.Duration KeepAlive time.Duration
Protector func(string, string, syscall.RawConn) error
RemoteAddr string RemoteAddr string
TransportMaker func() Transport TransportMaker func() Transport
} }

View File

@ -37,7 +37,7 @@ func DispatchConnection(conn net.Conn, sta *State) {
if redirPort == "" { if redirPort == "" {
_, redirPort, _ = net.SplitHostPort(conn.LocalAddr().String()) _, 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 { if err != nil {
log.Errorf("Making connection to redirection server: %v", err) log.Errorf("Making connection to redirection server: %v", err)
return return
@ -165,8 +165,7 @@ func DispatchConnection(conn net.Conn, sta *State) {
} }
} }
proxyAddr := sta.ProxyBook[ci.ProxyMethod] proxyAddr := sta.ProxyBook[ci.ProxyMethod]
d := net.Dialer{KeepAlive: sta.KeepAlive} localConn, err := sta.ProxyDialer.Dial(proxyAddr.Network(), proxyAddr.String())
localConn, err := d.Dial(proxyAddr.Network(), proxyAddr.String())
if err != nil { if err != nil {
log.Errorf("Failed to connect to %v: %v", ci.ProxyMethod, err) log.Errorf("Failed to connect to %v: %v", ci.ProxyMethod, err)
user.CloseSession(ci.SessionId, "Failed to connect to proxy server") user.CloseSession(ci.SessionId, "Failed to connect to proxy server")

View File

@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/cbeuw/Cloak/internal/server/usermanager" "github.com/cbeuw/Cloak/internal/server/usermanager"
"github.com/cbeuw/Cloak/internal/util"
"io/ioutil" "io/ioutil"
"net" "net"
"strings" "strings"
@ -30,19 +31,22 @@ type rawConfig struct {
// State type stores the global state of the program // State type stores the global state of the program
type State struct { type State struct {
BindAddr []net.Addr BindAddr []net.Addr
ProxyBook map[string]net.Addr ProxyBook map[string]net.Addr
ProxyDialer util.Dialer
Now func() time.Time Now func() time.Time
AdminUID []byte AdminUID []byte
Timeout time.Duration Timeout time.Duration
KeepAlive time.Duration //KeepAlive time.Duration
BypassUID map[[16]byte]struct{} BypassUID map[[16]byte]struct{}
staticPv crypto.PrivateKey staticPv crypto.PrivateKey
RedirHost net.Addr // TODO: this doesn't have to be a net.Addr; resolution is done in Dial automatically
RedirPort string RedirHost net.Addr
RedirPort string
RedirDialer util.Dialer
usedRandomM sync.RWMutex usedRandomM sync.RWMutex
usedRandom map[[32]byte]int64 usedRandom map[[32]byte]int64
@ -176,9 +180,9 @@ func (sta *State) ParseConfig(conf string) (err error) {
} }
if preParse.KeepAlive <= 0 { if preParse.KeepAlive <= 0 {
sta.KeepAlive = -1 sta.ProxyDialer = &net.Dialer{KeepAlive: -1}
} else { } 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) sta.RedirHost, sta.RedirPort, err = parseRedirAddr(preParse.RedirAddr)

7
internal/util/dialer.go Normal file
View File

@ -0,0 +1,7 @@
package util
import "net"
type Dialer interface {
Dial(network, address string) (net.Conn, error)
}