public void ToggleServer() { if (!SkynetServer.ServerRunning) { Program.Log.Insert(DateTime.Now.ToString(), "Starting server..."); SkynetServer = new SkynetServer(serverSettings); SkynetServer.StoppedServer += (ee) => { serverStatusLabel.ForeColor = Color.Red; serverStatusLabel.Text = "Server stopped, check log"; Program.Log.Insert(DateTime.Now.ToString(), "Server stopped. Code: " + ee.Code + "; Reason: " + ee.Reason); }; SkynetServer.StartedServer += () => { serverStatusLabel.ForeColor = Color.Green; serverStatusLabel.Text = "Server started! Point Skynet Client to: ws://" + SkynetServer.LocalIPAddress().ToString() + ":" + SkynetServer.SettingsCopy.LocalServerPort + "/Skynet"; Program.Log.Insert(DateTime.Now.ToString(), "Server started; running on: ws://" + SkynetServer.LocalIPAddress().ToString() + ":" + SkynetServer.SettingsCopy.LocalServerPort + "/Skynet"); }; serverStatusLabel.ForeColor = Color.Green; serverStatusLabel.Text = "Server running!"; SkynetServer.Start(); startStopButton.Text = "Stop"; SetControlState(false); } else { Program.Log.Insert(DateTime.Now.ToString(), "Stopping server..."); SetControlState(true); SkynetServer.Stop(WebSocketSharp.CloseStatusCode.Normal, "UI Asked to close"); serverStatusLabel.ForeColor = Color.Red; serverStatusLabel.Text = "User stopped server!"; startStopButton.Text = "Start"; } }
protected override void OnMessage(MessageEventArgs e) { Log.Debug(e.Data); if (e.Data == "GETSHOT") { if (SkynetServer.SettingsCopy != null && SkynetServer.SettingsCopy.AllowScreenshots) { string msg = JsonConvert.SerializeObject( new { type = "image", image = Convert.ToBase64String(Conversions.ImageToByte2(Screenshot.GetShotOfDesktop())), timestamp = DateTime.Now }); Program.Log.Insert(DateTime.Now.ToString(), "Responding to " + e.Data); Send(msg); } else { string msg = JsonConvert.SerializeObject(new { type = "error", reason = "AllowScreenshots not enabled.", timestamp = DateTime.Now }); Program.Log.Insert(DateTime.Now.ToString(), "Denying " + e.Data); Send(msg); } } else if (e.Data.StartsWith("{") && e.Data.EndsWith("}")) { JObject msgAsJson = JObject.Parse(e.Data); if (msgAsJson["type"].ToString() == "notice") { Program.mainForm.SendNotice(msgAsJson["message"].ToString(), msgAsJson["timeout"].ToObject <int>() == 0 ? 5000 : msgAsJson["timeout"].ToObject <int>()); Program.Log.Insert(DateTime.Now.ToString(), "Received notice by JSON"); Program.Log.Insert(DateTime.Now.ToString(), " Message: " + msgAsJson["message"].ToString()); Program.Log.Insert(DateTime.Now.ToString(), " Timeout: " + msgAsJson["timeout"].ToString() + "ms"); } else if (msgAsJson["type"].ToString() == "getframe") { if (SkynetServer.SettingsCopy != null && SkynetServer.SettingsCopy.AllowScreenshots) { string msg = JsonConvert.SerializeObject( new { type = "image", image = Convert.ToBase64String(Conversions.ImageToByte2(Screenshot.GetShotOfDesktop())), timestamp = DateTime.Now }); Program.Log.Insert(DateTime.Now.ToString(), "Responding to JSON with type getframe"); Send(msg); } else { string msg = JsonConvert.SerializeObject(new { type = "error", reason = "AllowScreenshots not enabled.", timestamp = DateTime.Now }); Program.Log.Insert(DateTime.Now.ToString(), "Denying getframe by JSON"); Send(msg); } } else if (msgAsJson["type"].ToString() == "getinfo") { string msg = JsonConvert.SerializeObject(new { type = "sysinfo", os = s.OperatingSystem, cpu = s.Processor, gpu = s.VideoCard, ram = s.TotalRAMAvailable, name = Environment.MachineName, localip = SkynetServer.LocalIPAddress().ToString(), timestamp = DateTime.Now }); Program.Log.Insert(DateTime.Now.ToString(), "Responding to getinfo by JSON"); Send(msg); } } else if (e.Data == "GETINFO") { string msg = JsonConvert.SerializeObject(new { type = "sysinfo", os = s.OperatingSystem, cpu = s.Processor, gpu = s.VideoCard, ram = s.TotalRAMAvailable, name = Environment.MachineName, localip = SkynetServer.LocalIPAddress().ToString(), timestamp = DateTime.Now }); Program.Log.Insert(DateTime.Now.ToString(), "Responding to " + e.Data); Send(msg); } }