/src/fluent-bit/plugins/out_websocket/websocket.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* Fluent Bit |
4 | | * ========== |
5 | | * Copyright (C) 2015-2022 The Fluent Bit Authors |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <fluent-bit/flb_info.h> |
21 | | #include <fluent-bit/flb_output.h> |
22 | | #include <fluent-bit/flb_utils.h> |
23 | | #include <fluent-bit/flb_network.h> |
24 | | #include <fluent-bit/flb_time.h> |
25 | | #include <fluent-bit/flb_upstream.h> |
26 | | #include <fluent-bit/flb_crypto.h> |
27 | | #include <fluent-bit/flb_pack.h> |
28 | | #include <fluent-bit/flb_sds.h> |
29 | | #include <fluent-bit/flb_http_client.h> |
30 | | #include <fluent-bit/flb_config_map.h> |
31 | | #include <msgpack.h> |
32 | | |
33 | | #include "websocket.h" |
34 | | #include "websocket_conf.h" |
35 | | struct flb_output_plugin out_websocket_plugin; |
36 | | |
37 | | #define SECURED_BY "Fluent Bit" |
38 | | |
39 | | |
40 | | static int flb_ws_handshake(struct flb_connection *u_conn, |
41 | | struct flb_out_ws *ctx) |
42 | 0 | { |
43 | 0 | int ret; |
44 | 0 | size_t bytes_sent; |
45 | 0 | struct flb_http_client *c; |
46 | |
|
47 | 0 | if (!u_conn) { |
48 | 0 | flb_error("[output_ws] upstream connection error"); |
49 | 0 | return -1; |
50 | 0 | } |
51 | | |
52 | | /* Compose HTTP Client request */ |
53 | 0 | c = flb_http_client(u_conn, FLB_HTTP_GET, ctx->uri, |
54 | 0 | NULL, 0, NULL, 0, NULL, 0); |
55 | 0 | if (!c) { |
56 | 0 | flb_upstream_conn_release(u_conn); |
57 | 0 | return -1; |
58 | 0 | } |
59 | | |
60 | 0 | flb_http_buffer_size(c, ctx->buffer_size); |
61 | 0 | flb_http_add_header(c, "Upgrade", 7, "websocket", 9); |
62 | 0 | flb_http_add_header(c, "Connection", 10, "Upgrade", 7); |
63 | 0 | flb_http_add_header(c, "Sec-WebSocket-Key", 17, "dGhlIHNhbXBsZSBub25jZQ==", 24); |
64 | 0 | flb_http_add_header(c, "Sec-WebSocket-Version", 21, "13", 2); |
65 | | |
66 | | /* Perform request*/ |
67 | 0 | ret = flb_http_do(c, &bytes_sent); |
68 | |
|
69 | 0 | if (ret != 0 || c->resp.status != 101) { |
70 | 0 | if (c->resp.payload_size > 0) { |
71 | 0 | flb_debug("[output_ws] Websocket Server Response\n%s", |
72 | 0 | c->resp.payload); |
73 | 0 | } |
74 | 0 | flb_http_client_destroy(c); |
75 | 0 | flb_upstream_conn_release(u_conn); |
76 | 0 | flb_debug("[out_ws] Http Get Operation ret = %i, http resp = %i", ret, c->resp.status); |
77 | 0 | return -1; |
78 | 0 | } |
79 | 0 | flb_http_client_destroy(c); |
80 | 0 | return 0; |
81 | 0 | } |
82 | | |
83 | | static void flb_ws_mask(char *data, int len, char *mask) |
84 | 0 | { |
85 | 0 | int i; |
86 | 0 | for (i=0;i<len;++i) { |
87 | 0 | *(data+i) ^= *(mask+(i%4)); |
88 | 0 | } |
89 | 0 | } |
90 | | |
91 | | static int flb_ws_sendDataFrameHeader(struct flb_connection *u_conn, |
92 | | struct flb_out_ws *ctx, const void *data, size_t bytes) |
93 | 0 | { |
94 | 0 | int ret = -1; |
95 | 0 | char* data_frame_head; |
96 | 0 | size_t bytes_sent; |
97 | 0 | int data_frame_head_len = 0; |
98 | | //TODO use random function to generate masking_key |
99 | 0 | char masking_key[4] = {0x12, 0x34, 0x56, 0x78}; |
100 | 0 | unsigned long long payloadSize = bytes; |
101 | |
|
102 | 0 | flb_ws_mask((char *)data, payloadSize, masking_key); |
103 | 0 | if (payloadSize < 126) { |
104 | 0 | data_frame_head = (char *)flb_malloc(6); |
105 | 0 | if (!data_frame_head) { |
106 | 0 | flb_errno(); |
107 | 0 | return -1; |
108 | 0 | } |
109 | 0 | data_frame_head[0] = 0x81; |
110 | 0 | data_frame_head[1] = (payloadSize & 0xff) | 0x80; |
111 | 0 | data_frame_head[2] = masking_key[0]; |
112 | 0 | data_frame_head[3] = masking_key[1]; |
113 | 0 | data_frame_head[4] = masking_key[2]; |
114 | 0 | data_frame_head[5] = masking_key[3]; |
115 | 0 | data_frame_head_len = 6; |
116 | 0 | } |
117 | 0 | else if (payloadSize < 65536) { |
118 | 0 | data_frame_head = (char *)flb_malloc(8); |
119 | 0 | if (!data_frame_head) { |
120 | 0 | flb_errno(); |
121 | 0 | return -1; |
122 | 0 | } |
123 | 0 | data_frame_head[0] = 0x81; |
124 | 0 | data_frame_head[1] = 126 | 0x80; |
125 | 0 | data_frame_head[2] = (payloadSize >> 8) & 0xff; |
126 | 0 | data_frame_head[3] = (payloadSize >> 0) & 0xff; |
127 | 0 | data_frame_head[4] = masking_key[0]; |
128 | 0 | data_frame_head[5] = masking_key[1]; |
129 | 0 | data_frame_head[6] = masking_key[2]; |
130 | 0 | data_frame_head[7] = masking_key[3]; |
131 | 0 | data_frame_head_len = 8; |
132 | 0 | } |
133 | 0 | else { |
134 | 0 | data_frame_head = (char *)flb_malloc(14); |
135 | 0 | if (!data_frame_head) { |
136 | 0 | flb_errno(); |
137 | 0 | return -1; |
138 | 0 | } |
139 | 0 | data_frame_head[0] = 0x81; |
140 | 0 | data_frame_head[1] = 127 | 0x80; |
141 | 0 | data_frame_head[2] = (payloadSize >> 56) & 0xff; |
142 | 0 | data_frame_head[3] = (payloadSize >> 48) & 0xff; |
143 | 0 | data_frame_head[4] = (payloadSize >> 40) & 0xff; |
144 | 0 | data_frame_head[5] = (payloadSize >> 32) & 0xff; |
145 | 0 | data_frame_head[6] = (payloadSize >> 24) & 0xff; |
146 | 0 | data_frame_head[7] = (payloadSize >> 16) & 0xff; |
147 | 0 | data_frame_head[8] = (payloadSize >> 8) & 0xff; |
148 | 0 | data_frame_head[9] = (payloadSize >> 0) & 0xff; |
149 | 0 | data_frame_head[10] = masking_key[0]; |
150 | 0 | data_frame_head[11] = masking_key[1]; |
151 | 0 | data_frame_head[12] = masking_key[2]; |
152 | 0 | data_frame_head[13] = masking_key[3]; |
153 | 0 | data_frame_head_len = 14; |
154 | 0 | } |
155 | 0 | ret = flb_io_net_write(u_conn, data_frame_head, data_frame_head_len, &bytes_sent); |
156 | 0 | if (ret == -1) { |
157 | 0 | flb_error("[out_ws] could not write dataframe header"); |
158 | 0 | goto error; |
159 | 0 | } |
160 | 0 | flb_free(data_frame_head); |
161 | 0 | return 0; |
162 | | |
163 | 0 | error: |
164 | 0 | flb_free(data_frame_head); |
165 | 0 | return -1; |
166 | 0 | } |
167 | | |
168 | | static int cb_ws_init(struct flb_output_instance *ins, |
169 | | struct flb_config *config, void *data) |
170 | 0 | { |
171 | 0 | struct flb_out_ws *ctx = NULL; |
172 | |
|
173 | 0 | ctx = flb_ws_conf_create(ins, config); |
174 | 0 | if (!ctx) { |
175 | 0 | return -1; |
176 | 0 | } |
177 | | |
178 | 0 | ctx->handshake = 1; |
179 | 0 | ctx->last_input_timestamp = time(NULL); |
180 | 0 | flb_output_set_context(ins, ctx); |
181 | 0 | return 0; |
182 | 0 | } |
183 | | |
184 | | static int cb_ws_exit(void *data, struct flb_config *config) |
185 | 0 | { |
186 | 0 | struct flb_out_ws *ctx = data; |
187 | 0 | flb_ws_conf_destroy(ctx); |
188 | 0 | return 0; |
189 | 0 | } |
190 | | |
191 | | static void cb_ws_flush(struct flb_event_chunk *event_chunk, |
192 | | struct flb_output_flush *out_flush, |
193 | | struct flb_input_instance *i_ins, |
194 | | void *out_context, |
195 | | struct flb_config *config) |
196 | 0 | { |
197 | 0 | int ret = -1; |
198 | 0 | size_t bytes_sent; |
199 | 0 | flb_sds_t json = NULL; |
200 | 0 | struct flb_upstream *u; |
201 | 0 | struct flb_connection *u_conn; |
202 | 0 | struct flb_out_ws *ctx = out_context; |
203 | 0 | time_t now; |
204 | | |
205 | | /* Get upstream context and connection */ |
206 | 0 | u = ctx->u; |
207 | 0 | u_conn = flb_upstream_conn_get(u); |
208 | |
|
209 | 0 | if (!u_conn) { |
210 | 0 | flb_error("[out_ws] no upstream connections available to %s:%i", u->tcp_host, u->tcp_port); |
211 | 0 | ctx->handshake = 1; |
212 | 0 | FLB_OUTPUT_RETURN(FLB_RETRY); |
213 | 0 | } |
214 | | |
215 | 0 | now = time(NULL); |
216 | | |
217 | | //TODO how to determine the interval? conn disconnet is about 30 sec, so we set 20 ssecnds here. |
218 | 0 | flb_debug("[out_ws] interval is %ld and handshake is %d", now - ctx->last_input_timestamp, ctx->handshake); |
219 | 0 | if ((now - ctx->last_input_timestamp > ctx->idle_interval) && (ctx->handshake == 0)) { |
220 | 0 | ctx->handshake = 1; |
221 | 0 | flb_upstream_conn_release(u_conn); |
222 | 0 | FLB_OUTPUT_RETURN(FLB_RETRY); |
223 | 0 | } |
224 | 0 | ctx->last_input_timestamp = now; |
225 | |
|
226 | 0 | if (ctx->handshake == 1) { |
227 | | /* Handshake with websocket server*/ |
228 | 0 | flb_info("[out_ws] handshake for ws"); |
229 | 0 | ret = flb_ws_handshake(u_conn, ctx); |
230 | 0 | if (ret == -1) { |
231 | 0 | flb_upstream_conn_release(u_conn); |
232 | 0 | FLB_OUTPUT_RETURN(FLB_RETRY); |
233 | 0 | } |
234 | 0 | ctx->handshake = 0; |
235 | 0 | } |
236 | | |
237 | | /* Data format process*/ |
238 | 0 | if (ctx->out_format != FLB_PACK_JSON_FORMAT_NONE) { |
239 | 0 | json = flb_pack_msgpack_to_json_format(event_chunk->data, |
240 | 0 | event_chunk->size, |
241 | 0 | ctx->out_format, |
242 | 0 | ctx->json_date_format, |
243 | 0 | ctx->json_date_key); |
244 | |
|
245 | 0 | if (!json) { |
246 | 0 | flb_error("[out_ws] error formatting JSON payload"); |
247 | 0 | flb_upstream_conn_release(u_conn); |
248 | 0 | FLB_OUTPUT_RETURN(FLB_ERROR); |
249 | 0 | } |
250 | 0 | } |
251 | | |
252 | | /* Write message header */ |
253 | 0 | if (ctx->out_format == FLB_PACK_JSON_FORMAT_NONE) { |
254 | 0 | ret = flb_ws_sendDataFrameHeader(u_conn, ctx, |
255 | 0 | event_chunk->data, |
256 | 0 | event_chunk->size); |
257 | 0 | } |
258 | 0 | else { |
259 | 0 | ret = flb_ws_sendDataFrameHeader(u_conn, ctx, json, flb_sds_len(json)); |
260 | 0 | } |
261 | |
|
262 | 0 | if (ret == -1) { |
263 | 0 | flb_error("[out_ws] dataFrameHeader sent failed"); |
264 | 0 | ctx->handshake = 1; |
265 | 0 | if (json) { |
266 | 0 | flb_sds_destroy(json); |
267 | 0 | } |
268 | 0 | flb_upstream_conn_release(u_conn); |
269 | 0 | FLB_OUTPUT_RETURN(FLB_RETRY); |
270 | 0 | } |
271 | | |
272 | | /* Write message body*/ |
273 | 0 | if (ctx->out_format == FLB_PACK_JSON_FORMAT_NONE) { |
274 | 0 | ret = flb_io_net_write(u_conn, |
275 | 0 | event_chunk->data, |
276 | 0 | event_chunk->size, |
277 | 0 | &bytes_sent); |
278 | 0 | } |
279 | 0 | else { |
280 | 0 | ret = flb_io_net_write(u_conn, json, flb_sds_len(json), &bytes_sent); |
281 | 0 | flb_sds_destroy(json); |
282 | 0 | } |
283 | | |
284 | | //flb_info("[out_ws] sendDataFrame number of bytes sent = %i", ret); |
285 | 0 | if (ret == -1) { |
286 | 0 | ctx->handshake = 1; |
287 | 0 | flb_upstream_conn_release(u_conn); |
288 | 0 | FLB_OUTPUT_RETURN(FLB_RETRY); |
289 | 0 | } |
290 | | |
291 | | /* Release the connection */ |
292 | 0 | flb_upstream_conn_release(u_conn); |
293 | 0 | FLB_OUTPUT_RETURN(FLB_OK); |
294 | 0 | } |
295 | | |
296 | | /* Configuration properties map */ |
297 | | static struct flb_config_map config_map[] = { |
298 | | { |
299 | | FLB_CONFIG_MAP_STR, "uri", NULL, |
300 | | 0, FLB_TRUE, offsetof(struct flb_out_ws, uri), |
301 | | "Specify an optional URI for the target web socket server, e.g: /something" |
302 | | }, |
303 | | { |
304 | | FLB_CONFIG_MAP_STR, "format", NULL, |
305 | | 0, FLB_FALSE, 0, |
306 | | "Set desired payload format: json, json_stream, json_lines, gelf or msgpack" |
307 | | }, |
308 | | { |
309 | | FLB_CONFIG_MAP_STR, "json_date_format", "double", |
310 | | 0, FLB_FALSE, 0, |
311 | | "Specify the format of the date" |
312 | | }, |
313 | | { |
314 | | FLB_CONFIG_MAP_STR, "json_date_key", "date", |
315 | | 0, FLB_TRUE, offsetof(struct flb_out_ws, json_date_key), |
316 | | "Specify the name of the date field in output" |
317 | | }, |
318 | | /* EOF */ |
319 | | {0} |
320 | | }; |
321 | | |
322 | | /* Plugin reference */ |
323 | | struct flb_output_plugin out_websocket_plugin = { |
324 | | .name = "websocket", |
325 | | .description = "Websocket", |
326 | | .cb_init = cb_ws_init, |
327 | | .cb_flush = cb_ws_flush, |
328 | | .cb_exit = cb_ws_exit, |
329 | | .config_map = config_map, |
330 | | .flags = FLB_OUTPUT_NET | FLB_IO_OPT_TLS, |
331 | | }; |