Beispiel #1
0
 private static async Task HandleConnection(Conductor conductor)
 {
     while (true)
     {
         var desktopName = Win32Interop.GetCurrentDesktop();
         if (desktopName.ToLower() != conductor.CurrentDesktopName.ToLower() && conductor.Viewers.Count > 0)
         {
             conductor.CurrentDesktopName = desktopName;
             Logger.Write($"Switching desktops to {desktopName}.");
             Win32Interop.SwitchToInputDesktop();
         }
         await Task.Delay(1000);
     }
 }
Beispiel #2
0
 static void Main(string[] args)
 {
     DesktopName = (args.Length > 0 ? args[0] : Win32Interop.GetCurrentDesktop()).ToLower();
     while (Process.GetProcesses().Any(x => x.ProcessName.ToLower().Contains("doxm remote control")))
     {
         var currentDesktop = Win32Interop.GetCurrentDesktop().ToLower();
         if (currentDesktop != DesktopName)
         {
             DesktopName = currentDesktop;
             Console.WriteLine(DesktopName);
             Environment.Exit(0);
         }
         System.Threading.Thread.Sleep(100);
     }
 }
Beispiel #3
0
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            SetWorkingDirectory();
            var argDict = ProcessArgs(args);

            JsonConvert.DefaultSettings = () =>
            {
                var settings = new JsonSerializerSettings();
                settings.Error = (sender, arg) =>
                {
                    arg.ErrorContext.Handled = true;
                };
                return(settings);
            };


            if (argDict.ContainsKey("update"))
            {
                Updater.CoreUpdate();
            }

            if (argDict.TryGetValue("mode", out var mode) && mode == "remotecontrol")
            {
                var rcBinaryPath = Path.Combine(Utilities.AppDataDir, "remote_control", OSUtils.RemoteControlExecutableFileName);
                var procInfo     = new ADVAPI32.PROCESS_INFORMATION();
                var desktop      = Win32Interop.GetCurrentDesktop();
                Win32Interop.OpenInteractiveProcess(rcBinaryPath + $" -mode unattended -requester {argDict["requester"]} -serviceid {argDict["serviceid"]} -desktop {desktop} -hostname {Utilities.GetConnectionInfo().Host.Split("//").Last()}", $"{desktop}", true, out procInfo);
                Environment.Exit(0);
            }

            if (OSUtils.IsWindows)
            {
                //ClientSocket.Connect();
                ServiceBase.Run(new WindowsService());
            }
            else
            {
                ClientSocket.Connect();
            }

            while (true)
            {
                Console.Read();
            }
        }
        private bool CreateProcessAsUser(uint dwSessionId, bool isPush = false)
        {
            var desktopName       = Win32Interop.GetCurrentDesktop();
            var openProcessString = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(Assembly.GetExecutingAssembly().Location));
            //用户进程启动
            var result = Win32Interop.OpenInteractiveProcess(openProcessString + $" \"-user\" \"-port:{_port}\" \"-sessionId:{dwSessionId}\"", desktopName, true, dwSessionId, out _);

            if (isPush)
            {
                _userProcessSessionIdList.Add(new UserProcessToken()
                {
                    SessionId = (int)dwSessionId,
                    Actived   = false//默认不激活状态,等待用户进程连接以激活
                });
            }

            return(result);
        }
Beispiel #5
0
        private static void CheckInitialDesktop()
        {
            var desktopName = Win32Interop.GetCurrentDesktop();

            if (desktopName.ToLower() != Conductor.CurrentDesktopName.ToLower())
            {
                Conductor.CurrentDesktopName = desktopName;
                Logger.Write($"Setting initial desktop to {desktopName}.");
                Conductor.ArgDict["desktop"] = desktopName;
                var openProcessString = Assembly.GetExecutingAssembly().Location;
                foreach (var arg in Conductor.ArgDict)
                {
                    openProcessString += $" -{arg.Key} {arg.Value}";
                }
                var result = Win32Interop.OpenInteractiveProcess(openProcessString, desktopName, true, out _);
                if (!result)
                {
                    Logger.Write($"Desktop relaunch to {desktopName} failed.");
                }
                Environment.Exit(0);
            }
        }
Beispiel #6
0
 public static async Task HandleConnection(Conductor conductor)
 {
     while (true)
     {
         var desktopName = Win32Interop.GetCurrentDesktop();
         if (desktopName.ToLower() != conductor.CurrentDesktopName.ToLower() && conductor.Viewers.Count > 0)
         {
             conductor.CurrentDesktopName = desktopName;
             Logger.Write($"Switching desktops to {desktopName}.");
             // TODO: SetThreadDesktop causes issues with input after switching.
             //var inputDesktop = Win32Interop.OpenInputDesktop();
             //User32.SetThreadDesktop(inputDesktop);
             //User32.CloseDesktop(inputDesktop);
             conductor.Connection.InvokeAsync("SwitchingDesktops", conductor.Viewers.Keys.ToList()).Wait();
             var result = Win32Interop.OpenInteractiveProcess(Assembly.GetExecutingAssembly().Location + $" -mode {conductor.Mode.ToString()} -requester {conductor.RequesterID} -serviceid {conductor.ServiceID} -host {conductor.Host} -relaunch true -desktop {desktopName} -viewers {String.Join(",", conductor.Viewers.Keys.ToList())}", desktopName, true, out _);
             if (!result)
             {
                 Logger.Write($"Desktop switch to {desktopName} failed.");
                 conductor.CasterSocket.SendConnectionFailedToViewers(conductor.Viewers.Keys.ToList()).Wait();
             }
         }
         await Task.Delay(100);
     }
 }
Beispiel #7
0
        public static async void BeginScreenCasting(string viewerID,
                                                    string requesterName,
                                                    ICapturer capturer,
                                                    Conductor conductor)
        {
            Viewer viewer;

            byte[] encodedImageBytes;


            Logger.Write($"Starting screen cast.  Requester: {requesterName}. Viewer ID: {viewerID}. Capturer: {capturer.GetType().ToString()}.  App Mode: {conductor.Mode}  Desktop: {conductor.CurrentDesktopName}");

            viewer = new Viewer()
            {
                Capturer            = capturer,
                DisconnectRequested = false,
                Name = requesterName,
                ViewerConnectionID = viewerID,
                HasControl         = true
            };

            conductor.Viewers.AddOrUpdate(viewerID, viewer, (id, v) => viewer);

            if (conductor.Mode == Enums.AppMode.Normal)
            {
                conductor.InvokeViewerAdded(viewer);
            }

            await conductor.CasterSocket.SendMachineName(Environment.MachineName, viewerID);

            await conductor.CasterSocket.SendScreenCount(
                capturer.SelectedScreen,
                capturer.GetScreenCount(),
                viewerID);

            await conductor.CasterSocket.SendScreenSize(capturer.CurrentScreenBounds.Width, capturer.CurrentScreenBounds.Height, viewerID);

            capturer.ScreenChanged += async(sender, bounds) =>
            {
                await conductor.CasterSocket.SendScreenSize(bounds.Width, bounds.Height, viewerID);
            };

            var desktopName = string.Empty;

            if (OSUtils.IsWindows)
            {
                desktopName = Win32Interop.GetCurrentDesktop();
            }

            while (!viewer.DisconnectRequested)
            {
                try
                {
                    var currentDesktopName = Win32Interop.GetCurrentDesktop();
                    if (desktopName.ToLower() != currentDesktopName.ToLower())
                    {
                        desktopName = currentDesktopName;
                        Logger.Write($"Switching to desktop {desktopName} in ScreenCaster.");
                        Win32Interop.SwitchToInputDesktop();
                        continue;
                    }

                    while (viewer.PendingFrames > 10)
                    {
                        await Task.Delay(1);
                    }

                    capturer.Capture();

                    var diffArea = ImageUtils.GetDiffArea(capturer.CurrentFrame, capturer.PreviousFrame, capturer.CaptureFullscreen);

                    if (diffArea.IsEmpty)
                    {
                        continue;
                    }

                    using (var newImage = capturer.CurrentFrame.Clone(diffArea, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                    {
                        if (capturer.CaptureFullscreen)
                        {
                            capturer.CaptureFullscreen = false;
                        }

                        encodedImageBytes = ImageUtils.EncodeBitmap(newImage, viewer.EncoderParams);

                        if (encodedImageBytes?.Length > 0)
                        {
                            await conductor.CasterSocket.SendScreenCapture(encodedImageBytes, viewerID, diffArea.Left, diffArea.Top, diffArea.Width, diffArea.Height, DateTime.UtcNow);

                            viewer.PendingFrames++;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                }
                finally
                {
                    // TODO: Even after disposing of the bitmap, GC doesn't collect in time.  Memory usage soars quickly.
                    // Need to revisit this later.
                    GC.Collect();
                }
            }
            Logger.Write($"Ended screen cast.  Requester: {requesterName}. Viewer ID: {viewerID}.");
            conductor.Viewers.TryRemove(viewerID, out _);

            capturer.Dispose();

            // Close if no one is viewing.
            if (conductor.Viewers.Count == 0 && conductor.Mode == Enums.AppMode.Unattended)
            {
                Environment.Exit(0);
            }
        }