Implement selecting tunnel port

This is useful for when you want to use boringproxy more like a
normal reverse proxy, ie if boringproxy and your service are on
the same machine, you can just use the port of the service as the
tunnel port and boringproxy will forward directly to it without
using SSH.

This would normally be used with no client set.
This commit is contained in:
Anders Pitman 2021-02-16 18:37:31 -07:00
parent ffc8e45e85
commit 2b74f6ee0c
3 changed files with 28 additions and 5 deletions

11
api.go
View File

@ -282,6 +282,16 @@ func (a *Api) CreateTunnel(tokenData TokenData, params url.Values) (*Tunnel, err
clientAddr = "127.0.0.1" clientAddr = "127.0.0.1"
} }
tunnelPort := 0
tunnelPortParam := params.Get("tunnel-port")
if tunnelPortParam != "" && tunnelPortParam != "Random" {
var err error
tunnelPort, err = strconv.Atoi(tunnelPortParam)
if err != nil {
return nil, errors.New("Invalid tunnel-port parameter")
}
}
allowExternalTcp := params.Get("allow-external-tcp") == "on" allowExternalTcp := params.Get("allow-external-tcp") == "on"
passwordProtect := params.Get("password-protect") == "on" passwordProtect := params.Get("password-protect") == "on"
@ -312,6 +322,7 @@ func (a *Api) CreateTunnel(tokenData TokenData, params url.Values) (*Tunnel, err
ClientName: clientName, ClientName: clientName,
ClientPort: clientPort, ClientPort: clientPort,
ClientAddress: clientAddr, ClientAddress: clientAddr,
TunnelPort: tunnelPort,
AllowExternalTcp: allowExternalTcp, AllowExternalTcp: allowExternalTcp,
AuthUsername: username, AuthUsername: username,
AuthPassword: password, AuthPassword: password,

View File

@ -70,12 +70,21 @@ func (m *TunnelManager) RequestCreateTunnel(tunReq Tunnel) (Tunnel, error) {
m.mutex.Lock() m.mutex.Lock()
defer m.mutex.Unlock() defer m.mutex.Unlock()
port, err := randomOpenPort() if tunReq.TunnelPort == 0 {
if err != nil { var err error
return Tunnel{}, err tunReq.TunnelPort, err = randomOpenPort()
if err != nil {
return Tunnel{}, err
}
} else {
for _, tun := range m.db.GetTunnels() {
if tunReq.TunnelPort == tun.TunnelPort {
return Tunnel{}, errors.New("Tunnel port already in use")
}
}
} }
privKey, err := m.addToAuthorizedKeys(tunReq.Domain, port, tunReq.AllowExternalTcp, tunReq.SshKey) privKey, err := m.addToAuthorizedKeys(tunReq.Domain, tunReq.TunnelPort, tunReq.AllowExternalTcp, tunReq.SshKey)
if err != nil { if err != nil {
return Tunnel{}, err return Tunnel{}, err
} }
@ -84,7 +93,6 @@ func (m *TunnelManager) RequestCreateTunnel(tunReq Tunnel) (Tunnel, error) {
tunReq.ServerPort = m.config.SshServerPort tunReq.ServerPort = m.config.SshServerPort
tunReq.ServerPublicKey = "" tunReq.ServerPublicKey = ""
tunReq.Username = m.user.Username tunReq.Username = m.user.Username
tunReq.TunnelPort = port
tunReq.TunnelPrivateKey = privKey tunReq.TunnelPrivateKey = privKey
m.db.SetTunnel(tunReq.Domain, tunReq) m.db.SetTunnel(tunReq.Domain, tunReq)

View File

@ -89,6 +89,10 @@
<input type="text" id="domain" name="domain" required> <input type="text" id="domain" name="domain" required>
<input type="hidden" id="tunnel-owner" name="owner" value="{{$.UserId}}"> <input type="hidden" id="tunnel-owner" name="owner" value="{{$.UserId}}">
</div> </div>
<div class='input'>
<label for="tunnel-port">Tunnel Port:</label>
<input type="text" id="tunnel-port" name="tunnel-port" value="Random">
</div>
<!-- <!--
<div class='input'> <div class='input'>