-
Notifications
You must be signed in to change notification settings - Fork 0
/
Worker.cs
204 lines (180 loc) · 7.86 KB
/
Worker.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
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NeonService.Functions;
using NeonService.Models;
using NeonService.Variables;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.IdentityModel.Tokens.Jwt;
using System.Timers;
namespace NeonService
{
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private HttpClient client;
private System.Timers.Timer restartTimer;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
InitializeRestartTimer(); // Initialize the timer in the constructor
}
public override Task StartAsync(CancellationToken cancellationToken)
{
client = new HttpClient();
return base.StartAsync(cancellationToken);
}
public override Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Neon Service stopping");
client.Dispose();
return base.StopAsync(cancellationToken);
}
public void ActionDeterminer(string listener, string result, string connectionID)
{
switch (listener)
{
case "devicefileslist":
Access AccessNeon = new Access(_logger);
AccessNeon.GetFilesList(result, connectionID);
break;
default:
break;
}
}
public void ExecuteEvent(string event_data, string authtoken)
{
var result = $"{{ {event_data} }}";
var handler = new JwtSecurityTokenHandler();
var jsonToken = handler.ReadToken(authtoken) as JwtSecurityToken;
if (jsonToken != null)
{
foreach (var claim in jsonToken.Claims)
{
if (claim.Type.Contains("token"))
{
var authjson = $"{{ token: \"{claim.Value}\", iat: \"\" }}";
var authdata = JsonConvert.DeserializeObject<AuthToken>(authjson);
var handlerauthcred = new JwtSecurityTokenHandler();
var jsonauthcred = handlerauthcred.ReadToken(authdata.token) as JwtSecurityToken;
if (jsonauthcred != null)
{
foreach (var credclaim in jsonauthcred.Claims)
{
if (credclaim.Type.Contains("userID"))
{
var data = JsonConvert.DeserializeObject<ResponseData>(result);
_logger.LogInformation("Neon Service: {listener} {token}", data.data.listener, data.data.result);
ActionDeterminer(data.data.listener, data.data.result, credclaim.Value);
}
}
}
else
{
_logger.LogInformation("Neon Service: Invalid Auth Cred Token");
}
}
}
}
else
{
_logger.LogInformation("Neon Service: Invalid Token");
}
}
private void InitializeRestartTimer()
{
restartTimer = new System.Timers.Timer();
restartTimer.Interval = 40000; // 10 seconds interval
restartTimer.AutoReset = false; // Only fire once per interval
restartTimer.Elapsed += async (sender, e) => await RestartTimerElapsed();
}
private async Task RestartTimerElapsed()
{
//_logger.LogInformation("Restart timer elapsed. No data received for 10 seconds. Restarting the connection.");
restartTimer.Stop();
// Trigger the cancellation token to restart the connection
await ExecuteAsync(CancellationToken.None); // This will restart the ExecuteAsync method
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
string NeonExeName = "neonauth.txt";
var finalExecutable = @$"C:\{NeonExeName}";
//_logger.LogInformation("Checking current directory {path}", Environment.CurrentDirectory);
//_logger.LogInformation("Scanning path {path}", Path.GetFullPath(finalExecutable));
while (!stoppingToken.IsCancellationRequested)
{
try
{
if (client != null)
client.Dispose(); // Dispose of the current client
client = new HttpClient(); // Create a new HttpClient instance
var authtext = await ReadAuthTokenAsync(finalExecutable);
Envs envs = new Envs();
var requestUri = $"{envs.API}{envs.SSEHandshake}{authtext}"; // neonaiserver.onrender.com
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3000))) // Set your desired timeout value
{
var stream = await client.GetStreamAsync(requestUri).WithCancellation(cts.Token);
restartTimer.Start(); // Start the timer when the stream is established
using (var reader = new StreamReader(stream))
{
while (!reader.EndOfStream && !stoppingToken.IsCancellationRequested)
{
var line = await reader.ReadLineAsync();
if (line != null && line.Contains("data"))
{
ExecuteEvent(line, authtext);
restartTimer.Stop(); // Reset the timer on each received data
restartTimer.Start();
}
}
}
}
// Log when the stream ends
_logger.LogInformation("Stream ended. Restarting in 3 seconds.");
await Task.Delay(3000, stoppingToken);
}
catch (OperationCanceledException)
{
_logger.LogInformation("Stream connection timed out. Restarting in 3 seconds.");
await Task.Delay(3000, stoppingToken);
}
catch (Exception ex)
{
_logger.LogInformation($"Stream disconnected. Retrying in 3 seconds. Exception: {ex.Message}");
await Task.Delay(3000, stoppingToken);
}
}
}
private async Task<string> ReadAuthTokenAsync(string filePath)
{
if (File.Exists(filePath))
{
return await File.ReadAllTextAsync(filePath);
}
else
{
_logger.LogInformation("Neon Service cannot detect authentication file at {path}", filePath);
return string.Empty;
}
}
}
public static class TaskExtensions
{
public static async Task<T> WithCancellation<T>(this Task<T> task, CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<bool>();
using (cancellationToken.Register(s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs))
{
if (task != await Task.WhenAny(task, tcs.Task))
{
throw new OperationCanceledException(cancellationToken);
}
}
return await task; // Unwrap the original exception
}
}
}