forked from tsaikd/gogstash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputsocket.go
211 lines (187 loc) · 4.58 KB
/
inputsocket.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
package inputsocket
import (
"bufio"
"context"
"io"
"net"
"os"
reuse "github.com/libp2p/go-reuseport"
"github.com/tsaikd/KDGoLib/errutil"
codecjson "github.com/tsaikd/gogstash/codec/json"
"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/goglog"
"github.com/tsaikd/gogstash/config/logevent"
"golang.org/x/sync/errgroup"
)
// ModuleName is the name used in config file
const ModuleName = "socket"
// ErrorTag tag added to event when process module failed
const ErrorTag = "gogstash_input_socket_error"
// InputConfig holds the configuration json fields and internal objects
type InputConfig struct {
config.InputConfig
Socket string `json:"socket"` // Type of socket, must be one of ["tcp", "udp", "unix", "unixpacket"].
// For TCP or UDP, address must have the form `host:port`.
// For Unix networks, the address must be a file system path.
Address string `json:"address"`
ReusePort bool `json:"reuseport"`
BufferSize int `json:"buffer_size"`
}
// DefaultInputConfig returns an InputConfig struct with default values
func DefaultInputConfig() InputConfig {
return InputConfig{
InputConfig: config.InputConfig{
CommonConfig: config.CommonConfig{
Type: ModuleName,
},
},
BufferSize: 4096,
}
}
// errors
var (
ErrorUnknownSocketType1 = errutil.NewFactory("%q is not a valid socket type")
ErrorSocketAccept = errutil.NewFactory("socket accept error")
)
// InitHandler initialize the input plugin
func InitHandler(ctx context.Context, raw *config.ConfigRaw) (config.TypeInputConfig, error) {
conf := DefaultInputConfig()
err := config.ReflectConfig(raw, &conf)
if err != nil {
return nil, err
}
conf.Codec, err = config.GetCodecDefault(ctx, *raw, codecjson.ModuleName)
if err != nil {
return nil, err
}
return &conf, nil
}
// Start wraps the actual function starting the plugin
func (i *InputConfig) Start(ctx context.Context, msgChan chan<- logevent.LogEvent) error {
logger := goglog.Logger
var l net.Listener
switch i.Socket {
case "unix", "unixpacket":
// Remove existing unix socket
os.Remove(i.Address)
// Listen to socket
address, err := net.ResolveUnixAddr(i.Socket, i.Address)
if err != nil {
return err
}
logger.Debugf("listen %q on %q", i.Socket, i.Address)
l, err = net.ListenUnix(i.Socket, address)
if err != nil {
return err
}
defer l.Close()
// Set socket permissions.
if err = os.Chmod(i.Address, 0777); err != nil {
return err
}
case "tcp":
address, err := net.ResolveTCPAddr(i.Socket, i.Address)
if err != nil {
return err
}
logger.Debugf("listen %q on %q", i.Socket, address.String())
if i.ReusePort {
l, err = reuse.Listen(i.Socket, address.String())
} else {
l, err = net.ListenTCP(i.Socket, address)
}
if err != nil {
return err
}
defer l.Close()
case "udp":
address, err := net.ResolveUDPAddr(i.Socket, i.Address)
if err != nil {
return err
}
logger.Debugf("listen %q on %q", i.Socket, address.String())
var conn net.PacketConn
if i.ReusePort {
conn, err = reuse.ListenPacket(i.Socket, i.Address)
} else {
conn, err = net.ListenPacket(i.Socket, i.Address)
}
if err != nil {
return err
}
return i.handleUDP(ctx, conn, msgChan)
default:
return ErrorUnknownSocketType1.New(nil, i.Socket)
}
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
<-ctx.Done()
return l.Close()
})
eg.Go(func() error {
for {
conn, err := l.Accept()
if err != nil {
return ErrorSocketAccept.New(err)
}
func(conn net.Conn) {
eg.Go(func() error {
defer conn.Close()
i.parse(ctx, conn, msgChan)
return nil
})
}(conn)
}
})
return eg.Wait()
}
func (i *InputConfig) handleUDP(ctx context.Context, conn net.PacketConn, msgChan chan<- logevent.LogEvent) error {
eg, ctx := errgroup.WithContext(ctx)
b := make([]byte, i.BufferSize) // read buf
pr, pw := io.Pipe()
defer pw.Close()
eg.Go(func() error {
<-ctx.Done()
pr.Close()
conn.Close()
return nil
})
eg.Go(func() error {
for {
select {
case <-ctx.Done():
return nil
default:
}
n, _, err := conn.ReadFrom(b)
if err == io.EOF {
break
} else if err != nil {
return err
}
pw.Write(b[:n])
}
return nil
})
eg.Go(func() error {
i.parse(ctx, pr, msgChan)
return nil
})
return eg.Wait()
}
func (i *InputConfig) parse(ctx context.Context, r io.Reader, msgChan chan<- logevent.LogEvent) {
b := bufio.NewReader(r)
for {
select {
case <-ctx.Done():
return
default:
}
line, err := b.ReadBytes('\n')
if err != nil {
// EOF
return
}
i.Codec.Decode(ctx, line, nil, []string{}, msgChan)
}
}