This repository was archived by the owner on Sep 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathReceiver.cs
316 lines (289 loc) · 11.2 KB
/
Receiver.cs
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Copyright 2012 Splunk, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"): you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
namespace Splunk
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Text;
/// <summary>
/// The receiver class. This class exposes methods to send events to splunk
/// via the simple or streaming receiver endpoint.
/// </summary>
public class Receiver
{
/// <summary>
/// A reference to the attached service.
/// </summary>
private Service service = null;
/// <summary>
/// Initializes a new instance of the <see cref="Receiver"/> class.
/// </summary>
/// <param name="service">The service</param>
public Receiver(Service service)
{
this.service = service;
}
/// <summary>
/// Gets or sets the RemoteCertificateValidationCallback delegate
/// responsible for validating the certificate supplied by the Splunk
/// server if SSL (i.e. https) is used.
/// If none is set (which is the default),
/// no validation will be performed.
/// </summary>
public RemoteCertificateValidationCallback
SSLRemoteCertificateValidationCallback
{
get; set;
}
/// <summary>
/// Gets or sets the LocalCertificateSelectionCallback delegate
/// responsible for selecting the certificate used for authentication
/// with the Splunk server if SSL (i.e. https) is used.
/// If none is set (which is the default),
/// no local certificate is used.
/// </summary>
public LocalCertificateSelectionCallback
SSLLocalCertificateValidationCallback
{
get; set;
}
/// <summary>
/// Gets or sets the EncryptionPolicy to use with the Splunk server
/// if SSL (i.e. https) is used.
/// </summary>
public EncryptionPolicy SSLEncryptionPolicy
{
get; set;
}
/// <summary>
/// Creates a socket to the splunk server using the default index, and
/// default port.
/// </summary>
/// <returns>The Stream</returns>
public Stream Attach()
{
return this.Attach(null, null);
}
/// <summary>
/// Creates a socket to the splunk server using the named index, and
/// default port.
/// </summary>
/// <param name="indexName">The index to write to</param>
/// <returns>The Stream</returns>
public Stream Attach(string indexName)
{
return this.Attach(indexName, null);
}
/// <summary>
/// Creates a socket to the splunk server using the default index and
/// variable arguments.
/// </summary>
/// <param name="args">The variable arguments</param>
/// <returns>The Socket</returns>
public Stream Attach(Args args)
{
return this.Attach(null, args);
}
/// <summary>
/// Creates a socket to the splunk server using the named index and
/// variable arguments.
/// </summary>
/// <param name="indexName">The index name</param>
/// <param name="args">The variable arguments</param>
/// <returns>The Socket</returns>
public Stream Attach(string indexName, Args args)
{
Stream stream;
if (this.service.Scheme == HttpService.SchemeHttps)
{
TcpClient tcp = new TcpClient();
tcp.Connect(this.service.Host, this.service.Port);
var sslStream = new SSLStreamWrapper(tcp);
sslStream.AuthenticateAsClient(this.service.Host);
stream = sslStream;
}
else
{
Socket socket = this.service.Open(this.service.Port);
stream = new NetworkStream(socket, true);
}
string postUrl = "POST /services/receivers/stream";
if (indexName != null)
{
postUrl = postUrl + "?index=" + indexName;
}
if (args != null && args.Count > 0)
{
postUrl = postUrl + ((indexName == null) ? "?" : "&");
postUrl = postUrl + args.Encode();
}
string header = string.Format(
"{0} HTTP/1.1\r\n" +
"Host: {1}:{2}\r\n" +
"Accept-Encoding: identity\r\n" +
"Authorization: {3}\r\n" +
"X-Splunk-Input-Mode: Streaming\r\n\r\n",
postUrl,
this.service.Host,
this.service.Port,
this.service.Token);
var bytes = Encoding.UTF8.GetBytes(header);
stream.Write(bytes, 0, bytes.Length);
stream.Flush();
return stream;
}
/// <summary>
/// Submits the data using HTTP post, to the default index
/// </summary>
/// <param name="data">The data</param>
public void Submit(string data)
{
this.Submit(null, null, data);
}
/// <summary>
/// Submits the data using HTTP post, to the named index
/// </summary>
/// <param name="indexName">The index name</param>
/// <param name="data">The data</param>
public void Submit(string indexName, string data)
{
this.Submit(indexName, null, data);
}
/// <summary>
/// Submits the data using HTTP post, using variable arguments to the
/// default index.
/// </summary>
/// <param name="args">The variable arguments</param>
/// <param name="data">The data</param>
public void Submit(Args args, string data)
{
this.Submit(null, args, data);
}
/// <summary>
/// Submits the data to the named index using variable arguments.
/// </summary>
/// <param name="indexName">The named index</param>
/// <param name="args">The variable arguments</param>
/// <param name="data">The data</param>
public void Submit(string indexName, Args args, string data)
{
string sendString = string.Empty;
RequestMessage request = new RequestMessage("POST");
request.Content = data;
if (indexName != null)
{
sendString = string.Format("?index={0}", indexName);
}
if (args != null && args.Count > 0)
{
sendString = sendString + ((indexName == null) ? "?" : "&");
sendString = sendString + args.Encode();
}
this.service.Send(
this.service.SimpleReceiverEndPoint + sendString, request);
}
/// <summary>
/// Alias for submit()
/// </summary>
/// <param name="data">The data</param>
public void Log(string data)
{
this.Submit(data);
}
/// <summary>
/// Alias for submit()
/// </summary>
/// <param name="indexName">The index name</param>
/// <param name="data">The data</param>
public void Log(string indexName, string data)
{
this.Submit(indexName, data);
}
/// <summary>
/// Alias for submit()
/// </summary>
/// <param name="args">The arguments</param>
/// <param name="data">The data</param>
public void Log(Args args, string data)
{
this.Submit(args, data);
}
/// <summary>
/// Alias for submit()
/// </summary>
/// <param name="indexName">The index name</param>
/// <param name="args">The arguments</param>
/// <param name="data">The data</param>
public void Log(string indexName, Args args, string data)
{
this.Submit(indexName, args, data);
}
/// <summary>
/// Wrapper class of SslStream for closing TCP connection
/// when closing the stream
/// </summary>
private class SSLStreamWrapper : SslStream
{
/// <summary>
/// The TcpClient object the SSLStream object is based on.
/// </summary>
private TcpClient tcpClient;
/// <summary>
/// Initializes a new instance of the <see cref="SSLStreamWrapper"/> class.
/// </summary>
/// <param name="tcpClient">
/// A TcpClient object the SSLStream object is based on.
/// </param>
public SSLStreamWrapper(
TcpClient tcpClient)
: base(
tcpClient.GetStream(),
false,
ServicePointManager.ServerCertificateValidationCallback)
{
this.tcpClient = tcpClient;
}
/// <summary>
/// Release resources including the tcpClient.
/// </summary>
/// <param name="disposing">True to release both managed and unmanaged resources;
/// false to release only unmanaged resources. </param>
protected override void Dispose(bool disposing)
{
// Dispose(bool disposing) executes in two distinct scenarios.
// If disposing equals true, the method has been called directly
// or indirectly by a user's code. Managed and unmanaged resources
// can be disposed.
// If disposing equals false, the method has been called by the
// runtime from inside the finalizer and you should not reference
// other objects. Only unmanaged resources can be disposed.
// If this is called from the finalizer, don't reference tcpClient.
// It is ok since its manage resources will be cleaned up as part of regular GC and
// the runtime will call its Dispose with 'disposing' being false.
if (disposing)
{
this.tcpClient.Close();
}
}
}
}
}