From 987dbe98c8ab55c5c8eb563820e54453c835cdde Mon Sep 17 00:00:00 2001
From: gnattu <gnattu@users.noreply.github.com>
Date: Tue, 10 Sep 2024 03:17:10 +0800
Subject: [PATCH] cli: add option to disable network change detection (#11253)

---
 Emby.Server.Implementations/ConfigurationOptions.cs   |  3 ++-
 Jellyfin.Server/StartupOptions.cs                     | 11 +++++++++++
 .../Extensions/ConfigurationExtensions.cs             |  5 +++++
 src/Jellyfin.Networking/Manager/NetworkManager.cs     |  9 +++++++--
 4 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/Emby.Server.Implementations/ConfigurationOptions.cs b/Emby.Server.Implementations/ConfigurationOptions.cs
index e860105133..702707297a 100644
--- a/Emby.Server.Implementations/ConfigurationOptions.cs
+++ b/Emby.Server.Implementations/ConfigurationOptions.cs
@@ -20,7 +20,8 @@ namespace Emby.Server.Implementations
             { PlaylistsAllowDuplicatesKey, bool.FalseString },
             { BindToUnixSocketKey, bool.FalseString },
             { SqliteCacheSizeKey, "20000" },
-            { FfmpegSkipValidationKey, bool.FalseString }
+            { FfmpegSkipValidationKey, bool.FalseString },
+            { DetectNetworkChangeKey, bool.TrueString }
         };
     }
 }
diff --git a/Jellyfin.Server/StartupOptions.cs b/Jellyfin.Server/StartupOptions.cs
index c3989751ca..91ac827ca6 100644
--- a/Jellyfin.Server/StartupOptions.cs
+++ b/Jellyfin.Server/StartupOptions.cs
@@ -67,6 +67,12 @@ namespace Jellyfin.Server
         [Option("published-server-url", Required = false, HelpText = "Jellyfin Server URL to publish via auto discover process")]
         public string? PublishedServerUrl { get; set; }
 
+        /// <summary>
+        /// Gets or sets a value indicating whether the server should not detect network status change.
+        /// </summary>
+        [Option("nonetchange", Required = false, HelpText = "Indicates that the server should not detect network status change.")]
+        public bool NoDetectNetworkChange { get; set; }
+
         /// <summary>
         /// Gets the command line options as a dictionary that can be used in the .NET configuration system.
         /// </summary>
@@ -90,6 +96,11 @@ namespace Jellyfin.Server
                 config.Add(FfmpegPathKey, FFmpegPath);
             }
 
+            if (NoDetectNetworkChange)
+            {
+                config.Add(DetectNetworkChangeKey, bool.FalseString);
+            }
+
             return config;
         }
     }
diff --git a/MediaBrowser.Controller/Extensions/ConfigurationExtensions.cs b/MediaBrowser.Controller/Extensions/ConfigurationExtensions.cs
index 0aaf4fcd90..7ca5084266 100644
--- a/MediaBrowser.Controller/Extensions/ConfigurationExtensions.cs
+++ b/MediaBrowser.Controller/Extensions/ConfigurationExtensions.cs
@@ -69,6 +69,11 @@ namespace MediaBrowser.Controller.Extensions
         /// </summary>
         public const string SqliteCacheSizeKey = "sqlite:cacheSize";
 
+        /// <summary>
+        /// The key for a setting that indicates whether the application should detect network status change.
+        /// </summary>
+        public const string DetectNetworkChangeKey = "DetectNetworkChange";
+
         /// <summary>
         /// Gets a value indicating whether the application should host static web content from the <see cref="IConfiguration"/>.
         /// </summary>
diff --git a/src/Jellyfin.Networking/Manager/NetworkManager.cs b/src/Jellyfin.Networking/Manager/NetworkManager.cs
index cf6a2cc553..b285b836bc 100644
--- a/src/Jellyfin.Networking/Manager/NetworkManager.cs
+++ b/src/Jellyfin.Networking/Manager/NetworkManager.cs
@@ -97,10 +97,15 @@ public class NetworkManager : INetworkManager, IDisposable
         _networkEventLock = new object();
         _remoteAddressFilter = new List<IPNetwork>();
 
+        _ = bool.TryParse(startupConfig[DetectNetworkChangeKey], out var detectNetworkChange);
+
         UpdateSettings(_configurationManager.GetNetworkConfiguration());
 
-        NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged;
-        NetworkChange.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged;
+        if (detectNetworkChange)
+        {
+            NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged;
+            NetworkChange.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged;
+        }
 
         _configurationManager.NamedConfigurationUpdated += ConfigurationUpdated;
     }