public Config(bool daemon = false) { Daemon = daemon; Root = ExecutableTools.Relative(@"Root"); WebRoot = PathTools.Combine(Root, "Web"); WebIP = "127.0.0.1"; WebPort = 12334; ShellRoot = PathTools.Combine(Root, "Test_{0}", TimeTools.Compact(DateTime.Now)); ShellIP = "127.0.0.1"; ShellPort = 12333; Directory.CreateDirectory(WebRoot); Directory.CreateDirectory(ShellRoot); var writers = new WriteLineCollection(); writers.Add(new StdoutWriteLine()); var logfile = PathTools.Combine(Root, "log.txt"); log = new StreamWriter(logfile, true); writers.Add(new TextWriterWriteLine(log)); Timed = new TimedWriter(writers); Logger.TRACE = Timed; writers.WriteLine(string.Empty); //separating line writers.WriteLine(""); Logger.Trace("-----------------------------------------------------------------------------"); Logger.Trace("Test case {0} starting...", TestContext.CurrentContext.Test.FullName); //System.InvalidOperationException : This property has already been set and cannot be modified. //Thread.CurrentThread.Name = "NUnit"; }
public Listener(Args args) { var certfile = ExecutableTools.Relative("DaemonManager.pfx"); certificate = new X509Certificate2(certfile, "none"); factory = args.ShellFactory; clients = new Dictionary <string, ClientRt>(); server = new TcpListener(args.EndPoint); server.MakeNotInheritable(); using (var disposer = new Disposer()) { //push must match dispose order register = new Runner(new Runner.Args { ThreadName = "Register" }); disposer.Push(register); accepter = new Runner(new Runner.Args { ThreadName = "Accepter" }); disposer.Push(accepter); disposer.Push(Dispose); //ensure cleanup order server.Start(); endpoint = server.LocalEndpoint as IPEndPoint; disposer.Clear(); } }
public static void Shell(Config config, Action <ITestShell> action) { Directory.CreateDirectory(config.Root); var path = ExecutableTools.Relative("SharpDaemon.Server.exe"); var args = $"Id=test Daemon={config.Daemon} Port={config.ShellPort} IP={config.ShellIP} Root=\"{config.ShellRoot}\""; Shell(path, args, action); }
private void ExecutePassword(IStream io, params string[] tokens) { var password = tokens[2]; var passfile = ExecutableTools.Relative("Password.txt"); File.WriteAllText(passfile, password); io.WriteLine("Password changed"); }
protected override void OnStart(string[] args) { base.OnStart(args); process = new DaemonProcess(new DaemonProcess.Args { Executable = ExecutableTools.Relative("SharpDaemon.Server.exe"), Arguments = "Daemon=True", }); }
public void ShellSystemPasswordTest() { using (var config = new Config()) { TestTools.Shell(config, (shell) => { var passfile = ExecutableTools.Relative("Password.txt"); var password = File.ReadAllText(passfile).Trim(); shell.Execute($@"system password {password}"); shell.WaitFor(400, @"<o Password changed"); }); } }
public Simple() { var writers = new WriteLineCollection(); writers.Add(new StdoutWriteLine()); var logfile = ExecutableTools.Relative("log.txt"); log = new StreamWriter(logfile, true); writers.Add(new TextWriterWriteLine(log)); Logger.TRACE = new TimedWriter(writers); writers.WriteLine(string.Empty); //separating line writers.WriteLine(""); Logger.Trace("-----------------------------------------------------------------------------"); Logger.Trace("Test case {0} starting...", TestContext.CurrentContext.Test.FullName); }
public void DaemonPingAndExitTest() { using (var simple = new Simple()) { var path = ExecutableTools.Relative("Daemon.Test.exe"); var args = "Daemon=false"; TestTools.Shell(path, args, (shell) => { shell.WaitFor(400, @"<o Ready"); shell.Execute("ping"); shell.WaitFor(400, @"<o pong"); shell.Execute("exit!"); shell.WaitFor(800, Environ.NewLines); }); } }
public static void Web(Config config, Action action) { Directory.CreateDirectory(config.WebRoot); var zippath = PathTools.Combine(config.WebRoot, "Daemon.StaticWebServer.zip"); if (File.Exists(zippath)) { File.Delete(zippath); } Logger.Trace("Zipping to {0}", zippath); ZipTools.ZipFromFiles(zippath, ExecutableTools.Directory() , "Daemon.StaticWebServer.exe" , "SharpDaemon.dll" ); var process = new DaemonProcess(new DaemonProcess.Args { Executable = ExecutableTools.Relative("Daemon.StaticWebServer.exe"), Arguments = $"EndPoint={config.WebEP} Root=\"{config.WebRoot}\"", }); Logger.Trace("Web process {0} {1} {2} {3}", process.Id, process.Name, process.Info.FileName, process.Info.Arguments); var reader = new Runner(new Runner.Args { ThreadName = "WEB" }); reader.Run(() => { var line = process.ReadLine(); while (line != null) { Logger.Trace("<w {0}", line); line = process.ReadLine(); } }); using (reader) { using (process) { action(); } } }
private void ReadLoop() { using (client) { var stream = new SslStream(client.GetStream()); stream.AuthenticateAsServer(certificate); var reader = new TextReaderReadLine(new StreamReader(stream)); var writer = new TextWriterWriteLine(new StreamWriter(stream)); if (!endpoint.ToString().StartsWith("127.0.0.1")) { var passfile = ExecutableTools.Relative("Password.txt"); var password = File.ReadAllText(passfile).Trim(); if (password != reader.ReadLine()) { return; } } ReadLoop(client, new ShellStream(writer, reader)); } }
static void Main(string[] args) { ProgramTools.Setup(); var config = new Config(); config.IP = "0.0.0.0"; config.Port = 22333; config.Delay = 5000; config.Timeout = 5000; config.Root = ExecutableTools.Relative("Root"); //args will always log to stderr because daemon flag not loaded yet ExecutableTools.LogArgs(new StderrWriteLine(), args, (arg) => { ConfigTools.SetProperty(config, arg); }); AssertTools.NotEmpty(config.Root, "Missing Root path"); AssertTools.NotEmpty(config.IP, "Missing IP"); AssertTools.Ip(config.IP, "Invalid IP"); var pid = Process.GetCurrentProcess().Id; var writers = new WriteLineCollection(); if (!config.Daemon) { writers.Add(new StderrWriteLine()); Logger.TRACE = new TimedWriter(writers); } Logger.Trace("Root {0}", config.Root); var dbpath = Path.Combine(config.Root, "SharpDaemon.LiteDb"); var logpath = Path.Combine(config.Root, "SharpDaemon.Log.txt"); var eppath = Path.Combine(config.Root, "SharpDaemon.Endpoint.txt"); var downloads = Path.Combine(config.Root, "Downloads"); Directory.CreateDirectory(downloads); //creates root as well //acts like mutex for the workspace var log = new StreamWriter(logpath, true); writers.Add(new TextWriterWriteLine(log)); ExecutableTools.LogArgs(new TextWriterWriteLine(log), args); var instance = new Instance(new Instance.Args { DbPath = dbpath, RestartDelay = config.Delay, Downloads = downloads, EndPoint = new IPEndPoint(IPAddress.Parse(config.IP), config.Port), }); Stdio.SetStatus("Listening on {0}", instance.EndPoint); using (var disposer = new Disposer()) { //wrap to add to disposable count disposer.Push(new Disposable.Wrapper(log)); disposer.Push(instance); disposer.Push(() => Disposing(config.Timeout)); if (config.Daemon) { Logger.Trace("Stdin loop..."); var line = Stdio.ReadLine(); while (line != null) { Logger.Trace("> {0}", line); if ("exit!" == line) { break; } line = Stdio.ReadLine(); } Logger.Trace("Stdin closed"); } else { Logger.Trace("Stdin loop..."); var shell = instance.CreateShell(); var stream = new ShellStream(new StdoutWriteLine(), new ConsoleReadLine()); //disposer.Push(stream); //stdin.readline wont return even after close/dispose call var line = stream.ReadLine(); while (line != null) { if ("exit!" == line) { break; } Shell.ParseAndExecute(shell, stream, line); line = stream.ReadLine(); } Logger.Trace("Stdin closed"); } } Environment.Exit(0); }