-
Notifications
You must be signed in to change notification settings - Fork 19
/
listen.go
236 lines (203 loc) · 6.02 KB
/
listen.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package sshego
import (
"context"
"fmt"
"log"
"net"
"sync"
"time"
"github.com/glycerine/sshego/xendor/github.com/glycerine/xcryptossh"
)
// BasicServer configures a simple embedded sshd server
// that only expects RSA key (or other) based authentication,
// and doesn't expect TOTP or passphase. This makes
// it suitable for using with unattended systems / to
// replace a TLS server.
type BasicServer struct {
cfg *SshegoConfig
}
// NewBasicServer in
// listen.go provides net.Listen() compatibility
// for running an embedded sshd. It refactors
// server.go's Start() into Listen() and Accept().
func NewBasicServer(cfg *SshegoConfig) *BasicServer {
cfg.NewEsshd()
return &BasicServer{cfg: cfg}
}
// Close releases all server port bindings.
func (b *BasicServer) Close() error {
// In case we haven't yet actually started, close Done too.
// Multiple Close() calls on Halter are fine.
b.cfg.Esshd.Halt.MarkDone()
return b.cfg.Esshd.Stop()
}
// Address satisfies the net.Addr interface, which
// BasicListener.Addr() returns.
type BasicAddress struct {
addr string
}
// Network returns the name of the network, "sshego"
func (a *BasicAddress) Network() string {
return "sshego"
}
// String returns the string form of the address.
func (a *BasicAddress) String() string {
return a.addr
}
// BasicListener satifies the net.Listener interface
type BasicListener struct {
bs *BasicServer
addr BasicAddress
esshd *Esshd
dom string
lsn net.Listener
attempt uint64
halt ssh.Halter
mut sync.Mutex
}
// Addr returns the listener's network address.
func (b *BasicListener) Addr() net.Addr {
return &BasicAddress{
addr: b.bs.cfg.EmbeddedSSHd.Addr,
}
}
// Close closes the listener.
// Any blocked Accept operations will be unblocked and return errors.
func (b *BasicListener) Close() error {
// in case we haven't yet actually started, close the Done
// channel too.
// global shutdown: works but not what we want!
// b.bs.cfg.Esshd.Halt.MarkDone()
// return b.bs.cfg.Esshd.Stop()
b.halt.MarkDone()
b.halt.RequestStop()
return nil
}
// Listen announces on the local network address laddr.
// The syntax of laddr is "host:port", like "127.0.0.1:2222".
// We listen on a TCP port.
func (bs *BasicServer) Listen(laddr string) (*BasicListener, error) {
bs.cfg.EmbeddedSSHd.Addr = laddr
err := bs.cfg.EmbeddedSSHd.ParseAddr()
if err != nil {
return nil, err
}
return bs.cfg.Esshd.Listen(bs)
}
// Essh add-on methods
// Listen and Accept support BasicServer functionality.
// Together, Listen() then Accept() replace Start().
func (e *Esshd) Listen(bs *BasicServer) (*BasicListener, error) {
log.Printf("Esshd.Listen() called. %s", SourceVersion())
p("about to listen on %v", e.cfg.EmbeddedSSHd.Addr)
// Once a ServerConfig has been configured, connections can be
// accepted.
domain := "tcp"
if e.cfg.EmbeddedSSHd.UnixDomainPath != "" {
domain = "unix"
}
p("info: Essh.Listen() in listen.go: listening on "+
"domain '%s', addr: '%s'", domain, e.cfg.EmbeddedSSHd.Addr)
listener, err := net.Listen(domain, e.cfg.EmbeddedSSHd.Addr)
if err != nil {
return nil, fmt.Errorf("failed to listen for connection on %v: %v",
e.cfg.EmbeddedSSHd.Addr, err)
}
return &BasicListener{
bs: bs,
esshd: e,
dom: domain,
lsn: listener,
halt: *ssh.NewHalter(),
}, nil
}
// Accept and Listen support BasicServer functionality.
// Accept waits for and returns the next connection to the listener.
func (b *BasicListener) Accept(ctx context.Context) (net.Conn, error) {
p("Accept for BasicListener called.")
e := b.esshd
// most of the auth state is per user, so it has
// to wait until we have a login and a
// username at hand.
a := NewAuthState(nil)
// we copy the host key here to avoid a data race later.
e.cfg.HostDb.saveMut.Lock()
a.HostKey = e.cfg.HostDb.HostSshSigner
e.cfg.HostDb.saveMut.Unlock()
// don't Close()! We may want to re-use this listener
// for another Accept().
// defer b.halt.MarkDone()
for {
// TODO: fail2ban: notice bad login IPs and if too many, block the IP.
timeoutMillisec := 1000
err := b.lsn.(*net.TCPListener).
SetDeadline(time.Now().
Add(time.Duration(timeoutMillisec) * time.Millisecond))
panicOn(err)
nConn, err := b.lsn.Accept()
p("back from Accept, err = %v", err)
if err != nil {
// simple timeout, check if stop requested
// 'accept tcp 127.0.0.1:54796: i/o timeout'
// p("simple timeout err: '%v'", err)
select {
case <-e.Halt.ReqStopChan():
p("e.Halt.ReqStop detected")
return nil, fmt.Errorf("shutting down")
case <-b.halt.ReqStopChan():
p("b.halt.ReqStop detected")
return nil, fmt.Errorf("shutting down")
default:
// no stop request, keep looping
//p("not stop request, keep looping")
}
continue
}
p("info: Essh.Accept() in listen.go: accepted new connection on "+
"domain '%s', addr: '%s'", b.dom, e.cfg.EmbeddedSSHd.Addr)
attempt := NewPerAttempt(a, e.cfg)
attempt.SetupAuthRequirements()
// need to get the direct-tcp connection back directly.
ca := &ConnectionAlert{
PortOne: make(chan ssh.Channel),
ShutDown: b.esshd.Halt.ReqStopChan(),
}
err = attempt.PerConnection(ctx, nConn, ca)
if err != nil {
return nil, err
}
select {
case <-b.halt.ReqStopChan():
return nil, fmt.Errorf("shutting down")
case <-b.esshd.Halt.ReqStopChan():
return nil, fmt.Errorf("shutting down")
case sshc := <-ca.PortOne:
return &withLocalAddr{sshc}, nil
}
} // end for
}
// withLocalAddr wraps an ssh.Channel to
// implements the net.Conn missing methods
type withLocalAddr struct {
ssh.Channel
}
func (w *withLocalAddr) LocalAddr() net.Addr {
panic("not implemented")
return &BasicAddress{}
}
func (w *withLocalAddr) RemoteAddr() net.Addr {
panic("not implemented")
return &BasicAddress{}
}
func (w *withLocalAddr) SetDeadline(t time.Time) error {
panic("not implemented")
return nil
}
func (w *withLocalAddr) SetReadDeadline(t time.Time) error {
panic("not implemented")
return nil
}
func (w *withLocalAddr) SetWriteDeadline(t time.Time) error {
panic("not implemented")
return nil
}