[Webserver]httplib webserver+https(openssl)(1)(MacOS,Linux)

๐Ÿ‘‰๐Ÿป http_lib ์„œ๋ฒ„์— openssl๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ…Œ์ŠคํŠธ์šฉ ์ธ์ฆ์„œ๋ฅผ ๋ฐ›์•„์„œ https๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
This is a method to use HTTPS by obtaining a test certificate using openssl on the http_lib server.

๐Ÿ‘‰๐Ÿป ์•„๋ž˜๋Š” MacOS์™€ Linux์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
Below is how to use it on MacOS and Linux.

๐Ÿ‘‰๐Ÿป Open SSL

โœ”๏ธ Let’s Encrypt๊ฐ™์€ ๊ณต์ธ์ธ์ฆ์„œ๊ฐ€ ์•„๋‹Œ ์Šค์Šค๋กœ ๋งŒ๋“œ๋Š” ์ธ์ฆ์„œ์ž…๋‹ˆ๋‹ค.
It is a certificate that you create yourself, not an accredited certificate like Let’s Encrypt.

โœ”๏ธ ๊ณต์ธ์ธ์ฆ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ธŒ๋ผ์šฐ์ € ์ ‘์†์‹œ https://์— ๋นจ๊ฐ„์ค„์ด ๋œน๋‹ˆ๋‹ค.
Since it is not an accredited certificate, a red line appears under https:// when accessing via a browser.

โœ”๏ธ httpsํ…Œ์ŠคํŠธ ์šฉ์œผ๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋ฉฐ ์‹ค์ œ ์„œ๋น„์Šค์—์„œ๋Š” Let’s Encrypt๊ฐ™์€ ๊ณต์ธ์ธ์ฆ์„œ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
It can be used for HTTPS testing, but an accredited certificate like Let’s Encrypt is required for actual services.


๐Ÿ‘‰๐Ÿปopenssl ์„ค์น˜ / install openssl

โœ”๏ธMacOS

— Open SSL์„ค์น˜ / install openssl

brew install openssl

— ํ…Œ์ŠคํŠธ์šฉ ์ธ์ฆ์„œ์™€ ํ‚คํŒŒ์ผ ์ƒ์„ฑ(cert.pem , key.pem)
Generate test certificate and key file (cert.pem, key.pem)

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes

โœ”๏ธ Linux

— Open SSL์„ค์น˜ / Install Open SSL

sudo apt update
sudo apt install g++ libssl-dev openssl wget -y

— ํ…Œ์ŠคํŠธ์šฉ ์ธ์ฆ์„œ์™€ ํ‚คํŒŒ์ผ ์ƒ์„ฑ(cert.pem , key.pem)
Generate test certificate and key file (cert.pem, key.pem)

โญ๏ธ CN=localhost๋ผ๋„ ์‚ฌ์šฉ๊ฐ€๋Šฅ ๋„๋ฉ”์ธ์ด ์žˆ๋‹ค๋ฉด ๋„๋ฉ”์ธ ์ž…๋ ฅํ• ๊ฒƒ(CN=host.domain.org)
If you have a usable domain even if CN=localhost, enter the domain (CN=host.domain.org)

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/C=KR/ST=Seoul/L=Seoul/O=MyOrg/OU=IT/CN=localhost"

๐Ÿ‘‰๐Ÿป httplib.h ๋‹ค์šด๋กœ๋“œ / httplib.h Download

curl -L https://raw.githubusercontent.com/yhirose/cpp-httplib/master/httplib.h -o httplib.h

๐Ÿ‘‰๐Ÿป server.cpp

#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "httplib.h"
#include <iostream>

int main( ) {
    // ์ธ์ฆ์„œ์™€ ํ‚ค ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
    // Specify the certificate and key file paths.
    httplib::SSLServer svr("./cert.pem", "./key.pem" );

    if (!svr.is_valid()) {
        std::cerr << "SSL ์„œ๋ฒ„ ์„ค์ • ์‹คํŒจ / SSL server setup failed!" << std::endl;
        return 1;
    }

    svr.Get("/", [](const httplib::Request&, httplib::Response& res ) {
        res.set_content("<h1> ๐Ÿ‘‰๐Ÿป HTTPS Server is Running! ๐Ÿ”ฅ </h1>", "text/html; charset=utf-8");
    });

    std::cout << "Server Listening: https://0.0.0.0:5080" << std::endl;
    svr.listen("0.0.0.0", 5080 );

    return 0;
}

๐Ÿ‘‰๐Ÿป ์ปดํŒŒ์ผ / Compiling

โœ”๏ธ MacOS

— Apple Silicon(M1,M2,M3)

g++ -o server server.cpp -std=c++11 -pthread \
-I/opt/homebrew/opt/openssl/include \
-L/opt/homebrew/opt/openssl/lib \
-lssl -lcrypto

— Intel

g++ -o server server.cpp -std=c++11 -pthread \
-I/usr/local/opt/openssl/include \
-L/usr/local/opt/openssl/lib \
-lssl -lcrypto

— ๊ฐ„๋‹จํžˆ pkg-config ์‚ฌ์šฉ๊ฐ€๋Šฅ / You can simply use pkg-config.

g++ -o server server.cpp -std=c++11 -pthread $(pkg-config --cflags --libs openssl)

โœ”๏ธ Linux

โญ๏ธ ๋ฆฌ๋ˆ…์Šค์—์„œ ์ปดํŒŒ์ผ์‹œ warning ๋ฌธ๊ตฌ๊ฐ€ ๋œฐ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹คํ–‰ํŒŒ์ผ์€ ์ƒ์„ฑ๋˜๋‹ˆ ๋ฌด์‹œํ•˜๊ณ  ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
Warning messages may appear when compiling on Linux. An executable file will be generated, so you can ignore them and use the application.

g++ -o server server.cpp -std=c++11 -pthread -lssl -lcrypto

๐Ÿ‘‰๐Ÿป ์„œ๋ฒ„ ์‹คํ–‰ / Run Server

โœ”๏ธ macOS & Linux

./server

๐Ÿ‘‰๐Ÿป ๋ธŒ๋ผ์šฐ์ € ์ ‘์† / Browser access

โœ”๏ธ macOS

Chrome browser,MacOS

โœ”๏ธ Linux

Chrome browser,Linux

โญ๏ธ ํฌ๋กฌ ๋ธŒ๋ผ์šฐ์ € ์ ‘์†์‹œ localhost(์•ˆ์ „ํ•˜์ง€ ์•Š์Œ)์œผ๋กœ ์ด๋™] ๋ฒ„ํŠผ์ด ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
If the [Go to localhost (unsafe)] button does not appear when accessing via Chrome browser

๋ธŒ๋ผ์šฐ์ €์— ์ €์žฅ๋œ HSTS ์„ค์ • ์‚ญ์ œ
Delete HSTS settings saved in the browser

๋งŒ์•ฝ ์œ„ ๋ฐฉ๋ฒ•์ด ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๊ธฐ์–ตํ•˜๊ณ  ์žˆ๋Š” ํ•ด๋‹น ๋„๋ฉ”์ธ์˜ ๋ณด์•ˆ ์ •์ฑ…์„ ๊ฐ•์ œ๋กœ ์ง€์›Œ์•ผ ํ•ฉ๋‹ˆ๋‹ค.ย 
If the above method does not work, you must forcibly clear the security policy for the domain that the browser remembers.

1)ํฌ๋กฌ ์ฃผ์†Œ์ฐฝ์—ย chrome://net-internals/#hsts๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์ ‘์†ํ•ฉ๋‹ˆ๋‹ค.
Enter chrome://net-internals/#hsts into the Chrome address bar and access it.

2)๋งจ ์•„๋ž˜ย Delete domain security policiesย ํ•ญ๋ชฉ์„ ์ฐพ์Šต๋‹ˆ๋‹ค.
Locate the "Delete domain security policies" item at the very bottom.

3)Domainย ์นธ์—ย (ex.) localhost ๋˜๋Š” host.domain.org๋ฅผ ์ž…๋ ฅํ•˜๊ณ ย Deleteย ๋ฒ„ํŠผ์„ ๋ˆ„๋ฆ…๋‹ˆ๋‹ค.
Enter (e.g.) localhost or host.domain.org in the Domain field and click the Delete button.

Leave a Reply