Skip to content

Commit 06b82d1

Browse files
authored
add seed to random number generator; log proxy error (#83)
* add seed to random number generator * use a single function to generate tunnel address * smol fix * log proxy error
1 parent 29293d7 commit 06b82d1

File tree

6 files changed

+36
-26
lines changed

6 files changed

+36
-26
lines changed

tunnel/internal/client/config/config.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ import (
1515
)
1616

1717
type Tunnel struct {
18-
Name string `yaml:"name"`
19-
Subdomain string `yaml:"subdomain"`
20-
Port int `yaml:"port"`
21-
Host string `yaml:"host"`
22-
Type constants.ConnectionType `yaml:"type"`
18+
Name string `yaml:"name"`
19+
Subdomain string `yaml:"subdomain"`
20+
Port int `yaml:"port"`
21+
Host string `yaml:"host"`
22+
Type constants.ConnectionType `yaml:"type"`
23+
RemotePort int
2324
}
2425

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

100-
func (c *ClientConfig) GetTcpTunnelAddr(port int) string {
101+
func (c *ClientConfig) GetTcpTunnelAddr() string {
101102
split := strings.Split(c.TunnelUrl, ":")
102-
return split[0] + ":" + fmt.Sprint(port)
103+
return split[0] + ":" + fmt.Sprint(c.Tunnel.RemotePort)
104+
}
105+
106+
func (c *ClientConfig) GetTunnelAddr() string {
107+
if c.Tunnel.Type == constants.Http {
108+
return c.GetHttpTunnelAddr()
109+
}
110+
return c.GetTcpTunnelAddr()
103111
}
104112

105113
func (c *ClientConfig) GetServerAddr() string {

tunnel/internal/client/ssh/ssh.go

+12-14
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ func (s *SshClient) startListenerForClient() error {
9999
}
100100

101101
sshClient, err := ssh.Dial("tcp", s.config.SshUrl, sshConfig)
102+
102103
if err != nil {
104+
if s.config.Debug {
105+
s.log.Error("failed to connect to ssh server", "error", err)
106+
}
103107
return err
104108
}
105109
defer sshClient.Close()
@@ -130,21 +134,15 @@ func (s *SshClient) startListenerForClient() error {
130134
return fmt.Errorf("failed to listen on remote endpoint")
131135
}
132136

137+
s.config.Tunnel.RemotePort = remotePort
138+
133139
defer s.listener.Close()
134140

135-
if tunnelType == constants.Http {
136-
fmt.Printf(
137-
"🎉 Tunnel connected: %s -> 🌐 -> %s\n",
138-
s.config.GetHttpTunnelAddr(),
139-
s.config.Tunnel.GetLocalAddr(),
140-
)
141-
} else {
142-
fmt.Printf(
143-
"🎉 Tunnel connected: %s -> 🌐 -> %s\n",
144-
s.config.GetTcpTunnelAddr(remotePort),
145-
s.config.Tunnel.GetLocalAddr(),
146-
)
147-
}
141+
fmt.Printf(
142+
"🎉 Tunnel connected: %s -> 🌐 -> %s\n",
143+
s.config.GetTunnelAddr(),
144+
s.config.Tunnel.GetLocalAddr(),
145+
)
148146

149147
for {
150148
// Accept incoming connections on the remote port
@@ -348,7 +346,7 @@ func (s *SshClient) Shutdown(ctx context.Context) error {
348346
if err != nil {
349347
return err
350348
}
351-
s.log.Info("stopping tunnel client server")
349+
s.log.Info("stopping tunnel connection", "address", s.config.GetTunnelAddr())
352350
return nil
353351
}
354352

tunnel/internal/server/config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func new() *Config {
6363

6464
domain := os.Getenv("PORTR_DOMAIN")
6565
if domain == "" {
66-
domain = "localhost:8000"
66+
domain = "localhost:8001"
6767
}
6868

6969
dbUrl := os.Getenv("PORTR_DB_URL")

tunnel/internal/server/proxy/proxy.go

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func unregisteredSubdomainError(w http.ResponseWriter, subdomain string) {
8181
}
8282

8383
func (p *Proxy) ErrHandle(res http.ResponseWriter, req *http.Request, err error) {
84+
p.log.Error("proxy error", "error", err)
8485
p.RemoveRoute(p.config.ExtractSubdomain(req.Host))
8586
unregisteredSubdomainError(res, p.config.ExtractSubdomain(req.Host))
8687
}

tunnel/internal/server/ssh/sshd.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ func (s *SshServer) Start() {
8484
s.log.Error("failed to add port to connection", "error", err)
8585
return false
8686
}
87-
}
88-
89-
if reservedConnection.Type == string(constants.Http) {
87+
} else {
9088
err = s.proxy.AddRoute(*reservedConnection.Subdomain, proxyTarget)
9189
if err != nil {
9290
s.log.Error("failed to add route", "error", err)

tunnel/internal/utils/random.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package utils
22

3-
import "math/rand"
3+
import (
4+
"math/rand"
5+
"time"
6+
)
47

58
func GenerateRandomNumbers(start, end, limit int) []int {
9+
rand := rand.New(rand.NewSource(time.Now().UnixNano()))
10+
611
randomNumbers := make([]int, limit)
712
for i := range limit {
813
randomNumbers[i] = rand.Intn(end-start) + start

0 commit comments

Comments
 (0)