feat: add https

This commit is contained in:
dancingCycle 2023-03-01 14:08:49 +01:00
parent 85162a14d5
commit 3d3d0e84ab
3 changed files with 203 additions and 0 deletions

141
https/main.c Normal file
View File

@ -0,0 +1,141 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ulfius.h>
#define ROUTE_HELLO "/hello"
/**
* static read file function
*/
static char * read_file(const char * filename) {
char * buffer = NULL;
long length;
FILE * f;
if (filename != NULL) {
f = fopen (filename, "rb");
if (f) {
fseek (f, 0, SEEK_END);
length = ftell (f);
fseek (f, 0, SEEK_SET);
buffer = malloc ((size_t)(length + 1));
if (buffer != NULL) {
fread (buffer, 1, (size_t)length, f);
buffer[length] = '\0';
}
fclose (f);
}
return buffer;
} else {
return NULL;
}
}
/**
* callback functions declaration
*/
int callback_get_hello (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_default (const struct _u_request * request, struct _u_response * response, void * user_data);
int main(int argc, char *argv[]) {
/*declaration*/
struct _u_instance instance;
unsigned int port;
int ret;
printf("main() Started...\n");
if (argc < 2) {
fprintf(stderr, "Usage: %s <port>\n", argv[0]);
exit(1);
}
printf("main() argv[1]: %s\n", argv[1]);
/*store command line argument in int variable*/
/*validate user input*/
/*omit injection*/
/*stream:argv*/
ret = sscanf(argv[1], "%d", &port);
/*valid user input:1 successfully filled item*/
if (ret != 1) {
fprintf(stderr, "The argument must be an integer\n");
exit(1);
}
if (port < 0) {
fprintf(stderr, "Error passing a negative port\n");
exit(1);
}
printf("main() port: %d\n", port);
if (ulfius_init_instance(&instance, port, NULL, NULL) != U_OK) {
printf("main() Error ulfius_init_instance, abort");
return(1);
}
//TODO Why?
u_map_put(instance.default_headers, "Access-Control-Allow-Origin", "*");
// Maximum body size sent by the client is 1 Kb
instance.max_post_body_size = 1024;
// Endpoint list declaration
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_HELLO, NULL, 0, &callback_get_hello, NULL);
// default_endpoint declaration
ulfius_set_default_endpoint(&instance, &callback_default, NULL);
// Start the framework
if (argc == 5 && strcmp("-secure", argv[2]) == 0) {
printf("main() https mode\n");
// If command-line options are -secure <key_file> <cert_file>, then open an https connection
char * key_pem = read_file(argv[3]);
char * cert_pem = read_file(argv[4]);
ret = ulfius_start_secure_framework(&instance, key_pem, cert_pem);
free(key_pem);
free(cert_pem);
} else {
printf("main() http mode\n");
// Open an http connection
ret = ulfius_start_framework(&instance);
}
if (ret == U_OK) {
printf("main() Start framework on port %d", instance.port);
// Wait for the user to press <enter> on the console to quit the application
getchar();
} else {
printf("main() Error starting framework; frameworkRet: %d\n",ret);
}
printf("main() End framework\n");
ulfius_stop_framework(&instance);
ulfius_clean_instance(&instance);
printf("main() Done.\n");
return 0;
}
/**
* Callback function that put a "Hello World!" string in the response
*/
int callback_get_hello (const struct _u_request * request, struct _u_response * response, void * user_data) {
ulfius_set_string_body_response(response, 200, "Hello World!");
return U_CALLBACK_CONTINUE;
}
/**
* Default callback function called if no endpoint has a match
*/
int callback_default (const struct _u_request * request, struct _u_response * response, void * user_data) {
ulfius_set_string_body_response(response, 404, "Page not found, do what you want");
return U_CALLBACK_CONTINUE;
}

24
https/makefile Normal file
View File

@ -0,0 +1,24 @@
# Docu
#
#$@ is the name of the file to be made
#$? is the names of the changed dependents
#$< the name of the related file that caused the action
#$* the prefix shared by target and dependent files
#
# Others
RM = /bin/rm -f
#
# Source, Executable, Includes, Library Defines
EXE = main
#
# Compiler, Linker Defines
CC = /usr/bin/gcc
#
all: main.o
$(CC) main.c -L/usr/lib/x86_64-linux-gnu -lulfius -Wall -o $(EXE)
#
main.o:
$(CC) -c main.c
# Clean Up Objects, Exectuables, Dumps out of source directory
clean:
$(RM) *.o $(EXE) *~

38
https/readme.md Normal file
View File

@ -0,0 +1,38 @@
* install dependencies on Debian Bullseye
```
sudo apt install libulfius-dev --no-install-recommends
```
* build
```
make
```
* create self-signed key/certificate
```
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -sha256 -days 365
```
* enter details
```
writing new private key to 'key.pem'
Enter PEM pass phrase: serfaus
Verifying - Enter PEM pass phrase:
Country Name (2 letter code) [AU]:DE
State or Province Name (full name) [Some-State]:Lower Saxony
Locality Name (eg, city) []:Brunswick
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Software Ingenieur Begerad
Organizational Unit Name (eg, section) []:IT
Common Name (e.g. server FQDN or YOUR name) []:hello.swingbe.de
Email Address []:admin@swingbe.de
```
* run in http mode
```
./main <port>
```
* run in https mode
```
./main <port> -secure <private key file> <certificate file>
```