public static bool Run(System.IO.DirectoryInfo info, int port, string configFile = null, bool? encryptData = null) { BaseDirectory = info; Area ws = Area.Load(info, true); if (ws == null) { Versionr.Utilities.MultiArchPInvoke.BindDLLs(); } Config = new ServerConfig(); if (!string.IsNullOrEmpty(configFile)) { ConfigFile = configFile; LoadConfig(); } if ((Config.IncludeRoot.HasValue && Config.IncludeRoot.Value) || (!Config.IncludeRoot.HasValue && Domains.Count == 0)) Domains[string.Empty] = new DomainInfo() { Bare = ws == null, Directory = info }; bool enableEncryption = encryptData.HasValue ? encryptData.Value : Config.Encrypted; if (enableEncryption) { Printer.PrintDiagnostics("Creating RSA pair..."); System.Security.Cryptography.RSACryptoServiceProvider rsaCSP = new System.Security.Cryptography.RSACryptoServiceProvider(); rsaCSP.KeySize = 2048; PrivateKey = rsaCSP; PrivateKeyData = rsaCSP.ExportParameters(true); PublicKey = rsaCSP.ExportParameters(false); Printer.PrintDiagnostics("RSA Fingerprint: {0}", PublicKey.Fingerprint()); } System.Net.Sockets.TcpListener listener = null; #if __MonoCS__ listener = new TcpListener(System.Net.IPAddress.Any, port); #else try { if (System.Net.Sockets.Socket.OSSupportsIPv6) { listener = new System.Net.Sockets.TcpListener(System.Net.IPAddress.IPv6Any, port); listener.Server.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0); } else listener = new TcpListener(System.Net.IPAddress.Any, port); } catch { listener = new TcpListener(System.Net.IPAddress.Any, port); } #endif Printer.PrintDiagnostics("Binding to {0}.", listener.LocalEndpoint); listener.Start(); if (Config.WebService != null && Config.WebService.HttpPort != 0 && false) { Task.Run(() => { RunWebServer(); }); } Printer.PrintMessage("Server started, bound to #b#{0}##.", listener.LocalEndpoint); while (true) { Printer.PrintDiagnostics("Waiting for connection."); var client = listener.AcceptTcpClientAsync(); Retry: if (client.Wait(5000)) { Task.Run(() => { Printer.PrintMessage("Received connection from {0}.", client.Result.Client.RemoteEndPoint); HandleConnection(info, client.Result); }); } else goto Retry; } listener.Stop(); return true; }
public RestService(ServerConfig config, System.IO.DirectoryInfo info) { Config = config; RestInterface.Info = info; }