using FastGithub.Configuration;
using FastGithub.ReverseProxy;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Net;
namespace FastGithub
{
    /// 
    /// Kestrel扩展
    /// 
    public static class KestrelServerOptionsExtensions
    {
        /// 
        /// 无限制
        /// 
        /// 
        public static void NoLimit(this KestrelServerOptions kestrel)
        {
            kestrel.Limits.MaxRequestBodySize = null;
        }
        /// 
        /// 监听ssh
        /// 
        /// 
        public static void ListenSsh(this KestrelServerOptions kestrel)
        {
            const int SSH_PORT = 22;
            if (LocalMachine.CanListenTcp(SSH_PORT) == true)
            {
                kestrel.Listen(IPAddress.Any, SSH_PORT, listen => listen.UseConnectionHandler());
                kestrel.GetLogger().LogInformation($"已监听tcp端口{SSH_PORT},github的ssh代理启动完成");
            }
        }
        /// 
        /// 监听http
        /// 
        /// 
        public static void ListenHttp(this KestrelServerOptions kestrel)
        {
            const int HTTP_PORT = 80;
            if (LocalMachine.CanListenTcp(HTTP_PORT) == true)
            {
                kestrel.Listen(IPAddress.Any, HTTP_PORT);
                kestrel.GetLogger().LogInformation($"已监听tcp端口{HTTP_PORT},http反向代理启动完成");
            }
        }
        /// 
        /// 监听https
        /// 
        /// 
        /// 
        public static void ListenHttps(this KestrelServerOptions kestrel)
        {
            const int HTTPS_PORT = 443;
            if (OperatingSystem.IsWindows())
            {
                TcpTable.KillPortOwner(HTTPS_PORT);
            }
            if (LocalMachine.CanListenTcp(HTTPS_PORT) == false)
            {
                throw new FastGithubException($"tcp端口{HTTPS_PORT}已经被其它进程占用");
            }
            var certService = kestrel.ApplicationServices.GetRequiredService();
            certService.CreateCaCertIfNotExists();
            certService.InstallAndTrustCaCert();
            kestrel.Listen(IPAddress.Any, HTTPS_PORT,
                listen => listen.UseHttps(https =>
                    https.ServerCertificateSelector = (ctx, domain) =>
                        certService.GetOrCreateServerCert(domain)));
            var logger = kestrel.GetLogger();
            logger.LogInformation($"已监听tcp端口{HTTPS_PORT},https反向代理启动完成");
        }
        /// 
        /// 获取日志
        /// 
        /// 
        /// 
        private static ILogger GetLogger(this KestrelServerOptions kestrel)
        {
            var loggerFactory = kestrel.ApplicationServices.GetRequiredService();
            return loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(ReverseProxy)}");
        }
    }
}