internal DeviceAnnouncement(UpnpClient client, DeviceType type, string udn, IEnumerable<string> locations)
 {
     this.client = client;
     this.type = type;
     this.udn = udn;
     this.locations = locations;
 }
 internal ServiceAnnouncement(UpnpClient client, ServiceType type, string deviceUdn, IEnumerable <string> locations)
 {
     this.client    = client;
     this.type      = type;
     this.deviceUdn = deviceUdn;
     this.locations = locations;
 }
Exemplo n.º 3
0
 internal DeviceAnnouncement(UpnpClient client, DeviceType type, string udn, IEnumerable <string> locations)
 {
     this.client    = client;
     this.type      = type;
     this.udn       = udn;
     this.locations = locations;
 }
 internal ServiceAnnouncement(UpnpClient client, ServiceType type, string deviceUdn, IEnumerable<string> locations)
 {
     this.client = client;
     this.type = type;
     this.deviceUdn = deviceUdn;
     this.locations = locations;
 }
 public ConnectionManager1Client(UpnpClient client)
 {
     if (client == null)
     {
         throw new ArgumentNullException("client");
     }
     this.client          = client;
     client.ServiceAdded += ClientServiceAdded;
 }
Exemplo n.º 6
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");
        }
Exemplo n.º 7
0
        private async ValueTask StartTcpListen()
        {
            using (await _asyncLock.LockAsync())
            {
                var listenAddressSet = new HashSet <OmniAddress>(_tcpAcceptOptions.ListenAddresses.ToArray());
                var useUpnp          = _tcpAcceptOptions.UseUpnp;

                UpnpClient?upnpClient = null;

                try
                {
                    // TcpListenerの追加処理
                    foreach (var listenAddress in listenAddressSet)
                    {
                        if (!TryGetEndpoint(listenAddress, out var ipAddress, out ushort port, false))
                        {
                            continue;
                        }

                        var tcpListener = new TcpListener(ipAddress, port);
                        tcpListener.Start(3);

                        _tcpListeners.Add(tcpListener);

                        if (useUpnp)
                        {
                            // "0.0.0.0"以外はUPnPでのポート開放対象外
                            if (ipAddress == IPAddress.Any)
                            {
                                if (upnpClient == null)
                                {
                                    upnpClient = new UpnpClient();
                                    await upnpClient.ConnectAsync();
                                }

                                await upnpClient.OpenPortAsync(UpnpProtocolType.Tcp, port, port, "Xeus");
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    _logger.Error(e);

                    throw e;
                }
                finally
                {
                    if (upnpClient != null)
                    {
                        upnpClient.Dispose();
                    }
                }
            }
        }
Exemplo n.º 8
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;
            }
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            var client = new UpnpClient ();
            client.ServiceAdded += HandleServiceAdded;
            //client.ContentDirectoryAdded += client_ContentDirectoryAdded;
            client.BrowseAll ();

            while (true) {
                System.Threading.Thread.Sleep (1000);
            }
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            var client = new UpnpClient();

            client.ServiceAdded += HandleServiceAdded;
            //client.ContentDirectoryAdded += client_ContentDirectoryAdded;
            client.BrowseAll();

            while (true)
            {
                System.Threading.Thread.Sleep(1000);
            }
        }
Exemplo n.º 11
0
        private async ValueTask StopTcpListen()
        {
            using (await _asyncLock.LockAsync())
            {
                var useUpnp = _tcpAcceptOptions.UseUpnp;

                UpnpClient?upnpClient = null;

                try
                {
                    foreach (var tcpListener in _tcpListeners)
                    {
                        var ipEndpoint = (IPEndPoint)tcpListener.LocalEndpoint;

                        tcpListener.Stop();

                        if (useUpnp)
                        {
                            // "0.0.0.0"以外はUPnPでのポート開放対象外
                            if (ipEndpoint.Address == IPAddress.Any)
                            {
                                if (upnpClient == null)
                                {
                                    upnpClient = new UpnpClient();
                                    await upnpClient.ConnectAsync();
                                }

                                await upnpClient.ClosePortAsync(UpnpProtocolType.Tcp, ipEndpoint.Port);
                            }
                        }
                    }

                    _tcpListeners.Clear();
                }
                catch (Exception e)
                {
                    _logger.Error(e);

                    throw e;
                }
                finally
                {
                    if (upnpClient != null)
                    {
                        upnpClient.Dispose();
                    }
                }
            }
        }
Exemplo n.º 12
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);
            }
        }
Exemplo n.º 13
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;
                }
            }
        }
Exemplo n.º 14
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;
            }
        }
Exemplo n.º 15
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));
                        }
                    }
                }
            }
        }
Exemplo n.º 16
0
        public static void Main(string[] args)
        {
            try
            {
                var client = new UpnpClient();
                var result = client.Discover(Core.Types.TargetType.All).Result;

                foreach (var @interface in result.GetInterfaces())
                {
                    Console.WriteLine($"interface: {@interface}");

                    foreach (var type in result.GetTargetTypes(@interface))
                    {
                        Console.WriteLine($"- type: {type}");

                        foreach (var target in result.GetTargets(@interface, type))
                        {
                            Console.WriteLine($"-- target: {target.Target}");
                            Console.WriteLine($"--- location: {target.Location}");

                            Console.WriteLine();
                        }
                    }
                }

                var extAddrList = client.GetExternalAddressList().Result;
                Console.WriteLine($"external address(es): {extAddrList.Aggregate( ( a, b ) => a + ", " + b )}");

                //client.AddPortMapping( 80, 81 ).Wait();
                //client.DeletePortMapping( 81 ).Wait();

                Console.WriteLine();
                Console.WriteLine("port mappings:");

                int index = 0;

                while (true)
                {
                    try
                    {
                        var portMappingList = client.GetGenericPortMappingEntry(index++).Result;

                        for (int i = 0; i < portMappingList.Count; i++)
                        {
                            var m = portMappingList[i];
                            Console.WriteLine($"{m.RemoteHost}:{m.ExternalPort} -> {m.InternalHost}:{m.InternalPort} ({m.Description})");
                        }
                    }
                    catch
                    {
                        break;
                    }
                }
            }
            catch (Exception x)
            {
                Console.WriteLine(x);
            }
            finally
            {
                Console.WriteLine("press any key...");
                Console.ReadKey(true);
            }
        }
 public ContentDirectoryClient(UpnpClient client)
 {
     this.client = client ?? new UpnpClient ();
     this.client.ServiceAdded += ClientServiceAdded;
 }
 public ContentDirectoryClient(UpnpClient client)
 {
     this.client = client ?? new UpnpClient();
     this.client.ServiceAdded += ClientServiceAdded;
 }
 public ConnectionManager1Client(UpnpClient client)
 {
     if (client == null) throw new ArgumentNullException ("client");
     this.client = client;
     client.ServiceAdded += ClientServiceAdded;
 }