<commit_before>﻿using Open.Nat;
using Server.Context;
using Server.Log;
using Server.Settings.Structures;
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

namespace Server.Upnp
{
    public class LmpPortMapper
    {
        private static readonly int LifetimeInSeconds = (int)TimeSpan.FromMinutes(5).TotalSeconds;
        private static readonly Lazy<NatDevice> Device = new Lazy<NatDevice>(DiscoverDevice);

        private static Mapping LmpPortMapping => new Mapping(Protocol.Udp, ConnectionSettings.SettingsStore.Port, ConnectionSettings.SettingsStore.Port, LifetimeInSeconds, $"LMPServer {ConnectionSettings.SettingsStore.Port}");

        private static NatDevice DiscoverDevice()
        {
            var nat = new NatDiscoverer();
            return nat.DiscoverDeviceAsync(PortMapper.Upnp, new CancellationTokenSource(ConnectionSettings.SettingsStore.UpnpMsTimeout)).Result;
        }

        /// <summary>
        /// Opens the port set in the settings using UPnP. With a lifetime of 10 minutes
        /// </summary>
        public static async Task OpenPort()
        {
            if (ConnectionSettings.SettingsStore.Upnp)
            {
                try
                {
                    await Device.Value.CreatePortMapAsync(LmpPortMapping);
                    LunaLog.Debug($"UPnP active. Port: {ConnectionSettings.SettingsStore.Port} opened!");
                }
                catch (Exception)
                {
                    // ignored
                }
            }
        }

        /// <summary>
        /// Refresh the UPnP port every 1 minute
        /// </summary>
        public static async void RefreshUpnpPort()
        {
            if (ConnectionSettings.SettingsStore.Upnp)
            {
                while (ServerContext.ServerRunning)
                {
                    await OpenPort();
                    await Task.Delay((int)TimeSpan.FromSeconds(60).TotalMilliseconds);
                }
            }
        }

        /// <summary>
        /// Closes the opened ports using UPnP
        /// </summary>
        public static async Task RemoveOpenedPorts()
        {
            if (ConnectionSettings.SettingsStore.Upnp)
            {
                try
                {
                    await Device.Value.DeletePortMapAsync(LmpPortMapping);
                    LunaLog.Debug($"UPnP active. Port: {ConnectionSettings.SettingsStore.Port} closed!");
                }
                catch (Exception)
                {
                    // ignored
                }
            }
        }<commit_msg>Remove verbose<commit_after>﻿using Open.Nat;
using Server.Context;
using Server.Log;
using Server.Settings.Structures;
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

namespace Server.Upnp
{
    public class LmpPortMapper
    {
        private static readonly int LifetimeInSeconds = (int)TimeSpan.FromMinutes(5).TotalSeconds;
        private static readonly Lazy<NatDevice> Device = new Lazy<NatDevice>(DiscoverDevice);

        private static Mapping LmpPortMapping => new Mapping(Protocol.Udp, ConnectionSettings.SettingsStore.Port, ConnectionSettings.SettingsStore.Port, LifetimeInSeconds, $"LMPServer {ConnectionSettings.SettingsStore.Port}");

        private static NatDevice DiscoverDevice()
        {
            var nat = new NatDiscoverer();
            return nat.DiscoverDeviceAsync(PortMapper.Upnp, new CancellationTokenSource(ConnectionSettings.SettingsStore.UpnpMsTimeout)).Result;
        }

        /// <summary>
        /// Opens the port set in the settings using UPnP. With a lifetime of 10 minutes
        /// </summary>
        public static async Task OpenPort(bool verbose = true)
        {
            if (ConnectionSettings.SettingsStore.Upnp)
            {
                try
                {
                    await Device.Value.CreatePortMapAsync(LmpPortMapping);
                    if (verbose)
                    LunaLog.Debug($"UPnP active. Port: {ConnectionSettings.SettingsStore.Port} opened!");
                }
                catch (Exception)
                {
                    // ignored
                }
            }
        }

        /// <summary>
        /// Refresh the UPnP port every 1 minute
        /// </summary>
        public static async void RefreshUpnpPort()
        {
            if (ConnectionSettings.SettingsStore.Upnp)
            {
                while (ServerContext.ServerRunning)
                {
                    await OpenPort(false);
                    await Task.Delay((int)TimeSpan.FromSeconds(60).TotalMilliseconds);
                }
            }
        }

        /// <summary>
        /// Closes the opened ports using UPnP
        /// </summary>
        public static async Task RemoveOpenedPorts()
        {
            if (ConnectionSettings.SettingsStore.Upnp)
            {
                try
                {
                    await Device.Value.DeletePortMapAsync(LmpPortMapping);
                    LunaLog.Debug($"UPnP active. Port: {ConnectionSettings.SettingsStore.Port} closed!");
                }
                catch (Exception)
                {
                    // ignored
                }
            }
        }
{'fraction_non_alphanumeric': 0.08569807037457434, 'fraction_numerical': 0.0022701475595913734, 'mean_word_length': 1.6108641975308642, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7631215', 'n_tokens_mistral': 1328, 'n_tokens_neox': 1263, 'n_words': 285}