-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathipc.go
More file actions
178 lines (146 loc) · 3.34 KB
/
Copy pathipc.go
File metadata and controls
178 lines (146 loc) · 3.34 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
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
package main
import (
"bufio"
"encoding/json"
"fmt"
"net"
"os"
"path/filepath"
"sync"
"time"
)
type IPCMessage struct {
Type string `json:"type"` // "READY", "CONNECT", "BIND", and debug/transport events
FD int `json:"fd"`
Port int `json:"port"`
Addr string `json:"addr"`
PID int `json:"pid"`
Detail string `json:"detail"`
}
type IPCServer struct {
listener net.Listener
socketPath string
msgChan chan IPCMessage
mu sync.Mutex
nextSubID int
subs map[int]chan IPCMessage
}
func NewIPCServer() (*IPCServer, error) {
// Create socket path in temp directory
socketPath := filepath.Join(os.TempDir(), fmt.Sprintf("wrapguard-%d.sock", os.Getpid()))
// Remove existing socket if it exists
os.Remove(socketPath)
listener, err := net.Listen("unix", socketPath)
if err != nil {
return nil, fmt.Errorf("failed to create IPC socket: %w", err)
}
server := &IPCServer{
listener: listener,
socketPath: socketPath,
msgChan: make(chan IPCMessage, 100),
subs: make(map[int]chan IPCMessage),
}
// Start accepting connections
go server.acceptConnections()
return server, nil
}
func (s *IPCServer) acceptConnections() {
for {
conn, err := s.listener.Accept()
if err != nil {
// Server is shutting down
break
}
// Handle connection in background
go s.handleConnection(conn)
}
}
func (s *IPCServer) handleConnection(conn net.Conn) {
defer conn.Close()
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
line := scanner.Text()
var msg IPCMessage
if err := json.Unmarshal([]byte(line), &msg); err != nil {
logger.Warnf("IPC failed to parse message: %v", err)
continue
}
s.dispatchMessage(msg)
}
}
func (s *IPCServer) dispatchMessage(msg IPCMessage) {
select {
case s.msgChan <- msg:
default:
logger.Warnf("IPC message channel full, dropping %s from pid %d", msg.Type, msg.PID)
}
s.mu.Lock()
defer s.mu.Unlock()
for id, ch := range s.subs {
select {
case ch <- msg:
default:
logger.Warnf("IPC subscriber %d channel full, dropping %s from pid %d", id, msg.Type, msg.PID)
}
}
}
func (s *IPCServer) SocketPath() string {
return s.socketPath
}
func (s *IPCServer) MessageChan() <-chan IPCMessage {
return s.msgChan
}
func (s *IPCServer) Subscribe() (int, <-chan IPCMessage) {
s.mu.Lock()
defer s.mu.Unlock()
id := s.nextSubID
s.nextSubID++
ch := make(chan IPCMessage, 32)
s.subs[id] = ch
return id, ch
}
func (s *IPCServer) Unsubscribe(id int) {
s.mu.Lock()
defer s.mu.Unlock()
ch, ok := s.subs[id]
if !ok {
return
}
delete(s.subs, id)
close(ch)
}
func (s *IPCServer) WaitForMessageType(msgType string, timeout time.Duration) (IPCMessage, error) {
subID, ch := s.Subscribe()
defer s.Unsubscribe(subID)
timer := time.NewTimer(timeout)
defer timer.Stop()
for {
select {
case msg, ok := <-ch:
if !ok {
return IPCMessage{}, fmt.Errorf("ipc subscriber closed while waiting for %s", msgType)
}
if msg.Type == msgType {
return msg, nil
}
case <-timer.C:
return IPCMessage{}, fmt.Errorf("timed out waiting for IPC message type %s", msgType)
}
}
}
func (s *IPCServer) Close() error {
if s.listener != nil {
s.listener.Close()
}
s.mu.Lock()
for id, ch := range s.subs {
delete(s.subs, id)
close(ch)
}
s.mu.Unlock()
// Clean up socket file
if s.socketPath != "" {
os.Remove(s.socketPath)
}
return nil
}