Skip to content

Commit

Permalink
Simplify TLS handler
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomika authored May 23, 2022
1 parent 8152624 commit 71222f4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
11 changes: 4 additions & 7 deletions httpmuxer/httpmuxer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package httpmuxer

import (
"bytes"
"crypto/tls"
"encoding/base64"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -361,18 +360,16 @@ func Start(state *utils.State) {
httpsListener = pListener
}

l := tls.NewListener(httpsListener, tlsConfig)

if tH != nil {
tH.Listener = l
tH.Listener = httpsListener

state.Listeners.Store(httpsServer.Addr, l)
state.Listeners.Store(httpsServer.Addr, httpsListener)
state.TCPListeners.Store(httpsServer.Addr, tH)
}

defer l.Close()
defer httpsListener.Close()

log.Fatal(httpsServer.Serve(l))
log.Fatal(httpsServer.ServeTLS(httpsListener, "", ""))
}()
}

Expand Down
22 changes: 19 additions & 3 deletions sshmuxer/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ import (
// commandSplitter is the character that terminates a prefix.
const commandSplitter = "="

// proxyProtoPrefix is used when deciding what proxy protocol
// proxyProtocolPrefix is used when deciding what proxy protocol
// version to use.
const proxyProtoPrefix = "proxyproto"
const proxyProtocolPrefix = "proxy-protocol"

// proxyProtoPrefixLegacy is used when deciding what proxy protocol
// version to use.
const proxyProtoPrefixLegacy = "proxyproto"

// hostHeaderPrefix is the host-header for a specific session.
const hostHeaderPrefix = "host-header"
Expand All @@ -41,6 +45,8 @@ const localForwardPrefix = "local-forward"
// autoClosePrefix defines whether or not a connection will close when all forwards are cleaned up.
const autoClosePrefix = "auto-close"

const tcpAddressPrefix = "tcp-address"

// handleSession handles the channel when a user requests a session.
// This is how we send console messages.
func handleSession(newChannel ssh.NewChannel, sshConn *utils.SSHConnection, state *utils.State) {
Expand Down Expand Up @@ -117,7 +123,9 @@ func handleSession(newChannel ssh.NewChannel, sshConn *utils.SSHConnection, stat
command, param := commandFlagParts[0], commandFlagParts[1]

switch command {
case proxyProtoPrefix:
case proxyProtocolPrefix:
fallthrough
case proxyProtoPrefixLegacy:
if !viper.GetBool("proxy-protocol") {
break
}
Expand Down Expand Up @@ -159,6 +167,14 @@ func handleSession(newChannel ssh.NewChannel, sshConn *utils.SSHConnection, stat
sshConn.SNIProxy = sniProxy

sshConn.SendMessage(fmt.Sprintf("SNI proxy for TCP forwards set to: %t", sshConn.SNIProxy), true)
case tcpAddressPrefix:
if viper.GetBool("force-tcp-address") {
break
}

sshConn.TCPAddress = param

sshConn.SendMessage(fmt.Sprintf("TCP address for TCP forwards set to: %s", sshConn.TCPAddress), true)
case tcpAliasPrefix:
if !viper.GetBool("tcp-aliases") {
break
Expand Down
1 change: 1 addition & 0 deletions utils/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type SSHConnection struct {
HostHeader string
StripPath bool
SNIProxy bool
TCPAddress string
TCPAlias bool
LocalForward bool
AutoClose bool
Expand Down
6 changes: 5 additions & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,11 @@ func GetOpenPort(addr string, port uint32, state *State, sshConn *SSHConnection,
bindAddr := addr
listenAddr := ""

if (bindAddr == "localhost" && viper.GetBool("localhost-as-all")) || viper.GetBool("force-tcp-address") || sniProxyEnabled {
if bindAddr == "" {
bindAddr = sshConn.TCPAddress
}

if (bindAddr == "localhost" && viper.GetBool("localhost-as-all")) || viper.GetBool("force-tcp-address") || (sniProxyEnabled && sshConn.TCPAddress == "") {
bindAddr = viper.GetString("tcp-address")
}

Expand Down

0 comments on commit 71222f4

Please # to comment.