Exemplo n.º 1
0
        public static bool Forward(string name, uint port, string ip, Protocol protocol = Protocol.TCP,
                                   bool @async = true)
        {
            try
            {
                // lblStatus.Text = "adding port forward...";
                if (_lastInstance == null)
                {
                    _lastInstance = new UPnP();
                }

                if (_lastInstance.Exists(port, protocol))
                {
                    MetroMessageBox.Show(Application.OpenForms[0], Locale.Tr("This port is aleady forwarded"),
                                         Locale.Tr("Port already in use"),
                                         MessageBoxButtons.OK,
                                         MessageBoxIcon.Error);
                    return(false);
                    //aLocale.Tready in use
                }
                if (!@async)
                {
                    _lastInstance.Add(ip, port, protocol, name);
                }
                else
                {
                    Thread t = new Thread((() => Forward(new PortMappingEntry(port, ip, name, protocol))))
                    {
                        Name = "Foward_" + port
                    };
                    t.SetApartmentState(ApartmentState.MTA);
                    t.Start();
                }
                return(true);
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevel.Warning, "PortForwarder", "Couldn't forward ports", ex.InnerException.Message);
                MetroMessageBox.Show(Application.OpenForms[0],
                                     Locale.Tr(
                                         "This port couldn't be forwarded. Something went wrong while communicating with your router"),
                                     Locale.Tr("Can't forward"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
        }