Skip to content

Commit

Permalink
add seed to random number generator; log proxy error (#83)
Browse files Browse the repository at this point in the history
* add seed to random number generator

* use a single function to generate tunnel address

* smol fix

* log proxy error
  • Loading branch information
amalshaji authored Aug 15, 2024
1 parent 29293d7 commit 06b82d1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 26 deletions.
22 changes: 15 additions & 7 deletions tunnel/internal/client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import (
)

type Tunnel struct {
Name string `yaml:"name"`
Subdomain string `yaml:"subdomain"`
Port int `yaml:"port"`
Host string `yaml:"host"`
Type constants.ConnectionType `yaml:"type"`
Name string `yaml:"name"`
Subdomain string `yaml:"subdomain"`
Port int `yaml:"port"`
Host string `yaml:"host"`
Type constants.ConnectionType `yaml:"type"`
RemotePort int
}

func (t *Tunnel) SetDefaults() {
Expand Down Expand Up @@ -97,9 +98,16 @@ func (c *ClientConfig) GetHttpTunnelAddr() string {
return protocol + "://" + c.Tunnel.Subdomain + "." + c.TunnelUrl
}

func (c *ClientConfig) GetTcpTunnelAddr(port int) string {
func (c *ClientConfig) GetTcpTunnelAddr() string {
split := strings.Split(c.TunnelUrl, ":")
return split[0] + ":" + fmt.Sprint(port)
return split[0] + ":" + fmt.Sprint(c.Tunnel.RemotePort)
}

func (c *ClientConfig) GetTunnelAddr() string {
if c.Tunnel.Type == constants.Http {
return c.GetHttpTunnelAddr()
}
return c.GetTcpTunnelAddr()
}

func (c *ClientConfig) GetServerAddr() string {
Expand Down
26 changes: 12 additions & 14 deletions tunnel/internal/client/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ func (s *SshClient) startListenerForClient() error {
}

sshClient, err := ssh.Dial("tcp", s.config.SshUrl, sshConfig)

if err != nil {
if s.config.Debug {
s.log.Error("failed to connect to ssh server", "error", err)
}
return err
}
defer sshClient.Close()
Expand Down Expand Up @@ -130,21 +134,15 @@ func (s *SshClient) startListenerForClient() error {
return fmt.Errorf("failed to listen on remote endpoint")
}

s.config.Tunnel.RemotePort = remotePort

defer s.listener.Close()

if tunnelType == constants.Http {
fmt.Printf(
"🎉 Tunnel connected: %s -> 🌐 -> %s\n",
s.config.GetHttpTunnelAddr(),
s.config.Tunnel.GetLocalAddr(),
)
} else {
fmt.Printf(
"🎉 Tunnel connected: %s -> 🌐 -> %s\n",
s.config.GetTcpTunnelAddr(remotePort),
s.config.Tunnel.GetLocalAddr(),
)
}
fmt.Printf(
"🎉 Tunnel connected: %s -> 🌐 -> %s\n",
s.config.GetTunnelAddr(),
s.config.Tunnel.GetLocalAddr(),
)

for {
// Accept incoming connections on the remote port
Expand Down Expand Up @@ -348,7 +346,7 @@ func (s *SshClient) Shutdown(ctx context.Context) error {
if err != nil {
return err
}
s.log.Info("stopping tunnel client server")
s.log.Info("stopping tunnel connection", "address", s.config.GetTunnelAddr())
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion tunnel/internal/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func new() *Config {

domain := os.Getenv("PORTR_DOMAIN")
if domain == "" {
domain = "localhost:8000"
domain = "localhost:8001"
}

dbUrl := os.Getenv("PORTR_DB_URL")
Expand Down
1 change: 1 addition & 0 deletions tunnel/internal/server/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func unregisteredSubdomainError(w http.ResponseWriter, subdomain string) {
}

func (p *Proxy) ErrHandle(res http.ResponseWriter, req *http.Request, err error) {
p.log.Error("proxy error", "error", err)
p.RemoveRoute(p.config.ExtractSubdomain(req.Host))
unregisteredSubdomainError(res, p.config.ExtractSubdomain(req.Host))
}
Expand Down
4 changes: 1 addition & 3 deletions tunnel/internal/server/ssh/sshd.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ func (s *SshServer) Start() {
s.log.Error("failed to add port to connection", "error", err)
return false
}
}

if reservedConnection.Type == string(constants.Http) {
} else {
err = s.proxy.AddRoute(*reservedConnection.Subdomain, proxyTarget)
if err != nil {
s.log.Error("failed to add route", "error", err)
Expand Down
7 changes: 6 additions & 1 deletion tunnel/internal/utils/random.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package utils

import "math/rand"
import (
"math/rand"
"time"
)

func GenerateRandomNumbers(start, end, limit int) []int {
rand := rand.New(rand.NewSource(time.Now().UnixNano()))

randomNumbers := make([]int, limit)
for i := range limit {
randomNumbers[i] = rand.Intn(end-start) + start
Expand Down

0 comments on commit 06b82d1

Please sign in to comment.