Skip to content

Commit

Permalink
Added HTTPS support
Browse files Browse the repository at this point in the history
  • Loading branch information
KerJoe committed Aug 27, 2023
1 parent b0abc1a commit 7adb924
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
14 changes: 14 additions & 0 deletions OctoConnect/ConnectionPanel.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions OctoConnect/ConnectionPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,20 @@ private void numericUpDownPort_ValueChanged(object sender, EventArgs e)
con.Port = (int)numericUpDownPort.Value;
}

private void checkBoxSsl_CheckedChanged(object sender, EventArgs e)
{
if (updating) return;
con.UseSsl = checkBoxSsl.Checked;
}

bool updating;
private void bindingConnection_CurrentItemChanged(object sender, EventArgs e)
{
updating = true;
textBoxApiKey.Text = con.Apikey;
textBoxHostname.Text = con.Hostname;
numericUpDownPort.Value = con.Port;
checkBoxSsl.Checked = con.UseSsl;
updating = false;
}
}
Expand Down
16 changes: 12 additions & 4 deletions OctoConnect/Connector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ dynamic getOctoPrintJSON(string apiname)
{
var wc = new WebClient();
wc.Headers["X-Api-Key"] = apikey; wc.Headers[HttpRequestHeader.ContentType] = "application/json";
string response = wc.DownloadString(string.Format(@"http://{0}:{1}/api/{2}?apikey={3}", hostname, port, apiname, apikey));
string response = wc.DownloadString(string.Format(@"{0}://{1}:{2}/api/{3}?apikey={4}", usessl ? "https" : "http", hostname, port, apiname, apikey));
return JObject.Parse(response == "" ? "{}" : response); // Parse method doesn't accept an empty string, so instead we give it an empty JSON
}
dynamic postOctoPrintJSON(string apiname, string json = "")
{
var wc = new WebClient();
wc.Headers["X-Api-Key"] = apikey; wc.Headers[HttpRequestHeader.ContentType] = "application/json";
string response = wc.UploadString(string.Format(@"http://{0}:{1}/api/{2}", hostname, port, apiname), json);
string response = wc.UploadString(string.Format(@"{0}://{1}:{2}/api/{3}", usessl ? "https" : "http", hostname, port, apiname), json);
return JObject.Parse(response == "" ? "{}" : response);
}
float uploadPercentDone = 0;
Expand All @@ -119,7 +119,7 @@ Task<byte[]> postOctoPrintGCode(string filepath)
wc.UploadProgressChanged += (o, e) => uploadPercentDone = ((float)e.BytesSent / e.TotalBytesToSend) * 100; // ProgressPercentage is int, not double
wc.UploadFileCompleted += (o, e) => uploadDone = true;
wc.Headers["X-Api-Key"] = apikey;
return wc.UploadFileTaskAsync(new Uri(string.Format(@"http://{0}:{1}/api/files/local", hostname, port)), filepath);
return wc.UploadFileTaskAsync(new Uri(string.Format(@"{0}://{1}:{2}/api/files/local", usessl ? "https" : "http", hostname, port)), filepath);
}
bool connectToPrinter()
{
Expand Down Expand Up @@ -187,7 +187,7 @@ public override bool Connect(bool failSilent = false)
string username = loginResponse["name"];
string session = loginResponse["session"];

string websocketUri = string.Format(@"ws://{0}:{1}/sockjs/websocket", hostname, port);
string websocketUri = string.Format(@"{0}://{1}:{2}/sockjs/websocket", usessl ? "wss" : "ws", hostname, port);
if (pushSocket != null)
if (pushSocket.Url.ToString() != websocketUri) // Test if address in panel changed
{
Expand Down Expand Up @@ -545,12 +545,14 @@ public override void LoadFromRegistry()
Apikey = key.GetString("opapikey", apikey);
Hostname = key.GetString("ophostname", hostname);
Port = key.GetInt("opport", port);
UseSsl = key.GetBool("opusessl", usessl);
}
public override void SaveToRegistry()
{
key.SetString("opapikey", apikey == null ? "" : apikey);
key.SetString("ophostname", hostname);
key.SetInt("opport", port);
key.SetBool("opusessl", usessl);
}


Expand Down Expand Up @@ -589,5 +591,11 @@ protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
get { return hostname; }
set { hostname = value; OnPropertyChanged(new PropertyChangedEventArgs("Hostname")); }
}

bool usessl = false; public bool UseSsl
{
get { return usessl; }
set { usessl = value; OnPropertyChanged(new PropertyChangedEventArgs("UseSsl")); }
}
}
}
6 changes: 3 additions & 3 deletions OctoConnect/OctoConnect.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\..\..\..\..\Programs\Repetier-Host\Newtonsoft.Json.dll</HintPath>
<HintPath>..\..\..\..\..\..\Program Files\Repetier-Host\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="RepetierHostExtender">
<HintPath>..\..\..\..\..\..\Programs\Repetier-Host\RepetierHostExtender.dll</HintPath>
<HintPath>..\..\..\..\..\..\Program Files\Repetier-Host\RepetierHostExtender.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -48,7 +48,7 @@
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="websocket-sharp">
<HintPath>..\..\..\..\..\..\Programs\Repetier-Host\plugins\RepetierServerConnector\websocket-sharp.dll</HintPath>
<HintPath>..\..\..\..\..\..\Program Files\Repetier-Host\plugins\RepetierServerConnector\websocket-sharp.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit 7adb924

Please sign in to comment.