Пример #1
0
        void OnServiceCommandOpened(object sender, CommandRequestedArgs e)
        {
            Console.WriteLine($"Channel {e.Channel.ServerChannelId} runs {e.SubSystemName}: \"{e.CommandText}\".");

            // var allow = true;  // func(e.ShellType, e.CommandText, e.AttachedUserauthArgs);

            if (!e.AttachedUserAuthArgs.Result) // TODO - check access rights per service
            {
                return;
            }

            if (e.SubSystemName == "shell")
            {
                // requirements: Windows 10 RedStone 5, 1809
                // also, you can call powershell.exe
                //var terminal = new Terminal("cmd.exe", windowWidth, windowHeight);

                //e.Channel.DataReceived += (ss, ee) => terminal.OnInput(ee);
                //e.Channel.CloseReceived += (ss, ee) => terminal.OnClose();
                //terminal.DataReceived += (ss, ee) => e.Channel.SendData(ee);
                //terminal.CloseReceived += (ss, ee) => e.Channel.SendClose(ee);

                //terminal.Run();
            }
            else if (e.SubSystemName == "exec")
            {
                //var parser = new Regex(@"(?<cmd>git-receive-pack|git-upload-pack|git-upload-archive) \'/?(?<proj>.+)\.git\'");
                //var match = parser.Match(e.CommandText);
                //var command = match.Groups["cmd"].Value;
                //var project = match.Groups["proj"].Value;
            }
            else if (e.SubSystemName == "sftp")
            {
                // do something more
                if (e.AttachedUserAuthArgs.Result)
                {
                    InitializeSftp(e.Channel, e.AttachedUserAuthArgs.Username);
                }

                return;
            }


            _logger.LogInformation($"Channel {e.Channel.ServerChannelId} runs command: \"{e.CommandText}\". on subsystem: {e.SubSystemName}");

            e.Channel.SendData(Encoding.UTF8.GetBytes($"You ran {e.CommandText}\n"));
            e.Channel.SendClose();
        }
Пример #2
0
        static void service_CommandOpened(object sender, CommandRequestedArgs e)
        {
            Console.WriteLine($"Channel {e.Channel.ServerChannelId} runs {e.ShellType}: \"{e.CommandText}\".");

            var allow = true;  // func(e.ShellType, e.CommandText, e.AttachedUserauthArgs);

            if (!allow)
            {
                return;
            }

            if (e.ShellType == "shell")
            {
                // requirements: Windows 10 RedStone 5, 1809
                // also, you can call powershell.exe
                var terminal = new Terminal("cmd.exe", windowWidth, windowHeight);

                e.Channel.DataReceived  += (ss, ee) => terminal.OnInput(ee);
                e.Channel.CloseReceived += (ss, ee) => terminal.OnClose();
                terminal.DataReceived   += (ss, ee) => e.Channel.SendData(ee);
                terminal.CloseReceived  += (ss, ee) => e.Channel.SendClose(ee);

                terminal.Run();
            }
            else if (e.ShellType == "exec")
            {
                var parser  = new Regex(@"(?<cmd>git-receive-pack|git-upload-pack|git-upload-archive) \'/?(?<proj>.+)\.git\'");
                var match   = parser.Match(e.CommandText);
                var command = match.Groups["cmd"].Value;
                var project = match.Groups["proj"].Value;

                var git = new GitService(command, project);

                e.Channel.DataReceived  += (ss, ee) => git.OnData(ee);
                e.Channel.CloseReceived += (ss, ee) => git.OnClose();
                git.DataReceived        += (ss, ee) => e.Channel.SendData(ee);
                git.CloseReceived       += (ss, ee) => e.Channel.SendClose(ee);

                git.Start();
            }
            else if (e.ShellType == "subsystem")
            {
                // do something more
            }
        }