Skip to content

All your computers and your phone/iPhone, working as one desk. Screens, apps, files, clipboard, calls, texts, notifications, mouse and keyboard, shared between Mac, Windows, Linux , Android, Web and ios / ipados . Free and open source. No account, no cloud, and no shared network needed.

3.3 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// The user's proxy (Internet Options, what Edge, Chrome and most apps use) for the phone's internet
// on this PC (#361, shared/phonenet.js): read it, point it at TwinDesk's proxy, put it back. What
// was there before is also put back by itself when TwinDesk goes (stdin closed, Program.cs), so a
// crash doesn't leave the PC without internet.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace MacShare
{
    static class SysProxy
    {
        [DllImport("wininet.dll", SetLastError = true)]
        static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
        const int INTERNET_OPTION_SETTINGS_CHANGED = 39, INTERNET_OPTION_REFRESH = 37;
        const string KEY = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";

        static Dictionary<string, object> pending; // what to put back when TwinDesk goes, while ours is set
        static readonly object gate = new object();

        public static Dictionary<string, object> Get()
        {
            var d = new Dictionary<string, object>();
            using (var k = Registry.CurrentUser.OpenSubKey(KEY))
            {
                object en = k != null ? k.GetValue("ProxyEnable", 0) : 0;
                d["enable"] = en is int && (int)en != 0;
                d["server"] = (k != null ? k.GetValue("ProxyServer", "") as string : "") ?? "";
                d["bypass"] = (k != null ? k.GetValue("ProxyOverride", "") as string : "") ?? "";
                d["pac"] = (k != null ? k.GetValue("AutoConfigURL", "") as string : "") ?? "";
            }
            return d;
        }

        // ours: TwinDesk's proxy going on (what's there now is kept to put back) or off (put back).
        public static void Set(bool enable, string server, string bypass, string pac)
        {
            lock (gate)
            {
                bool ours = enable && server.StartsWith("127.0.0.1:");
                if (ours && pending == null) pending = Get();
                if (!ours) pending = null;
                Write(enable, server, bypass, pac);
            }
        }

        public static void RestorePending()
        {
            lock (gate)
            {
                if (pending == null) return;
                var p = pending;
                pending = null;
                try { Write((bool)p["enable"], (string)p["server"], (string)p["bypass"], (string)p["pac"]); } catch { }
            }
        }

        static void Write(bool enable, string server, string bypass, string pac)
        {
            using (var k = Registry.CurrentUser.CreateSubKey(KEY))
            {
                k.SetValue("ProxyEnable", enable ? 1 : 0, RegistryValueKind.DWord);
                if (server.Length > 0) k.SetValue("ProxyServer", server); else k.DeleteValue("ProxyServer", false);
                if (bypass.Length > 0) k.SetValue("ProxyOverride", bypass); else k.DeleteValue("ProxyOverride", false);
                if (pac.Length > 0) k.SetValue("AutoConfigURL", pac); else k.DeleteValue("AutoConfigURL", false);
            }
            InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
            InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
        }
    }
}