/
main.go
71 lines (65 loc) · 1.49 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
package main
import (
"context"
_ "embed"
"fmt"
"github.com/perbu/sshproxy/proxy"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"os"
"os/signal"
"sync"
"syscall"
)
//go:embed id_rsa
var privKey []byte
func main() {
err := realMain()
if err != nil {
log.Fatal(err)
}
}
func realMain() error {
log.SetLevel(log.TraceLevel)
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
log.Info("sshproxy starting up")
signer, err := GetPrivateKey(privKey)
if err != nil {
return fmt.Errorf("GetPrivateKey: %w", err)
}
log.Debug("private key (signer) loaded successfully")
ctx, cancel := context.WithCancel(context.Background())
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
err := proxy.Run(ctx, proxy.MkConfig("localhost:4222", signer))
log.Info("Proxy shut down")
if err != nil {
log.Error("sshd: ", err)
}
wg.Done()
}()
<-sigChan
log.Info("signal caught")
cancel()
wg.Wait()
log.Info("Program exiting")
return nil
}
// GetPrivateKey reads a private key.
// It will cause a panic if we can't read or parse the key.
func GetPrivateKey(pemBytes []byte) (signer ssh.Signer, err error) {
privKey, err := ssh.ParseRawPrivateKey(pemBytes)
if err != nil {
return signer, fmt.Errorf("could not parse key: %w", err)
}
signer, err = ssh.NewSignerFromKey(privKey)
if err != nil {
return signer, fmt.Errorf("could not create a signer: %w", err)
}
return signer, nil
}