Пример #1
0
        public void Test_UpnpClient()
        {
            UpnpClient client = new UpnpClient();

            client.Connect(new TimeSpan(0, 0, 10));

            var ip = client.GetExternalIpAddress(new TimeSpan(0, 0, 10));

            Assert.AreNotEqual(ip, null, "UPnPClient #1");
        }
Пример #2
0
        private void Shutdown()
        {
            lock (_thisLock)
            {
                if (_settings.Ipv4Uri != null)
                {
                    if (this.RemoveUri(_settings.Ipv4Uri))
                    {
                        Log.Information(string.Format("Remove Node uri: {0}", _settings.Ipv4Uri));
                    }
                }
                _settings.Ipv4Uri = null;

                if (_settings.Ipv6Uri != null)
                {
                    if (this.RemoveUri(_settings.Ipv6Uri))
                    {
                        Log.Information(string.Format("Remove Node uri: {0}", _settings.Ipv6Uri));
                    }
                }
                _settings.Ipv6Uri = null;

                if (_settings.UpnpUri != null)
                {
                    if (this.RemoveUri(_settings.UpnpUri))
                    {
                        Log.Information(string.Format("Remove Node uri: {0}", _settings.UpnpUri));
                    }

                    try
                    {
                        using (UpnpClient client = new UpnpClient())
                        {
                            client.Connect(new TimeSpan(0, 0, 10));

                            var regex = new Regex(@"(.*?):(.*):(\d*)");
                            var match = regex.Match(_settings.UpnpUri);
                            if (!match.Success)
                            {
                                throw new Exception();
                            }
                            int port = int.Parse(match.Groups[3].Value);

                            client.ClosePort(UpnpProtocolType.Tcp, port, new TimeSpan(0, 0, 10));

                            Log.Information(string.Format("UPnP Close Port: {0}", port));
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
                _settings.UpnpUri = null;
            }
        }
Пример #3
0
        public async Task GetExternalIpAddressTest()
        {
            using (var tokenSource = new CancellationTokenSource(10 * 1000))
            {
                var upnp = new UpnpClient();

                try
                {
                    await upnp.Connect(tokenSource.Token);
                }
                catch (Exception)
                {
                    // UPnPに接続できない環境だった場合
                    return;
                }

                var ip = await upnp.GetExternalIpAddress(tokenSource.Token);

                Assert.True(ip != null);
            }
        }
Пример #4
0
        public override void Stop()
        {
            lock (_stateLockObject)
            {
                lock (_lockObject)
                {
                    if (this.State == ManagerState.Stop)
                    {
                        return;
                    }
                    _state = ManagerState.Stop;
                }

                _watchTimer.Stop();

                if (_ipv4TcpListener != null)
                {
                    try
                    {
                        _ipv4TcpListener.Server.Dispose();
                        _ipv4TcpListener.Stop();

                        _ipv4TcpListener = null;
                    }
                    catch (Exception e)
                    {
                        Log.Error(e);
                    }

                    // Close port
                    try
                    {
                        using (var client = new UpnpClient())
                        {
                            client.Connect(new TimeSpan(0, 0, 10));

                            client.ClosePort(UpnpProtocolType.Tcp, _watchIpv4Port, new TimeSpan(0, 0, 10));
                        }
                    }
                    catch (Exception)
                    {
                    }

                    _watchIpv4Port = -1;
                }

                if (_ipv6TcpListener != null)
                {
                    try
                    {
                        _ipv6TcpListener.Server.Dispose();
                        _ipv6TcpListener.Stop();

                        _ipv6TcpListener = null;
                    }
                    catch (Exception e)
                    {
                        Log.Error(e);
                    }

                    _watchIpv6Port = -1;
                }
            }
        }
Пример #5
0
        private void WatchListenerThread()
        {
            for (; ;)
            {
                var config = this.Config;

                string ipv4Uri = null;
                string ipv6Uri = null;

                if (config.Type.HasFlag(TcpConnectionType.Ipv4) && config.Ipv4Port != 0)
                {
                    UpnpClient upnpClient = null;

                    try
                    {
                        {
                            var ipAddress = GetMyGlobalIpAddresses().FirstOrDefault(n => n.AddressFamily == AddressFamily.InterNetwork);

                            if (ipAddress != null)
                            {
                                ipv4Uri = string.Format("tcp:{0}:{1}", ipAddress.ToString(), config.Ipv4Port);
                            }
                        }

                        if (ipv4Uri == null)
                        {
                            upnpClient = new UpnpClient();
                            upnpClient.Connect(new TimeSpan(0, 0, 30));

                            if (upnpClient.IsConnected)
                            {
                                var ipAddress = IPAddress.Parse(upnpClient.GetExternalIpAddress(new TimeSpan(0, 0, 10)));

                                if (ipAddress != null && IsGlobalIpAddress(ipAddress))
                                {
                                    ipv4Uri = string.Format("tcp:{0}:{1}", ipAddress.ToString(), config.Ipv4Port);
                                }
                            }
                        }

                        if (_ipv4TcpListener == null || _watchIpv4Port != config.Ipv4Port)
                        {
                            try
                            {
                                if (_ipv4TcpListener != null)
                                {
                                    _ipv4TcpListener.Server.Dispose();
                                    _ipv4TcpListener.Stop();

                                    _ipv4TcpListener = null;
                                }

                                _ipv4TcpListener = new TcpListener(IPAddress.Any, config.Ipv4Port);
                                _ipv4TcpListener.Start(3);
                            }
                            catch (Exception e)
                            {
                                Log.Error(e);
                            }

                            // Open port
                            if (upnpClient != null && upnpClient.IsConnected)
                            {
                                if (_watchIpv4Port != -1)
                                {
                                    upnpClient.ClosePort(UpnpProtocolType.Tcp, _watchIpv4Port, new TimeSpan(0, 0, 10));
                                }

                                upnpClient.OpenPort(UpnpProtocolType.Tcp, config.Ipv4Port, config.Ipv4Port, "Amoeba", new TimeSpan(0, 0, 10));
                            }

                            _watchIpv4Port = config.Ipv4Port;
                        }
                    }
                    finally
                    {
                        if (upnpClient != null)
                        {
                            upnpClient.Dispose();
                            upnpClient = null;
                        }
                    }
                }
                else
                {
                    if (_ipv4TcpListener != null)
                    {
                        try
                        {
                            _ipv4TcpListener.Server.Dispose();
                            _ipv4TcpListener.Stop();

                            _ipv4TcpListener = null;
                        }
                        catch (Exception e)
                        {
                            Log.Error(e);
                        }

                        // Close port
                        try
                        {
                            using (var client = new UpnpClient())
                            {
                                client.Connect(new TimeSpan(0, 0, 10));

                                client.ClosePort(UpnpProtocolType.Tcp, _watchIpv4Port, new TimeSpan(0, 0, 10));
                            }
                        }
                        catch (Exception)
                        {
                        }

                        _watchIpv4Port = -1;
                    }
                }

                if (config.Type.HasFlag(TcpConnectionType.Ipv6) && config.Ipv6Port != 0)
                {
                    {
                        var ipAddress = GetMyGlobalIpAddresses().FirstOrDefault(n => n.AddressFamily == AddressFamily.InterNetworkV6);

                        if (ipAddress != null)
                        {
                            ipv6Uri = string.Format("tcp:[{0}]:{1}", ipAddress.ToString(), config.Ipv6Port);
                        }
                    }

                    if (_ipv6TcpListener == null || _watchIpv6Port != config.Ipv6Port)
                    {
                        try
                        {
                            if (_ipv6TcpListener != null)
                            {
                                _ipv6TcpListener.Server.Dispose();
                                _ipv6TcpListener.Stop();

                                _ipv6TcpListener = null;
                            }

                            _ipv6TcpListener = new TcpListener(IPAddress.IPv6Any, config.Ipv6Port);
                            _ipv6TcpListener.Start(3);
                        }
                        catch (Exception e)
                        {
                            Log.Error(e);
                        }

                        _watchIpv6Port = config.Ipv6Port;
                    }
                }
                else
                {
                    if (_ipv6TcpListener != null)
                    {
                        try
                        {
                            _ipv6TcpListener.Server.Dispose();
                            _ipv6TcpListener.Stop();

                            _ipv6TcpListener = null;
                        }
                        catch (Exception e)
                        {
                            Log.Error(e);
                        }

                        _watchIpv6Port = -1;
                    }
                }

                lock (_lockObject)
                {
                    if (this.Config != config)
                    {
                        continue;
                    }

                    _locationUris.Clear();
                    if (ipv4Uri != null)
                    {
                        _locationUris.Add(ipv4Uri);
                    }
                    if (ipv6Uri != null)
                    {
                        _locationUris.Add(ipv6Uri);
                    }
                }

                return;
            }
        }
Пример #6
0
        public void Update()
        {
            lock (_thisLock)
            {
                if (this.State == ManagerState.Stop)
                {
                    return;
                }

                {
                    string ipv4Uri = null;

                    try
                    {
                        string uri = _amoebaManager.ListenUris.FirstOrDefault(n => n.StartsWith(string.Format("tcp:{0}:", IPAddress.Any.ToString())));

                        var regex = new Regex(@"(.*?):(.*):(\d*)");
                        var match = regex.Match(uri);
                        if (!match.Success)
                        {
                            throw new Exception();
                        }

                        int port = int.Parse(match.Groups[3].Value);

                        var myIpAddresses = new List <IPAddress>(ConnectionSettingManager.GetIpAddresses());

                        foreach (var myIpAddress in myIpAddresses.Where(n => n.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork))
                        {
                            if (IPAddress.Any.ToString() == myIpAddress.ToString() ||
                                IPAddress.Loopback.ToString() == myIpAddress.ToString() ||
                                IPAddress.Broadcast.ToString() == myIpAddress.ToString())
                            {
                                continue;
                            }
                            if (ConnectionSettingManager.IpAddressCompare(myIpAddress, IPAddress.Parse("10.0.0.0")) >= 0 &&
                                ConnectionSettingManager.IpAddressCompare(myIpAddress, IPAddress.Parse("10.255.255.255")) <= 0)
                            {
                                continue;
                            }
                            if (ConnectionSettingManager.IpAddressCompare(myIpAddress, IPAddress.Parse("172.16.0.0")) >= 0 &&
                                ConnectionSettingManager.IpAddressCompare(myIpAddress, IPAddress.Parse("172.31.255.255")) <= 0)
                            {
                                continue;
                            }
                            if (ConnectionSettingManager.IpAddressCompare(myIpAddress, IPAddress.Parse("127.0.0.0")) >= 0 &&
                                ConnectionSettingManager.IpAddressCompare(myIpAddress, IPAddress.Parse("127.255.255.255")) <= 0)
                            {
                                continue;
                            }
                            if (ConnectionSettingManager.IpAddressCompare(myIpAddress, IPAddress.Parse("192.168.0.0")) >= 0 &&
                                ConnectionSettingManager.IpAddressCompare(myIpAddress, IPAddress.Parse("192.168.255.255")) <= 0)
                            {
                                continue;
                            }

                            ipv4Uri = string.Format("tcp:{0}:{1}", myIpAddress.ToString(), port);

                            break;
                        }
                    }
                    catch (Exception)
                    {
                    }

                    if (ipv4Uri != _settings.Ipv4Uri)
                    {
                        if (this.RemoveUri(_settings.Ipv4Uri))
                        {
                            Log.Information(string.Format("Remove Node uri: {0}", _settings.Ipv4Uri));
                        }
                    }

                    _settings.Ipv4Uri = ipv4Uri;

                    if (_settings.Ipv4Uri != null)
                    {
                        if (this.AddUri(_settings.Ipv4Uri))
                        {
                            Log.Information(string.Format("Add Node uri: {0}", _settings.Ipv4Uri));
                        }
                    }
                }

                {
                    string ipv6Uri = null;

                    try
                    {
                        string uri = _amoebaManager.ListenUris.FirstOrDefault(n => n.StartsWith(string.Format("tcp:[{0}]:", IPAddress.IPv6Any.ToString())));

                        var regex = new Regex(@"(.*?):(.*):(\d*)");
                        var match = regex.Match(uri);
                        if (!match.Success)
                        {
                            throw new Exception();
                        }

                        int port = int.Parse(match.Groups[3].Value);

                        var myIpAddresses = new List <IPAddress>(ConnectionSettingManager.GetIpAddresses());

                        foreach (var myIpAddress in myIpAddresses.Where(n => n.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6))
                        {
                            if (IPAddress.IPv6Any.ToString() == myIpAddress.ToString() ||
                                IPAddress.IPv6Loopback.ToString() == myIpAddress.ToString() ||
                                IPAddress.IPv6None.ToString() == myIpAddress.ToString())
                            {
                                continue;
                            }
                            if (myIpAddress.ToString().StartsWith("fe80:"))
                            {
                                continue;
                            }
                            if (myIpAddress.ToString().StartsWith("2001:"))
                            {
                                continue;
                            }
                            if (myIpAddress.ToString().StartsWith("2002:"))
                            {
                                continue;
                            }

                            ipv6Uri = string.Format("tcp:[{0}]:{1}", myIpAddress.ToString(), port);

                            break;
                        }
                    }
                    catch (Exception)
                    {
                    }

                    if (ipv6Uri != _settings.Ipv6Uri)
                    {
                        if (this.RemoveUri(_settings.Ipv6Uri))
                        {
                            Log.Information(string.Format("Remove Node uri: {0}", _settings.Ipv6Uri));
                        }
                    }

                    _settings.Ipv6Uri = ipv6Uri;

                    if (_settings.Ipv6Uri != null)
                    {
                        if (this.AddUri(_settings.Ipv6Uri))
                        {
                            Log.Information(string.Format("Add Node uri: {0}", _settings.Ipv6Uri));
                        }
                    }
                }

                {
                    string upnpUri = null;

                    try
                    {
                        string uri = _amoebaManager.ListenUris.FirstOrDefault(n => n.StartsWith(string.Format("tcp:{0}:", IPAddress.Any.ToString())));

                        var regex = new Regex(@"(.*?):(.*):(\d*)");
                        var match = regex.Match(uri);
                        if (!match.Success)
                        {
                            throw new Exception();
                        }

                        int port = int.Parse(match.Groups[3].Value);

                        using (UpnpClient client = new UpnpClient())
                        {
                            client.Connect(new TimeSpan(0, 0, 10));

                            string ip = client.GetExternalIpAddress(new TimeSpan(0, 0, 10));
                            if (string.IsNullOrWhiteSpace(ip))
                            {
                                throw new Exception();
                            }

                            upnpUri = string.Format("tcp:{0}:{1}", ip, port);

                            if (upnpUri != _settings.UpnpUri)
                            {
                                if (_settings.UpnpUri != null)
                                {
                                    try
                                    {
                                        var match2 = regex.Match(_settings.UpnpUri);
                                        if (!match2.Success)
                                        {
                                            throw new Exception();
                                        }
                                        int port2 = int.Parse(match2.Groups[3].Value);

                                        client.ClosePort(UpnpProtocolType.Tcp, port2, new TimeSpan(0, 0, 10));
                                        Log.Information(string.Format("UPnP Close port: {0}", port2));
                                    }
                                    catch (Exception)
                                    {
                                    }
                                }

                                client.ClosePort(UpnpProtocolType.Tcp, port, new TimeSpan(0, 0, 10));

                                if (client.OpenPort(UpnpProtocolType.Tcp, port, port, "Amoeba", new TimeSpan(0, 0, 10)))
                                {
                                    Log.Information(string.Format("UPnP Open port: {0}", port));
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }

                    if (upnpUri != _settings.UpnpUri)
                    {
                        if (this.RemoveUri(_settings.UpnpUri))
                        {
                            Log.Information(string.Format("Remove Node uri: {0}", _settings.UpnpUri));
                        }
                    }

                    _settings.UpnpUri = upnpUri;

                    if (_settings.UpnpUri != null)
                    {
                        if (this.AddUri(_settings.UpnpUri))
                        {
                            Log.Information(string.Format("Add Node uri: {0}", _settings.UpnpUri));
                        }
                    }
                }
            }
        }