[GH-ISSUE #408] The UPNP Error does not popup on Quasar startup #201

Open
opened 2026-02-27 15:49:19 +03:00 by kerem · 4 comments
Owner

Originally created by @webiummedia on GitHub (Oct 23, 2015).
Original GitHub issue: https://github.com/quasar/Quasar/issues/408

When you open Quasar it starts listening to port by itself. But if i stop start manually, i get the Upnp error. That means, for some reason, the Upnp is not functional on startup and there's no popup to let me know.

Originally created by @webiummedia on GitHub (Oct 23, 2015). Original GitHub issue: https://github.com/quasar/Quasar/issues/408 When you open Quasar it starts listening to port by itself. But if i stop start manually, i get the Upnp error. That means, for some reason, the Upnp is not functional on startup and there's no popup to let me know.
Author
Owner

@yankejustin commented on GitHub (Oct 23, 2015):

What are your current settings?

<!-- gh-comment-id:150581818 --> @yankejustin commented on GitHub (Oct 23, 2015): What are your current settings?
Author
Owner

@webiummedia commented on GitHub (Oct 23, 2015):

image

This is what I got

<!-- gh-comment-id:150588401 --> @webiummedia commented on GitHub (Oct 23, 2015): ![image](https://cloud.githubusercontent.com/assets/4869819/10695178/5e6eb962-7970-11e5-95b3-b9deb4cfcc17.png) This is what I got
Author
Owner

@webiummedia commented on GitHub (Oct 23, 2015):

Theres no auto port forward failed notification on the quasar opening. I only get the failed notification if i manuely stop-start the listening. When i get the fail i need to open my rooter's control panel, close upnp then reopen it and whait 5 seconds then press start in quasar ... no idea why but I have to do that everytime or quasar can't receive any connections.

<!-- gh-comment-id:150588871 --> @webiummedia commented on GitHub (Oct 23, 2015): Theres no auto port forward failed notification on the quasar opening. I only get the failed notification if i manuely stop-start the listening. When i get the fail i need to open my rooter's control panel, close upnp then reopen it and whait 5 seconds then press start in quasar ... no idea why but I have to do that everytime or quasar can't receive any connections.
Author
Owner

@gozilla-paradise commented on GitHub (Nov 6, 2015):

Simply Copy and Paste this in .\Server\Core\Networking\Utilities\UPNP.cs

This will popped message if UPNP is failed

using System;
using System.Collections.Generic;
using Mono.Nat;
using System.Windows.Forms;

namespace xServer.Core.Networking.Utilities
{
    internal static class UPnP
    {
        private static Dictionary<int, Mapping> _mappings;
        private static bool _discoveryComplete;
        private static INatDevice _device;
        private static int _port = -1;

        /// <summary>
        /// Initializes the discovery of new UPnP devices.
        /// </summary>
        public static void Initialize()
        {
            _mappings = new Dictionary<int, Mapping>();

            try
            {
                NatUtility.DeviceFound += DeviceFound;
                NatUtility.DeviceLost += DeviceLost;

                _discoveryComplete = false;

                NatUtility.StartDiscovery();
            }
            catch (Exception)
            {
                MessageBox.Show("QuasarRAT: UPNP Initialize Failed");
            }
        }

        /// <summary>
        /// Initializes the discovery of new UPnP devices
        /// and creates a port map with the given port.
        /// </summary>
        /// <param name="port">The port to map.</param>
        public static void Initialize(int port)
        {
            _port = port;
            Initialize();
        }

        /// <summary>
        /// Tells if the class found an UPnP device.
        /// </summary>
        public static bool IsDeviceFound
        {
            get { return _device != null; }
        }

        /// <summary>
        /// Creates a new port map.
        /// </summary>
        /// <param name="port">The port to map.</param>
        /// <param name="externalPort">The port which has been mapped, -1 if it failed.</param>
        /// <returns>True if successfull, else False.</returns>
        public static bool CreatePortMap(int port, out int externalPort)
        {
            if (!_discoveryComplete)
            {
                externalPort = -1;
                return false;
            }

            try
            {
                Mapping mapping = new Mapping(Protocol.Tcp, port, port);

                for (int i = 0; i < 3; i++)
                    _device.CreatePortMap(mapping);

                if (_mappings.ContainsKey(mapping.PrivatePort))
                    _mappings[mapping.PrivatePort] = mapping;
                else
                    _mappings.Add(mapping.PrivatePort, mapping);

                externalPort = mapping.PublicPort;
                return true;
            }
            catch (MappingException)
            {
                externalPort = -1;
                return false;
            }
        }

        /// <summary>
        /// Deletes an existing port map.
        /// </summary>
        /// <param name="port">The port to delete.</param>
        public static void DeletePortMap(int port)
        {
            if (!_discoveryComplete)
                return;

            Mapping mapping;
            if (_mappings.TryGetValue(port, out mapping))
            {
                try
                {
                    for (int i = 0; i < 3; i++)
                        _device.DeletePortMap(mapping);
                }
                catch (MappingException)
                {
                }
            }
        }

        private static void DeviceFound(object sender, DeviceEventArgs args)
        {
            _device = args.Device;

            NatUtility.StopDiscovery();

            _discoveryComplete = true;

            if (_port > 0)
            {
                int outPort;
                CreatePortMap(_port, out outPort);
            }
        }

        private static void DeviceLost(object sender, DeviceEventArgs args)
        {
            _device = null;
            _discoveryComplete = false;
        }
    }
}

<!-- gh-comment-id:154436483 --> @gozilla-paradise commented on GitHub (Nov 6, 2015): Simply Copy and Paste this in .\Server\Core\Networking\Utilities\UPNP.cs This will popped message if UPNP is failed ``` using System; using System.Collections.Generic; using Mono.Nat; using System.Windows.Forms; namespace xServer.Core.Networking.Utilities { internal static class UPnP { private static Dictionary<int, Mapping> _mappings; private static bool _discoveryComplete; private static INatDevice _device; private static int _port = -1; /// <summary> /// Initializes the discovery of new UPnP devices. /// </summary> public static void Initialize() { _mappings = new Dictionary<int, Mapping>(); try { NatUtility.DeviceFound += DeviceFound; NatUtility.DeviceLost += DeviceLost; _discoveryComplete = false; NatUtility.StartDiscovery(); } catch (Exception) { MessageBox.Show("QuasarRAT: UPNP Initialize Failed"); } } /// <summary> /// Initializes the discovery of new UPnP devices /// and creates a port map with the given port. /// </summary> /// <param name="port">The port to map.</param> public static void Initialize(int port) { _port = port; Initialize(); } /// <summary> /// Tells if the class found an UPnP device. /// </summary> public static bool IsDeviceFound { get { return _device != null; } } /// <summary> /// Creates a new port map. /// </summary> /// <param name="port">The port to map.</param> /// <param name="externalPort">The port which has been mapped, -1 if it failed.</param> /// <returns>True if successfull, else False.</returns> public static bool CreatePortMap(int port, out int externalPort) { if (!_discoveryComplete) { externalPort = -1; return false; } try { Mapping mapping = new Mapping(Protocol.Tcp, port, port); for (int i = 0; i < 3; i++) _device.CreatePortMap(mapping); if (_mappings.ContainsKey(mapping.PrivatePort)) _mappings[mapping.PrivatePort] = mapping; else _mappings.Add(mapping.PrivatePort, mapping); externalPort = mapping.PublicPort; return true; } catch (MappingException) { externalPort = -1; return false; } } /// <summary> /// Deletes an existing port map. /// </summary> /// <param name="port">The port to delete.</param> public static void DeletePortMap(int port) { if (!_discoveryComplete) return; Mapping mapping; if (_mappings.TryGetValue(port, out mapping)) { try { for (int i = 0; i < 3; i++) _device.DeletePortMap(mapping); } catch (MappingException) { } } } private static void DeviceFound(object sender, DeviceEventArgs args) { _device = args.Device; NatUtility.StopDiscovery(); _discoveryComplete = true; if (_port > 0) { int outPort; CreatePortMap(_port, out outPort); } } private static void DeviceLost(object sender, DeviceEventArgs args) { _device = null; _discoveryComplete = false; } } } ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/Quasar#201
No description provided.