public void Update(MapPair pair)
        {
            UrlAddressParser inParser = pair.In;

            if (inParser.IsIp)
            {
                throw new Exception("输入地址不能为IP");
            }
            var p = maplist.FirstOrDefault(o => o.Id == pair.Id);

            p.In          = pair.In;
            p.Out         = pair.Out;
            p.Enabled     = pair.Enabled;
            p.UserSetting = pair.UserSetting;
            Save();
        }
        /// <summary>
        /// 新增一组映射
        /// </summary>
        /// <param name="in"></param>
        /// <param name="out">全局唯一的</param>
        public async Task <bool> Add(string @in, string @out)
        {
            UrlAddressParser inParser  = @in;
            UrlAddressParser outParser = @out;


            if (inParser.IsIp)
            {
                throw new Exception("输入地址不能为IP");
            }

            if (maplist.Where(a => a.In == inParser.Url).Count() > 0)
            {
                var confirm = await jsruntime.InvokeAsync <bool>("confirm", new object[] {
                    "已存在相同的输入地址,是否禁用旧的映射?"
                });

                if (confirm)
                {
                    maplist.Where(a => a.In == inParser.Url && a.Enabled).First().Enabled = false;
                }
                else
                {
                    throw new Exception("已存在相同的输入地址");
                }
            }


            var pair = new MapPair()
            {
                In      = inParser.Url,
                Out     = outParser.Url,
                Id      = Guid.NewGuid().ToString(),
                Enabled = true,
            };

            maplist.Add(pair);

            Save();

            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// 应用
        /// </summary>
        /// <param name="validPairs"></param>
        public async void Apply(MapPair[] validPairs)
        {
            var confContents = new List <string>();

            var hostexist = new List <string>(await File.ReadAllLinesAsync(HostFile));

            hostexist = hostexist
                        .Where(ln => !(ln.Contains(HostTag)))
                        .ToList();

            hostexist.Add(HostTag + " Start ---------");

            foreach (var item in validPairs)
            {
                UrlAddressParser inParse  = item.In;
                UrlAddressParser outParse = item.Out;

                #region Host文件
                hostexist.Add(string.Format("127.0.0.1  {0} {1} [{2} -> {3}]",
                                            inParse.Host,
                                            HostTag,
                                            inParse.Url,
                                            outParse.Url));
                #endregion

                #region Nginx.Conf
                var template = Properties.Resources.http;
                template = template.Replace("%端口%", inParse.Port.ToString());
                template = template.Replace("%主机%", inParse.Host);
                template = template.Replace("%终点%", outParse.Url.Trim('/') + "/");
                template = template.Replace("%自定义%", item.UserSetting ?? "");

                confContents.Add("");
                confContents.Add(template);
                #endregion
            }
            hostexist.Add(HostTag + " End ---------");

            Hosts = string.Join(Environment.NewLine, hostexist);

            NginxConf = Properties.Resources.nginx.Replace("%占位符%", string.Join(Environment.NewLine, confContents));


            #region 更新
            await File.WriteAllTextAsync(ConfFile, NginxConf);

            try
            {
                await File.WriteAllTextAsync(HostFile, Hosts);
            }
            catch (Exception)
            {
            }

            #endregion

            if (ModifyPatched != null)
            {
                ModifyPatched(this, null);
            }
        }