-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
89 lines (79 loc) · 2.21 KB
/
server.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
package sshgate
import (
"code.google.com/p/jeansebtr-crypto-ssh/ssh"
"log"
"strconv"
"sync"
)
type Server interface {
Listen(addr string, port int) error
}
type connections struct {
sync.RWMutex
m map[string]*tConnection
}
type tServer struct {
config *ssh.ServerConfig
connections connections
onConnection Authenticate
}
func NewServer(key []byte, cb Authenticate) (Server, error) {
server := &tServer{onConnection: cb}
server.connections.m = make(map[string]*tConnection)
server.config = &ssh.ServerConfig{
PublicKeyCallback: func(conn *ssh.ServerConn, user, algo string, pubkey []byte) bool {
return server.handleAuthKey(conn, user, algo, pubkey)
},
}
if err := server.config.SetRSAPrivateKey(key); err != nil {
return nil, err
}
return server, nil
}
func (s *tServer) Listen(addr string, port int) error {
if addr == "" {
addr = "0.0.0.0"
}
listener, err := ssh.Listen("tcp", addr+":"+strconv.Itoa(port), s.config)
if err != nil {
return err
}
for {
sConn, err := listener.Accept()
if err != nil {
log.Printf("Failed to accept incoming connection: %#v\n", err)
} else {
conn := tConnection{ServerConn: sConn, server: s}
s.addConnection(&conn)
go handleConnection(s, &conn)
}
}
}
func (s *tServer) handleAuthKey(sConn *ssh.ServerConn, user, algo string, pubkey []byte) bool {
conn := s.getConnection(sConn)
if ok, app := s.onConnection(conn, user, algo, pubkey); ok {
conn.app = app
return true
} else {
return false
}
}
func (s *tServer) addConnection(c *tConnection) {
addr := c.RemoteAddr().String()
s.connections.Lock()
s.connections.m[addr] = c
s.connections.Unlock()
}
func (s tServer) getConnection(c *ssh.ServerConn) *tConnection {
addr := c.RemoteAddr().String()
s.connections.RLock()
defer s.connections.RUnlock()
return s.connections.m[addr]
}
func (s *tServer) closeConnection(c *tConnection) {
addr := c.RemoteAddr().String()
s.connections.Lock()
delete(s.connections.m, addr)
s.connections.Unlock()
c.Close()
}