Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
trudyhood committed Aug 30, 2021
1 parent 818e48e commit 377b40e
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 27 deletions.
22 changes: 13 additions & 9 deletions VpnHood.Client.App.UI/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
// ReSharper disable InconsistentNaming
// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable UnusedAutoPropertyAccessor.Local
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Local

#pragma warning disable IDE1006 // Naming Styles
namespace VpnHood.Client.App.UI
{
internal class ApiController : WebApiController
{
private VpnHoodApp App => VpnHoodApp.Instance;
private static VpnHoodApp App => VpnHoodApp.Instance;

class LoadAppParam
private class LoadAppParam
{
public bool WithFeatures { get; set; }
public bool WithState { get; set; }
Expand Down Expand Up @@ -43,9 +47,9 @@ public async Task<LoadAppResult> loadApp()
return ret;
}

class AddClientProfileParam
private class AddClientProfileParam
{
public string AccessKey { get; } = null!;
public string AccessKey { get; set; } = null!;
}

[Route(HttpVerbs.Post, "/" + nameof(addAccessKey))]
Expand All @@ -57,7 +61,7 @@ public async Task<ClientProfile> addAccessKey()
return clientProfile;
}

class ConnectParam
private class ConnectParam
{
public Guid ClientProfileId { get; set; }
}
Expand Down Expand Up @@ -95,9 +99,9 @@ public async Task removeClientProfile()
App.ClientProfileStore.RemoveClientProfile(parameters.ClientProfileId);
}

class SetClientProfileParam
private class SetClientProfileParam
{
public ClientProfile ClientProfile { get; } = null!;
public ClientProfile ClientProfile { get; set; } = null!;
}

[Route(HttpVerbs.Post, "/" + nameof(setClientProfile))]
Expand Down Expand Up @@ -131,8 +135,8 @@ public async Task setUserSettings()
public async Task log()
{
Response.ContentType = MimeType.PlainText;
using var stream = HttpContext.OpenResponseStream();
using var StreamWriter = new StreamWriter(stream);
await using var stream = HttpContext.OpenResponseStream();
await using var StreamWriter = new StreamWriter(stream);
var log = App.GetLogForReport();
await StreamWriter.WriteAsync(log);
}
Expand Down
1 change: 0 additions & 1 deletion VpnHood.Client.App/ClientProfileStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ public ClientProfile AddAccessKey(string accessKey)
var token = Token.FromAccessKey(accessKey);

// update tokens
var oldToken = _tokens.FirstOrDefault(x => x.TokenId == token.TokenId);
_tokens = _tokens.Where(x => x.TokenId != token.TokenId).Concat(new[] { token }).ToArray();

// find Server Node if exists
Expand Down
3 changes: 2 additions & 1 deletion VpnHood.Common/Messaging/SessionRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public class SessionRequest
{
public Guid TokenId { get; set; }
public ClientInfo ClientInfo { get; set; }
public byte[] EncryptedClientId { get; set; }
public byte[] EncryptedClientId { get; set; }


[JsonConstructor]
public SessionRequest(Guid tokenId, ClientInfo clientInfo, byte[] encryptedClientId)
Expand Down
2 changes: 1 addition & 1 deletion VpnHood.Server.Access/AccessServers/RestAccessServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public RestAccessServer(Uri baseUri, string authorization, Guid serverId)
if (baseUri.ToString()[..1] != "/") baseUri = new Uri(baseUri.AbsoluteUri + "/");

BaseUri = baseUri ?? throw new ArgumentNullException(nameof(baseUri));
_authorization = authorization ?? throw new ArgumentNullException(nameof(authorization));
_authorization = authorization ?? throw new ArgumentNullException(nameof(authorization));
ServerId = serverId;
var handler = new HttpClientHandler
{
Expand Down
12 changes: 7 additions & 5 deletions VpnHood.Server.Access/Messaging/SessionRequestEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ public class SessionRequestEx : SessionRequest
{
[JsonConverter(typeof(IPEndPointConverter))]
public IPEndPoint HostEndPoint { get; set; }

[JsonConverter(typeof(IPAddressConverter))]
public IPAddress? ClientIp { get; set; }

[JsonConstructor]
public SessionRequestEx(Guid tokenId, ClientInfo clientInfo, byte[] encryptedClientId, IPEndPoint hostEndPoint) : base(tokenId, clientInfo, encryptedClientId)
public SessionRequestEx(Guid tokenId, ClientInfo clientInfo, byte[] encryptedClientId, IPEndPoint hostEndPoint)
: base(tokenId, clientInfo, encryptedClientId)
{
HostEndPoint = HostEndPoint = hostEndPoint;
HostEndPoint = hostEndPoint;
}

public SessionRequestEx(SessionRequest obj, IPEndPoint hostEndPoint) : base(obj)
public SessionRequestEx(SessionRequest obj, IPEndPoint hostEndPoint)
: base(obj)
{
HostEndPoint = hostEndPoint;
}
Expand Down
4 changes: 2 additions & 2 deletions VpnHood.Server.App.Net/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"RestBaseUrl": "https://localhost:5001/api/access",
"RestAuthorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJWcG5TZXJ2ZXIiLCJyb2xlcyI6IkFkbWluIiwicHJvamVjdF9pZCI6IjY0OEI5OTY4LTcyMjEtNDQ2My1CNzBBLTAwQTEwOTE5QUU2OSIsImV4cCI6MTk0Mzg4NzEzOCwiaXNzIjoiYXV0aC52cG5ob29kLmNvbSIsImF1ZCI6ImFjY2Vzcy52cG5ob29kLmNvbSJ9.ZXtfQeMKoEv43C3N0jJimAR420EwN4acSjRXERthdcE",
"RestBaseUrl": "https://localhost:5001/api/access/",
"RestAuthorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJWcG5TZXJ2ZXIiLCJyb2xlcyI6IlZwblNlcnZlciIsInByb2plY3RfaWQiOiI4RDBCNDRCMS04MDhBLTRBMzgtQUU0NS1CNDZBRjk4NUYyODAiLCJleHAiOjE5NDU4MzkyMzIsImlzcyI6ImF1dGgudnBuaG9vZC5jb20iLCJhdWQiOiJhY2Nlc3MudnBuaG9vZC5jb20ifQ.p9y3LIIjRu7SlEOk9AFcVIbC9G23dxSAjS3MGZnMS1o",
"Port": 9443,
"IsDiagnoseMode": false,
"OrgStreamReadBufferSize": 40960,
Expand Down
10 changes: 5 additions & 5 deletions VpnHood.Server/TcpHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private async Task ListenThread()
var tcpClient = await _tcpListener.AcceptTcpClientAsync();
tcpClient.NoDelay = true;

// create cancelation token
// create cancellation token
using var timeoutCt = new CancellationTokenSource(RemoteHostTimeout);
using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(timeoutCt.Token, _cancellationTokenSource.Token);
_ = ProcessClient(tcpClient, cancellationTokenSource.Token);
Expand Down Expand Up @@ -122,10 +122,10 @@ await sslStream.AuthenticateAsServerAsync(
catch (SessionException ex) when (tcpClientStream != null)
{
// reply error if it is SessionException
// do not catch other exception and should not reply anything when sesson has not been created
// do not catch other exception and should not reply anything when session has not been created
await StreamUtil.WriteJsonAsync(tcpClientStream.Stream, new ResponseBase(ex.SessionResponse), cancellationToken);

tcpClientStream?.Dispose();
tcpClientStream.Dispose();
tcpClient.Dispose();
VhLogger.Instance.LogTrace(GeneralEventId.Tcp, $"Connection has been closed. Error: {ex.Message}");
}
Expand Down Expand Up @@ -287,8 +287,8 @@ private async Task ProcessTcpProxyChannel(TcpClientStream tcpClientStream, Cance
// send response
await StreamUtil.WriteJsonAsync(tcpClientStream.Stream, session.SessionResponse, cancellationToken);

// Dispose ssl stream and replace it with a HeadCryptor
tcpClientStream.Stream.Dispose();
// Dispose ssl stream and replace it with a Head-Cryptor
await tcpClientStream.Stream.DisposeAsync();
tcpClientStream.Stream = StreamHeadCryptor.Create(tcpClientStream.TcpClient.GetStream(),
request.CipherKey, null, request.CipherLength);

Expand Down
5 changes: 2 additions & 3 deletions VpnHood.Server/VpnHoodServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
using VpnHood.Logging;
using VpnHood.Server.Exceptions;
using VpnHood.Server.SystemInformation;
using Timer = System.Threading.Timer;

namespace VpnHood.Server
{
public class VpnHoodServer : IDisposable
{
private bool _disposed;
private readonly TcpHost _tcpHost;
private readonly Timer _sendStatusTimer;
private readonly System.Threading.Timer _sendStatusTimer;
private readonly System.Timers.Timer _subscribeTimer;
private readonly bool _autoDisposeAccessServer;

Expand Down Expand Up @@ -56,7 +55,7 @@ public VpnHoodServer(IAccessServer accessServer, ServerOptions options)
ThreadPool.SetMinThreads(workerThreads, completionPortThreads * 30);

// update timers
_sendStatusTimer = new Timer(StatusTimerCallback, null, options.SendStatusInterval, options.SendStatusInterval);
_sendStatusTimer = new System.Threading.Timer(StatusTimerCallback, null, options.SendStatusInterval, options.SendStatusInterval);
_subscribeTimer = new System.Timers.Timer(options.SubscribeInterval.TotalMilliseconds) { AutoReset = false };
_subscribeTimer.Elapsed += OnSubscribeTimerOnElapsed;
}
Expand Down
1 change: 1 addition & 0 deletions VpnHood.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=advfirewall/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Analytics/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Cryptor/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Datagram/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=dow_0022_002C_0020St/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=localport/@EntryIndexedValue">True</s:Boolean>
Expand Down

0 comments on commit 377b40e

Please sign in to comment.