Basic Websocket support working, without authentication (auth broken)

This commit is contained in:
Alexander Horner
2022-12-07 21:36:03 +00:00
parent fe4ae354b2
commit 1e1187e71f
5 changed files with 286 additions and 94 deletions

View File

@@ -17,12 +17,107 @@ namespace ntfysh_client
public string Unique => $"{topicId.Text}@{serverUrl.Text}";
public bool UseWebsockets
{
get
{
switch (connectionType.Text)
{
case "Websockets (Recommended)":
return true;
case "Long HTTP JSON (Robust)":
return false;
default:
throw new InvalidOperationException();
}
}
}
public SubscribeDialog(ListBox notificationTopics)
{
_notificationTopics = notificationTopics;
InitializeComponent();
}
private void SubscribeDialog_Load(object sender, EventArgs e)
{
connectionType.SelectedIndex = 0;
}
private bool ReparseAddress()
{
//Separate schema and address
string[] parts = serverUrl.Text.Split("://", 2);
//Validate the basic formatting is correct
if (parts.Length != 2) return false;
//Take the schema aside for parsing
string schema = parts[0].ToLower();
//Ensure the schema is actually valid
switch (schema)
{
case "http":
case "https":
case "ws":
case "wss":
break;
default:
return false;
}
//Correct the schema based on connection type if required
if (UseWebsockets)
{
switch (schema)
{
case "http":
schema = "ws";
break;
case "https":
schema = "wss";
break;
case "ws":
case "wss":
break;
}
}
else
{
switch (schema)
{
case "ws":
schema = "http";
break;
case "wss":
schema = "https";
break;
case "http":
case "https":
break;
}
}
//Reconstruct the address
string finalAddress = schema + "://" + parts[1];
//Validate the address
if (!Uri.IsWellFormedUriString(finalAddress, UriKind.Absolute)) return false;
//Set the final address and OK it
serverUrl.Text = finalAddress;
return true;
}
private void button1_Click(object sender, EventArgs e)
{
if (topicId.Text.Length < 1)
@@ -35,7 +130,7 @@ namespace ntfysh_client
if (serverUrl.Text.Length < 1)
{
MessageBox.Show("You must specify a server URL. The default is https://ntfy.sh", "Server URL not specified", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show("You must specify a server URL. The default is wss://ntfy.sh", "Server URL not specified", MessageBoxButtons.OK, MessageBoxIcon.Error);
DialogResult = DialogResult.None;
serverUrl.Focus();
return;
@@ -65,6 +160,26 @@ namespace ntfysh_client
return;
}
try
{
if (!ReparseAddress())
{
MessageBox.Show($"The specified server URL is invalid. Accepted schemas are: http:// https:// ws:// wss://", "Invalid Server URL", MessageBoxButtons.OK, MessageBoxIcon.Error);
DialogResult = DialogResult.None;
connectionType.Focus();
return;
}
}
catch (InvalidOperationException)
{
MessageBox.Show($"The selected Connection Type '{connectionType.Text}' is invalid.", "Invalid Connection Type", MessageBoxButtons.OK, MessageBoxIcon.Error);
DialogResult = DialogResult.None;
connectionType.Focus();
return;
}
DialogResult = DialogResult.OK;
}
@@ -108,5 +223,10 @@ namespace ntfysh_client
e.SuppressKeyPress = true;
}
}
private void connectionType_TextChanged(object sender, EventArgs e)
{
ReparseAddress();
}
}
}