Skip to content

Commit 46593e2

Browse files
authored
Merge pull request #15 from sireliah/firewall-ubuntu
Ask user if it is fine to check the firewall config
2 parents 1ebb2fa + 30115f1 commit 46593e2

File tree

6 files changed

+104
-33
lines changed

6 files changed

+104
-33
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
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 = "dragit"
3-
version = "0.6.0"
3+
version = "0.6.1"
44
authors = ["sireliah"]
55
edition = "2018"
66
build = "build.rs"

com.sireliah.Dragit.metainfo.xml

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,33 @@
1515
</p>
1616
</description>
1717
<releases>
18-
<release version="0.6.0" date="2021-06-02">
19-
<description>
20-
<p>Updated the discovery behaviour to be more resilient to the network/firewall issues</p>
21-
<p>
22-
Fixed the bug with connection hanging when you rejected the file.
23-
Watch out, this change is not backward compatible, so make sure to update Dragit on all of your devices.
24-
</p>
25-
<p>
26-
Added notification when Dragit is waiting for the file to be accepted.
27-
This was indeed required, because one might have wondered if the program got the file or not.
28-
</p>
29-
</description>
30-
</release>
31-
<release version="0.5.3" date="2021-04-28">
32-
<description>
33-
<p>Updated all major dependencies to the newest versions.</p>
34-
<p>This includes libp2p, gtk and several others.</p>
35-
</description>
36-
</release>
37-
<release version="0.5.0" date="2021-03-06">
38-
<description>First release in Flatpak</description>
39-
</release>
18+
<release version="0.6.1" date="2021-06-27">
19+
<description>
20+
<p>Made Dragit check firewalld config when the application is started.</p>
21+
</description>
22+
</release>
23+
<release version="0.6.0" date="2021-06-02">
24+
<description>
25+
<p>Updated the discovery behaviour to be more resilient to the network/firewall issues</p>
26+
<p>
27+
Fixed the bug with connection hanging when you rejected the file.
28+
Watch out, this change is not backward compatible, so make sure to update Dragit on all of your devices.
29+
</p>
30+
<p>
31+
Added notification when Dragit is waiting for the file to be accepted.
32+
This was indeed required, because one might have wondered if the program got the file or not.
33+
</p>
34+
</description>
35+
</release>
36+
<release version="0.5.3" date="2021-04-28">
37+
<description>
38+
<p>Updated all major dependencies to the newest versions.</p>
39+
<p>This includes libp2p, gtk and several others.</p>
40+
</description>
41+
</release>
42+
<release version="0.5.0" date="2021-03-06">
43+
<description>First release in Flatpak</description>
44+
</release>
4045
</releases>
4146
<launchable type="desktop-id">com.sireliah.Dragit.desktop</launchable>
4247
<screenshots>

src/dnd/dialogs.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,24 @@ impl AcceptFileDialog {
4242
pub struct FirewallDialog(gtk::MessageDialog);
4343

4444
impl FirewallDialog {
45-
pub fn new(window: &gtk::ApplicationWindow, config: &UserConfig) -> FirewallDialog {
45+
pub fn new_for_check(window: &gtk::ApplicationWindow) -> FirewallDialog {
46+
let text = concat!(
47+
"Dragit can check if your firewall setup allows the application to work correctly.\n",
48+
"You might be prompted for password.\n",
49+
"\n",
50+
"Would you like to check the firewall now?"
51+
);
52+
let dialog = gtk::MessageDialog::new(
53+
Some(window),
54+
gtk::DialogFlags::MODAL,
55+
gtk::MessageType::Question,
56+
gtk::ButtonsType::YesNo,
57+
&text,
58+
);
59+
FirewallDialog(dialog)
60+
}
61+
62+
pub fn new_for_config(window: &gtk::ApplicationWindow, config: &UserConfig) -> FirewallDialog {
4663
let port = config.get_port();
4764
let message = concat!(
4865
"Your current firewall configuration prevents Dragit from working.\n",
@@ -70,4 +87,9 @@ impl FirewallDialog {
7087
self.0.close();
7188
resp
7289
}
90+
91+
pub fn close(&self) {
92+
self.0.hide();
93+
self.0.close();
94+
}
7395
}

src/dnd/mod.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,33 @@ fn handle_firewall(window: &gtk::ApplicationWindow) -> Result<(), Box<dyn Error>
146146
let config = UserConfig::new()?;
147147
let port = config.get_port();
148148

149-
let firewall = Firewall::new()?;
150-
let required_services = firewall.check_rules_needed(port)?;
151-
152-
if required_services.0 || required_services.1 {
153-
let dialog = FirewallDialog::new(window, &config);
154-
let response = dialog.run();
155-
match response {
156-
gtk::ResponseType::Yes => firewall.handle(required_services)?,
149+
if !config.get_firewall_checked() {
150+
// Please note that on some OS'es like Ubuntu, polkit will require password for querying firewalld D-Bus interface.
151+
let check_dialog = FirewallDialog::new_for_check(window);
152+
let check_response = check_dialog.run();
153+
check_dialog.close();
154+
155+
match check_response {
156+
gtk::ResponseType::Yes => {
157+
let firewall = Firewall::new()?;
158+
let required_services = firewall.check_rules_needed(port)?;
159+
160+
if required_services.0 || required_services.1 {
161+
let dialog = FirewallDialog::new_for_config(window, &config);
162+
let response = dialog.run();
163+
match response {
164+
gtk::ResponseType::Yes => firewall.handle(required_services)?,
165+
gtk::ResponseType::No => info!("Not checking firewall configuration"),
166+
_ => warn!("Unexpected answer"),
167+
};
168+
}
169+
}
157170
gtk::ResponseType::No => info!("Not changing firewall configuration"),
158171
_ => warn!("Unexpected answer"),
159172
};
173+
174+
// Write to config that firewall was checked, not to ask user again.
175+
config.set_firewall_checked(true)?;
160176
}
161177

162178
Ok(())

src/user_data/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use toml;
99

1010
// Unassigned in IANA
1111
const DEFAULT_LISTEN_PORT: u16 = 36571;
12+
const DEFAULT_FIREWALL_CHECKED: bool = false;
1213

1314
fn get_timestamp() -> u64 {
1415
let now = SystemTime::now();
@@ -71,12 +72,19 @@ struct Config {
7172

7273
#[serde(default = "default_port")]
7374
port: u16,
75+
76+
#[serde(default = "default_firewall_checked")]
77+
firewall_checked: bool,
7478
}
7579

7680
fn default_port() -> u16 {
7781
DEFAULT_LISTEN_PORT
7882
}
7983

84+
fn default_firewall_checked() -> bool {
85+
DEFAULT_FIREWALL_CHECKED
86+
}
87+
8088
pub struct UserConfig {
8189
conf: Config,
8290
conf_path: PathBuf,
@@ -108,6 +116,7 @@ impl UserConfig {
108116
None => base_dirs.home_dir().to_string_lossy().to_string(),
109117
},
110118
port: DEFAULT_LISTEN_PORT,
119+
firewall_checked: DEFAULT_FIREWALL_CHECKED,
111120
};
112121
let toml = Self::serialize_config(config)?;
113122
let mut file = fs::File::create(&joined_path)?;
@@ -133,13 +142,32 @@ impl UserConfig {
133142
self.conf.port
134143
}
135144

145+
pub fn get_firewall_checked(&self) -> bool {
146+
self.conf.firewall_checked
147+
}
148+
136149
pub fn set_downloads_dir(&self, path: &Path) -> Result<(), Error> {
137150
// Watch out, this ::create will truncate the file
138151
let mut file = fs::File::create(&self.conf_path.as_path())?;
139152

140153
let config: Config = Config {
141154
downloads: path.to_string_lossy().to_string(),
142155
port: self.conf.port,
156+
firewall_checked: self.conf.firewall_checked,
157+
};
158+
let toml = Self::serialize_config(config)?;
159+
file.write_all(&toml.as_bytes())?;
160+
Ok(())
161+
}
162+
163+
pub fn set_firewall_checked(&self, value: bool) -> Result<(), Error> {
164+
// Watch out, this ::create will truncate the file
165+
let mut file = fs::File::create(&self.conf_path.as_path())?;
166+
167+
let config: Config = Config {
168+
downloads: self.conf.downloads.to_owned(),
169+
port: self.conf.port,
170+
firewall_checked: value,
143171
};
144172
let toml = Self::serialize_config(config)?;
145173
file.write_all(&toml.as_bytes())?;

0 commit comments

Comments
 (0)