Skip to content

Commit

Permalink
Drop netsh for YARP
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed Jul 9, 2024
1 parent b7348c0 commit f0138ad
Show file tree
Hide file tree
Showing 27 changed files with 449 additions and 187 deletions.
20 changes: 11 additions & 9 deletions Protest-Tests/ListenerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ public class ListenerTests {
private readonly DirectoryInfo front;

public ListenerTests() {
if (OperatingSystem.IsWindows())
if (OperatingSystem.IsWindows()) {
front = new DirectoryInfo(@"..\..\..\..\..\Protest\front");
else
}
else {
front = new DirectoryInfo(@"../../../../../Protest/front");
}

if (!front.Exists) Assert.Fail($"\"front\" directory not found: {front.FullName}");
if (!front.Exists) {
Assert.Fail($"\"front\" directory not found: {front.FullName}");
}
}

[SetUp]
Expand All @@ -21,8 +25,6 @@ public void Setup() {
Http.Listener listener = new Http.Listener("127.0.0.1", 8080, front.FullName);
listener.Start();
});

//Thread.Sleep(100);
}

[Test]
Expand All @@ -46,7 +48,7 @@ public void Listener_NoneExistingPage_ReturnNotFound() {
}

[Test]
public void CsrfCheck_NoHostInReferer_ReturnOk() {
public void CsrfCheck_NoHostInReferrer_ReturnOk() {
using HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, "http://127.0.0.1:8080/");

using HttpClient httpClient = new HttpClient();
Expand All @@ -56,7 +58,7 @@ public void CsrfCheck_NoHostInReferer_ReturnOk() {
}

[Test]
public void CsrfCheck_SameHostInReferer_ReturnOk() {
public void CsrfCheck_SameHostInReferrer_ReturnOk() {
using HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, "http://127.0.0.1:8080/");
requestMessage.Headers.Add("Referer", "http://127.0.0.1:8080/");

Expand All @@ -67,9 +69,9 @@ public void CsrfCheck_SameHostInReferer_ReturnOk() {
}

[Test]
public void CsrfCheck_DifferentHostInReferer_ReturnImaTeapot() {
public void CsrfCheck_DifferentHostInReferrer_ReturnImaTeapot() {
using HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, "http://127.0.0.1:8080/");
requestMessage.Headers.Add("Referer", "http://127.0.0.2:8080");
requestMessage.Headers.Add("Referer", "http://127.0.0.2:8080/");

using HttpClient httpClient = new HttpClient();
HttpResponseMessage result = httpClient.Send(requestMessage);
Expand Down
92 changes: 2 additions & 90 deletions Protest/Front/automation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ class Automation extends List {
super();

this.AddCssDependencies("list.css");
//this.AddCssDependencies("automation.css");

const columns = ["name", "status", "progress"];
const columns = ["name", "status", "start", "task"];
this.SetupColumns(columns);

this.columnsOptions.style.display = "none";
Expand All @@ -21,97 +20,10 @@ class Automation extends List {
this.pauseButton = this.AddToolbarButton("Pause", "mono/pause.svg?light");
this.stopButton = this.AddToolbarButton("Stop", "mono/stop.svg?light");

this.createButton.disabled = true; //TODO: <-
this.createButton.disabled = true;
this.deleteButton.disabled = true;
this.startButton.disabled = true;
this.pauseButton.disabled = true;
this.stopButton.disabled = true;

this.ListTasks();
}

async ListTasks() {
try {
const response = await fetch("automation/list");
if (response.status !== 200) LOADER.HttpErrorHandler(response.status);

const json = await response.json();
if (json.error) throw (json.error);

this.link = json;

for (let task in this.link.data) {
const element = document.createElement("div");
element.id = task;
element.className = "list-element";
this.list.appendChild(element);

this.InflateElement(element, this.link.data[task]);

element.addEventListener("click", event=>this.Entry_onclick(event));
}
}
catch (ex) {
this.ConfirmBox(ex, true, "mono/error.svg");
}
}

InflateElement(element, entry) { //overrides
let icon;
switch (entry.name.v.toLowerCase()) {
case "lifeline": icon = "mono/lifeline.svg"; break;
case "lastseen": icon = "mono/lastseen.svg"; break;
case "watchdog": icon = "mono/watchdog.svg"; break;
case "issues" : icon = "mono/issues.svg"; break;
case "fetch" : icon = "mono/fetch.svg"; break;
default : icon = "mono/automation.svg"; break;
}

const iconBox = document.createElement("div");
iconBox.className = "list-element-icon";
iconBox.style.backgroundImage = `url(${icon})`;
element.appendChild(iconBox);

super.InflateElement(element, entry, null);

if (!element.ondblclick) {
element.ondblclick = event=> {
event.stopPropagation();
this.Entry_ondblclick(event);
};
}
}

Entry_onclick(event) {
this.deleteButton.disabled = true;
this.startButton.disabled = true;
this.stopButton.disabled = true;

if (!(this.args.select in this.link.data)) {
return;
}

if (this.link.data[this.args.select].name.v.toLowerCase() === "lifeline" ||
this.link.data[this.args.select].name.v.toLowerCase() === "watchdog" ||
this.link.data[this.args.select].name.v.toLowerCase() === "fetch") {
this.deleteButton.disabled = true;
}
else {
//this.deleteButton.disabled = false; //TODO: <-
}

if (this.link.data[this.args.select].status.v.toLowerCase() === "stopped") {
//this.startButton.disabled = false;
this.stopButton.disabled = true;
}
else {
this.startButton.disabled = true;
//this.stopButton.disabled = false;
}

}

Entry_ondblclick(event) {

}
}
24 changes: 24 additions & 0 deletions Protest/Front/certificates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Certificates extends List {
constructor() {
super();

this.AddCssDependencies("list.css");

const columns = ["name", "status", "start", "task"];
this.SetupColumns(columns);

this.columnsOptions.style.display = "none";

this.SetTitle("Certificates");
this.SetIcon("mono/certificate.svg");

this.SetupToolbar();
this.createButton = this.AddToolbarButton("Create task", "mono/add.svg?light");
this.deleteButton = this.AddToolbarButton("Delete", "mono/delete.svg?light");
this.downloadButton = this.AddToolbarButton("Delete", "mono/download.svg?light");

this.createButton.disabled = true;
this.deleteButton.disabled = true;
this.downloadButton.disabled = true;
}
}
2 changes: 1 addition & 1 deletion Protest/Front/keyboardtester.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ class KeyboardTester extends Window {

let gamepads = navigator.getGamepads();

for (var j = 0; j < gamepads.length; j++) {
for (let j=0; j<gamepads.length; j++) {
let gamepad = gamepads[j];
if (!gamepad) continue;

Expand Down
4 changes: 4 additions & 0 deletions Protest/Front/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ const LOADER = {
"cameratester.js",
"screencapture.js",
"automation.js",
"tasks.js",
"certificates.js",
"backup.js",
"log.js"
],
Expand Down Expand Up @@ -339,6 +341,8 @@ const LOADER = {
case "Personalize" : return new Personalize(command.args);
case "AccessControl": return new AccessControl(command.args);
case "Automation" : return new Automation(command.args);
case "Tasks" : return new Tasks(command.args);
case "Certificates" : return new Certificates(command.args);
case "Backup" : return new Backup(command.args);
case "Log" : return new Log(command.args);
}
Expand Down
6 changes: 6 additions & 0 deletions Protest/Front/mono/certificate.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions Protest/Front/mono/task.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
116 changes: 116 additions & 0 deletions Protest/Front/tasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
class Tasks extends List {
constructor() {
super();

this.AddCssDependencies("list.css");

const columns = ["name", "status", "progress"];
this.SetupColumns(columns);

this.columnsOptions.style.display = "none";

this.SetTitle("Tasks");
this.SetIcon("mono/task.svg");

this.SetupToolbar();
this.createButton = this.AddToolbarButton("Create task", "mono/add.svg?light");
this.deleteButton = this.AddToolbarButton("Delete", "mono/delete.svg?light");
this.toolbar.appendChild(this.AddToolbarSeparator());
this.startButton = this.AddToolbarButton("Start", "mono/play.svg?light");
this.pauseButton = this.AddToolbarButton("Pause", "mono/pause.svg?light");
this.stopButton = this.AddToolbarButton("Stop", "mono/stop.svg?light");

this.createButton.disabled = true; //TODO: <-
this.deleteButton.disabled = true;
this.startButton.disabled = true;
this.pauseButton.disabled = true;
this.stopButton.disabled = true;

this.ListTasks();
}

async ListTasks() {
try {
const response = await fetch("tasks/list");
if (response.status !== 200) LOADER.HttpErrorHandler(response.status);

const json = await response.json();
if (json.error) throw (json.error);

this.link = json;

for (let task in this.link.data) {
const element = document.createElement("div");
element.id = task;
element.className = "list-element";
this.list.appendChild(element);

this.InflateElement(element, this.link.data[task]);

element.addEventListener("click", event=>this.Entry_onclick(event));
}
}
catch (ex) {
this.ConfirmBox(ex, true, "mono/error.svg");
}
}

InflateElement(element, entry) { //overrides
let icon;
switch (entry.name.v.toLowerCase()) {
case "lifeline": icon = "mono/lifeline.svg"; break;
case "lastseen": icon = "mono/lastseen.svg"; break;
case "watchdog": icon = "mono/watchdog.svg"; break;
case "issues" : icon = "mono/issues.svg"; break;
case "fetch" : icon = "mono/fetch.svg"; break;
default : icon = "mono/task.svg"; break;
}

const iconBox = document.createElement("div");
iconBox.className = "list-element-icon";
iconBox.style.backgroundImage = `url(${icon})`;
element.appendChild(iconBox);

super.InflateElement(element, entry, null);

if (!element.ondblclick) {
element.ondblclick = event=> {
event.stopPropagation();
this.Entry_ondblclick(event);
};
}
}

Entry_onclick(event) {
this.deleteButton.disabled = true;
this.startButton.disabled = true;
this.stopButton.disabled = true;

if (!(this.args.select in this.link.data)) {
return;
}

if (this.link.data[this.args.select].name.v.toLowerCase() === "lifeline" ||
this.link.data[this.args.select].name.v.toLowerCase() === "watchdog" ||
this.link.data[this.args.select].name.v.toLowerCase() === "fetch") {
this.deleteButton.disabled = true;
}
else {
//this.deleteButton.disabled = false; //TODO: <-
}

if (this.link.data[this.args.select].status.v.toLowerCase() === "stopped") {
//this.startButton.disabled = false;
this.stopButton.disabled = true;
}
else {
this.startButton.disabled = true;
//this.stopButton.disabled = false;
}

}

Entry_ondblclick(event) {

}
}
2 changes: 2 additions & 0 deletions Protest/Front/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,9 @@ const MENU = {
{ t:"RBAC", i:"mono/rbac.svg?light", g:"manage", h:false, f:()=> new AccessControl("rbac"), k:"rbac acl role based users access control list permissions" },
{ t:"Open sessions", i:"mono/hourglass.svg?light", g:"manage", h:true, f:()=> new AccessControl("sessions"), k:"alive connections" },

{ t:"Tasks", i:"mono/task.svg?light", g:"manage", h:false, f:()=> new Tasks(), k:"" },
{ t:"Automation", i:"mono/automation.svg?light", g:"manage", h:false, f:()=> new Automation(), k:"" },
{ t:"Certificates", i:"mono/certificate.svg?light", g:"manage", h:false, f:()=> new Certificates(), k: "" },

{ t:"Backup", i:"mono/backup.svg?light", g:"manage", h:false, f:()=> new Backup() },
{ t:"Log", i:"mono/log.svg?light", g:"manage", h:false, f:()=> new Log() },
Expand Down
7 changes: 4 additions & 3 deletions Protest/Http/Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,13 @@ public static bool AttemptAuthentication(HttpListenerContext ctx, out string ses

public static string GrandAccess(HttpListenerContext ctx, string username) {
string sessionId = Cryptography.RandomStringGenerator(64);
string userHostName = ctx.Request.UserHostName.Split(':')[0];

//RFC6265: no port allowed in the Domain attribute
//RFC6265: no port in the "Domain" attribute
#if DEBUG
ctx.Response.AddHeader("Set-Cookie", $"sessionid={sessionId}; Domain={ctx.Request.UserHostName.Split(':')[0]}; Max-age=604800; HttpOnly; SameSite=Strict;");
ctx.Response.AddHeader("Set-Cookie", $"sessionid={sessionId}; Domain={userHostName}; Max-age=604800; HttpOnly; SameSite=Strict;");
#else
ctx.Response.AddHeader("Set-Cookie", $"sessionid={sessionId}; Domain={ctx.Request.UserHostName.Split(':')[0]}; Max-age=604800; HttpOnly; SameSite=Strict; Secure;");
ctx.Response.AddHeader("Set-Cookie", $"sessionid={sessionId}; Domain={userHostName}; Max-age=604800; HttpOnly; SameSite=Strict; Secure;");
#endif

Session newSession = new Session() {
Expand Down
9 changes: 7 additions & 2 deletions Protest/Http/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,16 @@ private Entry ConstructEntry(string name, byte[] bytes, bool isGzipped, string e
headers.Add(new KeyValuePair<string, string>("Last-Modified", birthdate));
headers.Add(new KeyValuePair<string, string>("Referrer-Policy", "no-referrer"));

if (name == "/" || name == "/login") {
headers.Add(new KeyValuePair<string, string>("Cache-Control", "no-store"));
}
else {
#if DEBUG
headers.Add(new KeyValuePair<string, string>("Cache-Control", "no-store"));
headers.Add(new KeyValuePair<string, string>("Cache-Control", "no-store"));
#else
headers.Add(new KeyValuePair<string, string>("Cache-Control", name == "//" ? "no-store" : $"max-age={CACHE_CONTROL_MAX_AGE}"));
headers.Add(new KeyValuePair<string, string>("Cache-Control", $"max-age={CACHE_CONTROL_MAX_AGE}"));
#endif
}

Entry entry = new Entry() {
bytes = raw,
Expand Down
Loading

0 comments on commit f0138ad

Please sign in to comment.