-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipupdate.c
94 lines (75 loc) · 2.55 KB
/
ipupdate.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#define CHECK_INTERVAL 300 // Interval in seconds to check for updates
volatile sig_atomic_t keep_running = 1;
// Signal handler to stop the service gracefully
void handle_sigterm(int signum) {
keep_running = 0;
}
void resolve_domain_to_ip(const char *domain, char *resolved_ip) {
struct addrinfo hints, *res, *p;
int status;
char ipstr[INET_ADDRSTRLEN];
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; // IPv4
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(domain, NULL, &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
exit(1);
}
// Loop through results and get the first IPv4 address
for (p = res; p != NULL; p = p->ai_next) {
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
inet_ntop(p->ai_family, &(ipv4->sin_addr), ipstr, sizeof(ipstr));
strcpy(resolved_ip, ipstr);
break;
}
freeaddrinfo(res); // Free the linked list
}
void update_ufw_firewall(const char *ip) {
char command[256];
// Construct the UFW command
snprintf(command, sizeof(command), "ufw allow from %s", ip);
// Execute the command
int ret = system(command);
if (ret != 0) {
fprintf(stderr, "Failed to update UFW firewall rules.\n");
exit(1);
}
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <domain>\n", argv[0]);
return 1;
}
const char *domain = argv[1];
char resolved_ip[INET_ADDRSTRLEN] = {0};
char last_ip[INET_ADDRSTRLEN] = {0};
// Set up signal handling for graceful termination
signal(SIGTERM, handle_sigterm);
signal(SIGINT, handle_sigterm);
while (keep_running) {
// Resolve domain to IP
printf("Checking domain %s...\n", domain);
resolve_domain_to_ip(domain, resolved_ip);
// Update UFW only if the IP has changed
if (strcmp(resolved_ip, last_ip) != 0) {
printf("Resolved IP: %s (Updating UFW)\n", resolved_ip);
update_ufw_firewall(resolved_ip);
strcpy(last_ip, resolved_ip);
} else {
printf("Resolved IP: %s (No changes)\n", resolved_ip);
}
// Sleep for the interval or until termination signal
for (int i = 0; i < CHECK_INTERVAL && keep_running; i++) {
sleep(1);
}
}
printf("Service stopping...\n");
return 0;
}