/
main.go
75 lines (67 loc) · 1.85 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"golang.org/x/crypto/ssh"
"github.com/PieterD/rlpoc/old/internal/internal/pkg/ansi"
"github.com/PieterD/rlpoc/old/internal/internal/pkg/sshserver"
)
func main() {
ctx := context.Background()
s := sshserver.NewSshServer(sshserver.SshServerConfig{
PrivateKeyPath: "id_rsa",
ListenAddr: "0.0.0.0:2222",
}, &customHandler{})
if err := s.Run(ctx); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
type customHandler struct{}
func (h *customHandler) Allow(ctx context.Context, pubKey ssh.PublicKey) error {
return nil
}
func (h *customHandler) Session(ctx context.Context, handle sshserver.Handle) error {
log.Printf("session with pubkey: %s", handle.AuthKey())
term := ansi.NewGenerator()
term.ClearScreen()
s := "press a key"
rows, cols := handle.Size()
log.Printf("rows: %d, cols: %d", rows, cols)
term.CursorPos(rows/2, cols/2-uint(len(s))/2)
term.Printf("%s", s)
for {
if err := term.Flush(handle); err != nil {
return fmt.Errorf("flushing: %w", err)
}
buf := make([]byte, 1)
if _, err := handle.Read(buf); err != nil {
return fmt.Errorf("reading: %w", err)
}
switch buf[0] {
case 3:
return fmt.Errorf("received ETX (end of text): %w", io.EOF)
case 4:
return fmt.Errorf("received EOT (end of transmission): %w", io.EOF)
}
term.ClearScreen()
term.CursorPos(1, 1)
term.BackgroundColor(ansi.Red)
term.ForegroundColor(ansi.Cyan, false)
term.Printf("cyan ")
term.ForegroundColor(ansi.Cyan, true)
term.Printf("bright cyan ")
term.ForegroundColor(ansi.Blue, false)
term.Printf("blue ")
term.ForegroundColor(ansi.Blue, true)
term.Printf("bright blue ")
term.Reset()
term.CursorPos(2, 1)
term.Printf("Extended Ascii: copyright symbol %c", 169)
term.CursorPos(3, 1)
term.Printf("read: %c (%d)", buf[0], buf[0])
}
}