예제 #1
0
        public async Task <ICFAPIResponse> RemoveRecord(IQueryConfig qConfig)
        {
            try {
                ICFAPIResponse aPI = await this.ExistsRecord(qConfig);

                if (aPI.success != true)
                {
                    return(new CFAPIResponse {
                        success = true, messages = new object[] { "Not Exists this record!!!" }
                    });
                }
                throw new NotImplementedException("!TODO");
            } catch (Exception e) {
                _logger.Write("Error occured Remove DNS TXT Record for {0} . Error=> {1}", qConfig.DOMAIN_NAME, e.Message);
                _logger.Write(e.StackTrace);
                return(new CFAPIResponse {
                    success = true, messages = new object[] { e.Message }
                });
            }
        }
예제 #2
0
        public async Task <ICFAPIResponse> ExistsRecord(IQueryConfig qConfig)
        {
            string           requestUriString = string.Format("{0}zones/{1}/dns_records?type={2}&name={3}&content={4}", _config.CF_URI, _config.CF_DNS_ZONE, qConfig.RECORD_TYPE, qConfig.NAME, qConfig.RECORD_CONTENT);
            IWebHttpResponse resp             = await _webHttp.GetAsync(requestUriString, this.GetHeader( ));

            if (resp.status != WebHttpStatus.SUCCESS)
            {
                _logger.Write("Error occured while checking {0} record for {1} . Error=>", qConfig.RECORD_NAME, qConfig.DOMAIN_NAME, resp.errorDescription);
                return(new CFAPIResponse {
                    success = false, errors = new object[] { resp.errorDescription }
                });
            }
            if (string.IsNullOrEmpty(resp.responseText))
            {
                return(new CFAPIResponse {
                    success = false, errors = new object[] { "No Response found from API!!!" }
                });
            }
            ICFAPIResponse cFAPIResponse = JsonConvert.DeserializeObject <CFAPIResponse>(resp.responseText, jsonSettings);

            if (cFAPIResponse.result == null)
            {
                return(new CFAPIResponse {
                    success = false, errors = new object[] { "No Response found from API!!!" }
                });
            }
            if (cFAPIResponse.result is Newtonsoft.Json.Linq.JArray)
            {
                Newtonsoft.Json.Linq.JArray rs = (Newtonsoft.Json.Linq.JArray)cFAPIResponse.result;
                if (rs.Count <= 0)
                {
                    return(new CFAPIResponse {
                        success = false, errors = new object[] { "Not Exists!!!" }
                    });
                }
                _logger.Write("{0} record already exists in {1}", qConfig.RECORD_NAME, qConfig.DOMAIN_NAME);
            }
            return(cFAPIResponse);
        }
예제 #3
0
        public async Task <ICFAPIResponse> AddRecord(IQueryConfig qConfig, bool checkExistence = true)
        {
            if (qConfig.RECORD_TYPE != CFRecordType.TXT)
            {
                throw new NotImplementedException("Not Implemented Yet!!!");
            }
            if (checkExistence)
            {
                ICFAPIResponse aPI = await this.ExistsRecord(qConfig);

                if (aPI.success == true)
                {
                    return(new CFAPIResponse {
                        success = true, messages = new object[] { "Exists this record!!!" }
                    });
                }
            }
            _logger.Write("Adding DNS Record for {0}, Record type {1}", qConfig.DOMAIN_NAME, this.GetRecordText(qConfig.RECORD_TYPE));
            IWebHttpResponse resp = await _webHttp.PostAsync(string.Format("{0}zones/{1}/dns_records", _config.CF_URI, _config.CF_DNS_ZONE), JsonConvert.SerializeObject(this.GetData(qConfig), jsonSettings), this.GetHeader());

            if (resp.status != WebHttpStatus.SUCCESS)
            {
                _logger.Write("Error occured while add {0} record for {1} . Error=> {2}", qConfig.RECORD_NAME, qConfig.DOMAIN_NAME, resp.errorDescription);
                return(new CFAPIResponse {
                    success = false, errors = new object[] { resp.errorDescription }
                });
            }
            if (string.IsNullOrEmpty(resp.responseText))
            {
                _logger.Write("Error occured while add {0} record for {1} . Error=> No Response found from API!!!", qConfig.RECORD_NAME, qConfig.DOMAIN_NAME);
                return(new CFAPIResponse {
                    success = false, errors = new object[] { "No Response found from API!!!" }
                });
            }
            return(JsonConvert.DeserializeObject <CFAPIResponse>(resp.responseText, jsonSettings));
        }
예제 #4
0
        public async Task <IOrderResult> CreateOrRenewCert(int rec = 0, bool forceRenew = false)
        {
            try {
                {
                    var(status, result) = IsValidConfig( );
                    if (!status)
                    {
                        _logger.Write(result);
                        return(new OrderResult {
                            success = false,
                            taskType = TaskType.NOTHING,
                            errorDescription = result
                        });
                    }
                }
                _logger.Write("Create or Renew Certificate for {0}", domain.ZoneName);
                ICertificate oldCertificate = GetCertificate( );
                if (oldCertificate.status)
                {
                    if (!oldCertificate.isExpired && forceRenew == false)
                    {
                        _logger.Write("Certificate not expired for {0}", domain.ZoneName);
                        return(new OrderResult {
                            success = true,
                            taskType = TaskType.INSTALL_CERT,
                            oldCertificate = oldCertificate.Cert
                        });
                    }
                }
                await this.PrepareContext( );

                IOrderResult orderResult = new OrderResult {
                    success = true
                };
                if (oldCertificate.status)
                {
                    _logger.Write("Revoke Certificate for {0}", domain.ZoneName);
                    try {
                        await _acmeCtx.Ctx.RevokeCertificate(oldCertificate.Cert.RawData, RevocationReason.Unspecified);
                    } catch (Exception r) {
                        _logger.Write("Error occured while Revoke Certificate for {0}; Error=>{1}", domain.ZoneName, r.Message);
                        _logger.Write(r.StackTrace);
                        if (r.Message.IndexOf("alreadyRevoked") <= -1)
                        {
                            return(new OrderResult {
                                taskType = TaskType.NOTHING,
                                errorDescription = r.Message,
                                success = false
                            });
                        }
                    }
                }
                if (domain.IsWildcard)
                {
                    _orderContext = await _acmeCtx.Ctx.NewOrder(new List <string> {
                        domain.ZoneName, domain.DomainName
                    });
                }
                else
                {
                    _orderContext = await _acmeCtx.Ctx.NewOrder(new List <string> {
                        domain.DomainName
                    });
                }
                List <IChallengeCtx> challengeCtxs = new List <IChallengeCtx>( );
                List <IDnsTxtStore>  dnsTxt        = this.GetDnsTXT( );
                if (domain.IsWildcard)
                {
                    if (dnsTxt == null)
                    {
                        dnsTxt = new List <IDnsTxtStore>( );
                    }
                    List <IDnsTxtStore> writeDnsTxt = new List <IDnsTxtStore>( );
                    _logger.Write("Defined acme challenge type DNS for Wildcard(*) {0}", domain.ZoneName);
                    _logger.Write("Get Authorization Context for {0}", domain.ZoneName);
                    IEnumerable <IAuthorizationContext> authCtx = await _orderContext.Authorizations( );

                    _logger.Write("Authorization Context found for {0}", domain.ZoneName);
                    foreach (IAuthorizationContext authz in authCtx)
                    {
                        IChallengeContext challengeCtx = await authz.Dns( );

                        string       txt         = _acmeCtx.Ctx.AccountKey.DnsTxt(challengeCtx.Token);
                        IDnsTxtStore dnsTxtStore = dnsTxt.FirstOrDefault(a => a.content == txt);
                        if (dnsTxtStore != null)
                        {
                            challengeCtxs.Add(new ChallengeCtx {
                                ctx = challengeCtx, txtName = dnsTxtStore.name
                            });
                            dnsTxt.Remove(dnsTxtStore);
                            continue;
                        }
                        ICFAPIResponse cFAPIResponse = await _cfDns.AddRecord(new QueryConfig {
                            DOMAIN_NAME    = domain.ZoneName,
                            RECORD_TYPE    = CFRecordType.TXT,
                            RECORD_NAME    = "_acme-challenge",
                            RECORD_CONTENT = txt,
                            NAME           = string.Concat("_acme-challenge.", domain.ZoneName)
                        });

                        if (!cFAPIResponse.success)
                        {
                            orderResult.success          = false;
                            orderResult.errorDescription = JsonConvert.SerializeObject(cFAPIResponse.messages, _cfDns.jsonSettings);
                            break;
                        }

                        IChallengeCtx cCtx = new ChallengeCtx {
                            ctx = challengeCtx
                        };
                        dnsTxtStore = new DnsTxtStore {
                            content = txt
                        };
                        string txtName = string.Empty;
                        if (cFAPIResponse.result is JObject)
                        {
                            JObject jObject = ( JObject )cFAPIResponse.result;
                            dnsTxtStore.id = GetJPropertyValue(jObject.Property("id"));
                            txtName        = GetJPropertyValue(jObject.Property("name"));
                        }
                        else
                        {
                            txtName = string.Concat("_acme-challenge.", domain.ZoneName);
                        }
                        dnsTxtStore.name = txtName;
                        writeDnsTxt.Add(dnsTxtStore);
                        cCtx.txtName = txtName;
                        challengeCtxs.Add(cCtx);
                    }
                    if (orderResult.success == false)
                    {
                        return(orderResult);
                    }
                    ;
                    if (writeDnsTxt.Count > 0)
                    {
                        this.WriteDnsTXT(writeDnsTxt); writeDnsTxt.Clear( );
                    }
                }
                else
                {
                    throw new NotImplementedException("Not Implemented!!!");
                }
                foreach (IChallengeCtx cCtx in challengeCtxs)
                {
                    _logger.Write("Validating acme-challenge => {0} for Domain {1}", cCtx.txtName, domain.ZoneName);
                    IChalageStatus chalageStatus = await this.ValidateChalage(cCtx.ctx);

                    if (chalageStatus.status == false)
                    {
                        orderResult.success          = false;
                        orderResult.errorDescription = chalageStatus.errorDescription;
                        break;
                    }
                }
                if (domain.IsWildcard)
                {
                    foreach (IDnsTxtStore txt in dnsTxt)
                    {
                        await _cfDns.RemoveRecord(new QueryConfig {
                            DOMAIN_NAME    = domain.ZoneName,
                            RECORD_TYPE    = CFRecordType.TXT,
                            RECORD_NAME    = "_acme-challenge",
                            RECORD_CONTENT = txt.content,
                            NAME           = txt.name,
                            RECORD_ID      = txt.id
                        });
                    }
                }

                if (!orderResult.success)
                {
                    _logger.Write("Error occured while creating order request for {0} . Error=>{1}", domain.ZoneName, orderResult.errorDescription);
                    return(orderResult);
                }

                orderResult.taskType       = TaskType.DOWNLOAD_CERT;
                orderResult.oldCertificate = oldCertificate.Cert;
                return(orderResult);
            } catch (Exception e) {
                if (rec >= MAX_TRY)
                {
                    _logger.Write("Error occured while creating order request for {0} . Error=>{1}", domain.ZoneName, e.Message);
                    _logger.Write(e.StackTrace);
                    return(new OrderResult {
                        taskType = TaskType.NOTHING,
                        errorDescription = e.Message,
                        success = false
                    });
                }
                return(await CreateOrRenewCert(rec ++));
            }
        }