private async Task <CommandResult <TargetDto> > ReCreateCertificate(Guid id) { var target = await _repository.GetById(id); if (target == null) { return(new CommandResult <TargetDto>() { IsSucceeded = false }); } var ssl = SSLHelper.CreateSSL(target.Domain); target.CertKey = ssl.CertKey; target.CertCrt = ssl.CertCrt; await _repository.Update(target); return(new CommandResult <TargetDto>() { IsSucceeded = true, Result = _mapper.Map <TargetDto>(target) }); }
public async Task <CommandResult <TargetDto> > Handle(Command message, CancellationToken cancellationToken) { if (message.Target.CreateSelfSignedCertificate) { return(await ReCreateCertificate(message.Target.Id)); } var theTargetWithDomain = await _repository.GetTargetWithTheDomain(message.Target.Domain); var target = await _repository.GetById(message.Target.Id); if (target == null || (theTargetWithDomain != null && theTargetWithDomain.Id != target.Id)) { return(new CommandResult <TargetDto>() { IsSucceeded = false }); } target.OriginIpAddress = message.Target.OriginIpAddress; target.UseHttps = message.Target.UseHttps; target.CertCrt = message.Target.CertCrt; target.CertKey = message.Target.CertKey; target.WAFEnabled = message.Target.WAFEnabled; target.AutoCert = message.Target.AutoCert; if (target.Domain != message.Target.Domain) { var ssl = SSLHelper.CreateSSL(message.Target.Domain); target.Domain = message.Target.Domain; } await _repository.Update(target); return(new CommandResult <TargetDto>() { IsSucceeded = true, Result = _mapper.Map <TargetDto>(target) }); }
public async Task <CommandResult <TargetDto> > Handle(Command message, CancellationToken cancellationToken) { var anyTarget = await _repository.GetTargetWithTheDomain(message.Target.Domain); if (anyTarget != null) { return(new CommandResult <TargetDto>() { IsSucceeded = false, Message = "Target in use." }); } message.Target.UseHttps = message.Target.AutoCert || message.Target.CreateSelfSignedCertificate; message.Target.WAFEnabled = true; var target = _mapper.Map <Infrastructure.Entity.Target>(message.Target); //TODO: Add verification process. target.State = TargetState.Redirected; if (message.Target.CreateSelfSignedCertificate) { var sslCert = SSLHelper.CreateSSL(target.Domain); target.CertCrt = sslCert.CertCrt; target.CertKey = sslCert.CertKey; } var ipAddress = await Dns.GetHostEntryAsync(target.Domain); target.OriginIpAddress = ipAddress.AddressList.FirstOrDefault()?.ToString() ?? null; target.Proto = await ObtainProtocol(target.Domain); await _repository.Add(target); return(new CommandResult <TargetDto>() { IsSucceeded = true, Result = _mapper.Map <TargetDto>(target) }); }