Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
82 lines (75 loc) · 2.16 KB
/
Copy pathmain.go
File metadata and controls
82 lines (75 loc) · 2.16 KB
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
package main
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"syscall"
"unsafe"
"github.com/gliderlabs/ssh"
"github.com/go-ini/ini"
"github.com/kr/pty"
gossh "golang.org/x/crypto/ssh"
)
func setWinsize(f *os.File, w, h int) {
syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), uintptr(syscall.TIOCSWINSZ),
uintptr(unsafe.Pointer(&struct{ h, w, x, y uint16 }{uint16(h), uint16(w), 0, 0})))
}
func main() {
cfg, err := ini.Load("config.ini")
if err != nil {
fmt.Printf("Fail to read file: %v", err)
os.Exit(1)
}
ssh.Handle(func(s ssh.Session) {
if s.User() == "gui" {
pwd, err := os.Executable()
if err != nil {
panic(err)
}
authorizedKey := gossh.MarshalAuthorizedKey(s.PublicKey())
/*app := tview.NewApplication()
modal := tview.NewModal().
SetText("Akmey recognizes the key : " + string(authorizedKey)).
AddButtons([]string{"Ok", "No"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Ok" {
app.Stop()
}
})
if err := app.SetRoot(modal, false).SetFocus(modal).Run(); err != nil {
panic(err)
}*/
cmd := exec.Command(cfg.Section("ssh").Key("uiexec").String(), string(authorizedKey))
ptyReq, winCh, isPty := s.Pty()
if isPty {
cmd.Env = append(cmd.Env, fmt.Sprintf("TERM=%s", ptyReq.Term))
cmd.Env = append(cmd.Env, fmt.Sprintf("PWD=%s", pwd))
f, err := pty.Start(cmd)
if err != nil {
panic(err)
}
go func() {
for win := range winCh {
setWinsize(f, win.Width, win.Height)
}
}()
go func() {
io.Copy(f, s) // stdin
}()
io.Copy(s, f) // stdout
} else {
io.WriteString(s, "No PTY requested.\n")
s.Exit(1)
}
} else {
io.WriteString(s, "Wrong user, i accept `api` and `gui`.\n")
}
})
publicKeyOption := ssh.PublicKeyAuth(func(ctx ssh.Context, key ssh.PublicKey) bool {
return true // allow all keys, or use ssh.KeysEqual() to compare against known keys
})
log.Println("starting ssh server on address : " + cfg.Section("ssh").Key("listen").String())
log.Fatal(ssh.ListenAndServe(cfg.Section("ssh").Key("listen").String(), nil, ssh.HostKeyFile(cfg.Section("ssh").Key("hostkey").String()), publicKeyOption))
}