public HttpCode Execute() { Clear(); // Connect NetworkChannel channel = null; try { channel = new NetworkChannel(Connection); // Request JsonPingRequestMessage jsonRequestMessage = new JsonPingRequestMessage(); JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage); HttpRequest httpRequest = new HttpRequest(Session.Id) { Data = Session.Encrypt(jsonRequest) }; // Response HttpResponse httpResponse; if (Owner) { channel.Send(httpRequest); channel.Receive(out httpResponse); } else { lock (Socket) { channel.Send(httpRequest); channel.Receive(out httpResponse); } } Code = httpResponse.Code; if (httpResponse.Ok) { JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data)); #if DEBUG Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif } } finally { if (channel != null) { channel.Shutdown(); } } return(Code); }
public List <BrowseComponent> Execute() { Clear(); NetworkChannel channel = null; List <BrowseComponent> list = new List <BrowseComponent>(); try { // Connect channel = new NetworkChannel(Connection); // Request JsonGroupRequestMessage jsonRequestMessage = new JsonGroupRequestMessage(); JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage); HttpRequest httpRequest = new HttpRequest(Session.Id) { Data = Session.Encrypt(jsonRequest) }; channel.Send(httpRequest); // Response HttpResponse httpResponse; channel.Receive(out httpResponse); Code = httpResponse.Code; if (httpResponse.Ok) { JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data)); JsonGroupResponseMessage jsonResponseMessage = JsonGroupResponseMessage.Parse(jsonResponse.Message); if (jsonResponseMessage != null) { // Clients foreach (JsonClient jsonClient in jsonResponseMessage.Clients) { BrowseComponent browse = new BrowseComponent(jsonClient.Name) { ClientId = jsonClient.Id, Owner = Entity }; list.Add(browse); } } #if DEBUG Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif } } finally { if (channel != null) { channel.Shutdown(); } } return(list); }
public HttpCode Execute() { Clear(); // Connect NetworkChannel channel = null; try { channel = new NetworkChannel(Connection); // Request JsonEntity jsonEntity = (JsonEntity)Entity; JsonClient jsonClient = (JsonClient)Client; JsonGroup jsonGroup = (JsonGroup)Group; JsonJoinRequestMessage jsonRequestMessage = new JsonJoinRequestMessage() { Entity = jsonEntity, Client = jsonClient, Group = jsonGroup }; JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage); HttpRequest httpRequest = new HttpRequest(Session.Id) { Data = Session.Encrypt(jsonRequest) }; channel.Send(httpRequest); // Response HttpResponse httpResponse; channel.Receive(out httpResponse); Code = httpResponse.Code; if (httpResponse.Ok) { JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data)); JsonJoinResponseMessage jsonResponseMessage = JsonJoinResponseMessage.Parse(jsonResponse.Message); // Data Server.Entities = jsonResponseMessage.Entities; Server.Sessions = jsonResponseMessage.Sessions; Server.Clients = jsonResponseMessage.Clients; Server.Groups = jsonResponseMessage.Groups; #if DEBUG Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif } } finally { if (channel != null) { channel.Shutdown(); } } return(Code); }
private void ExecuteThread() { NetworkChannel channel = null; try { // Connect channel = new NetworkChannel(Connection); // Request JsonSearchRequestMessage jsonRequestMessage = new JsonSearchRequestMessage(); JsonSearch jsonSearch = new JsonSearch() { Keyword = Keyword, Filter = Filter }; JsonSearchRequestData jsonRequestData = new JsonSearchRequestData() { Search = jsonSearch }; JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage, Group.Encrypt(jsonRequestData)); HttpRequest httpRequest = new HttpRequest(Session.Id) { Data = Session.Encrypt(jsonRequest) }; channel.Send(httpRequest); // Response HttpResponse httpResponse; channel.Receive(out httpResponse); Code = httpResponse.Code; if (httpResponse.Ok) { JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data)); JsonSearchResponseMessage jsonResponseMessage = JsonSearchResponseMessage.Parse(jsonResponse.Message); Debug.Assert(!string.IsNullOrEmpty(jsonResponseMessage.Id)); // Data SearchList.Id = jsonResponseMessage.Id; #if DEBUG Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (channel != null) { channel.Shutdown(); } } }
public HttpCode Execute(string jsonId, FileComponent file, string clientId = null) { Clear(); // Connect NetworkChannel channel = new NetworkChannel(Connection); // Request JsonChunk jsonChunk = new JsonChunk() { Size = file.Size }; JsonClient jsonClient = (clientId != null) ? new JsonClient(clientId) : null; JsonDownloadRequestMessage jsonRequestMessage = new JsonDownloadRequestMessage(jsonId) { Chunk = jsonChunk, Client = jsonClient }; JsonFile jsonFile = (JsonFile)file; JsonDownloadRequestData jsonRequestData = new JsonDownloadRequestData() { File = jsonFile }; JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage, Group.Encrypt(jsonRequestData)); HttpRequest httpRequest = new HttpRequest(Session.Id) { Data = Session.Encrypt(jsonRequest) }; channel.Send(httpRequest); // Response HttpResponse httpResponse; channel.Receive(out httpResponse); Code = httpResponse.Code; if (httpResponse.Ok) { JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data)); JsonDownloadResponseMessage jsonResponseMessage = JsonDownloadResponseMessage.Parse(jsonResponse.Message); Chunk = jsonResponseMessage.Chunk; Id = jsonResponseMessage.Id; JsonDownloadResponseData jsonResponseData = JsonDownloadResponseData.Parse(Group.Decrypt(jsonResponse.Data)); if (jsonResponseData != null) { Data = jsonResponseData.Data; } #if DEBUG jsonResponse.Data = null; Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif } return(httpResponse.Code); }
private void ExecuteThread() { Clear(); // Connect NetworkChannel channel = null; try { channel = new NetworkChannel(Connection); // Request JsonInfoRequestMessage jsonRequestMessage = new JsonInfoRequestMessage(); JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage); HttpRequest httpRequest = new HttpRequest(Session.Id) { Data = Session.Encrypt(jsonRequest) }; channel.Send(httpRequest); // Response HttpResponse httpResponse; channel.Receive(out httpResponse); Code = httpResponse.Code; if (httpResponse.Ok) { JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data)); JsonInfoResponseMessage jsonResponseMessage = JsonInfoResponseMessage.Parse(jsonResponse.Message); // Data Server.Entities = jsonResponseMessage.Entities; Server.Sessions = jsonResponseMessage.Sessions; Server.Clients = jsonResponseMessage.Clients; Server.Groups = jsonResponseMessage.Groups; #if DEBUG Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif } } catch (Exception ex) { Console.WriteLine(ex.Message); Machine.Status = DemonStatus.Warning; } finally { if (channel != null) { channel.Shutdown(); } } }
public JsonChunk Execute(string jsonId, byte[] jsonData, JsonChunk jsonChunk) { Clear(); // Connect NetworkChannel channel = new NetworkChannel(Connection); // Request JsonUploadRequestMessage jsonRequestMessage = new JsonUploadRequestMessage(jsonId) { Chunk = jsonChunk }; JsonUploadRequestData jsonRequestData = new JsonUploadRequestData() { Data = jsonData }; JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage, Group.Encrypt(jsonRequestData)); HttpRequest httpRequest = new HttpRequest(Session.Id) { Data = Session.Encrypt(jsonRequest) }; channel.Send(httpRequest); // Response HttpResponse httpResponse; channel.Receive(out httpResponse); Code = httpResponse.Code; if (httpResponse.Ok) { JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data)); JsonUploadResponseMessage jsonResponseMessage = JsonUploadResponseMessage.Parse(jsonResponse.Message); jsonChunk = jsonResponseMessage.Chunk; #if DEBUG jsonResponse.Data = null; Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif if (jsonResponseMessage.Delay > 0) { Thread.Sleep(TimeSpan.FromMilliseconds(jsonResponseMessage.Delay)); } } return(jsonChunk); }
private void ExecuteThread() { // Connect NetworkChannel channel = null; try { channel = new NetworkChannel(Connection); // Request JsonPingRequestMessage jsonRequestMessage = new JsonPingRequestMessage(); JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage); HttpRequest httpRequest = new HttpRequest(Session.Id) { Data = Session.Encrypt(jsonRequest) }; channel.Send(httpRequest); // Response HttpResponse httpResponse; channel.Receive(out httpResponse); Code = httpResponse.Code; if (httpResponse.Ok) { JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data)); #if DEBUG Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif } } catch (Exception ex) { Console.WriteLine(ex.Message); if (Owner && Machine != null) { Machine.Restart(); } } finally { if (channel != null) { channel.Shutdown(); } } }
public HttpCode Execute() { Clear(); // Connect NetworkChannel channel = null; try { channel = new NetworkChannel(Connection); // Request HttpRequest httpRequest = new HttpRequest() { Data = SecurityUtil.Token }; channel.Send(httpRequest); // Response HttpResponse httpResponse; channel.Receive(out httpResponse); Code = httpResponse.Code; if (httpResponse.Ok) { // Data Session.Id = httpResponse.Session; Session.Key = Key.Parse(httpResponse.Data); #if DEBUG Log.Add(httpRequest, httpResponse); #endif } } finally { if (channel != null) { channel.Shutdown(); } } return(Code); }
public string Execute(string jsonData) { Clear(); // Connect NetworkChannel channel = new NetworkChannel(Connection); // Request JsonSearchRequestMessage jsonRequestMessage = new JsonSearchRequestMessage(); JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage, jsonData); HttpRequest httpRequest = new HttpRequest() { Data = Session.Encrypt(jsonRequest) }; // Response HttpResponse httpResponse; // NOTE: We need to lock this send/receive message pairs lock (Socket) { channel.Send(httpRequest); channel.Receive(out httpResponse); } Code = httpResponse.Code; if (httpResponse.Ok) { JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data)); #if DEBUG Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif // Data return(jsonResponse.Data); } return(null); }
public HttpCode Execute(string jsonId, string jsonData, JsonChunk jsonChunk) { Clear(); // Connect NetworkChannel channel = new NetworkChannel(Connection); // Request JsonDownloadRequestMessage jsonRequestMessage = new JsonDownloadRequestMessage(jsonId) { Chunk = jsonChunk }; JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage, jsonData); HttpRequest httpRequest = new HttpRequest() { Data = Session.Encrypt(jsonRequest) }; // Response HttpResponse httpResponse; lock (Socket) { channel.Send(httpRequest); channel.Receive(out httpResponse); } Code = httpResponse.Code; if (httpResponse.Ok) { JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data)); #if DEBUG Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif } return(Code); }
private void ListenState() { NetworkChannel channel = null; try { // Connect if (!Connected) { State = DemonState.Restart; Status = DemonStatus.Info; return; } // NOTE: No need to close the channel channel = new NetworkChannel(Connection); Status = DemonStatus.Success; // Request HttpRequest httpRequest; channel.Receive(out httpRequest); if (httpRequest == null) { Status = DemonStatus.Warning; return; } // TODO: Version check // // Message SessionComponent session = Owner.Get <SessionComponent>(); string decrypted = session.Decrypt(httpRequest.Data); JsonPacket jsonRequest = JsonPacket.Parse(decrypted); JsonMessage jsonRequestMessage = JsonMessage.Parse(jsonRequest.Message); JsonType jsonType = jsonRequestMessage.Type; switch (jsonType) { case JsonType.Ping: { PingResponseCommand command = new PingResponseCommand(Owner, Connection); command.Execute(httpRequest, jsonRequest); break; } case JsonType.Search: { JsonAction action = jsonRequestMessage.Action; switch (action) { case JsonAction.Request: { SearchRequestCommand command = new SearchRequestCommand(Owner, Connection); command.Execute(httpRequest, jsonRequest); break; } case JsonAction.Response: { SearchResponseCommand command = new SearchResponseCommand(Owner, Connection); List <FileComponent> list = command.Execute(httpRequest, jsonRequest); OnListAdded(list); break; } default: { channel.SendBadRequest(); break; } } break; } case JsonType.Browse: { BrowseResponseCommand command = new BrowseResponseCommand(Owner, Connection); command.Execute(httpRequest, jsonRequest); break; } case JsonType.Download: { // TODO // DownloadResponseCommand command = new DownloadResponseCommand(Owner, Connection); // { Handler = UploadEvent }; command.Execute(httpRequest, jsonRequest); break; } default: { channel.SendBadRequest(); State = DemonState.Restart; Status = DemonStatus.Info; return; } } } catch (Exception ex) { Console.WriteLine(ex.Message); Status = DemonStatus.Error; if (channel != null) { try { channel.SendInternalServerError(); } catch (Exception) { } } } }
private void ProcessState(object obj) { Socket socket = (Socket)obj; NetworkChannel channel = null; JsonAction action = JsonAction.None; JsonType jsonType = JsonType.None; try { do { // Connect channel = new NetworkChannel(socket); // Receive HttpRequest httpRequest; channel.Receive(out httpRequest); if (httpRequest == null) { channel.SendBadRequest(); return; } // Data Status = DemonStatus.Success; // Handshake if (httpRequest.Session == null) { ServerStatusComponent status = Owner.Get <ServerStatusComponent>(); HandshakeResponseCommand command = new HandshakeResponseCommand(Owner, socket) { Listener = status.Update }; command.Execute(httpRequest); break; } // Session SessionMapComponent sessions = Owner.Get <SessionMapComponent>(); Entity entity = sessions.Get(httpRequest.Session); if (entity == null) { channel.SendUnauthorized(); break; } // TODO: Version check // // Message SessionComponent session = entity.Get <SessionComponent>(); string decrypted = session.Decrypt(httpRequest.Data); JsonPacket jsonRequest = JsonPacket.Parse(decrypted); JsonMessage jsonRequestMessage = JsonMessage.Parse(jsonRequest.Message); jsonType = jsonRequestMessage.Type; switch (jsonType) { case JsonType.Handshake: { HandshakeResponseCommand command = new HandshakeResponseCommand(Owner, socket); command.Execute(httpRequest, jsonRequest, session); break; } case JsonType.Ping: { PingResponseCommand command = new PingResponseCommand(Owner, socket); command.Execute(httpRequest, jsonRequest, session); break; } case JsonType.Info: { InfoResponseCommand command = new InfoResponseCommand(Owner, socket); command.Execute(httpRequest, jsonRequest, session); break; } case JsonType.Join: { JoinResponseCommand command = new JoinResponseCommand(Owner, socket); command.Execute(httpRequest, jsonRequest, session); break; } case JsonType.Tunnel: { TunnelResponseCommand command = new TunnelResponseCommand(Owner, socket); command.Execute(httpRequest, jsonRequest, session); break; } case JsonType.Search: { SearchResponseCommand command = new SearchResponseCommand(Owner, socket); command.Execute(httpRequest, jsonRequest, session); break; } case JsonType.Group: { GroupResponseCommand command = new GroupResponseCommand(Owner, socket); command.Execute(httpRequest, jsonRequest, session); break; } case JsonType.Browse: { BrowseResponseCommand command = new BrowseResponseCommand(Owner, socket); command.Execute(httpRequest, jsonRequest, session); break; } case JsonType.Download: { // TODO: Add max transfer check! // DownloadResponseCommand command = new DownloadResponseCommand(Owner, socket); action = command.Execute(httpRequest, jsonRequest, session); break; } case JsonType.Upload: { UploadResponseCommand command = new UploadResponseCommand(Owner, socket); action = command.Execute(httpRequest, jsonRequest, session); break; } case JsonType.Quit: { QuitResponseCommand command = new QuitResponseCommand(Owner, socket); command.Execute(httpRequest, jsonRequest, session); break; } default: { channel.SendBadRequest(); return; } } }while (action != JsonAction.None); } catch (Exception ex) { Console.WriteLine(ex.Message); Status = DemonStatus.Error; channel.SendInternalServerError(); } finally { if (channel != null) { channel.Shutdown(); } if (jsonType != JsonType.Tunnel) { socket.Shutdown(SocketShutdown.Both); socket.Close(); } } }
public List <BrowseComponent> Execute(string clientId, string fileId) { Clear(); NetworkChannel channel = null; try { // Connect channel = new NetworkChannel(Connection); // Request JsonClient jsonClient = new JsonClient(clientId); JsonBrowseRequestMessage jsonRequestMessage = new JsonBrowseRequestMessage() { Client = jsonClient }; JsonFile jsonFolder = !string.IsNullOrEmpty(fileId) ? new JsonFile(fileId) : null; JsonBrowseRequestData jsonRequestData = new JsonBrowseRequestData() { Folder = jsonFolder }; JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage, Group.Encrypt(jsonRequestData)); HttpRequest httpRequest = new HttpRequest(Session.Id) { Data = Session.Encrypt(jsonRequest) }; channel.Send(httpRequest); // Response HttpResponse httpResponse; channel.Receive(out httpResponse); Code = httpResponse.Code; if (httpResponse.Ok) { JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data)); JsonBrowseResponseMessage jsonResponseMessage = JsonBrowseResponseMessage.Parse(jsonResponse.Message); Debug.Assert(jsonResponseMessage != null); JsonBrowseResponseData jsonResponseData = JsonBrowseResponseData.Parse(Group.Decrypt(jsonResponse.Data)); Debug.Assert(jsonResponseData != null); // Data List <BrowseComponent> list = new List <BrowseComponent>(); // Folders if (jsonResponseData.Folders != null) { foreach (JsonFile json in jsonResponseData.Folders) { BrowseComponent comp = new BrowseComponent(json.Id, json.Name) { ClientId = jsonClient.Id, Owner = Entity }; list.Add(comp); } } // Files if (jsonResponseData.Files != null) { foreach (JsonFile json in jsonResponseData.Files) { BrowseComponent comp = new BrowseComponent(json.Id, json.Name, json.Size) { ClientId = jsonClient.Id, Owner = Entity }; list.Add(comp); } } #if DEBUG Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif return(list); } } finally { if (channel != null) { channel.Shutdown(); } } return(null); }
private void Listen() { NetworkChannel channel = null; try { // Connect if (!Connected) { Data.Status = DemonStatus.Info; State = DemonState.Restart; return; } // NOTE: No need to close the channel channel = new NetworkChannel(Connection); Data.Status = DemonStatus.Success; // Response HttpRequest httpRequest; channel.Receive(out httpRequest); if (httpRequest == null) { return; } string decrypted = Data.Session.Decrypt(httpRequest.Data); JsonPacket jsonRequest = JsonPacket.Parse(decrypted); if (jsonRequest == null) { return; } JsonMessage jsonRequestMessage = JsonMessage.Parse(jsonRequest.Message); switch (jsonRequestMessage.Type) { case JsonType.Ping: { //ClientPingCommand command = new ClientPingCommand(Connection, Data); //command.SetLogHandlers(RequestHandler, ResponseHandler, CommandHandler); //command.Execute(httpRequest, jsonRequest); break; } case JsonType.Search: { JsonAction action = jsonRequestMessage.Action; switch (action) { case JsonAction.Request: { //ClientSearchCommand command = new ClientSearchCommand(Connection, Data); //command.SetLogHandlers(RequestHandler, ResponseHandler, CommandHandler); //command.Execute(httpRequest, jsonRequest); break; } case JsonAction.Response: { //ClientSearchCommand2 command = new ClientSearchCommand2(Connection, Data) { SearchHandler = SearchEvent }; //command.SetLogHandlers(RequestHandler, ResponseHandler, CommandHandler); //command.Execute(httpRequest, jsonRequest); break; } default: { channel.SendBadRequest(); break; } } break; } case JsonType.Browse: { //ClientBrowseCommand command = new ClientBrowseCommand(Connection, Data); //command.SetLogHandlers(RequestHandler, ResponseHandler, CommandHandler); //command.Execute(httpRequest, jsonRequest); break; } case JsonType.Download: { //ClientDownloadCommand command = new ClientDownloadCommand(Connection, Data) { Handler = UploadEvent }; //command.SetLogHandlers(RequestHandler, ResponseHandler, CommandHandler); //command.Execute(httpRequest, jsonRequest); break; } default: { channel.SendBadRequest(); Data.Status = DemonStatus.Info; State = DemonState.Restart; return; } } Data.Status = DemonStatus.Success; } catch (Exception ex) { Console.WriteLine(ex.Message); Data.Status = DemonStatus.Info; State = DemonState.Restart; if (channel != null) { try { channel.SendInternalServerError(); } catch (Exception) { } } } }