Skip to content

Commit 73c0b44

Browse files
authored
Allow option to accept self-signed/invalid SSL certificates (#315)
* Allow option to accept self-signed/invalid SSL certificates * Invalid SSL checkbox styling
1 parent 3bfc885 commit 73c0b44

6 files changed

Lines changed: 50 additions & 13 deletions

File tree

src-tauri/src/ipc.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,23 @@ async fn proxy_fetch(
214214
let sound = req_headers.get("X-TrguiNG-Sound").is_some();
215215

216216
if let Some(query) = req.uri().query() {
217-
if let Some(url) = query
217+
let params: Vec<(&str, &str)> = query
218218
.split('&')
219-
.map(|p| {
220-
let parts: Vec<&str> = p.split('=').collect();
221-
(parts[0], parts[1])
219+
.filter_map(|p| {
220+
let mut parts = p.splitn(2, '=');
221+
match (parts.next(), parts.next()) {
222+
(Some(k), Some(v)) => Some((k, v)),
223+
_ => None,
224+
}
222225
})
223-
.find_map(|p| if p.0 == "url" { Some(p.1) } else { None })
226+
.collect();
227+
228+
let insecure = params.iter().any(|p| p.0 == "insecure" && p.1 == "true");
229+
230+
if let Some(url) = params.iter().find_map(|p| if p.0 == "url" { Some(p.1) } else { None })
224231
{
225-
let client = app.state::<reqwest::Client>();
232+
let clients = app.state::<crate::HttpClients>();
233+
let client = if insecure { &clients.insecure } else { &clients.default };
226234

227235
let url = urlencoding::decode(url).ok().unwrap().into_owned();
228236
let headers = req.headers().clone();

src-tauri/src/main.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,30 @@ fn setup(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
162162

163163
static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
164164

165-
fn http_client() -> reqwest::Client {
165+
pub struct HttpClients {
166+
pub default: reqwest::Client,
167+
pub insecure: reqwest::Client,
168+
}
169+
170+
fn client_builder() -> reqwest::ClientBuilder {
166171
reqwest::Client::builder()
167172
.user_agent(APP_USER_AGENT)
168173
.connect_timeout(Duration::from_secs(10))
169174
.read_timeout(Duration::from_secs(40))
170175
.timeout(Duration::from_secs(60))
176+
}
177+
178+
fn http_clients() -> HttpClients {
179+
let default = client_builder()
171180
.build()
172-
.expect("Failed to initialize http client")
181+
.expect("Failed to initialize http client");
182+
183+
let insecure = client_builder()
184+
.danger_accept_invalid_certs(true)
185+
.build()
186+
.expect("Failed to initialize insecure http client");
187+
188+
HttpClients { default, insecure }
173189
}
174190

175191
fn main() {
@@ -204,7 +220,7 @@ fn main() {
204220
.manage(PollerHandle::default())
205221
.manage(MmdbReaderHandle::default())
206222
.manage(CreationRequestsHandle::default())
207-
.manage(http_client())
223+
.manage(http_clients())
208224
.setup(setup);
209225

210226
#[cfg(target_os = "macos")]

src-tauri/src/poller.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ struct Connection {
3030
url: String,
3131
username: String,
3232
password: String,
33+
#[serde(default)]
34+
accept_invalid_certs: bool,
3335
}
3436
#[derive(Deserialize, Debug, Clone, PartialEq)]
3537
pub struct PollerConfig {
@@ -153,7 +155,8 @@ async fn poll(
153155
toast: bool,
154156
sound: bool,
155157
) -> Result<String, Option<String>> {
156-
let client = app.state::<reqwest::Client>();
158+
let clients = app.state::<crate::HttpClients>();
159+
let client = if connection.accept_invalid_certs { &clients.insecure } else { &clients.default };
157160

158161
let mut req = client
159162
.post(connection.url.clone())

src/components/modals/settings.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
import {
20-
ActionIcon, Box, Button, Flex, Grid, Group, PasswordInput, SegmentedControl,
20+
ActionIcon, Box, Button, Checkbox, Flex, Grid, Group, PasswordInput, SegmentedControl,
2121
Stack, Switch, Tabs, Text, Textarea, TextInput,
2222
} from "@mantine/core";
2323
import type { ServerConfig, WindowCloseOption, WindowMinimizeOption } from "config";
@@ -72,7 +72,7 @@ function ServerListPanel({ form, current, setCurrent }: ServerListPanelProps) {
7272
<ActionIcon variant="light"
7373
onClick={() => {
7474
form.insertListItem("servers", {
75-
connection: { url: "", username: "", password: "" },
75+
connection: { url: "", username: "", password: "", acceptInvalidCerts: false },
7676
name: "new",
7777
pathMappings: [],
7878
expandedDirFilters: [],
@@ -142,6 +142,12 @@ function ServerPanel(props: ServerPanelProps) {
142142
placeholder="http://1.2.3.4:9091/transmission/rpc"
143143
autoComplete="off" autoCorrect="off" autoCapitalize="off" spellCheck="false" />
144144

145+
<Checkbox
146+
my="md"
147+
label="Accept invalid SSL certificates"
148+
{...props.form.getInputProps(`servers.${props.current}.connection.acceptInvalidCerts`, { type: "checkbox" })}
149+
/>
150+
145151
<Grid>
146152
<Grid.Col span={6}>
147153
<TextInput

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface ServerConnection {
2828
url: string,
2929
username: string,
3030
password: string,
31+
acceptInvalidCerts?: boolean,
3132
}
3233

3334
export interface PathMapping {

src/rpc/client.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export class TransmissionClient {
7878
headers: Record<string, string>;
7979
timeout: number;
8080
sessionInfo: SessionInfo;
81+
insecure: boolean;
8182
ipsBatcher: Batcher<IpLookupResult, string>;
8283

8384
constructor(connection: ServerConnection, toastNotifications: boolean, toastNotificationSound: boolean, timeout = 15) {
@@ -96,6 +97,7 @@ export class TransmissionClient {
9697
this.headers.Authorization = auth;
9798
}
9899
this.timeout = timeout;
100+
this.insecure = connection.acceptInvalidCerts === true;
99101
this.sessionInfo = {};
100102
this.hostname = "unknown";
101103
try {
@@ -124,9 +126,10 @@ export class TransmissionClient {
124126
}
125127

126128
async _sendRpc(data: Record<string, unknown>) {
129+
const insecureParam = this.insecure ? "&insecure=true" : "";
127130
const url = this.url === ""
128131
? "../rpc"
129-
: `${RUST_BACKEND}/${data.method === "torrent-get" ? "torrentget" : "post"}?url=${this.url}`;
132+
: `${RUST_BACKEND}/${data.method === "torrent-get" ? "torrentget" : "post"}?url=${this.url}${insecureParam}`;
130133
const body = JSON.stringify(data);
131134
let response = await fetch(
132135
url, { method: "POST", redirect: "manual", headers: this.headers, body });

0 commit comments

Comments
 (0)