Skip to content

Commit af5e3c2

Browse files
committed
SSH server works!
1 parent fbb02ec commit af5e3c2

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

agent/agent.go renamed to agent/server.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ package agent
22

33
import (
44
"context"
5+
"crypto/rand"
6+
"crypto/rsa"
57
"errors"
8+
"fmt"
69
"io"
10+
"net"
711
"sync"
812
"time"
913

@@ -47,15 +51,37 @@ type server struct {
4751

4852
func (s *server) init(ctx context.Context) {
4953
forwardHandler := &ssh.ForwardedTCPHandler{}
54+
key, err := rsa.GenerateKey(rand.Reader, 2048)
55+
if err != nil {
56+
panic(err)
57+
}
58+
signer, err := gossh.NewSignerFromKey(key)
59+
if err != nil {
60+
panic(err)
61+
}
5062
s.sshServer = &ssh.Server{
63+
ChannelHandlers: ssh.DefaultChannelHandlers,
64+
ConnectionFailedCallback: func(conn net.Conn, err error) {
65+
fmt.Printf("Conn failed: %s\n", err)
66+
},
67+
Handler: func(s ssh.Session) {
68+
fmt.Printf("WE GOT %q %q\n", s.User(), s.RawCommand())
69+
},
70+
HostSigners: []ssh.Signer{signer},
5171
LocalPortForwardingCallback: func(ctx ssh.Context, destinationHost string, destinationPort uint32) bool {
72+
// Allow local port forwarding all!
73+
return true
74+
},
75+
PtyCallback: func(ctx ssh.Context, pty ssh.Pty) bool {
5276
return false
5377
},
5478
ReversePortForwardingCallback: func(ctx ssh.Context, bindHost string, bindPort uint32) bool {
55-
return false
79+
// Allow revere port forwarding all!
80+
return true
5681
},
57-
PtyCallback: func(ctx ssh.Context, pty ssh.Pty) bool {
58-
return false
82+
RequestHandlers: map[string]ssh.RequestHandler{
83+
"tcpip-forward": forwardHandler.HandleSSHRequest,
84+
"cancel-tcpip-forward": forwardHandler.HandleSSHRequest,
5985
},
6086
ServerConfigCallback: func(ctx ssh.Context) *gossh.ServerConfig {
6187
return &gossh.ServerConfig{
@@ -65,13 +91,12 @@ func (s *server) init(ctx context.Context) {
6591
// encrypted. If possible, we'd disable encryption entirely here.
6692
Ciphers: []string{"arcfour"},
6793
},
94+
PublicKeyCallback: func(conn gossh.ConnMetadata, key gossh.PublicKey) (*gossh.Permissions, error) {
95+
return &gossh.Permissions{}, nil
96+
},
6897
NoClientAuth: true,
6998
}
7099
},
71-
RequestHandlers: map[string]ssh.RequestHandler{
72-
"tcpip-forward": forwardHandler.HandleSSHRequest,
73-
"cancel-tcpip-forward": forwardHandler.HandleSSHRequest,
74-
},
75100
}
76101

77102
go s.run(ctx)

agent/agent_test.go renamed to agent/server_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package agent_test
22

33
import (
44
"context"
5-
"net"
65
"os"
76
"testing"
87

@@ -14,9 +13,14 @@ import (
1413
"github.com/coder/coder/provisionersdk"
1514
"github.com/pion/webrtc/v3"
1615
"github.com/stretchr/testify/require"
16+
"go.uber.org/goleak"
1717
"golang.org/x/crypto/ssh"
1818
)
1919

20+
func TestMain(m *testing.M) {
21+
goleak.VerifyTestMain(m)
22+
}
23+
2024
func TestAgent(t *testing.T) {
2125
t.Run("asd", func(t *testing.T) {
2226
ctx := context.Background()
@@ -45,9 +49,10 @@ func TestAgent(t *testing.T) {
4549
require.NoError(t, err)
4650
sshConn, channels, requests, err := ssh.NewClientConn(channel.NetConn(), "localhost:22", &ssh.ClientConfig{
4751
User: "kyle",
48-
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
49-
return nil
52+
Config: ssh.Config{
53+
Ciphers: []string{"arcfour"},
5054
},
55+
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
5156
})
5257
require.NoError(t, err)
5358
sshClient := ssh.NewClient(sshConn, channels, requests)

peer/channel.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ package peer
33
import (
44
"bufio"
55
"context"
6-
"fmt"
76
"io"
87
"net"
9-
"runtime/debug"
108
"sync"
119
"time"
1210

@@ -205,7 +203,6 @@ func (c *Channel) Read(bytes []byte) (int, error) {
205203
if c.isClosed() {
206204
return 0, c.closeError
207205
}
208-
debug.PrintStack()
209206
// An EOF always occurs when the connection is closed.
210207
// Alternative close errors will occur first if an unexpected
211208
// close has occurred.
@@ -253,8 +250,6 @@ func (c *Channel) Write(bytes []byte) (n int, err error) {
253250
// See: https://github.com/pion/sctp/issues/181
254251
time.Sleep(time.Microsecond)
255252

256-
fmt.Printf("Writing %d\n", len(bytes))
257-
258253
return c.rwc.Write(bytes)
259254
}
260255

0 commit comments

Comments
 (0)