Skip to content

Commit 668d7e7

Browse files
committed
feat(domains): return generated URL to ssh client
1 parent 29972fd commit 668d7e7

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

server/server.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package vexserver
33
import (
44
"net/http"
55
"net/http/httputil"
6-
// "net/http/httputil"
76
"net/url"
87
"strconv"
98
"strings"

server/ssh_server.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ type SSHServer struct {
2121
isRunning bool
2222
closer sync.Once
2323
clients map[string]Client
24+
domain string
2425
}
2526

2627
type Client struct {
2728
ID string
2829
net.Conn
2930
*ssh.ServerConn
3031
Listeners map[string]net.Listener
32+
Channel ssh.NewChannel
3133
Addr string
3234
Port uint32
3335
}
@@ -68,6 +70,7 @@ func (s *SSHServer) listen(addr string, domain string) error {
6870
log.Fatalf("Failed to listen on %s (%s)", addr, err)
6971
}
7072
s.listener = listener
73+
s.domain = domain
7174

7275
log.Printf("SSH server listening on %s, generating urls on *.%s ...\n", addr, domain)
7376

@@ -78,16 +81,17 @@ func (s *SSHServer) listen(addr string, domain string) error {
7881
continue
7982
}
8083

81-
sshConn, _, reqs, err := ssh.NewServerConn(tcpConn, s.config)
84+
sshConn, chans, reqs, err := ssh.NewServerConn(tcpConn, s.config)
8285
if err != nil {
8386
log.Printf("Failed to handshake (%s)\n", err)
8487
continue
8588
}
8689

87-
client := &Client{randID(), tcpConn, sshConn, make(map[string]net.Listener), "", 0}
90+
client := &Client{randID(), tcpConn, sshConn, make(map[string]net.Listener), nil, "", 0}
8891
log.Printf("New SSH connection from %s (%s)", sshConn.RemoteAddr(), sshConn.ClientVersion())
8992

9093
go s.handleRequests(client, reqs)
94+
go s.handleChannels(client, chans)
9195
}
9296
}
9397

@@ -111,9 +115,15 @@ func (s *SSHServer) Wait() error {
111115
return <-s.running
112116
}
113117

118+
func (s *SSHServer) handleChannels(client *Client, chans <-chan ssh.NewChannel) {
119+
for newChannel := range chans {
120+
client.Channel = newChannel
121+
}
122+
}
123+
114124
func (s *SSHServer) handleRequests(client *Client, reqs <-chan *ssh.Request) {
115125
for req := range reqs {
116-
client.Conn.SetDeadline(time.Now().Add(5 * time.Minute))
126+
client.Conn.SetDeadline(time.Now().Add(2 * time.Minute))
117127

118128
log.Printf("[%s] Out of band request: %v %v", client.ID, req.Type, req.WantReply)
119129

@@ -131,6 +141,15 @@ func (s *SSHServer) handleRequests(client *Client, reqs <-chan *ssh.Request) {
131141
client.Listeners[bindinfo.Bound] = listener
132142
s.clients[client.ID] = *client
133143

144+
channelConn, _, err := client.Channel.Accept()
145+
if err != nil {
146+
log.Printf("Could not accept channel (%s)", err)
147+
return
148+
}
149+
150+
generatedURL := "http://" + client.ID + "." + s.domain
151+
io.WriteString(channelConn, "[vexd] Generated URL: "+generatedURL)
152+
134153
go handleListener(client, bindinfo, listener)
135154
continue
136155
// client.Conn.Close()

0 commit comments

Comments
 (0)