コード例 #1
0
        /// <summary>
        /// Starts this UPnP control point. All device templates should be configured at the time this method gets called.
        /// The network tracker must be started after this method is called, else we might miss some connect events.
        /// </summary>
        public void Start()
        {
            lock (_cpData.SyncObj)
            {
                if (_isActive)
                {
                    throw new IllegalCallException("UPnP control point mustn't be started multiple times");
                }

                UPnPConfiguration.LOGGER.Debug("Will listen on IPs filtered by [{0}]", (UPnPConfiguration.IP_ADDRESS_BINDINGS != null ? String.Join(",", UPnPConfiguration.IP_ADDRESS_BINDINGS) : null));

                var servicePrefix = "/MediaPortal/UPnPControlPoint_" + Guid.NewGuid().GetHashCode().ToString("X");
                _cpData.ServicePrefix = servicePrefix;
                var startOptions = UPnPServer.BuildStartOptions(servicePrefix);

                IDisposable server = null;
                try
                {
                    server = WebApp.Start(startOptions, builder => { builder.Use((context, func) => HandleHTTPRequest(context)); });
                    UPnPConfiguration.LOGGER.Info("UPnPControlPoint: HTTP listener started on addresses {0}", String.Join(", ", startOptions.Urls));
                    _httpListeners.Add(server);
                }
                catch (SocketException e)
                {
                    server?.Dispose();
                    UPnPConfiguration.LOGGER.Warn("UPnPControlPoint: Error starting HTTP server", e);
                }

                _networkTracker.RootDeviceRemoved += OnRootDeviceRemoved;
                _networkTracker.DeviceRebooted    += OnDeviceRebooted;

                _isActive = true;
            }
        }
コード例 #2
0
        private void CreateAndStartServer()
        {
            try
            {
                ServerSettings settings = ServiceRegistration.Get <ISettingsManager>().Load <ServerSettings>();
                List <string>  filters  = settings.IPAddressBindingsList;

                _servicePrefix = ResourceHttpAccessUrlUtils.RESOURCE_SERVER_BASE_PATH + Guid.NewGuid().GetHashCode().ToString("X");
                var startOptions = UPnPServer.BuildStartOptions(_servicePrefix, filters);

                try
                {
                    lock (_syncObj)
                    {
                        _httpServer = WebApp.Start(startOptions, builder =>
                        {
                            foreach (Type middleWareType in _middleWares)
                            {
                                builder.Use(middleWareType);
                            }
                        });
                    }
                }
                catch (Exception ex)
                {
                    if (filters.Count > 0)
                    {
                        ServiceRegistration.Get <ILogger>().Warn("ResourceServer: Error starting HTTP server with filters. Fallback to no filters", ex);
                    }
                    else
                    {
                        throw ex;
                    }

                    _httpServer?.Dispose();
                    startOptions = UPnPServer.BuildStartOptions(_servicePrefix, new List <string>());
                    lock (_syncObj)
                    {
                        _httpServer = WebApp.Start(startOptions, builder =>
                        {
                            foreach (Type middleWareType in _middleWares)
                            {
                                builder.Use(middleWareType);
                            }
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                ServiceRegistration.Get <ILogger>().Error("ResourceServer: Error starting HTTP servers", ex);
            }
        }
コード例 #3
0
        private void CreateAndStartServer()
        {
            ServerSettings settings = ServiceRegistration.Get <ISettingsManager>().Load <ServerSettings>();
            List <string>  filters  = settings.IPAddressBindingsList;

            _servicePrefix = ResourceHttpAccessUrlUtils.RESOURCE_SERVER_BASE_PATH + Guid.NewGuid().GetHashCode().ToString("X");
            var startOptions = UPnPServer.BuildStartOptions(_servicePrefix, filters);

            lock (_syncObj)
            {
                _httpServer = WebApp.Start(startOptions, builder =>
                {
                    foreach (Type middleWareType in _middleWares)
                    {
                        builder.Use(middleWareType);
                    }
                });
            }
        }
コード例 #4
0
        private void CreateAndStartServer()
        {
            ServerSettings settings = ServiceRegistration.Get <ISettingsManager>().Load <ServerSettings>();
            List <string>  filters  = settings.IPAddressBindingsList;

            _serverPort    = UPnPServer.DEFAULT_UPNP_AND_SERVICE_PORT_NUMBER;
            _servicePrefix = ResourceHttpAccessUrlUtils.RESOURCE_SERVER_BASE_PATH;
            var startOptions = UPnPServer.BuildStartOptions(_servicePrefix, filters, _serverPort);

            lock (_syncObj)
            {
                if (_httpServer != null) //Already started
                {
                    return;
                }

                _httpServer = WebApp.Start(startOptions, builder =>
                {
                    // Configure OAuth Authorization Server
                    builder.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
                    {
                        AuthenticationType              = MEDIAPORTAL_AUTHENTICATION_TYPE,
                        TokenEndpointPath               = new PathString("/Token"),
                        ApplicationCanDisplayErrors     = true,
                        AuthorizationCodeExpireTimeSpan = TimeSpan.FromDays(7),
#if DEBUG
                        AllowInsecureHttp = true,
#endif
                        // Authorization server provider which controls the lifecycle of Authorization Server
                        Provider = new OAuthAuthorizationServerProvider
                        {
                            OnValidateClientAuthentication  = ValidateClientAuthentication,
                            OnGrantResourceOwnerCredentials = GrantResourceOwnerCredentials,
                        }
                    });
                    builder.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

                    // Configure Web API
                    HttpConfiguration config = new HttpConfiguration();

                    // Support conventional routing
                    var routeTemplate = (_servicePrefix + "/api/{controller}/{id}").TrimStart('/'); // No leading slash allowed
                    config.Routes.MapHttpRoute(
                        "DefaultApi",
                        routeTemplate,
                        new { id = RouteParameter.Optional }
                        );

                    // Support attribute based routing
                    config.MapHttpAttributeRoutes();

                    // Set json as default instead of xml
                    config.Formatters.JsonFormatter.MediaTypeMappings
                    .Add(new System.Net.Http.Formatting.RequestHeaderMapping(
                             "Accept", "text/html", StringComparison.InvariantCultureIgnoreCase, true, "application/json"));

                    builder.UseWebApi(config);

                    // Configure MiddleWare
                    foreach (Type middleWareType in _middleWares)
                    {
                        builder.Use(middleWareType);
                    }
                });
            }
        }