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

Work-In-Progress: Add relay server support #47

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
20c16dd
Work-In-Progress: Add relay server support
Jauler Dec 28, 2023
5496942
Finish implementing key exchange
Jauler Dec 29, 2023
9a7934b
Connect sending and receiving routines in wireguard with DERP
Jauler Dec 29, 2023
ad863e2
fix a few tados
baltrus1 Dec 29, 2023
556c0fa
Fix some offset mistakes when processing packets
Jauler Dec 29, 2023
7fda04a
Work-In-Progress: Add TLS support
Jauler Dec 29, 2023
2e56a71
Separate read from network and read from interface to two separate tasks
Jauler Jan 24, 2024
f04a5a5
Add multiple peers API
Hasan6979 Aug 15, 2024
2420e43
Fix comment
Hasan6979 Aug 15, 2024
73c73b1
Add remove wireguard API
Hasan6979 Aug 15, 2024
ce476e9
Add sanity checks
Hasan6979 Aug 16, 2024
029c2e3
Refactor and cleanup
Hasan6979 Aug 16, 2024
14fa2ab
Add interface to update peer
Hasan6979 Aug 16, 2024
a3a7581
Use public keys as lookup key
Hasan6979 Sep 4, 2024
e90e10c
Bug fix + adding extensive test
Hasan6979 Sep 4, 2024
28a39e5
Copy all allowed IPs from config instead of appending
Hasan6979 Sep 9, 2024
cf6594b
Merge pull request #1 from XmasHackathon2023/hasan/handle_multiple_peers
Hasan6979 Sep 9, 2024
08c6e8c
Add API to set derp IP and Port
Hasan6979 Aug 29, 2024
7557fb1
Add API to return derp status
Hasan6979 Sep 11, 2024
94490ec
Merge pull request #5 from XmasHackathon2023/hasan/add_derp_api
Hasan6979 Sep 19, 2024
3f307eb
Some changes to logging and removing endpoint info from wg peer
matislovas Sep 19, 2024
31f1afe
Add example running on qemu
olekoliinyk Sep 10, 2024
2dd96a8
Merge pull request #3 from XmasHackathon2023/olekoliinyk/demo-qemu-wi…
olekoliinyk Sep 19, 2024
c4ab916
Merge pull request #6 from XmasHackathon2023/matislovas/derp_api_addon
matislovas Sep 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/examples/*/sdkconfig
/examples/*/sdkconfig.*
/examples/*/.vagrant/
/.vscode
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"wireguard.h": "c"
}
}
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
idf_component_register(
SRCS "src/crypto.c"
"src/derp.c"
"src/wireguard.c"
"src/wireguardif.c"
"src/wireguard-platform.c"
Expand All @@ -12,7 +13,7 @@ idf_component_register(
"src/nacl/crypto_scalarmult/curve25519/ref/smult.c"
INCLUDE_DIRS "include"
PRIV_INCLUDE_DIRS "src"
REQUIRES esp_netif lwip mbedtls)
REQUIRES esp_netif esp-tls libsodium lwip mbedtls)

if(${IDF_VERSION_MAJOR} STREQUAL 5)
set_source_files_properties(src/crypto/refc/x25519.c
Expand Down
65 changes: 65 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
FROM ubuntu:22.04

ENV EXPECTED_ESP_IDF_COMMIT = "38eeba213aa695aabfd6d89aa9f5078dbe5a94c3"

RUN apt-get update && apt-get install -y \
git \
wget \
flex \
bison \
gperf \
python3 \
python3-pip \
python3-venv \
python3-setuptools \
cmake \
ccache \
libffi-dev \
libssl-dev \
dfu-util \
libusb-1.0-0 \
libgcrypt-dev \
libglib2.0-dev \
libfdt-dev \
libpixman-1-dev \
zlib1g-dev \
ninja-build \
libslirp-dev

RUN mkdir -p ~/esp \
&& cd ~/esp \
&& git clone --depth 1 -b v4.4.7 --recursive https://github.com/espressif/esp-idf.git

# Verify the esp-idf commit
RUN cd ~/esp/esp-idf \
&& ESP_IDF_COMMIT=$(git rev-parse HEAD) \
&& if [ $ESP_IDF_COMMIT != $EXPECTED_ESP_IDF_COMMIT ]; then \
echo "ESP IDF commit hash verification failed: expected $EXPECTED_ESP_IDF_COMMIT but got $ESP_IDF_COMMIT" >&2; \
exit 1; \
fi

RUN cd ~/esp/esp-idf \
&& ./install.sh \
&& /bin/bash -c "source ./export.sh"

RUN echo 'source /root/esp/esp-idf/export.sh' >> $HOME/.bashrc

RUN mkdir -p ~/qemu && cd ~/qemu && git clone --depth 1 -b esp-develop-9.0.0-20240606 --recursive https://github.com/espressif/qemu.git && cd qemu \
&& mkdir build && cd build \
&& ../configure --target-list=xtensa-softmmu --enable-gcrypt --enable-slirp --enable-debug --enable-sanitizers --disable-sdl --disable-strip --disable-user --disable-capstone --disable-vnc --disable-gtk \
&& cd .. \
&& ninja -C build \
&& ninja -C build install

WORKDIR /root

ENV PROJECT_ROOT=/root/esp/esp_wireguard
ENV EXAMPLE_PATH=$PROJECT_ROOT/examples/demo_qemu

COPY . $PROJECT_ROOT

WORKDIR $EXAMPLE_PATH

RUN ["/bin/bash", "-c", "source /root/esp/esp-idf/export.sh && idf.py build && cd build && esptool.py --chip esp32 merge_bin --fill-flash-size 4MB -o flash_image.bin @flash_args"]

CMD ["/bin/bash", "-c", "/root/qemu/qemu/build/qemu-system-xtensa -nic user,model=open_eth,id=lo0 -no-reboot -nographic -machine esp32 -drive file=${EXAMPLE_PATH}/build/flash_image.bin,if=mtd,format=raw"]
8 changes: 5 additions & 3 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ menu "WireGuard"

choice WIREGUARD_ESP_ADAPTER_SELECTION
prompt "TCP/IP adapter to use"
default WIREGUARD_ESP_NETIF
default WIREGUARD_ESP_NETIF_WIFI
config WIREGUARD_ESP_TCPIP_ADAPTER
bool "TCP/IP Adapter (pre esp-idf v4.1, ESP8266 RTOS SDK)"
config WIREGUARD_ESP_NETIF
config WIREGUARD_ESP_NETIF_WIFI
bool "ESP-NETIF"
config WIREGUARD_ESP_NETIF_ETHERNET
bool "ESP-NETIF ETHERNET"
endchoice

config WIREGUARD_MAX_PEERS
int "Max number of peers"
default 1
default 2

config WIREGUARD_MAX_SRC_IPS
int "Max number of SRC IP addresses"
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ wg_config.listen_port = CONFIG_WG_LOCAL_PORT;
wg_config.public_key = CONFIG_WG_PEER_PUBLIC_KEY;
wg_config.allowed_ip = CONFIG_WG_LOCAL_IP_ADDRESS;
wg_config.allowed_ip_mask = CONFIG_WG_LOCAL_IP_NETMASK;
wg_config.endpoint = CONFIG_WG_PEER_ADDRESS;
wg_config.port = CONFIG_WG_PEER_PORT;

/* If the device is behind NAT or stateful firewall, set persistent_keepalive.
persistent_keepalive is disabled by default */
Expand Down
49 changes: 49 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
networks:
esp_wireguard_network:
driver: bridge
ipam:
config:
- subnet: 172.18.0.0/16

x-wireguard:
&wireguard
build:
context: ./examples/demo_qemu/wireguard_peer
dockerfile: Dockerfile
image: wireguard
networks:
esp_wireguard_network:
privileged: true
tty: true

services:
wireguard_peer1:
<<: *wireguard
container_name: wireguard_peer1
volumes:
- ${PWD}/examples/demo_qemu/wireguard_peer/peer1/wg0.conf:/etc/wireguard/wg0.conf
networks:
esp_wireguard_network:
ipv4_address: 172.18.0.3

wireguard_peer2:
<<: *wireguard
container_name: wireguard_peer2
volumes:
- ${PWD}/examples/demo_qemu/wireguard_peer/peer2/wg0.conf:/etc/wireguard/wg0.conf
networks:
esp_wireguard_network:
ipv4_address: 172.18.0.5

esp_wireguard_examples_demo:
build:
context: ./
dockerfile: Dockerfile
image: esp_wireguard
container_name: esp_wireguard_client
networks:
esp_wireguard_network:
ipv4_address: 172.18.0.4
privileged: true
stdin_open: true
tty: true
53 changes: 36 additions & 17 deletions examples/demo/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,69 @@ menu "Example Configuration"

config WG_PRIVATE_KEY
string "Wireguard Private Key"
default "IsvT72MAXzA8EtV0FSD1QT59B4x0oe6Uea5rd/dDzhE="
default "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
help
Private key of the WireGuard device.

config WG_LOCAL_IP_ADDRESS
string "Wireguard local IP address"
default "192.168.4.58"
default "10.0.0.2"
help
Local IP address of the WireGuard device.

config WG_LOCAL_IP_NETMASK
string "Wireguard local netmask"
default "255.255.255.0"
default "255.255.255.255"
help
Netmask of the local network the WireGuard device belongs to.

config WG_LOCAL_PORT
int "Wireguard local port"
default 11010
default 53820
help
Local port to listen.

config WG_PEER_PUBLIC_KEY
config WG_PEER_ONE_PUBLIC_KEY
string "Wireguard remote peer public key"
default "FjrsQ/HD1Q8fUlFILIasDlOuajMeZov4NGqMJpkswiw="
default "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
help
Public key of the remote peer.
Public key of the 1st remote peer.

config WG_PEER_TWO_PUBLIC_KEY
string "Wireguard remote peer public key"
default "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
help
Public key of the 2nd remote peer.

config WG_PRESHARED_KEY
string "Wireguard pre-shared symmetric key"
default "0/2H97Sd5EJ9LAAAYUglVjPYv7ihNIm/ziuv6BtSI50="
default ""
help
Public key of the remote peer.

config WG_PEER_ADDRESS
config WG_PEER_ONE_ADDRESS
string "Wireguard remote peer address"
default "10.0.0.1"
help
Address of the 1st remote peer.

config WG_PEER_TWO_ADDRESS
string "Wireguard remote peer address"
default "demo.wireguard.com"
default "10.0.0.3"
help
Address of the remote peer.
Address of the 2nd remote peer.

config WG_PEER_PORT
int "Wireguard remote peer port"
default 12912
config WG_PEER_ONE_MASK
string "Wireguard remote peer net mask"
default "255.255.255.255"
help
Port number of the remote peer.
Address of the 1st remote peer netmask

config WG_PEER_TWO_MASK
string "Wireguard remote peer net mask"
default "255.255.255.255"
help
Address of the 2nd remote peer netmask

config WG_PERSISTENT_KEEP_ALIVE
int "Interval to send an authenticated empty packet"
Expand All @@ -74,9 +92,10 @@ menu "Example Configuration"
A seconds interval, between 1 and 65535 inclusive, of how often to
send an authenticated empty packet to the peer for the purpose of
keeping a stateful firewall or NAT mapping valid persistently
config EXAMPLE_PING_ADDRESS

config EXAMPLE_FALSE_ADDRESS
string "Target IP address or name"
default "192.168.4.254"
default "10.0.2.3"
help
Target IP address to send ICMP echo requests.
endmenu
Loading