Skip to content

Commit

Permalink
log file for exceptions, delete all logs, disable stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
william-stacken committed Aug 6, 2021
1 parent 4581b53 commit 78f77d7
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 115 deletions.
2 changes: 1 addition & 1 deletion Plugins/TiriryaraiMitm/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.

[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.*")]

// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
Expand Down
2 changes: 1 addition & 1 deletion Plugins/TuxEverywhere/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.

[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.*")]

// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ The program creates the following folders and files:
on an array of "log entries", where each log entry has a 16 byte initialization vector field,
a 4 byte "length" field, followed by a "length" byte encrypted HTML representation of
a HTTP message. Custom objects can also be logged by the custom plugins.
- `-Debug-.tirlog`: Log file that mainly contains stack traces and other useful information
for troubleshooting.
- `-RootCA-.pfx`: PKCS12 file containing the Root CA certificate that will be used to sign
certificates generated by Tiriryarai. The Root CA certificate needs to be
installed in your client, refer to [6. How To Use](#6-how-to-use) for details.
Expand Down
2 changes: 1 addition & 1 deletion Tiriryarai/Http/HttpMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ protected Dictionary<string, string> ParseUrlEncoded(string urlEncoded)
else if (i > 0)
{
key = keyVal.Substring(0, i).ToLower();
val = keyVal.Substring(i + 1);
val = HttpUtility.UrlDecode(keyVal.Substring(i + 1));
}
else
{
Expand Down
28 changes: 14 additions & 14 deletions Tiriryarai/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ static void Main(string[] args)
HttpsMitmProxy proxy = null;
List<string> extraOpts = null;
Dictionary<string, string> props = new Dictionary<string, string>();
Logger logger = Logger.GetSingleton();
try
{
HttpsMitmProxyConfig conf = HttpsMitmProxyConfig.GetSingleton();
Expand Down Expand Up @@ -96,34 +97,33 @@ static void Main(string[] args)
Environment.Exit(-1);
}

PrintStartup();
if (!conf.DisableStdout)
PrintStartup();
if (!conf.Authenticate)
{
Console.WriteLine("NOTICE: Authentication for accessing admin pages is disabled.");
Console.WriteLine("Hosting Tiriryarai on the public internet or an untrusted network is strongly discouraged.");
Console.WriteLine("If this was unintentional, see the help by using the \"-h\" flag.");
Console.WriteLine();
logger.WriteStdout("NOTICE: Authentication for accessing admin pages is disabled.");
logger.WriteStdout("Hosting Tiriryarai on the public internet or an untrusted network is strongly discouraged.");
logger.WriteStdout("If this was unintentional, see the help by using the \"-h\" flag.\n");
}
Console.Write("Starting server and generating certificates... ");
logger.WriteStdout("Starting server and generating certificates... ");

proxy = new HttpsMitmProxy(conf);

Console.WriteLine("Done");
Console.WriteLine();
Console.WriteLine("Tiriryarai has started!");
Console.WriteLine("Configure your client to use host " + conf.Hostname + " and port " + conf.Port + " as a HTTP proxy.");
Console.WriteLine("Then open http://" + Resources.HOSTNAME + " for more information.");
logger.WriteStdout("Done\n");
logger.WriteStdout("Tiriryarai has started!");
logger.WriteStdout("Configure your client to use host " + conf.Hostname + " and port " + conf.Port + " as a HTTP proxy.");
logger.WriteStdout("Then open http://" + Resources.HOSTNAME + " for more information.");
}
catch (Exception e)
{
if (e is TargetInvocationException t)
e = t.InnerException;
Console.WriteLine("\nFailed to initialize server:");
Console.WriteLine(e.Message);
logger.WriteStdout("Failed to initialize server:");
logger.WriteStdout(e.Message);
Environment.Exit(-2);
}
proxy.Start();
Console.WriteLine("Tiriryarai shut down...");
logger.WriteStdout("Tiriryarai shut down...");
}

private static void PrintStartup()
Expand Down
98 changes: 75 additions & 23 deletions Tiriryarai/Server/HttpsMitmProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public HttpsMitmProxy(HttpsMitmProxyConfig conf)
}
}},
{"favicon.ico", (req, resp) => {
if (!string.Empty.Equals(req.SubPath(1)))
{
DefaultNotFound(resp, req);
return;
}
switch (req.Method)
{
case Method.HEAD:
Expand All @@ -129,6 +134,11 @@ public HttpsMitmProxy(HttpsMitmProxyConfig conf)
logger.Log(15, req.Host, "OUTGOING INTERNAL RESPONSE", resp);
}},
{"cert", (req, resp) => {
if (!string.Empty.Equals(req.SubPath(1)))
{
DefaultNotFound(resp, req);
return;
}
switch (req.Method)
{
case Method.HEAD:
Expand All @@ -154,6 +164,11 @@ public HttpsMitmProxy(HttpsMitmProxyConfig conf)
}},
{Resources.CA_ISSUER_PATH, (req, resp) =>
{
if (!string.Empty.Equals(req.SubPath(1)))
{
DefaultNotFound(resp, req);
return;
}
logger.Log(8, req.Host, "INCOMMING ISSUER REQUEST", req);
switch (req.Method)
{
Expand All @@ -179,6 +194,11 @@ public HttpsMitmProxy(HttpsMitmProxyConfig conf)
}},
{Resources.OCSP_PATH, (req, resp) =>
{
if (!string.Empty.Equals(req.SubPath(1)))
{
DefaultNotFound(resp, req);
return;
}
logger.Log(8, req.Host, "INCOMMING OCSP REQUEST", req);
switch (req.Method)
{
Expand All @@ -196,7 +216,8 @@ public HttpsMitmProxy(HttpsMitmProxyConfig conf)
}
catch (Exception e)
{
logger.LogException(e, req);
logger.LogDebug(10, e);
logger.LogDebug(10, req);
}
X509OCSPResponse ocspResp = ocspReq != null ?
cache.GetOCSPResponse(ocspReq) :
Expand All @@ -223,6 +244,11 @@ public HttpsMitmProxy(HttpsMitmProxyConfig conf)
}},
{Resources.CRL_PATH, (req, resp) =>
{
if (!string.Empty.Equals(req.SubPath(1)))
{
DefaultNotFound(resp, req);
return;
}
logger.Log(8, req.Host, "INCOMMING CRL REQUEST", req);
switch (req.Method)
{
Expand Down Expand Up @@ -255,7 +281,7 @@ public HttpsMitmProxy(HttpsMitmProxyConfig conf)
DateTime? ifModified = req.GetDateHeader("If-Modified-Since");
string logFile = req.SubPath(1);

if ("".Equals(logFile))
if (string.Empty.Equals(logFile))
{
// Request to log directory
switch (req.Method)
Expand All @@ -281,15 +307,35 @@ public HttpsMitmProxy(HttpsMitmProxyConfig conf)
string.Format(Resources.LOG_PAGE, entryBuilder)
), false, req);
return;
case Method.POST:
if (!"application/x-www-form-urlencoded".Equals(req.ContentTypeWithoutCharset))
{
DefaultBadMediaType(resp, req);
return;
}
else if ("on".Equals(req.GetBodyParam("sure")) && "Delete All".Equals(req.GetBodyParam("deleteall")))
{
foreach (string log in logger.LogNames)
logger.DeleteLog(log);
}
break;
case Method.DELETE:
foreach (string log in logger.LogNames)
logger.DeleteLog(log);
break;
case Method.OPTIONS:
DefaultOptions(resp, req);
DefaultOptions(resp, req, Method.POST, Method.DELETE);
return;
default:
DefaultUnsupported(resp, req);
return;
}
resp.Status = 303;
resp.SetHeader("Location", "/logs");
resp.ContentLength = 0;
return;
}
else if (logger.Exists(logFile) && "".Equals(req.SubPath(2))) // Only one path level allowed
else if (logger.Exists(logFile) && string.Empty.Equals(req.SubPath(2))) // Only one path level allowed
{
switch (req.Method)
{
Expand Down Expand Up @@ -339,7 +385,7 @@ public HttpsMitmProxy(HttpsMitmProxyConfig conf)
DefaultNotFound(resp, req);
}},
{"config", (req, resp) => {
if (conf.Configuration)
if (conf.Configuration && string.Empty.Equals(req.SubPath(1)))
{
logger.Log(8, req.Host, "INCOMMING CONFIG REQUEST", req);
string value, description;
Expand Down Expand Up @@ -413,7 +459,7 @@ public HttpsMitmProxy(HttpsMitmProxyConfig conf)
}
catch (Exception e)
{
logger.LogException(e);
logger.LogDebug(5, e);
success = false;
}
if (clearCache)
Expand Down Expand Up @@ -521,14 +567,6 @@ private void ProcessClient(TcpClient client)
}
catch (Exception e)
{
if (e is IOException ||
e is ObjectDisposedException ||
e.InnerException is IOException)
{
// Connection has become inactive or was closed by the remote
keepAlive = false;
break;
}
resp = DefaultHttpResponse(400);
resp.ToStream(stream);
throw e;
Expand Down Expand Up @@ -582,14 +620,17 @@ e is ObjectDisposedException ||
catch (Exception e)
{
if (e is IOException ||
e is SocketException ||
e is ObjectDisposedException ||
e.InnerException is IOException)
e.InnerException is IOException ||
e is AggregateException)
{
// Connection has become inactive or was closed by the remote
logger.LogDebug(12, e);
keepAlive = false;
break;
}
logger.LogException(e);
logger.LogDebug(8, e);
resp = DefaultHttpResponse(400);
}
resp.ToStream(sslStream);
Expand All @@ -599,7 +640,7 @@ e is ObjectDisposedException ||
}
catch (Exception e)
{
logger.LogException(e);
logger.LogDebug(13, e);
}
finally
{
Expand Down Expand Up @@ -638,7 +679,18 @@ e is ObjectDisposedException ||
}
catch (Exception e)
{
logger.LogException(e);
if (e is IOException ||
e is SocketException ||
e is ObjectDisposedException ||
e.InnerException is IOException)
{
// Connection was probably closed by the remote
logger.LogDebug(15, e);
}
else
{
logger.LogDebug(8, e);
}
}
finally
{
Expand All @@ -652,8 +704,8 @@ private HttpResponse HandleRequest(HttpRequest req, HttpsClient destination, boo
HttpMessage http;
try
{
Console.WriteLine("\n--------------------\n" +
req.Method + (tls ? " https://" : " http://") + destination.HostnameWithPort + req.Path);
logger.WriteStdout("\n--------------------\n" +
req.Method + (tls ? " https://" : " http://") + destination.HostnameWithPort);
if (!conf.MitM.Block(destination.Hostname))
{
logger.Log(3, destination.Hostname, "RECEIVED REQUEST", req);
Expand All @@ -673,7 +725,7 @@ private HttpResponse HandleRequest(HttpRequest req, HttpsClient destination, boo
if (e is IOException || e is SocketException)
return DefaultHttpResponse(504);

logger.LogException(e);
logger.LogDebug(6, e);
return DefaultHttpResponse(502);
}
if (modified.HeaderContains("Connection", "close") || resp.HeaderContains("Connection", "close"))
Expand Down Expand Up @@ -702,7 +754,7 @@ private HttpResponse HandleRequest(HttpRequest req, HttpsClient destination, boo
}
catch (Exception e)
{
logger.LogException(e);
logger.LogDebug(2, e);
resp = DefaultHttpResponse(500, req);
}
return resp;
Expand Down Expand Up @@ -799,7 +851,7 @@ private HttpResponse HomePage(HttpRequest req, string host, IPAddress client, bo
}
catch (Exception e)
{
logger.LogException(e);
logger.LogDebug(2, e);
resp = DefaultHttpResponse(500, req);
}
return resp;
Expand Down
5 changes: 3 additions & 2 deletions Tiriryarai/Util/HttpsMitmProxyCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public X509Certificate2 GetRootCA()
return AddOrGetExisting(rootCA, certPath => {
if (!(certPath is string path))
throw new ArgumentException("certPath must be a string");
Console.WriteLine("\n--------------------\nNOTICE: The root CA certificate has expired and will be replaced." +
logger.LogDebug(1, "NOTICE: The root CA certificate has expired and will be replaced." +
"Please install the new certificate and remove the old one.");
// TODO Clear the cache somehow since essentially everything in the cache is now invalid.
File.Delete(path);
Expand Down Expand Up @@ -558,7 +558,8 @@ private X509OCSPResponse CreateOCSPResponse(object id)
}
catch (Exception e)
{
logger.LogException(e, certId);
logger.LogDebug(10, e);
logger.LogDebug(10, certId);
ocsp = new X509OCSPResponse(X509OCSPResponse.ResponseStatus.MalformedRequest);
}
return new X509OCSPResponse(ocsp.Sign(ca));
Expand Down
Loading

0 comments on commit 78f77d7

Please sign in to comment.