예제 #1
0
        private Tuple <string, string> ResponseWithJsFile(Model.Data.PacUrlParams urlParam)
        {
            var response = ResponseWithPacFile(urlParam);
            var mime     = "application/javascript";

            return(new Tuple <string, string>(response.Item1, mime));
        }
예제 #2
0
        static StringBuilder GenDefaultPacFile(
            Model.Data.PacUrlParams urlParam,
            string[] customLists)
        {
            var proxy = urlParam.isSocks ?
                        "SOCKS5 {0}:{1}; SOCKS {0}:{1}; DIRECT" :
                        "PROXY {0}:{1}; DIRECT";

            var isWhiteList = urlParam.isWhiteList;
            var mode        = isWhiteList ? "white" : "black";

            // update cache
            if (defaultPacCache[isWhiteList] == null)
            {
                defaultPacCache[isWhiteList] =
                    GenDefaultPacCacahe(
                        Lib.Utils.GetPacDomainList(isWhiteList),
                        Lib.Utils.GetPacCidrList(isWhiteList),
                        customLists[isWhiteList ? 0 : 1]);
            }

            var domainAndCidrs = defaultPacCache[isWhiteList];
            var content        = new StringBuilder(StrConst.PacJsTpl)
                                 .Replace("__PROXY__", string.Format(proxy, urlParam.ip, urlParam.port))
                                 .Replace("__MODE__", mode)
                                 .Replace("__DOMAINS__", domainAndCidrs[0])
                                 .Replace("__CIDRS__", domainAndCidrs[1]);

            return(content);
        }
예제 #3
0
        void CopyPACUrlToClipboard(bool isWhiteList)
        {
            var index = cboxInbound.SelectedIndex;

            if (index == (int)Model.Data.Enum.ProxyTypes.Config)
            {
                MessageBox.Show(I18N.SysProxyRequireHttpOrSocksMode);
                return;
            }


            Lib.Utils.TryParseIPAddr(tboxInboundAddr.Text, out string ip, out int port);
            var p = new Model.Data.PacUrlParams
            {
                ip          = ip,
                port        = port,
                isWhiteList = isWhiteList,
                isSocks     = index == (int)Model.Data.Enum.ProxyTypes.SOCKS,
            };

            var pacUrl = pacServer.GenPacUrl(p);

            pacServer.StartPacServer();

            MessageBox.Show(
                Lib.Utils.CopyToClipboard(pacUrl) ?
                I18N.LinksCopied :
                I18N.CopyFail);
        }
예제 #4
0
        private Tuple <string, string> ResponsWithDebugger(Model.Data.PacUrlParams urlParam)
        {
            var tpl  = StrConst.PacDebuggerTpl;
            var html = tpl.Replace("__PacServerUrl__", GenPacUrl(urlParam));
            var mime = "text/html; charset=utf-8";

            return(new Tuple <string, string>(html, mime));
        }
예제 #5
0
        public void SetPACProx(Model.Data.PacUrlParams param)
        {
            var proxy = new Model.Data.ProxyRegKeyValue();

            proxy.autoConfigUrl = GenPacUrl(param);
            Lib.Sys.ProxySetter.SetProxy(proxy);
            setting.SaveSysProxySetting(proxy);
            StartPacServer();
            InvokeOnPACServerStatusChanged();
        }
예제 #6
0
        private Tuple <string, string> ResponsWithDebugger(Model.Data.PacUrlParams urlParam)
        {
            var tpl    = StrConst.PacDebuggerTpl;
            var url    = GenPacUrl(urlParam);
            var prefix = url.Split('?')[0];
            var html   = tpl.Replace("__PacServerUrl__", url)
                         .Replace("__PacPrefixUrl__", prefix);
            var mime = "text/html; charset=UTF-8";

            return(new Tuple <string, string>(html, mime));
        }
예제 #7
0
        public string GenPacUrl(Model.Data.PacUrlParams param)
        {
            var pacSetting = setting.GetPacServerSettings();

            // e.g. http://localhost:3000/pac/?&port=5678&ip=1.2.3.4&proto=socks&type=whitelist&key=rnd
            return(string.Format(
                       "{0}?&type={1}&proto={2}&ip={3}&port={4}&timeout={5}",
                       GenPrefix(pacSetting.port),
                       param.isWhiteList ? "whitelist" : "blacklist",
                       param.isSocks ? "socks" : "http",
                       param.ip == "0.0.0.0" ? "127.0.0.1" : param.ip,
                       param.port.ToString(),
                       Lib.Utils.RandomHex(16)));
        }
예제 #8
0
        StringBuilder GenCustomPacFile(
            Model.Data.PacUrlParams urlParam,
            string[] customLists,
            string customPacFileContent)
        {
            var header = new Model.Data.CustomPacHeader(
                urlParam,
                customLists[0],
                customLists[1]);

            return(new StringBuilder("var customSettings = ")
                   .Append(vgcUtils.SerializeObject(header))
                   .Append(";")
                   .Append(customPacFileContent ?? string.Empty));
        }
예제 #9
0
        StringBuilder GenCustomPacFile(
            Model.Data.PacUrlParams urlParam,
            Model.Data.PacServerSettings pacSetting)
        {
            var header = new Model.Data.PacCustomHeader(
                urlParam,
                pacSetting.customWhiteList,
                pacSetting.customBlackList);

            if (string.IsNullOrEmpty(customPacCache))
            {
                // 抢救一下,还不行就算了
                UpdateCustomPacCache();
            }

            return(new StringBuilder("var customSettings = ")
                   .Append(JsonConvert.SerializeObject(header))
                   .Append(";")
                   .Append(customPacCache));
        }
예제 #10
0
        public void LazySysProxyUpdater(bool isSocks, string ip, int port)
        {
            lazySysProxyUpdaterTimer?.Release();
            lazySysProxyUpdaterTimer = null;

            var proxySetting = setting.GetSysProxySetting();

            Action setProxy = null;

            switch (DetectSystemProxyMode(proxySetting))
            {
            case Model.Data.Enum.SystemProxyMode.None:
                return;

            case Model.Data.Enum.SystemProxyMode.PAC:
                // get current pac mode (white list or black list)
                var p = Lib.Utils.GetProxyParamsFromUrl(proxySetting.autoConfigUrl);
                if (p == null)
                {
                    p = new Model.Data.PacUrlParams();
                }
                p.ip      = ip;
                p.port    = port;
                p.isSocks = isSocks;
                setProxy  = () => SetPACProx(p);
                break;

            case Model.Data.Enum.SystemProxyMode.Global:
                // global proxy must be http
                if (isSocks)
                {
                    return;
                }
                setProxy = () => SetGlobalProxy(ip, port);
                break;
            }

            lazySysProxyUpdaterTimer = new Lib.Sys.CancelableTimeout(setProxy, 1000);
            lazySysProxyUpdaterTimer.Start();
        }
예제 #11
0
        public Tuple <string, string> GenPacFileResponse(
            bool isUseCustomPac,
            Model.Data.PacUrlParams urlParam,
            string[] customLists,
            string customPacFileContent)
        {
            // ie require this header
            var mime = "application/x-ns-proxy-autoconfig; charset=UTF-8";

            StringBuilder content;

            if (isUseCustomPac)
            {
                content = GenCustomPacFile(urlParam, customLists, customPacFileContent);
            }
            else
            {
                content = GenDefaultPacFile(urlParam, customLists);
            }

            return(new Tuple <string, string>(content.ToString(), mime));
        }
예제 #12
0
        private StringBuilder GenDefaultPacFile(
            Model.Data.PacUrlParams urlParam,
            string customWhiteList,
            string customBlackList)
        {
            var proxy = urlParam.isSocks ?
                        "SOCKS5 {0}:{1}; SOCKS {0}:{1}; DIRECT" :
                        "PROXY {0}:{1}; DIRECT";
            var mode           = urlParam.isWhiteList ? "white" : "black";
            var domainAndCidrs = GenDomainAndCidrContent(
                urlParam.isWhiteList,
                customWhiteList,
                customBlackList);

            var content = new StringBuilder(StrConst.PacJsTpl)
                          .Replace("__PROXY__", string.Format(proxy, urlParam.ip, urlParam.port))
                          .Replace("__MODE__", mode)
                          .Replace("__DOMAINS__", domainAndCidrs[0])
                          .Replace("__CIDRS__", domainAndCidrs[1]);

            return(content);
        }
예제 #13
0
        private Tuple <string, string> ResponseWithPacFile(Model.Data.PacUrlParams urlParam)
        {
            // ie require this header
            var mime = "application/x-ns-proxy-autoconfig; charset=UTF-8";

            var           pacSetting = setting.GetPacServerSettings();
            StringBuilder content;

            if (pacSetting.isUseCustomPac)
            {
                content = GenCustomPacFile(urlParam, pacSetting);
            }
            else
            {
                content = GenDefaultPacFile(
                    urlParam,
                    pacSetting.customWhiteList,
                    pacSetting.customBlackList);
            }

            return(new Tuple <string, string>(content.ToString(), mime));
        }
예제 #14
0
        private void SetSysProxyToPACMode(bool isWhiteList)
        {
            var index = cboxInbound.SelectedIndex;

            if (index == (int)Model.Data.Enum.ProxyTypes.Config)
            {
                MessageBox.Show(I18N.SysProxyRequireHttpOrSocksMode);
                return;
            }

            Lib.Utils.TryParseIPAddr(tboxInboundAddr.Text, out string ip, out int port);
            var p = new Model.Data.PacUrlParams
            {
                ip          = ip,
                port        = port,
                isSocks     = index == (int)Model.Data.Enum.ProxyTypes.SOCKS,
                isWhiteList = isWhiteList,
            };

            pacServer.SetPACProx(p);
            Lib.UI.ShowMessageBoxDoneAsync();
        }