예제 #1
0
파일: Add.cs 프로젝트: asalih/GuardianUI
            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)
                });
            }