예제 #1
0
        private void StartConsole()
        {
            inputLinesBuffer = new BlockingCollection <string>();
            proxy            = new ConsoleProxy(this);

            var controller = MainActivity.Service?.Controller;

            if (controller == null)
            {
                appendText("Cannot initialize console: the controller is not running. Please start the service and try again.\n", Color.Red);
                return;
            }

            consoleHub = new ConsoleHub();
            Commands.AddCommands(consoleHub.CommandHub, controller, "", new[] { "all" });
            new System.Threading.Thread(() => {
                try {
                    consoleHub.CommandHub.CmdLoop(proxy);
                } catch (System.Exception e) {
                    Logging.exception(e, Logging.Level.Warning, "ConsoleCmdLoop thread");
                }
            })
            {
                Name = "ConsoleCmdLoop"
            }.Start();
        }
예제 #2
0
 protected override void OnStart()
 {
     base.OnStart();
     if (passwd.IsNullOrEmpty() && no_passwd == false)
     {
         Logger.warning("'passwd' is null or empty while 'no_passwd' is false!");
     }
     if (consoleHub == null)
     {
         consoleHub = new ConsoleHub();
         var cmdHub = consoleHub.CommandHub;
         Commands.AddCommands(cmdHub, Controller, "", cmds.ToArray());
     }
 }
예제 #3
0
        static void MainContinued(ArgParseResult ar, Controller controller)
        {
            LogFileWriter logWriter = null;

            ar.TryGetValue("--cmd", out var argcmd);
            if (argcmd == null)
            {
                Logging.info(NameWithVertionText);
                if (GuiMode)
                {
                    Logging.info("(GUI mode)");
                    LogStdout = false;
                }
            }
            if (ar.TryGetValue("--log-file", out var logFile))
            {
                Logging.info($"Logging file: {logFile.FirstParaOrThrow}");
                logWriter = new LogFileWriter(logFile.FirstParaOrThrow, Logging.RootLogger);
                logWriter.Start();
                logWriter.WriteHistoryLog();
            }
            if (ar.TryGetValue("-c", out var v))
            {
                specifiedConfigPath = v.FirstParaOrThrow;
                Logging.info($"configuation file: {specifiedConfigPath}");
            }
            else if (ar.ContainsKey("--config-stdin"))
            {
                Logging.info("reading configuration from stdin until EOF...");
                specifiedConfigContent = Console.In.ReadToEnd();
                Logging.info($"configuration read {specifiedConfigContent.Length} chars");
            }

            if (ar.TryGetValue("--socket-impl", out var socketImpl))
            {
                MyStream.SetSocketImpl(socketImpl.FirstParaOrThrow);
            }

            long lastPackets = 0, lastBytes = 0;

            void updateTitle()
            {
                lock (CmdConsole.ConsoleOnStdIO.Lock) {
                    var p = MyStream.TotalCopiedPackets;
                    var b = MyStream.TotalCopiedBytes;
                    Console.Title = $"{controller.RunningConnections}/{controller.TotalHandledConnections} current/total connections | relayed {p:N0} Δ{p - lastPackets:N0} packets / {b:N0} Δ{b - lastBytes:N0} bytes - {BuildInfo.AppName}";
                    lastPackets   = p;
                    lastBytes     = b;
                }
            }

            bool  titleUpdateRunning = false;
            Timer titleUpdateTimer   = null;

            titleUpdateTimer = new Timer((x) => {
                if (titleUpdateRunning)
                {
                    updateTitle();
                }
                else
                {
                    titleUpdateTimer.Change(-1, -1);
                    Console.Title = BuildInfo.AppName;
                }
            });
            controller.ConfigTomlLoaded += (x) => {
                if (x.TryGetValue <string>("log_file", out var log_file))
                {
                    log_file = controller.ProcessFilePath(log_file);
                    if (logWriter?.LogFile != log_file)
                    {
                        logWriter?.Stop();
                        if (log_file != null)
                        {
                            logWriter = new LogFileWriter(log_file, Logging.RootLogger);
                            logWriter.Start();
                        }
                    }
                }
                var toRun = x.TryGetValue("update_title", Environment.OSVersion.Platform == PlatformID.Win32NT) && !GuiMode;
                if (toRun != titleUpdateRunning)
                {
                    if (toRun)
                    {
                        titleUpdateRunning = true;
                        titleUpdateTimer.Change(1000, 1000);
                    }
                    else
                    {
                        titleUpdateRunning = false;
                    }
                }
            };

            if (ar.ContainsKey("--force-jit"))
            {
                ForceJit();
            }
            else if (ar.ContainsKey("--force-jit-async"))
            {
                Task.Run(() => ForceJit());
            }


            if (argcmd == null || specifiedConfigPath != null || specifiedConfigContent != null)
            {
                if (specifiedConfigPath != null)
                {
                    Commands.loadController(controller, specifiedConfigPath);
                }
                else if (specifiedConfigContent != null)
                {
                    controller.FuncGetConfigFile = () => Controller.ConfigFile.FromContent(specifiedConfigContent);
                    controller.Load();
                    controller.Start();
                }
                else
                {
                    var paths = GetConfigFilePaths();
                    controller.LoadConfigFileFromMultiPaths(paths);
                    controller.Start();
                }
            }
            var cmdHub = new CommandHub();

            CommandHub    = cmdHub;
            cmdHub.Prompt = $"{BuildInfo.AppName}>";
            Commands.AddCommands(cmdHub, controller, null, new string[] { "all" });
            cmdHub.AddCmdHandler("newbie", (cmd) => Commands.NewbieWizard(cmd, controller, specifiedConfigPath ?? configFilePath));
            cmdHub.AddCmdHandler("ver", (cmd) => cmd.WriteLine(NameWithVertionText));
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                cmdHub.AddCmdHandler("openfolder", (cmd) => {
                    if (cmd.args.Length == 0)
                    {
                        Process.Start("explorer", ".");
                    }
                    else if (cmd.args.Length > 1)
                    {
                        goto WRONT_ARG;
                    }
                    else if (cmd.args[0] == "exe")
                    {
                        OpenFolerAndShowFile(Process.GetCurrentProcess().MainModule.FileName);
                    }
                    else if (cmd.args[0] == "config")
                    {
                        OpenFolerAndShowFile(controller.CurrentConfig.FilePath);
                    }
                    else
                    {
                        goto WRONT_ARG;
                    }
                    return;

                    WRONT_ARG:
                    cmd.statusCode = 1;
                    cmd.WriteLine("wrong arguments");
                    void OpenFolerAndShowFile(string fileName) => Process.Start("explorer", $"/select, \"{fileName}\"");
                }, "Usage: openfolder [exe|config]");
            }
            if (argcmd != null)
            {
                HandleArgCmd(argcmd, cmdHub);
                return;
            }
#if NS_WINFORM
            cmdHub.AddCmdHandler("gui", (cmd) => {
                var winform = new WinForm.WinFormController(controller);
                winform.StartUIThread();
                winform.Invoke(() => winform.ShowControllerForm(false));
            });
            if (GuiMode)
            {
                return;
            }
#endif
            if (ar.ContainsKey("--no-cli"))
            {
                goto WAITFOREVER;
            }
            if (InteractiveConsole(cmdHub))
            {
                Environment.Exit(0);
                return;
            }

WAITFOREVER:
#if NS_WINFORM
            if (GuiMode)
            {
                return;
            }
#endif
            while (true)
            {
                Thread.Sleep(int.MaxValue);
            }
        }