Skip to content

Commit

Permalink
fix server TcpHost is already Started error; Server send last config …
Browse files Browse the repository at this point in the history
…error to access server
  • Loading branch information
trudyhood committed Jan 22, 2022
1 parent 5384a9e commit f8741cb
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 23 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Upcoming
### Client
* Update: Tune TCP connections for games
* Fix: Error when setting PacketCapture include filter

### Server
* Fix: Always show TcpHost is already Started!
* Fix: Linux install on some distribution
* Feature: Server sends its last config error to access server
* Fix: TcpHost is already Started error
* Fix: Linux installation on some distribution
* Fix: LogLevel.Trace in DiagnoseMode

# v2.4.292
### Client
Expand Down
4 changes: 2 additions & 2 deletions Pub/Version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Major": 2,
"Minor": 4,
"Build": 293,
"BumpTime": "2022-01-10T02:41:28.5172812Z"
"Build": 294,
"BumpTime": "2022-01-21T22:12:06.9694918Z"
}
6 changes: 3 additions & 3 deletions VpnHood.App.Launcher/VpnHood.App.Launcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
<PackageIcon>VpnHood.png</PackageIcon>
<Product>VpnHood.App.Launcher</Product>
<ApplicationIcon>Resources\VpnHood.ico</ApplicationIcon>
<Version>2.4.293</Version>
<AssemblyVersion>2.4.293</AssemblyVersion>
<FileVersion>2.4.293</FileVersion>
<Version>2.4.294</Version>
<AssemblyVersion>2.4.294</AssemblyVersion>
<FileVersion>2.4.294</FileVersion>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
</PropertyGroup>
Expand Down
6 changes: 3 additions & 3 deletions VpnHood.Client/TcpProxyHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public void Start()
_tcpListenerIpV4 = new TcpListener(IPAddress.Any, 0);
_tcpListenerIpV4.Start();
_localEndpointIpV4 = (IPEndPoint)_tcpListenerIpV4.LocalEndpoint; //it is slow; make sure to cache it
VhLogger.Instance.LogInformation(
$"{VhLogger.FormatTypeName(this)} is listening on {VhLogger.Format(_localEndpointIpV4)}");
VhLogger.Instance.LogInformation($"{VhLogger.FormatTypeName(this)} is listening on {VhLogger.Format(_localEndpointIpV4)}");
_ = AcceptTcpClientLoop(_tcpListenerIpV4);

// IpV6
Expand All @@ -78,6 +77,7 @@ public void Start()
private async Task AcceptTcpClientLoop(TcpListener tcpListener)
{
var cancellationToken = _cancellationTokenSource.Token;
var localEp = (IPEndPoint)tcpListener.LocalEndpoint;

try
{
Expand All @@ -94,7 +94,7 @@ private async Task AcceptTcpClientLoop(TcpListener tcpListener)
}
finally
{
VhLogger.Instance.LogInformation("Listener has been closed.");
VhLogger.Instance.LogInformation($"{VhLogger.FormatTypeName(this)} Listener on {localEp} has been closed.");
}
}

Expand Down
1 change: 0 additions & 1 deletion VpnHood.Common/AppBaseNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public abstract class AppBaseNet<T> : IDisposable where T : AppBaseNet<T>
private const string FileNameSettings = "appsettings.json";
private const string FileNameSettingsDebug = "appsettings.Debug.json";
private const string FileNameNLogConfig = "NLog.config";
private const string FileNameNLogXsd = "NLog.xsd";

private static T? _instance;
private readonly string _appCommandFilePath;
Expand Down
1 change: 1 addition & 0 deletions VpnHood.Server.Access/ServerInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ public ServerInfo(Version version,
public long TotalMemory { get; set; }
public string? MachineName { get; set; }
public Guid? ConfigCode { get; set; }
public string? LastError { get; set; }
}
}
8 changes: 7 additions & 1 deletion VpnHood.Server.App.Net/ServerApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using VpnHood.Server.AccessServers;
using VpnHood.Server.App.SystemInformation;
using VpnHood.Server.SystemInformation;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

namespace VpnHood.Server.App
{
Expand All @@ -29,7 +30,12 @@ public ServerApp() : base("VpnHoodServer")
: new AppSettings();

// logger
using var loggerFactory = LoggerFactory.Create(builder => builder.AddNLog(NLogConfigFilePath));
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddNLog(NLogConfigFilePath);
if (AppSettings.IsDiagnoseMode)
builder.SetMinimumLevel(LogLevel.Trace);
});
VhLogger.Instance = loggerFactory.CreateLogger("NLog");
VhLogger.IsDiagnoseMode = AppSettings.IsDiagnoseMode;

Expand Down
6 changes: 3 additions & 3 deletions VpnHood.Server.App.Net/VpnHood.Server.App.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<RepositoryUrl>https://github.com/vpnhood/vpnhood</RepositoryUrl>
<PackageLicenseExpression>LGPL-2.1-only</PackageLicenseExpression>
<PackageIcon>VpnHood.png</PackageIcon>
<Version>2.4.293</Version>
<AssemblyVersion>2.4.293</AssemblyVersion>
<FileVersion>2.4.293</FileVersion>
<Version>2.4.294</Version>
<AssemblyVersion>2.4.294</AssemblyVersion>
<FileVersion>2.4.294</FileVersion>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
</PropertyGroup>
Expand Down
5 changes: 4 additions & 1 deletion VpnHood.Server/TcpHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,13 @@ public async Task Stop()
await _startTask;
_startTask = null;
}
IsStarted = false;
}

private async Task ListenTask(TcpListener tcpListener, CancellationToken cancellationToken)
{
var localEp = (IPEndPoint)tcpListener.LocalEndpoint;

try
{
// Listening for new connection
Expand All @@ -119,7 +122,7 @@ private async Task ListenTask(TcpListener tcpListener, CancellationToken cancell
finally
{
tcpListener.Stop();
VhLogger.Instance.LogInformation($"{VhLogger.FormatTypeName(this)} Listener has been closed.");
VhLogger.Instance.LogInformation($"Stop listening on {VhLogger.Format(localEp)}");
}
}

Expand Down
24 changes: 17 additions & 7 deletions VpnHood.Server/VpnHoodServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class VpnHoodServer : IDisposable
private readonly System.Timers.Timer _configureTimer;
private Timer? _updateStatusTimer;
private bool _disposed;
private string? _lastConfigError;

public VpnHoodServer(IAccessServer accessServer, ServerOptions options)
{
Expand Down Expand Up @@ -119,6 +120,11 @@ private async Task Configure(Guid? configurationCode = null)

try
{
// stop update status timer
if (_updateStatusTimer != null)
await _updateStatusTimer.DisposeAsync();
_updateStatusTimer = null;

// get server info
VhLogger.Instance.LogInformation("Configuring by the Access Server...");
var serverInfo = new ServerInfo(
Expand All @@ -133,10 +139,12 @@ private async Task Configure(Guid? configurationCode = null)
OsInfo = SystemInfoProvider.GetOperatingSystemInfo(),
OsVersion = Environment.OSVersion.ToString(),
TotalMemory = SystemInfoProvider.GetSystemInfo().TotalMemory,
ConfigCode = configurationCode
ConfigCode = configurationCode,
LastError = _lastConfigError
};

// get configuration from access server
VhLogger.Instance.LogTrace("Sending config request to the Access Server...");
var serverConfig = await ReadConfig(serverInfo);
VhLogger.Instance.LogInformation($"ServerConfig: {JsonSerializer.Serialize(serverConfig)}");
SessionManager.TrackingOptions = serverConfig.TrackingOptions;
Expand All @@ -146,7 +154,7 @@ private async Task Configure(Guid? configurationCode = null)
_tcpHost.TcpTimeout = serverConfig.SessionOptions.TcpTimeout;

// starting the listeners
var verb = _tcpHost.IsStarted ? "Starting" : "Restarting";
var verb = _tcpHost.IsStarted ? "Restarting" : "Starting";
VhLogger.Instance.LogInformation($"{verb} {VhLogger.FormatTypeName(_tcpHost)}...");
if (_tcpHost.IsStarted) await _tcpHost.Stop();
_tcpHost.Start(serverConfig.TcpEndPoints);
Expand All @@ -156,20 +164,22 @@ private async Task Configure(Guid? configurationCode = null)
// set _updateStatusTimer
if (serverConfig.UpdateStatusInterval != TimeSpan.Zero)
{
VhLogger.Instance.LogInformation($"Set {nameof(serverConfig.UpdateStatusInterval)} to {serverConfig.UpdateStatusInterval} seconds.");
if (_updateStatusTimer != null)
VhLogger.Instance.LogInformation($"Set {nameof(serverConfig.UpdateStatusInterval)} to {serverConfig.UpdateStatusInterval.TotalSeconds} seconds.");
if (_updateStatusTimer != null)
await _updateStatusTimer.DisposeAsync();
_updateStatusTimer = new Timer(StatusTimerCallback, null, TimeSpan.Zero, serverConfig.UpdateStatusInterval);
}

_lastConfigError = null;
VhLogger.Instance.LogInformation("Server is ready!");
}
catch (Exception ex)
{
_lastConfigError = ex.Message;
VhLogger.Instance.LogError($"Could not configure server! Retrying after {_configureTimer.Interval / 1000} seconds. Message: {ex.Message}");
await _tcpHost.Stop();

VhLogger.Instance.LogInformation($"Retrying after {_configureTimer.Interval / 1000} seconds...");
// start configure timer
_configureTimer.Start();
}
}
Expand Down Expand Up @@ -229,13 +239,13 @@ private async Task SendStatusToAccessServer()
Guid? configurationCode = null;
try
{
VhLogger.Instance.LogTrace("Sending status to AccessServer!");
VhLogger.Instance.LogTrace("Sending status to AccessServer...");
var res = await AccessServer.Server_UpdateStatus(Status);
configurationCode = res.ConfigCode;
}
catch (Exception ex)
{
VhLogger.Instance.LogError(ex, "Could not send server status!");
VhLogger.Instance.LogError(ex, "Could not send server status.");
}

// reconfigure
Expand Down

0 comments on commit f8741cb

Please sign in to comment.