-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (61 loc) · 1.72 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
package main
import (
"flag"
"github.com/gliderlabs/ssh"
"github.com/jonasbak/yasp/tui"
"log"
"net/http"
"os"
"regexp"
"time"
)
var runTui = flag.Bool("tui", false, "run the client TUI")
var colorRegex = regexp.MustCompile(`\[(.+?)\]`)
func main() {
flag.Parse()
if *runTui {
tui.Run()
return
}
keyLocation := os.Getenv("KEY_LOCATION")
publicKeyHandler := getPublicKeyHandler()
passwordHandler := getPasswordHandler()
log.Printf("using public key auth: %t", publicKeyHandler != nil)
log.Printf("using password auth: %t", passwordHandler != nil)
if publicKeyHandler == nil && passwordHandler == nil {
log.Println("WARNING: not using auth")
}
forwardHandler := &forwardedTCPHandler{}
s := &http.Server{
Addr: ":8080",
Handler: http.HandlerFunc(forwardHandler.httpMuxHandler),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
go func() {
log.Println("starting http server on port 8080...")
log.Fatal(s.ListenAndServe())
}()
server := ssh.Server{
Addr: ":2222",
Handler: ssh.Handler(forwardHandler.sshHandler),
RequestHandlers: map[string]ssh.RequestHandler{
"tcpip-forward": forwardHandler.HandleSSHRequest,
"cancel-tcpip-forward": forwardHandler.HandleSSHRequest,
},
PublicKeyHandler: publicKeyHandler,
PasswordHandler: passwordHandler,
}
if keyLocation == "" {
log.Println("No key location specified, creating new...")
} else {
if _, err := os.Stat(keyLocation); err == nil {
server.SetOption(ssh.HostKeyFile(keyLocation))
} else {
log.Println("Key location specified but no key found, creating new...")
}
}
log.Println("starting ssh server on port 2222...")
log.Fatal(server.ListenAndServe())
}