blacktirion / email-oauth2-proxy-docker

Dockerized Version of simonrob/email-oauth2-proxy
40 stars 7 forks source link

Connection to container immediately terminates #21

Closed gerneio closed 10 months ago

gerneio commented 11 months ago

For me, when attempting to connect to the email proxy container from my local dev machine (windows PC) using the host port mapping and send an email via SMTP (for example), the connection just immediately closes. If I instead open a terminal directly to the email proxy container, I can send that SMTP email just fine. Additionally, if I create another container connected to same network (via compose auto-generated network), while that container can ping the email proxy container, it also won't connect to it via the container port mapping.

From host PC telnet localhost 1587 just immediately terminates (no errors). If I try to use an SMTP sending utility, it says Unable to read data from the transport connection: The connection was closed..

From the other container on the compose auto-generated network, the telnet command just says telnet: can't connect to remote host (172.20.0.3): Connection refused.

From within the email proxy container itself, the telnet command shows the proxied email server response as you would expect.

/app # telnet localhost 1587
Connected to localhost
220 SN7P220CA0017.outlook.office365.com Microsoft ESMTP MAIL Service ready at Thu, 14 Dec 2023 23:56:00 +0000
Here's my `compose.yaml` file ```yaml version: '3.7' services: test-connection: image: alpine container_name: test-connection # network_mode: "bridge" command: - /bin/sh - -c - | # Install `telnet` and check if we can reach `emailproxy` apk add busybox-extras telnet emailproxy 1587 ping emailproxy -i 10 ping 8.8.8.8 emailproxy: image: blacktirion/email-oauth2-proxy-docker container_name: emailproxy volumes: [ ./config:/config ] ports: [ 1587:1587 ] # network_mode: "bridge" environment: LOGFILE: true DEBUG: true ```

Within my compose file, I've tried adjusting the network settings in various ways, such as using network_mode: host or network_mode: bridge, but I can't figure out why outside connections aren't going through to the email proxy container.

FYI, the same behavior occurs for both SMTP, IMAP & POP service ports.

Any ideas on where I could be going wrong here? Appreciate any direction you can provide here.

moriahmorgan commented 11 months ago

What OS is hosting the container? SE Linux could be turned on, and various other things can block connections, even from "localhost" if they are enabled. I would recommend setting SE Linux to permissive and any firewalls to off to make sure that those aren't causing an issue. Your config looks fine, though I'm not intimately familiar with Docker compose to the point where I understand what the square brackets in the port section are for.

gerneio commented 11 months ago

I'm running on a Windows 11 laptop & using docker desktop. The square bracket notation in compose is just a way to denote a list (IIRC), so no special behavior there really.

I played around with it a bit more today. I added a node web app to my compose file, just so I can confirm that port connectivity was working to some degree (i.e. mostly ruling out FW/network configuration at the host & docker level), and it did work. I then tried creating a quick TCP listener w/ netcat (see) on the email proxy container, and was able to connect to it from the other test containers just fine.

Ultimately, I tracked it down to the local_address setting configured per protocol & port within the emailproxy.config file. I copied the config from the repo initially, so by default it pointed to 127.0.0.1. Modifying this to be the hostname of the container (emailproxy in this case) would allow connections from outside (my local PC as well as other containers), but connections from within the container using localhost now wouldn't work. Reading the docs closer, it looks like this option can be omitted altogether which will allow the port to bind to all interfaces, which is the proper solution for me in this case.

The local_address property can be used to set an IP address or hostname for the proxy to listen on. Both IPv4 and IPv6 are supported. If not specified, this value is set to :: (i.e., dual-stack IPv4/IPv6 on all interfaces). When a hostname is set the proxy will first resolve this to an IP address, preferring IPv6 over IPv4 if both are available. When running in an IPv6 environment with dual-stack support, the proxy will attempt to listen on both IPv4 and IPv6 hosts simultaneously. Note that tools such as netstat do not always accurately show dual-stack mode; if you are having trouble connecting to the proxy, it is worth actually testing both IPv4 and IPv6 connections.

[SMTP-1587]
server_address = smtp.office365.com
server_port = 587
starttls = True
local_address = 127.0.0.1     <-- remove this line (or change to proper hostname/ip)

This might be a good idea to mention somewhere in the readme of this repo, since I'd imagine most people that are coming at this for the first time by starting with a container configuration first, might make the same mistake of copying the example config file from the main repo initially.

But anyhow, glad I'm over that hump now. hope this helps someone else!

moriahmorgan commented 10 months ago

Closing this issue and will integrate the other issue currently open into the readme when I have time. I may reference this issue in that issue just to tie everything together.