Skip to content

Commit 16b5f3a

Browse files
Adding third-party sources to resolve PUBLIC_IP (#24)
Co-authored-by: Sean Lawlor <[email protected]>
1 parent 90f7615 commit 16b5f3a

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,26 @@ If you already have a proxy to use, you can connect it to WhatsApp by following
2828
docker pull facebook/whatsapp_proxy:latest
2929
```
3030

31-
You can then skip down to **Running the proxy** and substitute any tag of `whatsapp_proxy:1.0` with `facebook/whatsapp_proxy:latest`.
31+
You can then skip down to **Running the proxy** and substitute any tag of `whatsapp_proxy:1.0` with `facebook/whatsapp_proxy:latest`.
3232

3333
### 1. Clone the repository to your local machine
34+
3435
```bash
3536
git clone https://github.com/WhatsApp/proxy.git
3637
```
38+
3739
You should see a folder called `proxy` created in the current directory.
3840

3941
### 2. [Install Docker](https://docs.docker.com/get-docker/) for your system
42+
4043
To confirm Docker is successfully installed:
44+
4145
```bash
4246
docker --version
4347
```
48+
4449
should display a line similar to `Docker version 20.10.21, build baeda1f`.
50+
4551
### 2. (Optional) Install Docker compose
4652

4753
For Linux users, if your [version of Docker](https://docs.docker.com/desktop/install/linux-install/) doesn't come pre-installed with Docker compose, you can install a one-off version (For Linux).
@@ -52,6 +58,7 @@ sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-c
5258
# Enable execution of the script
5359
sudo chmod +x /usr/bin/docker-compose
5460
```
61+
5562
### 3. Build the proxy host container
5663

5764
Navigate to the repo directory
@@ -80,9 +87,9 @@ docker run -it -p 80:80 -p 443:443 -p 5222:5222 -p 8080:8080 -p 8443:8443 -p 822
8087

8188
You will see lines ending with `Certificate generation completed.`. The HAProxy is running in the background and will continue to do so until you close this process.
8289

83-
8490
### Check your connection
85-
To confirm HAProxy is running, visit `http://<host-ip>:8199` where `<host-ip>` is your **public** IP address. You can also use this link to monitor proxy statistics.
91+
92+
To confirm HAProxy is running, visit `http://<host-ip>:8199` where `<host-ip>` is your **public** IP address. You can also use this link to monitor proxy statistics.
8693

8794
> NOTE: If your public IP address is not accessible, you will need to enable port forwarding (for the ports above) for the router/gateway you are using. Since this operation is device-specific, we are not going to go into it in details in this doc.
8895
@@ -105,7 +112,6 @@ on connections. If you have a network load balancer you can preserve the client
105112
2. 8443: Standard web traffic, encrypted (HTTPS) with PROXY protocol expected
106113
3. 8222: Jabber protocol traffic (WhatsApp default) with PROXY protocol expected
107114

108-
109115
## Certificate generation for SSL encrypted ports
110116

111117
Ports 443 and 8443 are protected by a self-signed encryption certificate generated at container start time. There are some custom options should you wish to tweak the settings of the generated certificates
@@ -119,7 +125,6 @@ They can be set with commands like
119125
docker build . --build-arg SSL_DNS=test.example.com
120126
```
121127

122-
123128
## Advanced
124129

125130
### Automate the container lifecycle with Docker compose
@@ -178,13 +183,15 @@ If you would like to configure your proxy using Kubernetes, or run the Docker ru
178183
Read more about other type of deployments [here](/docs/deployments.md).
179184

180185
# Contributors
186+
181187
------------
182188

183189
The authors of this code are Sean Lawlor ([@slawlor](https://github.com/slawlor)).
184190

185191
To learn more about contributing to this project, [see this document](https://github.com/whatsapp/proxy/blob/main/CONTRIBUTING.md).
186192

187193
# License
188-
-------
194+
195+
------------
189196

190197
This project is licensed under [MIT](https://github.com/novifinancial/akd/blob/main/LICENSE-MIT).

proxy/src/set_public_ip_and_start.sh

100644100755
Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,21 @@
99
# with the the real public ip. There's an order of priority here which is
1010
# 1. Environment variable
1111
# 2. AWS EC2 Metadata endpoint
12-
#
12+
# 3. Third-party sources
1313
# If all fails, we'll just not set the destination IP address
1414

1515
CONFIG_FILE="/usr/local/etc/haproxy/haproxy.cfg"
1616

17+
## Custom function to use as curl wrapper
18+
# --silent: to reduce the nois eof response
19+
# --show-error: to show errors in the response
20+
# --fail: to fail on non-200 responses
21+
# --ipv4: to force ipv4 resolution
22+
# --max-time: to set a timeout
23+
function fetch() {
24+
curl --silent --show-error --fail --ipv4 --max-time 2 "$@"
25+
}
26+
1727
## PUBLIC_IP supplied from environment variable
1828
if [[ $PUBLIC_IP == '' ]]
1929
then
@@ -24,16 +34,35 @@ fi
2434
if [[ $PUBLIC_IP == '' ]]
2535
then
2636
# Attempt retrieval of the public ip from the meta-data instance
27-
PUBLIC_IP=$(curl --max-time 2 -s http://169.254.169.254/latest/meta-data/public-ipv4)
37+
PUBLIC_IP=$(fetch http://169.254.169.254/latest/meta-data/public-ipv4)
2838
if [[ $PUBLIC_IP == '' ]]
2939
then
3040
echo "[PROXYHOST] Failed to retrieve public ip address from AWS URI within 2s"
3141
fi
3242
fi
3343

44+
## PUBLIC_IP retrieved from third-party sources
45+
if [[ $PUBLIC_IP == '' ]]
46+
then
47+
urls=(
48+
'https://icanhazip.com/'
49+
'https://ipinfo.io/ip'
50+
'https://domains.google.com/checkip'
51+
)
52+
53+
# Attempt retrieval of the public ip from the third-party sources
54+
for url in "${urls[@]}"; do
55+
PUBLIC_IP="$(fetch "${url}")" && break
56+
done
57+
if [[ $PUBLIC_IP == '' ]]
58+
then
59+
echo "[PROXYHOST] Failed to retrieve public ip address from third-party sources within 2s"
60+
fi
61+
fi
62+
3463
# Now if the public IP is available (test is for not-empty)
3564
# then replace the instances in all haproxy config lines
36-
if [[ ! -z "$PUBLIC_IP" ]]
65+
if [[ -n "$PUBLIC_IP" ]]
3766
then
3867
echo "[PROXYHOST] Public IP address ($PUBLIC_IP) in-place replacement occurring on $CONFIG_FILE"
3968
# Replace all instances of #PUBLIC_IP with the

0 commit comments

Comments
 (0)