Skip to content

Commit

Permalink
FormatSessionId in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
trudyhood committed Jan 4, 2022
1 parent 4cb1d56 commit b920c21
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
### Server
* Update: Use keep-alive for tcp timeout
* Fix: Double Configure at startup
* Fix: Sending multiple request to access server for session recovery
* Fix: Memory leak! Some dead sessions remain in memory
* Fix: Memory leak! TcpProxy remains in memory when just one peer has gone
* Fix: Memory leak! UdpProxy remains in memory
Expand Down
5 changes: 5 additions & 0 deletions VpnHood.Common/Logging/VhLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public static string FormatId(object? id)
return IsAnonymousMode ? "**" + str[(str.Length / 2)..] : str;
}

public static string FormatSessionId(object? id)
{
return id?.ToString() ?? "<null>";
}

public static string FormatDns(string dnsName)
{
if (IPEndPointConverter.TryParse(dnsName, out var ipEndPoint))
Expand Down
1 change: 1 addition & 0 deletions VpnHood.Server.App.Net/nlog.config
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<when condition="'${event-properties:EventId}'=='Udp'" action="Ignore" /> <!--Requires IsDiagnoseMode-->
<when condition="'${event-properties:EventId}'=='DatagramChannel'" action="Log" />
<when condition="'${event-properties:EventId}'=='StreamChannel'" action="Ignore" />
<when condition="'${event-properties:EventId}'=='Session'" action="Log" />
<when condition="'${event-properties:EventId}'=='Track'" action="Ignore" /> <!--Keep it Ignore. see target:trackfile -->
</filters>
</logger>
Expand Down
6 changes: 3 additions & 3 deletions VpnHood.Server/SessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private Session CreateSession(SessionResponse sessionResponse, IPEndPoint hostEn
if (!Sessions.TryAdd(session.SessionId, session))
{
session.Dispose(true);
throw new Exception($"Could not add session to collection: SessionId: {session.SessionId}");
throw new Exception($"Could not add session to collection: SessionId: {VhLogger.FormatSessionId(session.SessionId)}");
}

return session;
Expand All @@ -80,7 +80,7 @@ public async Task<SessionResponse> CreateSession(HelloRequest helloRequest, IPEn
session.UseUdpChannel = true;

_ = _tracker?.TrackEvent("Usage", "SessionCreated");
VhLogger.Instance.Log(LogLevel.Information, $"New session has been created. SessionId: {VhLogger.FormatId(session.SessionId)}");
VhLogger.Instance.Log(LogLevel.Information, $"New session has been created. SessionId: {VhLogger.FormatSessionId(session.SessionId)}");
return sessionResponse;
}

Expand Down Expand Up @@ -127,7 +127,7 @@ internal async Task<Session> GetSession(RequestBase sessionRequest, IPEndPoint h
}
else
{
throw new UnauthorizedAccessException("Invalid SessionId");
throw new UnauthorizedAccessException($"Invalid SessionId: SessionId: {VhLogger.FormatSessionId(sessionRequest.SessionId)}");
}

// any session Exception must be after validation
Expand Down
14 changes: 7 additions & 7 deletions VpnHood.Server/TcpHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ private async Task ProcessHello(TcpClientStream tcpClientStream, CancellationTok
}

// reply hello session
VhLogger.Instance.LogTrace(GeneralEventId.Hello,
$"Replying Hello response. SessionId: {VhLogger.FormatId(sessionResponse.SessionId)}");
VhLogger.Instance.LogTrace(GeneralEventId.Hello,
$"Replying Hello response. SessionId: {VhLogger.FormatSessionId(sessionResponse.SessionId)}");
var helloResponse = new HelloResponse(sessionResponse)
{
SessionId = sessionResponse.SessionId,
Expand Down Expand Up @@ -293,7 +293,7 @@ private async Task ProcessBye(TcpClientStream tcpClientStream, CancellationToken
var request = await StreamUtil.ReadJsonAsync<RequestBase>(tcpClientStream.Stream, cancellationToken);

// finding session
using var scope = VhLogger.Instance.BeginScope($"SessionId: {VhLogger.FormatId(request.SessionId)}");
using var scope = VhLogger.Instance.BeginScope($"SessionId: {VhLogger.FormatSessionId(request.SessionId)}");
var session = await _sessionManager.GetSession(request, tcpClientStream.LocalEndPoint, tcpClientStream.RemoteEndPoint.Address);

// Before calling CloseSession session must be validated by GetSession
Expand All @@ -307,7 +307,7 @@ private async Task ProcessTcpDatagramChannel(TcpClientStream tcpClientStream, Ca
var request = await StreamUtil.ReadJsonAsync<TcpDatagramChannelRequest>(tcpClientStream.Stream, cancellationToken);

// finding session
using var scope = VhLogger.Instance.BeginScope($"SessionId: {VhLogger.FormatId(request.SessionId)}");
using var scope = VhLogger.Instance.BeginScope($"SessionId: {VhLogger.FormatSessionId(request.SessionId)}");
var session = await _sessionManager.GetSession(request, tcpClientStream.LocalEndPoint,
tcpClientStream.RemoteEndPoint.Address);

Expand All @@ -318,7 +318,7 @@ private async Task ProcessTcpDatagramChannel(TcpClientStream tcpClientStream, Ca
session.UseUdpChannel = false;

// add channel
VhLogger.Instance.LogTrace(GeneralEventId.DatagramChannel, $"Creating a {nameof(TcpDatagramChannel)} channel. SessionId: {VhLogger.FormatId(session.SessionId)}");
VhLogger.Instance.LogTrace(GeneralEventId.DatagramChannel, $"Creating a {nameof(TcpDatagramChannel)} channel. SessionId: {VhLogger.FormatSessionId(session.SessionId)}");
var channel = new TcpDatagramChannel(tcpClientStream);
try { session.Tunnel.AddChannel(channel); }
catch { channel.Dispose(); throw; }
Expand All @@ -330,7 +330,7 @@ private async Task ProcessTcpProxyChannel(TcpClientStream tcpClientStream, Cance
var request = await StreamUtil.ReadJsonAsync<TcpProxyChannelRequest>(tcpClientStream.Stream, cancellationToken);

// find session
using var _ = VhLogger.Instance.BeginScope($"SessionId: {VhLogger.FormatId(request.SessionId)}");
using var _ = VhLogger.Instance.BeginScope($"SessionId: {VhLogger.FormatSessionId(request.SessionId)}");
var isRequestedEpException = false;

TcpClient? tcpClient2 = null;
Expand Down Expand Up @@ -372,7 +372,7 @@ await Util.RunTask(tcpClient2.ConnectAsync(request.DestinationEndPoint.Address,
request.CipherKey, null, request.CipherLength);

// add the connection
VhLogger.Instance.LogTrace(GeneralEventId.StreamChannel, $"Adding a {nameof(TcpProxyChannel)}. SessionId: {VhLogger.FormatId(session.SessionId)}, CipherLength: {request.CipherLength}");
VhLogger.Instance.LogTrace(GeneralEventId.StreamChannel, $"Adding a {nameof(TcpProxyChannel)}. SessionId: {VhLogger.FormatSessionId(session.SessionId)}, CipherLength: {request.CipherLength}");

tcpClientStream2 = new TcpClientStream(tcpClient2, tcpClient2.GetStream());
tcpProxyChannel = new TcpProxyChannel(tcpClientStream2, tcpClientStream, TcpTimeout,
Expand Down
22 changes: 10 additions & 12 deletions VpnHood.Tunneling/GeneralEventId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ namespace VpnHood.Tunneling
{
public static class GeneralEventId
{
public static EventId Hello = new((int) EventCode.Hello, nameof(Hello));
public static EventId Nat = new((int) EventCode.Nat, nameof(Nat));
public static EventId Ping = new((int) EventCode.Ping, nameof(Ping));
public static EventId Dns = new((int) EventCode.Dns, nameof(Dns));
public static EventId Tcp = new((int) EventCode.Tcp, nameof(Tcp));
public static EventId Udp = new((int) EventCode.Udp, nameof(Udp));
public static EventId Track = new((int) EventCode.Track, nameof(Track));
public static EventId StreamChannel = new((int) EventCode.StreamChannel, nameof(StreamChannel));
public static EventId Session = new((int) EventCode.Session, nameof(Session));

public static EventId DatagramChannel =
new((int) EventCode.DatagramChannel, EventCode.DatagramChannel.ToString());
public static EventId Hello = new((int)EventCode.Hello, nameof(Hello));
public static EventId Nat = new((int)EventCode.Nat, nameof(Nat));
public static EventId Ping = new((int)EventCode.Ping, nameof(Ping));
public static EventId Dns = new((int)EventCode.Dns, nameof(Dns));
public static EventId Tcp = new((int)EventCode.Tcp, nameof(Tcp));
public static EventId Udp = new((int)EventCode.Udp, nameof(Udp));
public static EventId Track = new((int)EventCode.Track, nameof(Track));
public static EventId StreamChannel = new((int)EventCode.StreamChannel, nameof(StreamChannel));
public static EventId Session = new((int)EventCode.Session, nameof(Session));
public static EventId DatagramChannel = new((int)EventCode.DatagramChannel, EventCode.DatagramChannel.ToString());

private enum EventCode
{
Expand Down

0 comments on commit b920c21

Please sign in to comment.