コード例 #1
0
ファイル: Controller.cs プロジェクト: smeoow/Naive
 private void SetCurrentConfig(LoadedConfig loadedConfig)
 {
     CurrentConfig = loadedConfig;
     if (!loadedConfig.SocketImpl.IsNullOrEmpty())
     {
         MyStream.SetSocketImpl(loadedConfig.SocketImpl);
     }
     MyStream.TwoWayCopier.DefaultUseLoggerAsVerboseLogger = IsDebugFlagEnabled("copier_v");
     Channel.Debug        = IsDebugFlagEnabled("mux_v");
     YASocket.Debug       = IsDebugFlagEnabled("ya_v");
     Logging.AsyncLogging = IsDebugFlagEnabled("asynclog");
     ConfigTomlLoaded?.Invoke(loadedConfig.TomlTable);
 }
コード例 #2
0
ファイル: NaiveSocksCli.cs プロジェクト: smeoow/Naive
        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);
            }
        }