예제 #1
0
        public static void AddCertbotValidation(this DefaultAcsClient client, string domain, string validation)
        {
            try
            {
                var request = new AddDomainRecordRequest
                {
                    DomainName = domain,
                    RR         = @"_acme-challenge",
                    Type       = @"TXT",
                    _Value     = validation
                };

                var response = client.GetAcsResponse(request);

                Console.WriteLine($@"RecordId: {response.RecordId}");
                Console.WriteLine($@"RequestId: {response.RequestId}");
            }
            catch (ServerException e)
            {
                Console.WriteLine(e);
            }
            catch (ClientException e)
            {
                Console.WriteLine(e);
            }
        }
예제 #2
0
 /// <summary>
 /// 新增解析记录
 /// </summary>
 /// <param name="record"></param>
 /// <param name="strContent"></param>
 /// <returns></returns>
 public static bool AddDomainRecords(DescribeDomainRecordsResponse.Record record, out string strContent)
 {
     try
     {
         IClientProfile         clientProfile = DefaultProfile.GetProfile("cn-hangzhou", CGlobalConfig.AccessKey, CGlobalConfig.AccessKeySecret);
         DefaultAcsClient       client        = new DefaultAcsClient(clientProfile);
         AddDomainRecordRequest request       = new AddDomainRecordRequest();
         request.DomainName = record.DomainName;
         request.RR         = record.RR;
         request.Type       = record.Type;
         request.Value      = record.Value;
         try
         {
             AddDomainRecordResponse response = client.GetAcsResponse(request);
             strContent = response.RecordId;
             return(!string.IsNullOrEmpty(strContent));
         }
         catch (ServerException e)
         {
             strContent = " AddDomainRecords 发送异常:" + e.ErrorCode + "\t" + e.ErrorMessage;
             CLogHelper.WriteError(strContent);
         }
         catch (ClientException e)
         {
             strContent = " AddDomainRecords 发送异常:" + e.ErrorCode + "\t" + e.ErrorMessage;
             CLogHelper.WriteError(strContent);
         }
     }
     catch (Exception ex)
     {
         strContent = " AddDomainRecords 发送异常:" + ex.ToString();
         CLogHelper.WriteError(strContent);
     }
     return(false);
 }
예제 #3
0
파일: main.cs 프로젝트: zakailynn/AliDDNS
        private void addDomainRecord()
        {
            string[] symbols = new string[1] {
                "."
            };
            string[] data       = fullDomainName.Text.Split(symbols, 30, StringSplitOptions.RemoveEmptyEntries);
            string   domainRR   = data[0];
            string   domainName = data[1] + "." + data[2];

            AddDomainRecordRequest request = new AddDomainRecordRequest();

            request.Type       = "A";
            request.RR         = domainRR;
            request.DomainName = domainName;
            request.Value      = localIP.Text;
            try
            {
                AddDomainRecordResponse response = client.GetAcsResponse(request);
                recordId.Text = response.RecordId;
            }
            //处理错误
            catch (ServerException e)
            {
                MessageBox.Show(e.ErrorCode + e.ErrorMessage);
            }
            catch (ClientException e)
            {
                MessageBox.Show(e.ErrorCode + e.ErrorMessage);
            }
        }
예제 #4
0
        /// <summary>
        /// 添加解析记录,默认TTL为600
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public override DomainRecordActionResult AddDomainRecord(AddDomainRecordParam param)
        {
            var request = new AddDomainRecordRequest()
            {
                DomainName = param.DomainName,
                RR         = param.RR,
                Type       = param.Type.ToString(),
                TTL        = param.TTL,
                _Value     = param.Value
            };

            try
            {
                var response = client.GetAcsResponse(request);
                if (response == null || response.HttpResponse.Content == null || response.HttpResponse.Content.Length == 0)
                {
                    throw new Exception("Add subdomain records info failed.");
                }
                string result    = Encoding.UTF8.GetString(response.HttpResponse.Content);
                var    resultObj = new JsonSerializer().Deserialize <DomainRecordActionResult>(new JsonTextReader(new StringReader(result)));
                if (resultObj != null)
                {
                    resultObj.Status = true;
                }
                return(resultObj);
            }
            catch (ServerException e)
            {
                throw new Exception($"Aliyun server error. {e.Message}");
            }
            catch (ClientException e)
            {
                throw new Exception($"Reuqest client error. errcode is {e.ErrorCode}, {e.Message}");
            }
        }
        public static void AddDomainRecords(DescribeDomainRecordsResponse.Record record)
        {
            IClientProfile         clientProfile = DefaultProfile.GetProfile("cn-hangzhou", CGlobalConfig.AccessKey, CGlobalConfig.AccessKeySecret);
            DefaultAcsClient       client        = new DefaultAcsClient(clientProfile);
            AddDomainRecordRequest request       = new AddDomainRecordRequest();

            request.DomainName = record.DomainName;
            request.RR         = record.RR;
            request.Type       = record.Type;
            request.Value      = record.Value;
            AddDomainRecordResponse response = client.GetAcsResponse(request);
        }
예제 #6
0
        /// <summary>
        /// 添加解析记录。
        /// </summary>
        /// <param name="type">记录类型</param>
        /// <param name="domain">域名或子域名</param>
        /// <param name="ip">公网ip</param>
        /// <returns>添加成功返回true,否则返回false。</returns>
        private bool AddRecord(IpType type, string domain, string ip)
        {
            try
            {
                ParseSubDomainAndLine(domain, out string subDomain, out string line);
                string pattern = @"^(\S*)\.(\S+)\.(\S+)$";
                Regex  regex   = new Regex(pattern);
                var    match   = regex.Match(subDomain);
                string domainName;
                string rr;
                if (match.Success)
                {
                    rr         = match.Groups[1].Value;
                    domainName = match.Groups[2].Value + "." + match.Groups[3].Value;
                }
                else
                {
                    rr         = "@";
                    domainName = subDomain;
                }

                var client = GetNewClient();
                AddDomainRecordRequest request = new AddDomainRecordRequest
                {
                    DomainName = domainName,
                    RR         = rr,
                    Type       = type.ToString(),
                    _Value     = ip,
                    TTL        = Options.Instance.TTL,
                    Line       = line
                };
                var response = client.GetAcsResponse(request);
                if (response.HttpResponse.isSuccess())
                {
                    Log.Print($"成功增加{ type.ToString() }记录{ domain }为{ ip }。");
                    return(true);
                }
                else
                {
                    Log.Print($"增加{ type.ToString() }记录{ domain }失败。");
                    return(false);
                }
            }
            catch (Exception e)
            {
                Log.Print($"增加{ type.ToString() }记录{ domain }时发生异常: { e }");
                return(false);
            }
        }
예제 #7
0
        /// <summary>
        /// 添加域名解析
        /// </summary>
        /// <param name="domainName"></param>
        /// <returns></returns>
        public AddDomainRecordResponse AddDomainRecord(string domainName, string hostRecord, string ip, string type)
        {
            profile       = DefaultProfile.GetProfile(domainName, accessKeyId, accessSecret);
            defaultClient = new DefaultAcsClient(profile);

            var request = new AddDomainRecordRequest();

            request.DomainName = domainName;
            request.RR         = hostRecord;
            request._Value     = ip;
            request.Type       = type;
            var response = defaultClient.GetAcsResponse(request);

            return(response);
        }
예제 #8
0
        /// <summary>
        /// 添加记录值
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public OutAliyunDNSVO AddDomainRecord(InAliyunDNSVO info)
        {
            IClientProfile   profile = DefaultProfile.GetProfile("cn-hangzhou", info.accessKeyId, info.accessSecret);
            DefaultAcsClient client  = new DefaultAcsClient(profile);
            var request = new AddDomainRecordRequest()
            {
                DomainName = info.DomainName,
                RR         = info.RR,
                _Value     = info.Value,
                Type       = info.Type
            };
            var response = client.GetAcsResponse(request);

            return(JsonConvert.DeserializeObject <OutAliyunDNSVO>(Encoding.Default.GetString(response.HttpResponse.Content)));
        }
예제 #9
0
        private void AddDNS(string ip)
        {
            var addRequest = new AddDomainRecordRequest()
            {
                DomainName = _domainName,
                RR         = _rr,
                Type       = "A",
                _Value     = ip
            };
            var addResponse     = _acsClient.GetAcsResponse(addRequest);
            var addStringResult = Encoding.Default.GetString(addResponse.HttpResponse.Content);
            var addJsonResult   = JsonConvert.DeserializeObject <JObject>(addStringResult);

            _logger.LogInformation(addJsonResult.ToString());
            _logger.LogInformation($"[Add DNS] {_rr}.{_domainName} -> {ip}");
        }
예제 #10
0
        public void SetupRecord(string domain, string rrKeyword, string newValue)
        {
            var reqRecords = new DescribeDomainRecordsRequest();

            reqRecords.DomainName = domain;
            reqRecords.RRKeyWord  = rrKeyword;
            reqRecords.Type       = "A";
            var records = getResponse(reqRecords).DomainRecords;

            if (records.Count == 0)
            {
                var add = new AddDomainRecordRequest
                {
                    RR         = rrKeyword,
                    _Value     = newValue,
                    Type       = "A",
                    TTL        = TTL,
                    DomainName = domain
                };
                getResponse(add);
                return;
            }

            var record = records[0];

            if (newValue == record._Value &&
                record.RR == rrKeyword &&
                record.Type == "A" &&
                record.TTL == TTL)   //解析值没有变化则跳出
            {
                return;
            }

            var reqUpdate = new UpdateDomainRecordRequest();

            reqUpdate.RR       = rrKeyword;
            reqUpdate.RecordId = record.RecordId;
            reqUpdate._Value   = newValue;
            reqUpdate.Type     = "A";
            reqUpdate.TTL      = TTL;
            getResponse(reqUpdate);
        }
예제 #11
0
        static void AddRecord()
        {
            var domainParts = currentDomain.Split('.');
            var rr          = "@";
            var domainName  = currentDomain;

            if (domainParts.Length >= 3)
            {
                var rrParts = new List <string>();
                for (var i = 0; i < domainParts.Length - 2; i++)
                {
                    rrParts.Add(domainParts[i]);
                }
                rr = string.Join(".", rrParts);

                var domainNameParts = new List <String>();
                for (var i = domainParts.Length - 2; i < domainParts.Length; i++)
                {
                    domainNameParts.Add(domainParts[i]);
                }
                domainName = string.Join(".", domainNameParts);
            }
            var request = new AddDomainRecordRequest
            {
                RR         = rr,
                DomainName = domainName,
                Type       = "A",
                Value      = currentIp,
                TTL        = ttl
            };
            var response = Client.GetAcsResponse(request);

            if (response.HttpResponse.isSuccess())
            {
                Log(string.Format("{0} 解析已添加 {1}", currentDomain, currentIp));
            }
            else
            {
                Log(string.Format("{0} 解析添加失败 {1}", currentDomain, response.HttpResponse.Content));
            }
        }
예제 #12
0
        /// <summary>
        /// 添加域名记录
        /// </summary>
        /// <returns></returns>
        private bool addDomainRecord()
        {
            string[] symbols = new string[1] {
                "."
            };
            string[] data       = fullDomainName.Text.Split(symbols, 30, StringSplitOptions.RemoveEmptyEntries);
            string   domainRR   = data[0];
            string   domainName = data[1] + "." + data[2];

            clientProfile = DefaultProfile.GetProfile("cn-hangzhou", textBox_accessKeyId.Text.ToString(), textBox_accessKeySecret.Text.ToString());
            client        = new DefaultAcsClient(clientProfile);
            AddDomainRecordRequest request = new AddDomainRecordRequest();

            request.Type       = "A";
            request.RR         = domainRR;
            request.DomainName = domainName;
            request.TTL        = Convert.ToInt32(textBox_TTL.Text);
            request.Value      = localIP.Text;
            try
            {
                textBox_log.AppendText(System.DateTime.Now.ToString() + " " + "正在向阿里云DNS服务添加域名:" + fullDomainName.Text + "\r\n");
                AddDomainRecordResponse response = client.GetAcsResponse(request);
                if (response.RecordId != null)
                {
                    textBox_log.AppendText(System.DateTime.Now.ToString() + " " + " 域名:" + fullDomainName.Text + "添加成功!" + "服务器返回RecordId:" + response.RecordId + "\r\n");
                    textBox_recordId.Text = response.RecordId.ToString();
                    cfg.SaveAppSetting("RecordID", response.RecordId.ToString());
                    globalDomainType.Text          = request.Type;
                    globalRR.Text                  = request.RR;
                    globalValue.Text               = domainIP.Text = request.Value;
                    label_DomainIpStatus.Text      = "已绑定";
                    label_DomainIpStatus.ForeColor = System.Drawing.Color.FromArgb(0, 0, 0, 255);
                    return(true);
                }
                else
                {
                    textBox_log.AppendText(System.DateTime.Now.ToString() + " " + " 域名:" + fullDomainName.Text + "添加失败!" + "\r\n");
                    label_DomainIpStatus.Text      = "未绑定";
                    domainIP.Text                  = "0.0.0.0";
                    textBox_recordId.Text          = "null";
                    globalRR.Text                  = "null";
                    globalDomainType.Text          = "null";
                    globalValue.Text               = "null";
                    label_TTL.Text                 = "null";
                    label_DomainIpStatus.ForeColor = System.Drawing.Color.FromArgb(255, 255, 0, 0);
                    return(false);
                }
            }
            //处理错误
            catch (Exception error)
            {
                textBox_log.AppendText(System.DateTime.Now.ToString() + " " + "updateDomainRecord() Exception:  " + error + "\r\n");
            }

            /*
             * catch (ServerException e)
             * {
             *  textBox_log.AppendText(System.DateTime.Now.ToString() + " " + "Server Exception:  " + e.ErrorCode + e.ErrorMessage + "\r\n");
             * }
             * catch (ClientException e)
             * {
             *  textBox_log.AppendText(System.DateTime.Now.ToString() + " " + "Client Exception:  " + e.ErrorCode + e.ErrorMessage + "\r\n");
             * }*/
            return(false);
        }
예제 #13
0
        //定时执行事件
        private void TimedEvent(object sender, System.Timers.ElapsedEventArgs e)
        {
            //每次都重新获取配置(可以动态修改而不用重启服务)
            GetConfig();
            if (!configExist_)
            {
                logger.Error(" Not Config Yet ...... 【Skip】");
                return;
            }
            string currentInternetIp = GetInternetIP();

            if (currentInternetIp.Length == 0)
            {
                logger.Info("Can't Get Current Internet Ip ...... 【Skip】");
                return;
            }
            IClientProfile               clientProfile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId_, accessKeySecret_);
            DefaultAcsClient             client        = new DefaultAcsClient();
            DescribeDomainRecordsRequest reqFetch      = new DescribeDomainRecordsRequest();

            reqFetch.AcceptFormat = Aliyun.Acs.Core.Http.FormatType.JSON;
            reqFetch.DomainName   = domainName_;

            string dnsIp = "", recordId = "";

            try {
                DescribeDomainRecordsResponse resFetch = client.GetAcsResponse(reqFetch);
                foreach (var r in resFetch.DomainRecords)
                {
                    if (r.RR == subDomainName_)
                    {
                        dnsIp    = r.Value;
                        recordId = r.RecordId;
                    }
                }
            } catch (ServerException ex) {
                logger.Error("Server Error >> code : " + ex.ErrorCode + " | Error Message : " + ex.ErrorMessage);
            } catch (ClientException ex) {
                logger.Error("Client Error >> code : " + ex.ErrorCode + " | Error Message : " + ex.ErrorMessage);
            }
            if (dnsIp.Length == 0)
            {
                logger.Info("Can't Get Dns Record , Add New Record .");
                AddDomainRecordRequest reqAdd = new AddDomainRecordRequest();
                reqAdd.AcceptFormat = Aliyun.Acs.Core.Http.FormatType.JSON;
                reqAdd.DomainName   = domainName_;
                reqAdd.RR           = subDomainName_;
                reqAdd.Type         = "A";
                reqAdd.Value        = currentInternetIp;
                try {
                    //添加解析记录
                    AddDomainRecordResponse resAdd = client.GetAcsResponse(reqAdd);
                    logger.Info("\r\n Dns Record Add ...... 【OK】 \r\n New Dns Record Is : " + currentInternetIp);
                } catch (ServerException ex) {
                    logger.Error("Dns Record Add ...... 【Error】 \r\n Server Error >> code : " + ex.ErrorCode + " | Error Message : " + ex.ErrorMessage);
                } catch (ClientException ex) {
                    logger.Error("Dns Record Add ...... 【Error】 \r\n Client Error >> code : " + ex.ErrorCode + " | Error Message : " + ex.ErrorMessage);
                }
            }
            else
            {
                if (currentInternetIp == dnsIp)
                {
                    logger.Info("Current Internet Ip Is : " + currentInternetIp + " ,Same As Dns Record ...... 【Skip】");
                }
                else
                {
                    //更新记录
                    UpdateDomainRecordRequest reqUpdate = new UpdateDomainRecordRequest();
                    reqUpdate.AcceptFormat = Aliyun.Acs.Core.Http.FormatType.JSON;
                    reqUpdate.RecordId     = recordId;
                    reqUpdate.RR           = subDomainName_;
                    reqUpdate.Type         = "A";
                    reqUpdate.Value        = currentInternetIp;
                    try {
                        //更新解析记录
                        UpdateDomainRecordResponse resUpdate = client.GetAcsResponse(reqUpdate);
                        logger.Info("\r\n Update Dns  Record ...... 【OK】 \r\n New Dns Record Is : " + currentInternetIp);
                    } catch (ServerException ex) {
                        logger.Error("Dns Record Update ...... 【Error】 \r\n Server Error >> code : " + ex.ErrorCode + " | Error Message : " + ex.ErrorMessage);
                    } catch (ClientException ex) {
                        logger.Error("Dns Record Update ...... 【Error】 \r\n Client Error >> code : " + ex.ErrorCode + " | Error Message : " + ex.ErrorMessage);
                    }
                }
            }
        }