-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
82 lines (76 loc) · 1.86 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
package main
import (
"flag"
"fmt"
"log"
"os"
_ "github.com/wzshiming/sshd/directstreamlocal"
_ "github.com/wzshiming/sshd/directtcp"
_ "github.com/wzshiming/sshd/session"
_ "github.com/wzshiming/sshd/streamlocalforward"
_ "github.com/wzshiming/sshd/tcpforward"
"github.com/wzshiming/sshd"
"golang.org/x/crypto/ssh"
)
var address string
var username string
var password string
var authorized string
var hostkey string
func init() {
flag.StringVar(&address, "a", ":22", "listen on the address")
flag.StringVar(&username, "u", "", "username")
flag.StringVar(&password, "p", "", "password")
flag.StringVar(&authorized, "f", "", "authorized file")
flag.StringVar(&hostkey, "h", "", "hostkey file")
flag.Parse()
}
func main() {
logger := log.New(os.Stderr, "[sshd] ", log.LstdFlags)
svc := sshd.NewServer()
svc.Logger = logger
if hostkey != "" {
key, err := sshd.GetHostkey(hostkey)
if err != nil {
logger.Println(err)
return
}
svc.ServerConfig.AddHostKey(key)
} else {
key, err := sshd.RandomHostkey()
if err != nil {
logger.Println(err)
return
}
svc.ServerConfig.AddHostKey(key)
}
if username != "" {
svc.ServerConfig.PasswordCallback = func(conn ssh.ConnMetadata, pwd []byte) (*ssh.Permissions, error) {
if conn.User() == username && password == string(pwd) {
return nil, nil
}
return nil, fmt.Errorf("denied")
}
}
if authorized != "" {
keys, err := sshd.GetAuthorizedFile(authorized)
if err != nil {
logger.Println(err)
return
}
svc.ServerConfig.PublicKeyCallback = func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
ok, _ := keys.Allow(key)
if ok {
return nil, nil
}
return nil, fmt.Errorf("denied")
}
}
if username == "" && authorized == "" {
svc.ServerConfig.NoClientAuth = true
}
err := svc.ListenAndServe("tcp", address)
if err != nil {
logger.Println(err)
}
}