private static void Main() { Console.Title = "SteamWebPipes"; AppDomain.CurrentDomain.UnhandledException += OnSillyCrashHandler; Config = JsonConvert.DeserializeObject <Configuration>(File.ReadAllText(Path.Combine(Path.GetDirectoryName(typeof(Bootstrap).Assembly.Location), "settings.json"))); if (string.IsNullOrWhiteSpace(Config.DatabaseConnectionString)) { Config.DatabaseConnectionString = null; Log("Database connectiong string is empty, will not try to get app names"); } var server = new WebSocketServer(Config.Location) { SupportedSubProtocols = new[] { "steam-pics" } }; if (File.Exists(Config.X509Certificate)) { Log("Using certificate"); server.Certificate = new X509Certificate2(Config.X509Certificate); } server.Start(socket => { socket.OnOpen = () => { var apps = new List <uint>(); foreach (var sub in socket.ConnectionInfo.SubProtocol.Split(',')) { if (sub.Trim().StartsWith("app-")) { if (!uint.TryParse(sub.Substring(5), out var appid) || apps.Count > 50) { socket.Close(); return; } apps.Add(appid); } } socket.ConnectionInfo.Headers.TryGetValue("X-Forwarded-For", out var clientIp); if (string.IsNullOrEmpty(clientIp)) { clientIp = $"{socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}"; } if (apps.Count > 0) { SubscribedAppClients.TryAdd(socket, apps); Log($"Subscriber #{SubscribedAppClients.Count} connected: {clientIp} (Apps: {string.Join(", ", apps)})"); return; } lock (ConnectedClients) { ConnectedClients.Add(socket); } socket.Send(JsonConvert.SerializeObject(new UsersOnlineEvent(ConnectedClients.Count))); if (ConnectedClients.Count < 500) { Log($"Client #{ConnectedClients.Count} connected: {clientIp}"); } }; socket.OnClose = () => { lock (ConnectedClients) { ConnectedClients.Remove(socket); } SubscribedAppClients.TryRemove(socket, out _); }; }); var steam = new Steam(); if (File.Exists("last-changenumber.txt")) { steam.PreviousChangeNumber = uint.Parse(File.ReadAllText("last-changenumber.txt")); } var thread = new Thread(steam.Tick) { Name = "Steam" }; thread.Start(); var timer = new Timer(); timer.Elapsed += TimerTick; timer.Interval = TimeSpan.FromSeconds(30).TotalMilliseconds; timer.Start(); Console.CancelKeyPress += delegate { Console.WriteLine("Ctrl + C detected, shutting down."); File.WriteAllText("last-changenumber.txt", steam.PreviousChangeNumber.ToString()); steam.IsRunning = false; timer.Stop(); ConnectedClients.ToList().ForEach(socket => socket?.Close()); server.Dispose(); Environment.Exit(0); }; }
private static void Main() { Console.Title = "SteamWebPipes"; AppDomain.CurrentDomain.UnhandledException += OnSillyCrashHandler; Config = JsonSerializer.Deserialize <Configuration>(File.ReadAllText(Path.Combine(Path.GetDirectoryName(typeof(Bootstrap).Assembly.Location), "settings.json"))); if (string.IsNullOrWhiteSpace(Config.DatabaseConnectionString)) { Config.DatabaseConnectionString = null; Log("Database connectiong string is empty, will not try to get app names"); } var server = new WebSocketServer(Config.Location) { SupportedSubProtocols = new[] { "steam-pics" } }; if (File.Exists(Config.X509Certificate)) { Log("Using certificate"); server.Certificate = new X509Certificate2(Config.X509Certificate); } server.Start(socket => { socket.OnOpen = () => { int users; lock (ConnectedClients) { ConnectedClients.Add(socket); users = ConnectedClients.Count; } socket.Send(JsonSerializer.Serialize(new UsersOnlineEvent(users))); if (users >= 500) { return; } socket.ConnectionInfo.Headers.TryGetValue("X-Forwarded-For", out var clientIp); if (string.IsNullOrEmpty(clientIp)) { clientIp = $"{socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}"; } Log($"Client #{users} connected: {clientIp}"); }; socket.OnClose = () => { lock (ConnectedClients) { ConnectedClients.Remove(socket); } }; }); var steam = new Steam(); if (File.Exists("last-changenumber.txt")) { steam.PreviousChangeNumber = uint.Parse(File.ReadAllText("last-changenumber.txt")); } var timer = new Timer(); timer.Elapsed += TimerTick; timer.Interval = TimeSpan.FromSeconds(30).TotalMilliseconds; timer.Start(); Console.CancelKeyPress += delegate { Console.WriteLine("Ctrl + C detected, shutting down."); File.WriteAllText("last-changenumber.txt", steam.PreviousChangeNumber.ToString()); steam.IsRunning = false; timer.Stop(); lock (ConnectedClients) { ConnectedClients.ToList().ForEach(socket => socket?.Close()); } server.Dispose(); Environment.Exit(0); }; steam.Tick(); }