Skip to content
This repository has been archived by the owner on Aug 15, 2022. It is now read-only.

Commit

Permalink
Added configuration that controls minimum logging level
Browse files Browse the repository at this point in the history
No longer forwarding CONNECT request when there's no forwarding proxy
  • Loading branch information
Nathan Alden, Sr committed Apr 5, 2017
1 parent b67aff6 commit 5a3366d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
10 changes: 5 additions & 5 deletions src/Proxy/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ public static int Main()
Console.WriteLine("Press CTRL+C to exit");
Console.WriteLine();

Log.Logger = new LoggerConfiguration()
.WriteTo.ColoredConsole()
.MinimumLevel.Verbose()
.CreateLogger();

try
{
InitializeAutofac();
InitializeConfig();

var configService = _container.Resolve<IConfigService>();

Log.Logger = new LoggerConfiguration()
.WriteTo.ColoredConsole()
.MinimumLevel.Is(configService.Config.Logging.MinimumLevel)
.CreateLogger();

ConfigModel.ForwardProxiesModel.ForwardProxyModel httpForwardProxy = configService.Config.ForwardProxies?.Http;
ConfigModel.ForwardProxiesModel.ForwardProxyModel httpsForwardProxy = configService.Config.ForwardProxies?.Https;

Expand Down
19 changes: 16 additions & 3 deletions src/Proxy/Services/ConfigService/ConfigModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Net;
using NathanAlden.Proxy.Hosts;
using Serilog.Events;
using YamlDotNet.Serialization;

namespace NathanAlden.Proxy.Services.ConfigService
Expand All @@ -13,6 +14,7 @@ public class ConfigModel
private BindingsModel _bindings = new BindingsModel();
private IEnumerable<string> _disallowedHosts = Enumerable.Empty<string>();
private ForwardProxiesModel _forwardProxies = new ForwardProxiesModel();
private LoggingModel _logging = new LoggingModel();
private OptionsModel _options = new OptionsModel();
private SocketsModel _sockets = new SocketsModel();

Expand All @@ -22,6 +24,12 @@ public BindingsModel Bindings
set => _bindings = value ?? new BindingsModel();
}

public ForwardProxiesModel ForwardProxies
{
get => _forwardProxies;
set => _forwardProxies = value ?? new ForwardProxiesModel();
}

public IEnumerable<string> DisallowedHosts
{
get => _disallowedHosts;
Expand All @@ -30,10 +38,10 @@ public IEnumerable<string> DisallowedHosts

public IEnumerable<Host> ParsedDisallowedHosts => DisallowedHosts.Select(x => Host.TryParse(x, out Host host) ? host : null).Where(x => x != null);

public ForwardProxiesModel ForwardProxies
public LoggingModel Logging
{
get => _forwardProxies;
set => _forwardProxies = value ?? new ForwardProxiesModel();
get => _logging;
set => _logging = value ?? new LoggingModel();
}

public SocketsModel Sockets
Expand Down Expand Up @@ -118,6 +126,11 @@ public class BasicModel
}
}

public class LoggingModel
{
public LogEventLevel MinimumLevel { get; set; } = LogEventLevel.Information;
}

public class SocketsModel
{
public int ReceiveTimeout { get; set; } = 120000;
Expand Down
38 changes: 21 additions & 17 deletions src/Proxy/Services/DownstreamClientService/DownstreamClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,27 +184,31 @@ async Task<bool> ConnectToUpstreamServerAsync()
_downstreamClient.Flush();

LogDownstreamText(LogEventLevel.Information).LeftArrow(true).Text(responseStatusLine.ToString()).Write();
LogForwardProxyText(LogEventLevel.Information).LeftArrow(true).Text($"{requestLine}{(hostHeader != null ? $", {hostHeader}" : "")}").Write();

_upstreamServer.WriteRequestLine(requestLine);
_upstreamServer.WriteHeaders(hostHeader);
_upstreamServer.WriteNewLine();
if (forwardProxy != null)
{
LogForwardProxyText(LogEventLevel.Information).LeftArrow(true).Text($"{requestLine}{(hostHeader != null ? $", {hostHeader}" : "")}").Write();

(GetResponseStatusLineResult result, ResponseStatusLine responseStatusLine) responseStatusLineResult = _upstreamServer.GetResponseStatusLine();
_upstreamServer.WriteRequestLine(requestLine);
_upstreamServer.WriteHeaders(hostHeader);
_upstreamServer.WriteNewLine();

switch (responseStatusLineResult.result)
{
case GetResponseStatusLineResult.Success:
LogForwardProxyText(LogEventLevel.Information).RightArrow(true).Text(responseStatusLineResult.responseStatusLine.ToString()).Write();
break;
case GetResponseStatusLineResult.InvalidResponseStatusLine:
LogForwardProxyText(LogEventLevel.Error, "Invalid response status line").Write();
break;
default:
throw new ArgumentOutOfRangeException();
}
(GetResponseStatusLineResult result, ResponseStatusLine responseStatusLine) responseStatusLineResult = _upstreamServer.GetResponseStatusLine();

_upstreamServer.GetHeaders();
switch (responseStatusLineResult.result)
{
case GetResponseStatusLineResult.Success:
LogForwardProxyText(LogEventLevel.Information).RightArrow(true).Text(responseStatusLineResult.responseStatusLine.ToString()).Write();
break;
case GetResponseStatusLineResult.InvalidResponseStatusLine:
LogForwardProxyText(LogEventLevel.Error, "Invalid response status line").Write();
break;
default:
throw new ArgumentOutOfRangeException();
}

_upstreamServer.GetHeaders();
}
}
else
{
Expand Down
3 changes: 3 additions & 0 deletions src/Proxy/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ disallowedHosts: # Proxying to these hosts is disallowed. Effectiv
#- example.com
#- .example.com

logging:
minimumLevel: Information # Options: Verbose, Debug, Information, Warning, Error, Fatal

sockets:
receiveTimeout: 120000 # The timeout, in milliseconds, of all socket receive operations. Default is 120000 (2 minutes).
sendTimeout: 120000 # The timeout, in milliseconds, of all socket send operations. Default is 120000 (2 minutes).
Expand Down

0 comments on commit 5a3366d

Please sign in to comment.