-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserve.go
More file actions
83 lines (72 loc) · 2.42 KB
/
Copy pathserve.go
File metadata and controls
83 lines (72 loc) · 2.42 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
83
package agent
import (
"encoding/json"
"net"
"sync"
"time"
)
// connDeadline bounds how long a single client may take to send its request and
// read its response, so a hung or slow client cannot stall the agent.
const connDeadline = 5 * time.Second
// Server serves an Agent over a net.Listener: one request/response per connection,
// newline-free JSON framed by connection close. The Agent's own mutex serializes
// the actual work, so connections are handled concurrently only to keep a slow
// client from blocking others' accept.
type Server struct {
agent *Agent
ln net.Listener
log Logger
stop chan struct{}
once sync.Once
}
// NewServer wraps an Agent and a bound listener.
func NewServer(a *Agent, ln net.Listener, log Logger) *Server {
if log == nil {
log = nopLogger{}
}
return &Server{agent: a, ln: ln, log: log, stop: make(chan struct{})}
}
// Serve accepts connections until Stop is called (or the listener errors).
func (s *Server) Serve() {
for {
conn, err := s.ln.Accept()
if err != nil {
return // listener closed by Stop, or a fatal accept error
}
go s.handleConn(conn)
}
}
func (s *Server) handleConn(conn net.Conn) {
defer func() { _ = conn.Close() }()
_ = conn.SetDeadline(time.Now().Add(connDeadline))
// Authorize the peer BEFORE reading or acting on any request: only a process
// owned by the same user as the agent may talk to it (defense-in-depth over the
// socket's 0600 permissions). Fail-closed on any error obtaining the credential.
if err := authorizePeer(conn); err != nil {
s.log.Event("rejected_peer", nil)
_ = json.NewEncoder(conn).Encode(errResponse("unauthorized"))
return
}
var req Request
if err := json.NewDecoder(conn).Decode(&req); err != nil {
_ = json.NewEncoder(conn).Encode(errResponse("malformed request"))
return
}
resp := s.agent.Handle(req)
_ = json.NewEncoder(conn).Encode(resp)
// Free the socket promptly whenever a request leaves the agent locked — an
// explicit `lock`/shutdown, or idle/max-TTL enforcement that fired during this
// request. Serve() then returns and runAgent removes the socket, so clients fall
// back to direct-open and a fresh `pass-cli agent` can rebind, instead of hitting
// a locked-but-running agent for up to one expiry tick.
if s.agent.Locked() {
s.Stop()
}
}
// Stop closes the listener, causing Serve to return. Idempotent.
func (s *Server) Stop() {
s.once.Do(func() {
close(s.stop)
_ = s.ln.Close()
})
}