-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
135 lines (105 loc) · 3.13 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* Feel free to use this example code in any way
you see fit (Public Domain) */
#include <sys/types.h>
#ifndef _WIN32
#include <sys/select.h>
#include <sys/socket.h>
#else
#include <winsock2.h>
#endif
#include <string.h>
#include <microhttpd.h>
#include <stdio.h>
#include <stdlib.h>
#include "hookVerif/hookVerif.h"
#include "loader/loader.h"
#include "postProcess/postProcess.h"
#include "status.h"
#include "led/led.h"
#include "led/tty.h"
#define PORT 44443
#define KEY "ssl/cert.key"
#define CERT "ssl/cert.pem"
static int answer_to_connection (void *cls, struct MHD_Connection *connection,
const char *url, const char *method,
const char *version, const char *upload_data,
size_t *upload_data_size, void **con_cls)
{
int ret;
if(strcmp(method, "GET") == 0 && strcmp(url, "/webhook") == 0){
printf("GET %s\n", url);
ret = hookVerif(connection);
}else
if(strcmp(method, "POST") == 0 && strcmp(url, "/webhook") == 0){
printf("POST %s\n", url);
if (NULL == *con_cls) {
return postFirstTime(connection, con_cls);
}else {
postNotFirstTime(connection, upload_data, upload_data_size, con_cls);
}
//ret = hookVerif(connection);
}
else{
struct MHD_Response *response;
const char *error = "404 Not Found";
response =
MHD_create_response_from_buffer (strlen(error), (void *) error,
MHD_RESPMEM_PERSISTENT);
printf("404: %s\n", url);
ret = MHD_queue_response (connection, 404, response);
MHD_destroy_response (response);
}
return ret;
}
void request_completed (void *cls, struct MHD_Connection *connection,
void **con_cls,
enum MHD_RequestTerminationCode toe)
{
struct connection_info_struct *con_info = *con_cls;
if (NULL == con_info) return;
if (con_info->data) free (con_info->data);
free (con_info);
*con_cls = NULL;
}
int running;
int main (int argc, char const *argv[]) {
struct MHD_Daemon *daemon;
char *key_pem;
char *cert_pem;
pthread_mutex_init(&ledStatusMutex, NULL);
running =1;
currentStatus = malloc(sizeof(struct status));
currentStatus->r =0;
currentStatus->g =0;
currentStatus->b =0;
currentStatus->mode =0;
key_pem = load_file (KEY);
cert_pem = load_file (CERT);
if ((key_pem == NULL) || (cert_pem == NULL))
{
printf ("The key/certificate files could not be read.\n");
return 1;
}
daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_SSL, PORT, NULL, NULL,
&answer_to_connection, NULL,
MHD_OPTION_NOTIFY_COMPLETED, &request_completed, NULL,
MHD_OPTION_HTTPS_MEM_KEY, key_pem,
MHD_OPTION_HTTPS_MEM_CERT, cert_pem,
MHD_OPTION_END);
if (NULL == daemon){
printf("daemon is NULL\n");
return 1;
}
int initResult = init(argv[1]);
if (initResult == -1) {
running = -1;
}
ledLoop();
MHD_stop_daemon (daemon);
pthread_mutex_destroy(&ledStatusMutex);
end();
free(key_pem);
free(cert_pem);
free(currentStatus);
return 0;
}