-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
110 lines (83 loc) · 1.97 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"sync"
"github.com/gliderlabs/ssh"
"github.com/teris-io/shortid"
gossh "golang.org/x/crypto/ssh"
)
const privateKeyPath = "keys/test_id_rsa"
var clients sync.Map
type HTTPHandler struct{}
func (h *HTTPHandler) handleWebhook(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
ch, ok := clients.Load(id)
if !ok {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("id not found"))
return
}
b, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, "error reading request body")
log.Printf("error: %v", err)
return
}
defer r.Body.Close()
ch.(chan string) <- string(b)
}
func startHTTPServer() error {
router := http.NewServeMux()
handler := &HTTPHandler{}
router.HandleFunc("/{id}/*", handler.handleWebhook)
return http.ListenAndServe(":5000", router)
}
func startSSHServer() error {
generateMockSSHKeys()
sshPort := ":2222"
handler := &SSHHandler{}
server := &ssh.Server{
Addr: sshPort,
Handler: handler.handleSSHSession,
ServerConfigCallback: func(ctx ssh.Context) *gossh.ServerConfig {
cfg := &gossh.ServerConfig{
ServerVersion: "SSH-2.0-sendit",
}
cfg.Ciphers = []string{"[email protected]"}
return cfg
},
PublicKeyHandler: func(ctx ssh.Context, key ssh.PublicKey) bool {
return true
},
}
b, err := os.ReadFile(privateKeyPath)
if err != nil {
log.Fatal(err)
}
signer, err := gossh.ParsePrivateKey(b)
if err != nil {
log.Fatal(err)
}
server.AddHostKey(signer)
return server.ListenAndServe()
}
func main() {
go startSSHServer()
startHTTPServer()
}
type SSHHandler struct{}
func (h *SSHHandler) handleSSHSession(session ssh.Session) {
id := shortid.MustGenerate()
webhookURL := "http://hookpipe.com/" + id
session.Write([]byte(webhookURL + "\n"))
respChan := make(chan string)
clients.Store(id, respChan)
for data := range respChan {
session.Write([]byte(data + "\n"))
}
}