Exemple #1
0
        async static Task <int> Main(string[] args)
        {
            CancellationTokenSource = new CancellationTokenSource();
            cancellationToken       = CancellationTokenSource.Token;
            Console.CancelKeyPress += Console_CancelKeyPress;

            LogLine("Launching Portforwarded.Server, loading configuration");

            var(valid, errors) = LoadAndValidateConfig(args);
            if (!valid)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                LogLine("Configuration not valid, reasons stated below:", ConsoleColor.Red);
                errors.ForEach(l => LogLine(l, ConsoleColor.Red));

                return(-1);
            }

            UPnPDevice = await GetUPnPDevice();

            if (UPnPDevice == null)
            {
                LogLine("No UPnP device found, exiting.", ConsoleColor.Red);
                return(-1);
            }

            if (!WorkingDirectoryExists())
            {
                LogLine("Configured executable or directory does not exist, please check the path and file variables", ConsoleColor.Red);
                return(-1);
            }

            try
            {
                await SetupPortforwarding();
                await LaunchProcess();
            }
            catch (Exception ex)
            {
                LogLine(ex.ToString(), ConsoleColor.Red);
            } finally
            {
                CancellationTokenSource.Dispose();
                await TeardownPortforwarding();
            }

            return(0);
        }
Exemple #2
0
        /// <summary>
        /// Used to get the local IP address, that is on the same network as the UPnP device
        /// </summary>
        /// <param name="upnpDevice"></param>
        /// <returns></returns>
        private static IPAddress GetLocalIP(Open.Nat.NatDevice upnpDevice)
        {
            var hostEntries = Dns.GetHostEntry(Dns.GetHostName());
            var ipAddresses = hostEntries.AddressList.Select(i => i.ToString());

            var upnpDeviceEndpoint = upnpDevice.ToString().Split("\n").FirstOrDefault(d => d.Contains("EndPoint"));

            var endpointAddress   = string.Join('.', upnpDeviceEndpoint.Replace("EndPoint: ", "").Trim().Split('.').Take(2)) + ".";
            var matchingIPAddress = ipAddresses.FirstOrDefault(a => a.StartsWith(endpointAddress));

            if (string.IsNullOrWhiteSpace(matchingIPAddress))
            {
                return(null);
            }

            return(IPAddress.Parse(matchingIPAddress));
        }