|
1 | 1 | namespace SyslogProxy |
2 | 2 | { |
3 | | - using System; |
4 | | - using System.Net; |
5 | | - using System.Net.Http; |
6 | | - using System.Net.Sockets; |
7 | | - using System.Text; |
8 | | - using System.Threading; |
9 | | - using System.Threading.Tasks; |
| 3 | + using System.Collections.Generic; |
| 4 | + using System.ComponentModel; |
| 5 | + using System.ServiceProcess; |
10 | 6 |
|
11 | | - public class Program |
| 7 | + using SimpleServices; |
| 8 | + |
| 9 | + [RunInstaller(true)] |
| 10 | + public class Program : SimpleServiceApplication |
12 | 11 | { |
13 | 12 | static void Main(string[] args) |
14 | 13 | { |
15 | | - var tcp = new TcpListener(IPAddress.Any, 6514); |
16 | | - |
17 | | - tcp.Start(); |
18 | | - AcceptConnection(tcp); |
19 | | - while (true) |
20 | | - { |
21 | | - Thread.Sleep(10000); |
22 | | - } |
23 | | - } |
24 | | - |
25 | | - static async Task AcceptConnection(TcpListener listener) |
26 | | - { |
27 | | - while (true) |
28 | | - { |
29 | | - var client = await listener.AcceptTcpClientAsync().ConfigureAwait(false); |
30 | | - EchoAsync(client); |
31 | | - } |
32 | | - } |
33 | | - |
34 | | - static async Task EchoAsync(TcpClient client) |
35 | | - { |
36 | | - Console.WriteLine("New client connected."); |
37 | | - using (client) |
38 | | - { |
39 | | - var buf = new byte[4096]; |
40 | | - var stream = client.GetStream(); |
41 | | - while (true) |
42 | | - { |
43 | | - var timeoutTask = Task.Delay(TimeSpan.FromSeconds(15)); |
44 | | - var amountReadTask = stream.ReadAsync(buf, 0, buf.Length); |
45 | | - var completedTask = await Task.WhenAny(timeoutTask, amountReadTask) |
46 | | - .ConfigureAwait(false); |
47 | | - if (completedTask == timeoutTask) |
48 | | - { |
49 | | - Console.WriteLine("Client timed out"); |
50 | | - break; |
51 | | - } |
52 | | - |
53 | | - var amountRead = amountReadTask.Result; |
54 | | - if (amountRead == 0) break; //end of stream. |
55 | | - // stuff buff into a json thing |
56 | | - await WriteToSeq(new SyslogJson(Encoding.UTF8.GetString(buf).TrimEnd('\0'))); |
57 | | - } |
58 | | - } |
59 | | - Console.WriteLine("Client disconnected"); |
60 | | - } |
61 | | - |
62 | | - public static async Task WriteToSeq(SyslogJson syslog) |
63 | | - { |
64 | | - using (var http = new HttpClient()) |
65 | | - { |
66 | | - using (var content = new StringContent("{\"events\":[" +syslog.ToString() + "]}", Encoding.UTF8, "application/json")) |
67 | | - { |
68 | | - var response = await http.PostAsync("http://10.2.10.156:5341/api/events/raw", content); |
69 | | - if (!response.IsSuccessStatusCode) |
70 | | - { |
71 | | - Console.WriteLine("ERROR: Could not send to SEQ."); |
72 | | - } |
73 | | - } |
74 | | - } |
| 14 | + new Service(args, |
| 15 | + new List<IWindowsService> { new ProxyService() }.ToArray, |
| 16 | + installationSettings: (serviceInstaller, serviceProcessInstaller) => |
| 17 | + { |
| 18 | + serviceInstaller.ServiceName = "SyslogProxy"; |
| 19 | + serviceInstaller.Description = "A simple Syslog proxy for Seq."; |
| 20 | + serviceInstaller.StartType = ServiceStartMode.Automatic; |
| 21 | + serviceProcessInstaller.Account = ServiceAccount.LocalService; |
| 22 | + serviceProcessInstaller.Installers.Add(new EventSourceInstaller()); |
| 23 | + }, |
| 24 | + configureContext: x => { x.Log = Logger.Information; }) |
| 25 | + .Host(); |
75 | 26 | } |
76 | 27 | } |
77 | 28 | } |
0 commit comments