Skip to content

Commit 499d842

Browse files
committed
version 0.1.2
1 parent 154408b commit 499d842

6 files changed

Lines changed: 98 additions & 28 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Unreleased
22

3+
# v0.1.2
4+
## Changes
5+
- When setting qBittorrent port value check the expected port matches the actual set port value
6+
7+
## Fixes
8+
- Fix qBittorrent port update request json value
9+
310
# v0.1.1
411
## Changes
512
- use local time for timestamps

‎Cargo.lock‎

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "vpn-port-forward-manager"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
edition = "2024"
55
authors = ["Kevin Wheelans <kevin.wheelans@proton.me>"]
66
description = "Updates the listen port for the supported applications to the port forward by the VPN provider"

‎src/apps.rs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ mod qbittorrent;
44
use crate::LINE_FEED;
55
use anyhow::Context;
66
use reqwest::blocking::Client;
7+
use std::fmt::Debug;
78
use std::path::Path;
89
use std::str::FromStr;
910
use std::thread::sleep;
1011
use std::time::Duration;
1112
use strum::{Display, EnumString};
12-
use tracing::{debug, warn};
13+
use tracing::{debug, trace, warn};
1314

1415
// Environment Variables
1516
const APPLICATION: &str = "APPLICATION";
@@ -45,7 +46,7 @@ pub trait App {
4546
warn!("Path to port forward value does not exist");
4647
}
4748
let value = std::fs::read_to_string(self.port_forward_path())?;
48-
debug!("{:?}", value);
49+
trace!("Found port value {}", value);
4950
let value = value.trim_matches(LINE_FEED);
5051
Ok(value.parse::<u16>()?)
5152
}
@@ -103,6 +104,15 @@ pub fn app_init() -> anyhow::Result<Box<dyn App>> {
103104
.unwrap_or(PORT_FORWARD_PATH_DEFAULT.into())
104105
.into();
105106

107+
// Print selected values
108+
debug!("application: {}", application);
109+
debug!("protocol: {}", protocol);
110+
debug!("port: {}", application);
111+
debug!("interval: {:?}", interval);
112+
debug!("hostname: {}", hostname);
113+
debug!("username: {}", username);
114+
debug!("port_forward_path: {:?}", port_forward_path);
115+
106116
Ok(match application {
107117
Application::QBittorrent => Box::new(qbittorrent::Qbittorrent {
108118
client,

‎src/apps/qbittorrent.rs‎

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::apps::{App, Protocol, endpoint};
22
use reqwest::blocking::Client;
3+
use serde_json::Value;
34
use std::collections::HashMap;
45
use std::path::{Path, PathBuf};
56
use std::time::Duration;
@@ -8,6 +9,7 @@ use tracing::{debug, error, info, warn};
89
// API Endpoints
910
const QB_LOGIN_ENDPOINT: &str = "/api/v2/auth/login";
1011
const QB_SET_PREFERENCES_ENDPOINT: &str = "/api/v2/app/setPreferences";
12+
const QB_GET_PREFERENCES_ENDPOINT: &str = "/api/v2/app/preferences";
1113

1214
pub struct Qbittorrent {
1315
pub client: Client,
@@ -51,18 +53,35 @@ impl App for Qbittorrent {
5153

5254
fn set_port(&self, port: u16) -> bool {
5355
let client = &self.client;
54-
let json = HashMap::from([("json".to_string(), format!("{{listen_port:{} }}", port))]);
56+
let json = HashMap::from([("json".to_string(), format!("{{\"listen_port\":{} }}", port))]);
5557
let response = client
5658
.post(self.set_preference_endpoint())
5759
.form(&json)
5860
.send();
59-
6061
match response {
6162
Ok(r) => {
6263
let status = r.status();
6364
if status.is_success() {
64-
info!("Port updated to {}", port);
65-
true
65+
let actual_port = self.get_current_listen_port();
66+
debug!("actual_port: {:?}", actual_port);
67+
match actual_port {
68+
None => {
69+
error!("unable to get actual port value");
70+
false
71+
}
72+
Some(actual) => {
73+
if port == actual {
74+
info!("Port updated to {}", port);
75+
true
76+
} else {
77+
warn!(
78+
"Actual port {} does not match expected port number {}",
79+
actual, port
80+
);
81+
false
82+
}
83+
}
84+
}
6685
} else {
6786
warn!("Port update request failed with status code: {}", status);
6887
false
@@ -109,4 +128,38 @@ impl Qbittorrent {
109128
QB_SET_PREFERENCES_ENDPOINT,
110129
)
111130
}
131+
132+
fn get_preference_endpoint(&self) -> String {
133+
endpoint(
134+
self.protocol,
135+
self.hostname.as_str(),
136+
self.port,
137+
QB_GET_PREFERENCES_ENDPOINT,
138+
)
139+
}
140+
141+
fn get_current_listen_port(&self) -> Option<u16> {
142+
let client = &self.client;
143+
let response = client.get(self.get_preference_endpoint()).send();
144+
145+
match response {
146+
Ok(r) => {
147+
let status = r.status();
148+
if status.is_success() {
149+
let json: Value = r.json().expect("cannot get preference json");
150+
let current_port = json.as_object().unwrap().get("listen_port").unwrap();
151+
current_port
152+
.as_number()
153+
.map(|p| p.as_u64().unwrap_or_default() as u16)
154+
} else {
155+
warn!("get preference request failed with status code: {}", status);
156+
None
157+
}
158+
}
159+
Err(e) => {
160+
error!("{}", e);
161+
None
162+
}
163+
}
164+
}
112165
}

‎src/main.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::apps::app_init;
22
use std::str::FromStr;
3-
use tracing::{debug, error};
3+
use tracing::{error, trace};
44
use tracing_subscriber::filter::LevelFilter;
55
use tracing_subscriber::fmt::time::LocalTime;
66

@@ -37,7 +37,7 @@ fn run() -> anyhow::Result<()> {
3737
last_port = port;
3838
}
3939
} else {
40-
debug!("Current and previous port match. No update required.")
40+
trace!("Current and previous port match. No update required.")
4141
}
4242
}
4343
Err(error) => {

0 commit comments

Comments
 (0)