update to latest changes on tunnel plugin
All checks were successful
gitea-sync/antd-tunnel-publishers/pipeline/head This commit looks good

This commit is contained in:
DanyLE 2023-03-29 12:09:40 +02:00
parent 439ed880a2
commit a16e89b087
10 changed files with 138 additions and 25 deletions

Binary file not shown.

View File

@ -243,7 +243,7 @@ int main(int argc, char **argv)
signal(SIGABRT, SIG_IGN);
signal(SIGINT, int_handler);
// now try to request new channel from hotline
fd = open_unix_socket(argv[1]);
fd = open_socket(argv[1]);
if (fd == -1)
{
M_ERROR(MODULE_NAME, "Unable to open the hotline: %s", argv[1]);
@ -262,7 +262,7 @@ int main(int argc, char **argv)
(void)close(fd);
return -1;
}
M_DEBUG(MODULE_NAME, "Wait for comfirm creation of %s", argv[2]);
M_DEBUG(MODULE_NAME, "Wait for confirm creation of %s", argv[2]);
// now wait for message
if (msg_read(fd, &response) == -1)
{

View File

@ -42,6 +42,7 @@ static void execute_command(list_t *plist)
ASSERT(pid != -1, "Unable to fork: %s", strerror(errno));
if (pid == 0)
{
M_LOG(MODULE_NAME, "Running %s", cmd.params[0]);
execve(cmd.params[0], &cmd.params[0], &cmd.envs[0]);
// Nothing below this line should be executed by child process. If so,
// it means that the execl function wasn't successfull, so lets exit:

View File

@ -1,21 +1,27 @@
[vterm]
exec = /opt/www/bin/vterm
param = /opt/www/tmp/channels/antd_hotline.sock
param = unix:/opt/www/tmp/antd_hotline.sock
debug = 0
[notification_fifo]
# [notification_fifo]
# exec = /opt/www/bin/wfifo
# param = unix:/opt/www/tmp/antd_hotline.sock
# param = notification
# param = /var/wfifo_notification
# param = r
# debug = 1
# [broadcast]
# exec = /opt/www/bin/broadcast
# param = unix:/opt/www/tmp/antd_hotline.sock
# param = broadcast
# debug = 1
# used only by tunnel to authentificate user
[tunnel_keychain]
exec = /opt/www/bin/wfifo
param = /opt/www/tmp/channels/antd_hotline.sock
param = notification
param = /var/wfifo_notification
param = unix:/opt/www/tmp/antd_hotline.sock
param = keychain
param = /opt/www/tmp/antunnel_keychain
param = r
debug = 1
[broadcast]
exec = /opt/www/bin/broadcast
param = /opt/www/tmp/channels/antd_hotline.sock
param = broadcast
debug = 1
;service=/opt/www/bin/wfifo /opt/www/tmp/channels/antd_hotline.sock server_to_client /var/wfifo_s2c r
;service=/opt/www/bin/wfifo /opt/www/tmp/channels/antd_hotline.sock client_to_server /var/wfifo_c2s w

View File

@ -95,7 +95,7 @@ int main(int argc, char **argv)
M_LOG(MODULE_NAME, "Unix domain socket: %s created", argv[3]);
M_LOG(MODULE_NAME, "Hotline is: %s", argv[1]);
// now try to request new channel from hotline
fd = open_unix_socket(argv[1]);
fd = open_socket(argv[1]);
if (fd == -1)
{
M_ERROR(MODULE_NAME, "Unable to open the hotline: %s", argv[1]);

107
tunnel.c
View File

@ -5,11 +5,16 @@
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include "tunnel.h"
#define MODULE_NAME "api"
#ifdef MAX_PATH_LEN
#undef MAX_PATH_LEN
#endif
#define MAX_PATH_LEN 108
static int guard_read(int fd, void* buffer, size_t size)
{
@ -126,7 +131,7 @@ static uint8_t* msg_read_payload(int fd, uint32_t* size)
}
int open_unix_socket(char* path)
static int open_unix_socket(char* path)
{
struct sockaddr_un address;
address.sun_family = AF_UNIX;
@ -147,6 +152,69 @@ int open_unix_socket(char* path)
return fd;
}
static int open_tcp_socket(char * address, int port)
{
struct sockaddr_in servaddr;
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
M_ERROR(MODULE_NAME, "Cannot create TCP socket %s:%d: %s",address, port, strerror(errno));
return -1;
}
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(address);
servaddr.sin_port = htons(port);
// connect the client socket to server socket
if (connect(fd, (struct sockaddr*)&servaddr, sizeof(servaddr))!= 0) {
M_ERROR(MODULE_NAME, "Unable to connect to socket '%s:%d': %s", address, port, strerror(errno));
close(fd);
return -1;
}
M_LOG(MODULE_NAME, "Connected to server: %s:%d at [%d]", address, port, fd);
return fd;
}
int open_socket( char *path)
{
regmatch_t regex_matches[3];
if(strncmp(path,"unix:", 5) == 0)
{
if(strlen(path + 5) > MAX_PATH_LEN - 1)
{
M_ERROR(MODULE_NAME, "socket configuration is too long: %s", path);
return -1;
}
M_LOG(MODULE_NAME, "Found Unix domain socket configuration: %s", path + 5);
return open_unix_socket(path + 5);
}
else if(regex_match("^([a-zA-Z0-9\\-_\\.]+):([0-9]+)$", path,3, regex_matches))
{
if(regex_matches[1].rm_eo - regex_matches[1].rm_so > MAX_PATH_LEN - 1)
{
M_ERROR(MODULE_NAME, "socket configuration is too long: %s", path);
return -1;
}
char address[MAX_PATH_LEN];
memcpy(address, path + regex_matches[2].rm_so, regex_matches[2].rm_eo - regex_matches[2].rm_so);
int port = atoi(address);
(void*) memset(address, 0, MAX_PATH_LEN);
memcpy(address, path + regex_matches[1].rm_so, regex_matches[1].rm_eo - regex_matches[1].rm_so);
M_LOG(MODULE_NAME, "Found TCP socket configuration: %s:%d", address, port);
return open_tcp_socket(address, port);
}
else
{
M_ERROR(MODULE_NAME, "Unknown socket configuration: %s", path);
return -1;
}
return 0;
}
int msg_read(int fd, tunnel_msg_t* msg)
{
@ -249,3 +317,40 @@ int msg_write(int fd, tunnel_msg_t* msg)
}
return 0;
}
int regex_match(const char* expr,const char* search, int msize, regmatch_t* matches)
{
regex_t regex;
int reti;
char msgbuf[100];
int ret;
/* Compile regular expression */
reti = regcomp(&regex, expr, REG_ICASE | REG_EXTENDED);
if( reti ){
//ERROR("Could not compile regex: %s",expr);
regerror(reti, &regex, msgbuf, sizeof(msgbuf));
M_ERROR(MODULE_NAME, "Regex match failed: %s", msgbuf);
//return 0;
}
/* Execute regular expression */
reti = regexec(&regex, search, msize, matches, 0);
if( !reti ){
//LOG("Match");
ret = 1;
}
else if( reti == REG_NOMATCH ){
//LOG("No match");
ret = 0;
}
else{
regerror(reti, &regex, msgbuf, sizeof(msgbuf));
//ERROR("Regex match failed: %s\n", msgbuf);
ret = 0;
}
regfree(&regex);
return ret;
}

View File

@ -2,6 +2,7 @@
#define TUNNEL_H
#include <stdint.h>
#include <netinet/in.h>
#include <regex.h>
#include "log.h"
#define MAX_CHANNEL_PATH 108
@ -31,10 +32,9 @@ typedef struct{
uint8_t* data;
} tunnel_msg_t;
int open_unix_socket(char* path);
int open_socket(char* path);
int msg_write(int fd, tunnel_msg_t* msg);
int msg_read(int fd, tunnel_msg_t* msg);
int regex_match(const char* expr,const char* search, int msize, regmatch_t* matches);
#endif

View File

@ -449,7 +449,7 @@ int main(const int argc, const char **argv)
{
exit(1);
}
sock = open_unix_socket((char *)argv[1]);
sock = open_socket((char *)argv[1]);
if (sock == -1)
{
M_ERROR(MODULE_NAME, "Unable to open the hotline: %s", argv[1]);

View File

@ -336,7 +336,7 @@ int main(int argc, char **argv)
signal(SIGINT, int_handler);
M_LOG(MODULE_NAME, "Hotline is: %s", argv[1]);
// now try to request new channel from hotline
fd = open_unix_socket(argv[1]);
fd = open_socket(argv[1]);
if (fd == -1)
{
M_ERROR(MODULE_NAME, "Unable to open the hotline: %s", argv[1]);

View File

@ -213,6 +213,7 @@ int main(int argc, char **argv)
if (argc != 5)
{
M_LOG(MODULE_NAME, "Usage: %s path/to/hotline/socket channel_name input_file r/w\n", argv[0]);
printf("Usage: %s path/to/hotline/socket channel_name input_file r/w\n", argv[0]);
return -1;
}
@ -221,7 +222,7 @@ int main(int argc, char **argv)
signal(SIGINT, int_handler);
// now try to request new channel from hotline
fd = open_unix_socket(argv[1]);
fd = open_socket(argv[1]);
if (fd == -1)
{
M_ERROR(MODULE_NAME, "Unable to open the hotline: %s", argv[1]);
@ -240,7 +241,7 @@ int main(int argc, char **argv)
(void)close(fd);
return -1;
}
M_LOG(MODULE_NAME, "Wait for comfirm creation of %s", argv[2]);
M_LOG(MODULE_NAME, "Wait for confirm creation of %s", argv[2]);
// now wait for message
if (msg_read(fd, &msg) == -1)
{