Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Add Second Device" doesn't work on Windows because of firewall sometimes #3218

Closed
WofWca opened this issue Apr 26, 2023 · 9 comments · Fixed by deltachat/deltachat-pages#799
Labels
bug Something isn't working

Comments

@WofWca
Copy link
Collaborator

WofWca commented Apr 26, 2023

  • Operating System (Linux/Mac/Windows/iOS/Android): Windows 10
  • Delta Chat Version: 1.36.1
  • Expected behavior: The second device manages to connect to the sending instance of DC
  • Actual behavior: Connection times out on the second device
  • Steps to reproduce the problem:
    1. Install Delta Chat desktop (if you already have one, you may download the portable version)
    2. Install Delta Chat on another device (say, Android) (if you already have one, you can install an extra nightly one)
    3. On desktop, make sure you're logged in.
    4. On desktop, click "Settings" -> "Add Second Device" -> "Continue"
    5. On the second device, on the log in screen tap "Add as second device" and scan the QR code from the first device.
  • Screenshots:
  • Logs:

Debugging

The connection fails, unless you have added DC desktop in "Allow an app through Windows Firewall", or unless it was added there in another way, say through the Windows Firewall alert that can pop up when an app tries to listen on a non-localhost network interface.

The "Add Second Device" feature was added here deltachat/deltachat-core-rust#4007, and it uses iroh. The binding address is specified here, down to here, but it doesn't trigger the Windows Firewall alert popup, like it usually does with, say, a simple HTTP server app.

I have a suspicion that it may be related to the fact that DC listens for UDP packets, not TCP. iroh uses the QUIC protocol, which is UDP-based. I just tried

std::net::UdpSocket::bind(("0.0.0.0", 0))?;

And it did not trigger the alert, but with a concrete port ("0.0.0.0", 59999) the alert is triggered. And, again, yes, if you set up an TCP listener, say, with

std::net::TcpListener::bind(("0.0.0.0", 0))?

The alert does get triggered.

Possible solutions

  • A simple hack would be to create another dummy (TCP) interface binding at the same time we create the real (QUIC) one, in order to trigger the Windows Firewall alert and let the user decide whether to allow DC through firewall (see fix: "Add Second Device" not working on Windows deltachat-core-rust#4351)
  • @Simon-Laux and @flub pointed me to the windows-rs library that DC already depends on, namely these two functions 1, 2. But I myself am not sure how they work. I heard they may require elevation ("Run as administrator"). But not only it is a proper solution, I also suspect that it might actually be better because it might (based on my intuition) cause the Firewall alert to pop up a second time if network access was previously denied for the application.
@WofWca
Copy link
Collaborator Author

WofWca commented Apr 27, 2023

Edited the issue with the info about UdpSocket::bind().

WofWca added a commit to WofWca/deltachat-core-rust that referenced this issue Apr 27, 2023
@WofWca
Copy link
Collaborator Author

WofWca commented Apr 28, 2023

About the windows-rs solution.
With the help of microsoft/windows-rs#2466, I have created the following prototype app that adds a firewall rule. It doesn't cause any user prompts, just silently does its thing.

But the problem is that it fails unless you run it as admin.

  • Maybe there is a way to make the firewall rule user-specific, so that it doesn't require elevation?
  • Or, maybe it can be run when DC is being installed? Though IIRC elevation is only required when you install an app for all Windows users.
  • Or maybe there is a way to invoke a user prompt that can allow DC to add the rule.
Code
use windows::{
    core::BSTR,
    Win32::{
        Foundation::VARIANT_BOOL,
        NetworkManagement::WindowsFirewall::{
            INetFwPolicy2, INetFwRule, NetFwPolicy2, NetFwRule, NET_FW_ACTION_ALLOW,
        },
        System::Com::{
            CoCreateInstance, CoInitializeEx, CLSCTX_INPROC_SERVER, COINIT_MULTITHREADED,
        },
    },
};

fn main() -> std::io::Result<()> {
    unsafe {
        CoInitializeEx(None, COINIT_MULTITHREADED)?;

        let fw_policy: INetFwPolicy2 = CoCreateInstance(&NetFwPolicy2, None, CLSCTX_INPROC_SERVER)?;

        let rule: INetFwRule = CoCreateInstance(&NetFwRule, None, CLSCTX_INPROC_SERVER)?;
        rule.SetApplicationName(&BSTR::from(
            "C:\\firewall-test\\target\\debug\\firewall-test.exe",
        ))?;
        rule.SetAction(NET_FW_ACTION_ALLOW)?;
        rule.SetEnabled(VARIANT_BOOL::from(true))?;
        rule.SetName(&BSTR::from("firewall-test.exe"))?;

        fw_policy.Rules()?.Add(&rule)?;
    }

    println!("Success!");
    Ok(())
}

@Simon-Laux
Copy link
Member

can we check if the transfer would be blocked by the firewall? so that we can hint the user at what they need to do to fix it?

@Simon-Laux Simon-Laux added windows bug Something isn't working labels Jun 13, 2023
@link2xt
Copy link
Collaborator

link2xt commented Nov 10, 2023

There is a PR deltachat/deltachat-core-rust#4351, but I think this should be solved with a firewall rule without any user action if possible, so the user cannot accidentally block DC.

@WofWca
Copy link
Collaborator Author

WofWca commented Dec 1, 2023

A thing that wasn't mentioned: the alert pops up when you launch a webxdc app for the first time.
And apparently after each DC update of a Windows Store installation the state gets cleared (cause it's a new app every time I guess) the alert pops up again.

@Simon-Laux
Copy link
Member

he alert pops up when you launch a webxdc app for the first time.

maybe because of fill500?

@WofWca
Copy link
Collaborator Author

WofWca commented Dec 1, 2023

That's right.
I couldn't disclose this back when I created this issue haha

@Simon-Laux
Copy link
Member

A manual solution was shared in the forum: https://support.delta.chat/t/add-hint-select-on-windows-desktop-private-network-to-add-a-second-device/2921

maybe we should incorporate it into the local help / FAQ and add a trouble shoot button that links into the inApp Help? this could solve this issue without the need for us to understand those apis.

@r10s
Copy link
Member

r10s commented Feb 13, 2024

added a maybe sufficient hint at deltachat/deltachat-pages#799

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
4 participants