From 8ec5b5cc0eb38d2358e758510beecec7d508cbae Mon Sep 17 00:00:00 2001 From: 552020 Date: Sat, 18 May 2024 11:41:06 +0200 Subject: [PATCH 1/2] bug: IPv6 binding on Mac but not on Linux(Docker) - WIP --- multiIPv4+IPv6.conf | 20 ++++++ multiIPv4.conf | 17 +++++ multiIPv6.conf | 17 +++++ multi_mixed.conf | 17 +++++ tests/multi_sockets/Dockerfile | 8 ++- tests/multi_sockets/results.txt | 9 +++ tests/multi_sockets/testIPv4+IPv6.sh | 69 ++++++++++++++++++++ tests/multi_sockets/{test.sh => testIPv4.sh} | 2 +- tests/multi_sockets/testIPv6.sh | 61 +++++++++++++++++ tests/multi_sockets/test_mixed.sh | 66 +++++++++++++++++++ 10 files changed, 282 insertions(+), 4 deletions(-) create mode 100644 multiIPv4+IPv6.conf create mode 100644 multiIPv4.conf create mode 100644 multiIPv6.conf create mode 100644 multi_mixed.conf create mode 100644 tests/multi_sockets/results.txt create mode 100755 tests/multi_sockets/testIPv4+IPv6.sh rename tests/multi_sockets/{test.sh => testIPv4.sh} (97%) mode change 100644 => 100755 create mode 100755 tests/multi_sockets/testIPv6.sh create mode 100755 tests/multi_sockets/test_mixed.sh diff --git a/multiIPv4+IPv6.conf b/multiIPv4+IPv6.conf new file mode 100644 index 00000000..bc90d32f --- /dev/null +++ b/multiIPv4+IPv6.conf @@ -0,0 +1,20 @@ +server { + listen 0.0.0.0:8081; + listen [::]:8081; + server_name www.development_site; + root /var/www/html; +} + +server { + listen 127.0.0.1:8080; + listen [::1]:8080; + server_name www.saladbook; + root /var/www/html; +} + +server { + listen 127.0.0.1:8082; + listen [::1]:8082; + server_name www.python_site.com; + root /var/www/html; +} diff --git a/multiIPv4.conf b/multiIPv4.conf new file mode 100644 index 00000000..acc06337 --- /dev/null +++ b/multiIPv4.conf @@ -0,0 +1,17 @@ +server { + listen 0.0.0.0:8081; + server_name www.development_site; + root /var/www/html; +} + +server { + listen 127.0.0.1:8080; + server_name www.saladbook; + root /var/www/html; +} + +server { + listen 127.0.0.1:8082; + server_name www.python_site.com; + root /var/www/html; +} diff --git a/multiIPv6.conf b/multiIPv6.conf new file mode 100644 index 00000000..21b38f24 --- /dev/null +++ b/multiIPv6.conf @@ -0,0 +1,17 @@ +server { + listen [::]:8081; + server_name www.development_site; + root /var/www/html; +} + +server { + listen [::1]:8080; + server_name www.saladbook; + root /var/www/html; +} + +server { + listen [::1]:8082; + server_name www.python_site.com; + root /var/www/html; +} diff --git a/multi_mixed.conf b/multi_mixed.conf new file mode 100644 index 00000000..60061fc7 --- /dev/null +++ b/multi_mixed.conf @@ -0,0 +1,17 @@ +server { + listen 0.0.0.0:8081; + server_name www.development_site; + root /var/www/html; +} + +server { + listen [::1]:8080; + server_name www.saladbook; + root /var/www/html; +} + +server { + listen [::1]:8082; + server_name www.python_site.com; + root /var/www/html; +} diff --git a/tests/multi_sockets/Dockerfile b/tests/multi_sockets/Dockerfile index 2bbb1e65..ca74b84c 100644 --- a/tests/multi_sockets/Dockerfile +++ b/tests/multi_sockets/Dockerfile @@ -30,7 +30,9 @@ RUN make WORKDIR /app/tests/multi_sockets # Make the test script executable -RUN chmod +x test.sh +RUN chmod +x testIPv4.sh +RUN chmod +x testIPv6.sh +RUN chmod +x testIPv4+IPv6.sh -# Command to start the server and run tests -CMD ["./test.sh"] \ No newline at end of file +# Just start the shell +CMD ["/bin/bash"] diff --git a/tests/multi_sockets/results.txt b/tests/multi_sockets/results.txt new file mode 100644 index 00000000..622d1e5e --- /dev/null +++ b/tests/multi_sockets/results.txt @@ -0,0 +1,9 @@ +127.0.0.1:8080 - Expected 404 - HTTP status code: 404 ✔ +0.0.0.0:8081 - Expected 404 - HTTP status code: 404 ✔ +127.0.0.1:8082 - Expected 404 - HTTP status code: 404 ✔ +127.0.0.1:8080 - Expected 404 - HTTP status code: 404 ✔ +0.0.0.0:8081 - Expected 404 - HTTP status code: 404 ✔ +127.0.0.1:8082 - Expected 404 - HTTP status code: 404 ✔ +127.0.0.1:8080 - Expected 404 - HTTP status code: 404 ✔ +0.0.0.0:8081 - Expected 404 - HTTP status code: 404 ✔ +127.0.0.1:8082 - Expected 404 - HTTP status code: 404 ✔ diff --git a/tests/multi_sockets/testIPv4+IPv6.sh b/tests/multi_sockets/testIPv4+IPv6.sh new file mode 100755 index 00000000..ba569d02 --- /dev/null +++ b/tests/multi_sockets/testIPv4+IPv6.sh @@ -0,0 +1,69 @@ +#!/bin/bash + +# Colors for output +GREEN='\033[0;32m' +RED='\033[0;31m' +NC='\033[0m' # No Color + +# Check if the webserv binary exists +if [ ! -f ./../../webserv ]; then + echo -e "${RED}Error: webserv binary not found!${NC}" + exit 1 +fi + +echo "Starting test script..." + +# Modify /etc/hosts to add the new localhost aliases +echo "127.0.0.2 localhost2" >> /etc/hosts +echo "127.0.0.3 localhost3" >> /etc/hosts +echo "::2 localhost2" >> /etc/hosts +echo "::3 localhost3" >> /etc/hosts + +# Start the server +echo "Starting the server..." +./../../webserv ./../../multiIPv4+IPv6.conf & +SERVER_PID=$! +sleep 2 # Wait for the server to start + +# Check if the server started successfully +if ! ps -p $SERVER_PID > /dev/null; then + echo -e "${RED}Failed to start the server.${NC}" + exit 1 +fi + +# Function to store response +store_response() { + local url=$1 + local expected=$2 + local response=$(curl -s -o /dev/null -w "%{http_code}" $url) + if [ "$response" == "$expected" ]; then + echo -e "$url - Expected $expected - HTTP status code: $response ${GREEN}✔${NC}" >> results.txt + else + echo -e "$url - Expected $expected - HTTP status code: $response ${RED}✘${NC}" >> results.txt + fi +} + +# Run curl commands and store responses +store_response "127.0.0.1:8080" "404" +store_response "0.0.0.0:8081" "404" +store_response "127.0.0.1:8082" "404" +store_response "127.0.0.2:8080" "000" +store_response "127.0.0.3:8080" "000" +store_response "127.0.0.1:8081" "404" +store_response "[::1]:8080" "404" +store_response "[::]:8081" "404" +store_response "[::1]:8082" "404" +store_response "[::2]:8080" "000" +store_response "[::3]:8080" "000" +store_response "[::1]:8081" "404" + +# Stop the server +echo "Stopping the server..." +kill $SERVER_PID + +# Print all responses +echo "Test results:" +cat results.txt +rm results.txt + +echo "Test script completed." diff --git a/tests/multi_sockets/test.sh b/tests/multi_sockets/testIPv4.sh old mode 100644 new mode 100755 similarity index 97% rename from tests/multi_sockets/test.sh rename to tests/multi_sockets/testIPv4.sh index bcd756f2..7650736a --- a/tests/multi_sockets/test.sh +++ b/tests/multi_sockets/testIPv4.sh @@ -19,7 +19,7 @@ echo "127.0.0.3 localhost3" >> /etc/hosts # Start the server echo "Starting the server..." -./../../webserv ./../../multi.conf & +./../../webserv ./../../multiIPv4.conf & SERVER_PID=$! sleep 2 # Wait for the server to start diff --git a/tests/multi_sockets/testIPv6.sh b/tests/multi_sockets/testIPv6.sh new file mode 100755 index 00000000..41975e1a --- /dev/null +++ b/tests/multi_sockets/testIPv6.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +# Colors for output +GREEN='\033[0;32m' +RED='\033[0;31m' +NC='\033[0m' # No Color + +# Check if the webserv binary exists +if [ ! -f ./../../webserv ]; then + echo -e "${RED}Error: webserv binary not found!${NC}" + exit 1 +fi + +echo "Starting test script..." + +# Modify /etc/hosts to add the new localhost aliases +echo "::2 localhost2" >> /etc/hosts +echo "::3 localhost3" >> /etc/hosts + +# Start the server +echo "Starting the server..." +./../../webserv ./../../multiIPv6.conf & +SERVER_PID=$! +sleep 2 # Wait for the server to start + +# Check if the server started successfully +if ! ps -p $SERVER_PID > /dev/null; then + echo -e "${RED}Failed to start the server.${NC}" + exit 1 +fi + +# Function to store response +store_response() { + local url=$1 + local expected=$2 + local response=$(curl -s -o /dev/null -w "%{http_code}" $url) + if [ "$response" == "$expected" ]; then + echo -e "$url - Expected $expected - HTTP status code: $response ${GREEN}✔${NC}" >> results.txt + else + echo -e "$url - Expected $expected - HTTP status code: $response ${RED}✘${NC}" >> results.txt + fi +} + +# Run curl commands and store responses +store_response "[::1]:8080" "404" +store_response "[::]:8081" "404" +store_response "[::1]:8082" "404" +store_response "[::2]:8080" "000" +store_response "[::3]:8080" "000" +store_response "[::1]:8081" "404" + +# Stop the server +echo "Stopping the server..." +kill $SERVER_PID + +# Print all responses +echo "Test results:" +cat results.txt +rm results.txt + +echo "Test script completed." diff --git a/tests/multi_sockets/test_mixed.sh b/tests/multi_sockets/test_mixed.sh new file mode 100755 index 00000000..5f720b43 --- /dev/null +++ b/tests/multi_sockets/test_mixed.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +# Colors for output +GREEN='\033[0;32m' +RED='\033[0;31m' +NC='\033[0m' # No Color + +# Check if the webserv binary exists +if [ ! -f ./../../webserv ]; then + echo -e "${RED}Error: webserv binary not found!${NC}" + exit 1 +fi + +echo "Starting test script..." + +# Modify /etc/hosts to add the new localhost aliases +echo "127.0.0.2 localhost2" >> /etc/hosts +echo "127.0.0.3 localhost3" >> /etc/hosts +echo "::2 localhost2" >> /etc/hosts +echo "::3 localhost3" >> /etc/hosts + +# Start the server +echo "Starting the server..." +./../../webserv ./../../multiIPv4+IPv6.conf & +SERVER_PID=$! +sleep 2 # Wait for the server to start + +# Check if the server started successfully +if ! ps -p $SERVER_PID > /dev/null; then + echo -e "${RED}Failed to start the server.${NC}" + exit 1 +fi + +# Function to store response +store_response() { + local url=$1 + local expected=$2 + local response=$(curl -s -o /dev/null -w "%{http_code}" $url) + if [ "$response" == "$expected" ]; then + echo -e "$url - Expected $expected - HTTP status code: $response ${GREEN}✔${NC}" >> results.txt + else + echo -e "$url - Expected $expected - HTTP status code: $response ${RED}✘${NC}" >> results.txt + fi +} + +# Run curl commands and store responses +store_response "0.0.0.0:8081" "404" +store_response "127.0.0.2:8080" "000" +store_response "127.0.0.3:8080" "000" +store_response "[::1]:8080" "404" +store_response "[::]:8081" "404" +store_response "[::1]:8082" "404" +store_response "[::2]:8080" "000" +store_response "[::3]:8080" "000" +store_response "[::1]:8081" "404" + +# Stop the server +echo "Stopping the server..." +kill $SERVER_PID + +# Print all responses +echo "Test results:" +cat results.txt +rm results.txt + +echo "Test script completed." From be2711440ea0e7c74a898b66580a96e20e2fde3c Mon Sep 17 00:00:00 2001 From: 552020 Date: Sat, 18 May 2024 12:49:50 +0200 Subject: [PATCH 2/2] tests(multi socket Ipv6): fix requests in test IPv6.sh --- tests/multi_sockets/Dockerfile | 20 +++++++---------- tests/multi_sockets/multi_socket.md | 34 +++++++++++++++++++++++++++++ tests/multi_sockets/results.txt | 9 -------- tests/multi_sockets/testIPv6.sh | 33 +++++++++++++++++++++++++++- 4 files changed, 74 insertions(+), 22 deletions(-) delete mode 100644 tests/multi_sockets/results.txt diff --git a/tests/multi_sockets/Dockerfile b/tests/multi_sockets/Dockerfile index ca74b84c..e4dc4ac6 100644 --- a/tests/multi_sockets/Dockerfile +++ b/tests/multi_sockets/Dockerfile @@ -3,24 +3,14 @@ FROM ubuntu:20.04 # Install necessary packages RUN apt-get update && \ - apt-get install -y build-essential curl vim + apt-get install -y build-essential curl vim iproute2 net-tools # Set the working directory WORKDIR /app -# Copy the entire project into the container +# Copy the entire project into the containerenet-tools COPY . . - -# List the contents of the /app directory to verify the presence of the Makefile -RUN echo "Contents of /app:" && ls -al /app - -# List the contents of the tests directory to verify the structure -RUN echo "Contents of /app/tests:" && ls -al /app/tests - -# List the contents of the multi_sockets directory to verify the structure -RUN echo "Contents of /app/tests/multi_sockets:" && ls -al /app/tests/multi_sockets - # Compile the project in the root directory where the Makefile is located RUN make re -C /app # Compile the project @@ -33,6 +23,12 @@ WORKDIR /app/tests/multi_sockets RUN chmod +x testIPv4.sh RUN chmod +x testIPv6.sh RUN chmod +x testIPv4+IPv6.sh +RUN chmod +x test_mixed.sh + +# Enable IPv6 in the container +RUN sysctl -w net.ipv6.conf.all.disable_ipv6=0 && \ + sysctl -w net.ipv6.conf.default.disable_ipv6=0 && \ + sysctl -w net.ipv6.conf.lo.disable_ipv6=0 # Just start the shell CMD ["/bin/bash"] diff --git a/tests/multi_sockets/multi_socket.md b/tests/multi_sockets/multi_socket.md index 3d13d5b2..efed6167 100644 --- a/tests/multi_sockets/multi_socket.md +++ b/tests/multi_sockets/multi_socket.md @@ -21,6 +21,40 @@ We send requests to: - `127.0.0.3:8080`: "000" - `127.0.0.1:8081`: "404" +### Docker IPv6 Setup Guide + +#### 0. Enable IPv6 in Docker + +To enable IPv6 support in Docker, follow these steps: + +1. **Edit Docker daemon configuration**: + Edit the `/etc/docker/daemon.json` file to include the necessary IPv6 parameters: + + ```json + { + "ipv6": true, + "fixed-cidr-v6": "2001:db8:1::/64", + "experimental": true, + "ip6tables": true + } + ``` + + Step 1 on Mac needs to be done in the Desktop app > Settings > Engine + +2. **Restart Docker**: + Save the configuration file and restart the Docker daemon for the changes to take effect: + + ```sh + sudo systemctl restart docker + ``` + +3. **Create an IPv6 network**: + Create a Docker network that uses IPv6: + + ```sh + docker network create --ipv6 --subnet 2001:db8:1::/64 ip6net + ``` + #### 1. Build the Docker Image From the root of `webserv`: diff --git a/tests/multi_sockets/results.txt b/tests/multi_sockets/results.txt deleted file mode 100644 index 622d1e5e..00000000 --- a/tests/multi_sockets/results.txt +++ /dev/null @@ -1,9 +0,0 @@ -127.0.0.1:8080 - Expected 404 - HTTP status code: 404 ✔ -0.0.0.0:8081 - Expected 404 - HTTP status code: 404 ✔ -127.0.0.1:8082 - Expected 404 - HTTP status code: 404 ✔ -127.0.0.1:8080 - Expected 404 - HTTP status code: 404 ✔ -0.0.0.0:8081 - Expected 404 - HTTP status code: 404 ✔ -127.0.0.1:8082 - Expected 404 - HTTP status code: 404 ✔ -127.0.0.1:8080 - Expected 404 - HTTP status code: 404 ✔ -0.0.0.0:8081 - Expected 404 - HTTP status code: 404 ✔ -127.0.0.1:8082 - Expected 404 - HTTP status code: 404 ✔ diff --git a/tests/multi_sockets/testIPv6.sh b/tests/multi_sockets/testIPv6.sh index 41975e1a..0b5b6249 100755 --- a/tests/multi_sockets/testIPv6.sh +++ b/tests/multi_sockets/testIPv6.sh @@ -5,6 +5,12 @@ GREEN='\033[0;32m' RED='\033[0;31m' NC='\033[0m' # No Color +# Install net-tools if not present +if ! command -v netstat &> /dev/null; then + echo "Installing net-tools..." + apt-get update && apt-get install -y net-tools +fi + # Check if the webserv binary exists if [ ! -f ./../../webserv ]; then echo -e "${RED}Error: webserv binary not found!${NC}" @@ -29,6 +35,18 @@ if ! ps -p $SERVER_PID > /dev/null; then exit 1 fi +# Verify server is listening on the expected address and port +echo "Checking server listening ports..." +netstat -tuln | grep 8081 >> netstat_results.txt +netstat -tuln | grep 8080 >> netstat_results.txt +netstat -tuln | grep 8082 >> netstat_results.txt + +# Check IPv6 configuration +echo "IPv6 configuration:" +ip -6 addr > ipv6_config.txt +ip -6 route >> ipv6_config.txt + + # Function to store response store_response() { local url=$1 @@ -43,7 +61,8 @@ store_response() { # Run curl commands and store responses store_response "[::1]:8080" "404" -store_response "[::]:8081" "404" +# store_response "[::]:8081" "404" +store_response "[::0]:8081" "404" store_response "[::1]:8082" "404" store_response "[::2]:8080" "000" store_response "[::3]:8080" "000" @@ -58,4 +77,16 @@ echo "Test results:" cat results.txt rm results.txt +# Print netstat results +echo "Netstat results:" +cat netstat_results.txt +rm netstat_results.txt + +# Print IPv6 configuration +echo "IPv6 configuration:" +cat ipv6_config.txt +rm ipv6_config.txt + + + echo "Test script completed."