2019-11-19 17:20:11 +01:00
|
|
|
#define PLUGIN_IMPLEMENT 1
|
2018-09-19 15:10:48 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-09-23 14:09:43 +02:00
|
|
|
#include <unistd.h>
|
2018-09-19 15:10:48 +02:00
|
|
|
#include <rfb/rfbclient.h>
|
2018-10-09 17:24:44 +02:00
|
|
|
#include <pthread.h>
|
2018-09-20 00:04:32 +02:00
|
|
|
#include <jpeglib.h>
|
2019-11-13 18:01:14 +01:00
|
|
|
#include <antd/plugin.h>
|
2020-08-25 17:26:10 +02:00
|
|
|
#include <antd/scheduler.h>
|
|
|
|
#include <antd/ws.h>
|
2022-08-16 21:26:23 +02:00
|
|
|
#include <sys/time.h>
|
2020-08-25 17:26:10 +02:00
|
|
|
|
2018-09-19 15:10:48 +02:00
|
|
|
#define get_user_data(x) ((wvnc_user_data_t *)x)
|
|
|
|
/*
|
|
|
|
Vnc to web socket using the
|
|
|
|
libvncserver/libvncclient
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t cmd;
|
|
|
|
uint16_t size;
|
|
|
|
uint8_t *data;
|
|
|
|
|
|
|
|
} wvnc_cmd_t;
|
2018-10-06 19:58:57 +02:00
|
|
|
|
2020-12-29 20:30:16 +01:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
DISCONNECTED,
|
|
|
|
READY,
|
|
|
|
CONNECTED
|
|
|
|
} wvnc_connect_t;
|
2018-10-06 19:58:57 +02:00
|
|
|
|
2018-09-19 15:10:48 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2018-10-06 19:58:57 +02:00
|
|
|
antd_request_t *wscl;
|
|
|
|
wvnc_connect_t status;
|
2018-10-08 19:30:22 +02:00
|
|
|
rfbClient *vncl;
|
2018-09-23 14:09:43 +02:00
|
|
|
uint8_t quality;
|
2022-08-16 21:26:23 +02:00
|
|
|
long long last_update;
|
|
|
|
uint16_t ux;
|
|
|
|
uint16_t uy;
|
|
|
|
uint16_t uw;
|
|
|
|
uint16_t uh;
|
2018-09-22 01:18:13 +02:00
|
|
|
//int rate;
|
2018-09-19 15:10:48 +02:00
|
|
|
} wvnc_user_data_t;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t r_shift;
|
|
|
|
uint8_t g_shift;
|
|
|
|
uint8_t b_shift;
|
|
|
|
uint8_t r_max;
|
|
|
|
uint8_t g_max;
|
|
|
|
uint8_t b_max;
|
|
|
|
} wvnc_pixel_format_t;
|
|
|
|
|
|
|
|
void *vnc_fatal(void *client, const char *msg);
|
|
|
|
void *process(void *cl, int wait);
|
|
|
|
static rfbBool resize(rfbClient *client);
|
|
|
|
void open_session(void *client, const char *addr);
|
|
|
|
void *consume_client(void *cl, wvnc_cmd_t header);
|
|
|
|
static rfbCredential *get_credential(rfbClient *cl, int credentialType);
|
|
|
|
static void update(rfbClient *cl, int x, int y, int w, int h);
|
2022-08-16 21:26:23 +02:00
|
|
|
static void finish_update(rfbClient *cl);
|
2018-09-19 15:10:48 +02:00
|
|
|
|
2022-08-16 21:26:23 +02:00
|
|
|
static long long current_timestamp() {
|
|
|
|
struct timeval te;
|
|
|
|
gettimeofday(&te, NULL);
|
|
|
|
long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000;
|
|
|
|
return milliseconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
int jpeg_compress(uint8_t *buff, int w, int h, int quality)
|
2018-09-20 00:04:32 +02:00
|
|
|
{
|
|
|
|
struct jpeg_compress_struct cinfo = {0};
|
|
|
|
struct jpeg_error_mgr jerror = {0};
|
|
|
|
jerror.trace_level = 10;
|
|
|
|
cinfo.err = jpeg_std_error(&jerror);
|
|
|
|
cinfo.err->trace_level = 10;
|
|
|
|
jpeg_create_compress(&cinfo);
|
2018-09-23 14:09:43 +02:00
|
|
|
uint8_t *out = NULL;
|
2018-09-20 00:04:32 +02:00
|
|
|
unsigned long outbuffer_size = 0;
|
|
|
|
jpeg_mem_dest(&cinfo, &out, &outbuffer_size);
|
|
|
|
cinfo.image_width = w;
|
|
|
|
cinfo.image_height = h;
|
2022-08-16 21:26:23 +02:00
|
|
|
cinfo.input_components = 4;
|
|
|
|
cinfo.in_color_space = JCS_EXT_RGBA;
|
2018-09-20 00:04:32 +02:00
|
|
|
jpeg_set_defaults(&cinfo);
|
2018-09-21 01:35:12 +02:00
|
|
|
jpeg_set_quality(&cinfo, quality, true);
|
2018-09-20 00:04:32 +02:00
|
|
|
jpeg_start_compress(&cinfo, true);
|
2022-08-17 17:58:27 +02:00
|
|
|
if(!out)
|
|
|
|
{
|
|
|
|
ERROR("Unable to allocate output jpeg memory");
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-20 00:04:32 +02:00
|
|
|
//unsigned counter = 0;
|
|
|
|
JSAMPROW row_pointer[1];
|
2018-10-08 19:30:22 +02:00
|
|
|
row_pointer[0] = NULL;
|
2018-09-20 00:04:32 +02:00
|
|
|
while (cinfo.next_scanline < cinfo.image_height)
|
|
|
|
{
|
2022-08-16 21:26:23 +02:00
|
|
|
row_pointer[0] = (JSAMPROW)(&buff[cinfo.next_scanline * w * 4]);
|
2018-10-08 19:30:22 +02:00
|
|
|
jpeg_write_scanlines(&cinfo, row_pointer, 1);
|
2018-09-20 00:04:32 +02:00
|
|
|
}
|
|
|
|
jpeg_finish_compress(&cinfo);
|
|
|
|
jpeg_destroy_compress(&cinfo);
|
2022-08-17 17:58:27 +02:00
|
|
|
if (outbuffer_size < (unsigned long)(w * h * 4))
|
|
|
|
{
|
|
|
|
memcpy(buff, out, outbuffer_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
outbuffer_size = 0;
|
|
|
|
}
|
2018-09-20 00:04:32 +02:00
|
|
|
free(out);
|
|
|
|
return outbuffer_size;
|
|
|
|
}
|
2020-12-29 20:30:16 +01:00
|
|
|
|
2018-09-19 15:10:48 +02:00
|
|
|
int get_pixel_format(uint8_t deep, wvnc_pixel_format_t *d)
|
|
|
|
{
|
|
|
|
switch (deep)
|
|
|
|
{
|
|
|
|
case 32:
|
|
|
|
d->r_shift = 0;
|
|
|
|
d->g_shift = 8;
|
|
|
|
d->b_shift = 16;
|
|
|
|
d->r_max = d->b_max = d->g_max = 255;
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
// RGB 565 format
|
|
|
|
d->r_shift = 0;
|
|
|
|
d->g_shift = 5;
|
|
|
|
d->b_shift = 11;
|
|
|
|
d->r_max = 31;
|
|
|
|
d->b_max = 31;
|
|
|
|
d->g_max = 63;
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *process(void *data, int wait)
|
|
|
|
{
|
|
|
|
wvnc_user_data_t *user_data = get_user_data(data);
|
|
|
|
uint8_t *buff = NULL;
|
|
|
|
ws_msg_header_t *h = NULL;
|
2020-12-29 20:30:16 +01:00
|
|
|
while (!(h = ws_read_header(user_data->wscl->client)) && user_data->status != DISCONNECTED && wait)
|
|
|
|
;
|
2018-09-19 15:10:48 +02:00
|
|
|
if (h)
|
|
|
|
{
|
|
|
|
if (h->mask == 0)
|
|
|
|
{
|
2022-08-17 00:39:53 +02:00
|
|
|
ERROR("Data is not masked opcode 0x%04x data len %d", h->opcode,h->plen);
|
2018-10-06 19:58:57 +02:00
|
|
|
ws_close(user_data->wscl->client, 1012);
|
|
|
|
user_data->status = DISCONNECTED;
|
2018-09-19 15:10:48 +02:00
|
|
|
free(h);
|
|
|
|
return NULL;
|
|
|
|
//break;
|
|
|
|
}
|
|
|
|
if (h->opcode == WS_CLOSE)
|
|
|
|
{
|
2022-08-17 00:39:53 +02:00
|
|
|
LOG("Websocket: connection closed");
|
2018-10-06 19:58:57 +02:00
|
|
|
ws_close(user_data->wscl->client, 1011);
|
|
|
|
user_data->status = DISCONNECTED;
|
2018-09-19 15:10:48 +02:00
|
|
|
free(h);
|
|
|
|
return 0;
|
|
|
|
//break;
|
|
|
|
}
|
|
|
|
else if (h->opcode == WS_BIN)
|
|
|
|
{
|
|
|
|
int l;
|
|
|
|
buff = (uint8_t *)malloc(h->plen + 1);
|
|
|
|
if (!buff)
|
|
|
|
{
|
|
|
|
free(h);
|
|
|
|
return vnc_fatal(user_data, "Cannot alloc memory for the command");
|
|
|
|
}
|
|
|
|
// read the command from the client
|
|
|
|
int len = h->plen;
|
2018-10-06 19:58:57 +02:00
|
|
|
if ((l = ws_read_data(user_data->wscl->client, h, h->plen, buff)) > 0)
|
2018-09-19 15:10:48 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
// process client command
|
|
|
|
wvnc_cmd_t header;
|
|
|
|
header.cmd = buff[0];
|
|
|
|
buff[len] = '\0';
|
|
|
|
header.data = (buff + 3);
|
|
|
|
memcpy(&header.size, buff + 1, 2);
|
|
|
|
void *st = consume_client(user_data, header);
|
|
|
|
if (buff)
|
|
|
|
free(buff);
|
|
|
|
free(h);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vnc_fatal(user_data, "Invalid request");
|
|
|
|
if (buff)
|
|
|
|
free(buff);
|
|
|
|
free(h);
|
|
|
|
return 0;
|
|
|
|
//break;
|
|
|
|
}
|
|
|
|
//buff = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vnc_fatal(user_data, "Unknow opcode");
|
|
|
|
free(h);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rfbBool resize(rfbClient *client)
|
|
|
|
{
|
|
|
|
int width = client->width;
|
|
|
|
int height = client->height;
|
2018-09-20 20:03:05 +02:00
|
|
|
//int depth = client->format.bitsPerPixel;
|
2018-09-19 15:10:48 +02:00
|
|
|
client->updateRect.x = client->updateRect.y = 0;
|
|
|
|
client->updateRect.w = width;
|
|
|
|
client->updateRect.h = height;
|
2018-10-06 19:58:57 +02:00
|
|
|
void *data = rfbClientGetClientData(client, client);
|
2018-09-19 15:10:48 +02:00
|
|
|
wvnc_user_data_t *user_data = get_user_data(data);
|
|
|
|
//client->width = sdl->pitch / (depth / 8);
|
|
|
|
if (client->frameBuffer)
|
2018-10-08 19:30:22 +02:00
|
|
|
{
|
2018-09-19 15:10:48 +02:00
|
|
|
free(client->frameBuffer);
|
2018-10-08 19:30:22 +02:00
|
|
|
client->frameBuffer = NULL;
|
|
|
|
}
|
2022-08-16 21:26:23 +02:00
|
|
|
client->frameBuffer = (uint8_t *)malloc(width * height * 4);
|
2018-09-19 15:10:48 +02:00
|
|
|
wvnc_pixel_format_t pxf;
|
2022-08-16 21:26:23 +02:00
|
|
|
if (!get_pixel_format(32, &pxf))
|
2018-09-19 15:10:48 +02:00
|
|
|
{
|
|
|
|
vnc_fatal(user_data, "Cannot get pixel format");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2022-08-16 21:26:23 +02:00
|
|
|
client->format.bitsPerPixel = 32;
|
2018-09-19 15:10:48 +02:00
|
|
|
client->format.redShift = pxf.r_shift;
|
|
|
|
client->format.greenShift = pxf.g_shift;
|
|
|
|
client->format.blueShift = pxf.b_shift;
|
|
|
|
client->format.redMax = pxf.r_max;
|
|
|
|
client->format.greenMax = pxf.g_max;
|
|
|
|
client->format.blueMax = pxf.b_max;
|
2020-12-29 20:30:16 +01:00
|
|
|
if (SetFormatAndEncodings(client))
|
|
|
|
{
|
|
|
|
LOG("width %d, height %d, depth %d\n", width, height, client->format.bitsPerPixel);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERROR("Unable to set VNC format and Encoding");
|
|
|
|
}
|
2018-09-19 15:10:48 +02:00
|
|
|
/* create or resize the window */
|
|
|
|
// send data to client
|
2022-08-16 21:26:23 +02:00
|
|
|
uint8_t cmd[5];
|
2018-09-19 15:10:48 +02:00
|
|
|
cmd[0] = 0x83; // resize command
|
|
|
|
cmd[1] = (uint8_t)(width & 0xFF);
|
|
|
|
cmd[2] = (uint8_t)(width >> 8);
|
|
|
|
cmd[3] = (uint8_t)(height & 0xFF);
|
|
|
|
cmd[4] = (uint8_t)(height >> 8);
|
2022-08-16 21:26:23 +02:00
|
|
|
ws_b(user_data->wscl->client, cmd, 5);
|
2018-09-19 15:10:48 +02:00
|
|
|
uint8_t *ack = (uint8_t *)process(user_data, 1);
|
|
|
|
if (!ack || !(*ack))
|
|
|
|
{
|
|
|
|
LOG("Client fail to resize\n");
|
|
|
|
if (ack)
|
|
|
|
free(ack);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
free(ack);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2022-08-16 21:26:23 +02:00
|
|
|
static void update(rfbClient *cl, int x, int y, int w, int h)
|
|
|
|
{
|
|
|
|
wvnc_user_data_t *user_data = get_user_data(rfbClientGetClientData(cl, cl));
|
|
|
|
user_data->ux = x < user_data->ux?x: user_data->ux;
|
|
|
|
user_data->uy = y < user_data->uy?y: user_data->uy;
|
|
|
|
int b1 = x + w;
|
|
|
|
int b2 = user_data->ux + user_data->uw;
|
|
|
|
user_data->uw = b1 > b2 ? b1 - user_data->ux : b2 - user_data-> ux;
|
|
|
|
b1 = y + h;
|
|
|
|
b2 = user_data->uy + user_data->uh;
|
|
|
|
user_data->uh = b1 > b2 ? b1 - user_data->uy : b2 - user_data->uy;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void finish_update(rfbClient *client)
|
2018-09-19 15:10:48 +02:00
|
|
|
{
|
2018-10-06 19:58:57 +02:00
|
|
|
wvnc_user_data_t *user_data = get_user_data(rfbClientGetClientData(client, client));
|
2022-08-16 21:26:23 +02:00
|
|
|
long long current_time = current_timestamp();
|
|
|
|
if(current_time - user_data->last_update < 40)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
user_data->last_update = current_time;
|
|
|
|
int cw = user_data->uw;
|
|
|
|
int ch = user_data->uh;
|
|
|
|
int x = user_data->ux;
|
|
|
|
int y = user_data->uy;
|
|
|
|
int size = cw * ch * 4;
|
2022-08-17 17:58:27 +02:00
|
|
|
uint8_t flag = 0;
|
|
|
|
uint8_t *cmd = (uint8_t *)malloc(size + 10); // + 9
|
|
|
|
uint8_t *tmp = cmd + 10;
|
2022-08-16 23:05:52 +02:00
|
|
|
//LOG("w %d h %d x %d y %d", cw, ch, x, y);
|
2022-08-16 21:26:23 +02:00
|
|
|
|
2022-08-17 17:58:27 +02:00
|
|
|
if (!cmd || !tmp)
|
2018-09-19 15:10:48 +02:00
|
|
|
{
|
|
|
|
vnc_fatal(user_data, "Cannot allocate data for update");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!client->frameBuffer)
|
|
|
|
{
|
2022-08-17 17:58:27 +02:00
|
|
|
LOG("Client frame buffer data not found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(size == 0)
|
|
|
|
{
|
2018-09-19 15:10:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-09-20 00:04:32 +02:00
|
|
|
uint8_t *dest_ptr = tmp;
|
2018-09-19 15:10:48 +02:00
|
|
|
uint8_t *src_ptr;
|
|
|
|
|
|
|
|
//cpy line by line
|
2022-08-16 21:26:23 +02:00
|
|
|
|
|
|
|
for (int j = y; j < ch + y; j++)
|
2018-09-19 15:10:48 +02:00
|
|
|
{
|
2022-08-16 21:26:23 +02:00
|
|
|
src_ptr = client->frameBuffer + (j * client->width * 4 + x*4 );
|
|
|
|
memcpy(dest_ptr, src_ptr, cw * 4);
|
|
|
|
for (int i = 3; i < cw * 4; i += 4)
|
|
|
|
dest_ptr[i] = 255;
|
|
|
|
dest_ptr += cw * 4;
|
2018-09-19 15:10:48 +02:00
|
|
|
}
|
|
|
|
cmd[0] = 0x84; //update command
|
|
|
|
cmd[1] = (uint8_t)(x & 0xFF);
|
|
|
|
cmd[2] = (uint8_t)(x >> 8);
|
|
|
|
cmd[3] = (uint8_t)(y & 0xFF);
|
|
|
|
cmd[4] = (uint8_t)(y >> 8);
|
2022-08-16 21:26:23 +02:00
|
|
|
cmd[5] = (uint8_t)(cw & 0xFF);
|
|
|
|
cmd[6] = (uint8_t)(cw >> 8);
|
|
|
|
cmd[7] = (uint8_t)(ch & 0xFF);
|
|
|
|
cmd[8] = (uint8_t)(ch >> 8);
|
|
|
|
int ret = jpeg_compress(tmp, cw, ch, user_data->quality);
|
|
|
|
if (ret > 0)
|
2018-09-20 00:04:32 +02:00
|
|
|
{
|
2022-08-17 17:58:27 +02:00
|
|
|
flag |= 0x01;
|
2022-08-16 21:26:23 +02:00
|
|
|
size = ret;
|
2018-09-20 00:04:32 +02:00
|
|
|
}
|
2022-08-17 17:58:27 +02:00
|
|
|
cmd[9] = flag;
|
|
|
|
ws_b(user_data->wscl->client, cmd, size + 10);
|
2022-08-16 21:26:23 +02:00
|
|
|
user_data->ux = 0xFFFF;
|
|
|
|
user_data->uy = 0xFFFF;
|
|
|
|
user_data->uw = 0;
|
|
|
|
user_data->uh = 0;
|
2018-09-19 15:10:48 +02:00
|
|
|
free(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static rfbCredential *get_credential(rfbClient *cl, int credentialType)
|
|
|
|
{
|
2018-10-06 19:58:57 +02:00
|
|
|
wvnc_user_data_t *user_data = get_user_data(rfbClientGetClientData(cl, cl));
|
2018-09-19 15:10:48 +02:00
|
|
|
rfbCredential *c = malloc(sizeof(rfbCredential));
|
|
|
|
c->userCredential.username = malloc(RFB_BUF_SIZE);
|
|
|
|
c->userCredential.password = malloc(RFB_BUF_SIZE);
|
|
|
|
|
|
|
|
if (credentialType != rfbCredentialTypeUser)
|
|
|
|
{
|
|
|
|
vnc_fatal(user_data, "something else than username and password required for authentication\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
uint8_t cmd[1];
|
|
|
|
cmd[0] = 0x82;
|
2018-10-06 19:58:57 +02:00
|
|
|
ws_b(user_data->wscl->client, cmd, 1);
|
2018-09-19 15:10:48 +02:00
|
|
|
char *up = (char *)process(user_data, 1);
|
|
|
|
if (!up)
|
|
|
|
{
|
|
|
|
if (c)
|
|
|
|
{
|
|
|
|
free(c->userCredential.username);
|
|
|
|
free(c->userCredential.password);
|
|
|
|
free(c);
|
|
|
|
return vnc_fatal(user_data, "Cannot get user credential");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
char *pass = up;
|
|
|
|
while (*pass != '\0')
|
|
|
|
pass++;
|
|
|
|
pass++;
|
|
|
|
LOG("User name %s, pass: %s\n", up, pass);
|
|
|
|
memcpy(c->userCredential.username, up, strlen(up) + 1);
|
|
|
|
memcpy(c->userCredential.password, pass, strlen(pass) + 1);
|
|
|
|
free(up);
|
|
|
|
// remove trailing newlines
|
|
|
|
//c->userCredential.username[strcspn(c->userCredential.username, "\n")] = 0;
|
|
|
|
//c->userCredential.password[strcspn(c->userCredential.password, "\n")] = 0;
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *get_password(rfbClient *client)
|
|
|
|
{
|
|
|
|
uint8_t cmd[1];
|
2018-10-06 19:58:57 +02:00
|
|
|
void *data = rfbClientGetClientData(client, client);
|
2018-09-19 15:10:48 +02:00
|
|
|
wvnc_user_data_t *user_data = get_user_data(data);
|
|
|
|
cmd[0] = 0x81; // resize command
|
2018-10-06 19:58:57 +02:00
|
|
|
ws_b(user_data->wscl->client, cmd, 1);
|
2018-09-19 15:10:48 +02:00
|
|
|
|
|
|
|
// call process to get the password
|
|
|
|
char *pwd = (char *)process(user_data, 1);
|
|
|
|
//not free
|
|
|
|
if (!pwd)
|
|
|
|
{
|
|
|
|
vnc_fatal(user_data, "Cannot read user password");
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-10-08 19:30:22 +02:00
|
|
|
//LOG("Password is '%s'\n", pwd);
|
2018-09-19 15:10:48 +02:00
|
|
|
return pwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void open_session(void *data, const char *addr)
|
|
|
|
{
|
|
|
|
int argc = 2;
|
|
|
|
char *argv[2];
|
|
|
|
argv[0] = "-listennofork";
|
2018-09-23 14:09:43 +02:00
|
|
|
int len = 0;
|
|
|
|
FILE *fp = NULL;
|
|
|
|
char *buffer = NULL;
|
|
|
|
char c;
|
2020-12-29 20:30:16 +01:00
|
|
|
int st;
|
2018-09-19 15:10:48 +02:00
|
|
|
wvnc_user_data_t *user_data = get_user_data(data);
|
2018-09-23 14:09:43 +02:00
|
|
|
if (access(addr, F_OK) != -1)
|
|
|
|
{
|
|
|
|
//open the file
|
|
|
|
fp = fopen(addr, "r");
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
|
|
|
vnc_fatal(data, "Unable to read server file");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find length of first line
|
|
|
|
// lines end in "\n", but some malformed text files may not have this char at all
|
|
|
|
// and whole file contents will be considered as the first line
|
|
|
|
while ((c = fgetc(fp)) != EOF)
|
|
|
|
{
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate memory for size of first line (len)
|
2020-12-29 20:30:16 +01:00
|
|
|
buffer = (char *)malloc(sizeof(char) * (len + 1));
|
2018-09-23 14:09:43 +02:00
|
|
|
|
|
|
|
// seek to beginning of file
|
|
|
|
fseek(fp, 0, SEEK_SET);
|
2018-10-08 19:30:22 +02:00
|
|
|
buffer[len] = '\0';
|
2020-12-29 20:30:16 +01:00
|
|
|
st = fread(buffer, sizeof(char), len, fp);
|
|
|
|
UNUSED(st);
|
2018-09-23 14:09:43 +02:00
|
|
|
fclose(fp);
|
|
|
|
argv[1] = buffer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
argv[1] = (char *)addr;
|
|
|
|
}
|
2022-08-16 21:26:23 +02:00
|
|
|
LOG("client.BBP: %d\n", 32);
|
2018-09-23 14:09:43 +02:00
|
|
|
LOG("client.JPEG.quality: %d\n", user_data->quality);
|
|
|
|
LOG("Server: %s\n", argv[1]);
|
2018-09-22 01:18:13 +02:00
|
|
|
//LOG("Rate is %d\n", user_data->rate);
|
2018-09-19 15:10:48 +02:00
|
|
|
if (!rfbInitClient(user_data->vncl, &argc, argv))
|
|
|
|
{
|
|
|
|
user_data->vncl = NULL; /* rfbInitClient has already freed the client struct */
|
|
|
|
//cleanup(vncl);
|
|
|
|
vnc_fatal(user_data, "Cannot connect to the server");
|
2020-12-29 20:30:16 +01:00
|
|
|
if (buffer)
|
|
|
|
free(buffer);
|
2018-09-19 15:10:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-12-29 20:30:16 +01:00
|
|
|
if (buffer)
|
|
|
|
free(buffer);
|
2018-10-06 19:58:57 +02:00
|
|
|
user_data->status = CONNECTED;
|
|
|
|
}
|
|
|
|
|
2020-12-29 20:30:16 +01:00
|
|
|
void waitfor(void *data)
|
2018-10-06 19:58:57 +02:00
|
|
|
{
|
2018-10-19 12:38:46 +02:00
|
|
|
fd_set fd_in;
|
2018-10-06 19:58:57 +02:00
|
|
|
wvnc_user_data_t *user_data = get_user_data(data);
|
2020-12-29 20:30:16 +01:00
|
|
|
struct timeval timeout;
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 500;
|
2018-10-09 17:24:44 +02:00
|
|
|
while (user_data->status != DISCONNECTED)
|
2018-10-06 19:58:57 +02:00
|
|
|
{
|
2018-10-19 12:38:46 +02:00
|
|
|
FD_ZERO(&fd_in);
|
2020-12-29 20:30:16 +01:00
|
|
|
FD_SET(user_data->wscl->client->sock, &fd_in);
|
|
|
|
int rc = select(user_data->wscl->client->sock + 1, &fd_in, NULL, NULL, &timeout);
|
|
|
|
if (rc == -1)
|
2018-10-19 12:38:46 +02:00
|
|
|
{
|
2022-08-17 00:39:53 +02:00
|
|
|
LOG("Client may disconnected");
|
2018-10-19 12:38:46 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-12-29 20:30:16 +01:00
|
|
|
if (rc > 0 && FD_ISSET(user_data->wscl->client->sock, &fd_in))
|
2018-10-19 12:38:46 +02:00
|
|
|
{
|
|
|
|
process(user_data, 0);
|
|
|
|
}
|
2018-10-09 17:24:44 +02:00
|
|
|
if (user_data->status == CONNECTED)
|
2018-09-19 15:10:48 +02:00
|
|
|
{
|
2018-10-09 17:24:44 +02:00
|
|
|
// process other message
|
2018-10-19 12:38:46 +02:00
|
|
|
int status = WaitForMessage(user_data->vncl, 200); //500
|
2018-10-09 17:24:44 +02:00
|
|
|
if (status < 0)
|
2018-09-19 15:10:48 +02:00
|
|
|
{
|
2022-08-17 00:39:53 +02:00
|
|
|
ERROR("VNC WaitForMessage return %d", status);
|
2018-10-09 17:24:44 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (status)
|
|
|
|
{
|
2022-08-17 00:39:53 +02:00
|
|
|
status = HandleRFBServerMessage(user_data->vncl);
|
|
|
|
if (!status)
|
2018-10-09 17:24:44 +02:00
|
|
|
{
|
2022-08-17 00:39:53 +02:00
|
|
|
ERROR("VNC HandleRFBServerMessage fail");
|
2018-10-09 17:24:44 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-09-19 15:10:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *vnc_fatal(void *data, const char *msg)
|
|
|
|
{
|
|
|
|
// print the message then close the
|
|
|
|
// connection
|
|
|
|
wvnc_user_data_t *user_data = get_user_data(data);
|
|
|
|
|
|
|
|
int len, size;
|
|
|
|
len = strlen(msg);
|
|
|
|
size = len + 1;
|
2022-08-17 00:39:53 +02:00
|
|
|
ERROR("VNC FATAL: %s", msg);
|
2018-09-19 15:10:48 +02:00
|
|
|
uint8_t *cmd = (uint8_t *)malloc(size);
|
|
|
|
cmd[0] = 0xFE; // error opcode
|
|
|
|
if (cmd)
|
|
|
|
{
|
|
|
|
memcpy(cmd + 1, (uint8_t *)msg, len);
|
2018-10-06 19:58:57 +02:00
|
|
|
ws_b(user_data->wscl->client, cmd, size);
|
2018-09-19 15:10:48 +02:00
|
|
|
free(cmd);
|
|
|
|
}
|
|
|
|
// quit the socket
|
2018-10-06 19:58:57 +02:00
|
|
|
ws_close(user_data->wscl->client, 1011);
|
2018-10-19 12:38:46 +02:00
|
|
|
user_data->status = DISCONNECTED;
|
2018-09-19 15:10:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *consume_client(void *ptr, wvnc_cmd_t header)
|
|
|
|
{
|
|
|
|
uint8_t cmd = header.cmd;
|
|
|
|
uint16_t size = header.size;
|
|
|
|
wvnc_user_data_t *user_data = get_user_data(ptr);
|
|
|
|
uint8_t *data;
|
|
|
|
// in case of string
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case 0x01: /*client open a connection*/
|
2022-08-16 21:26:23 +02:00
|
|
|
user_data->quality = header.data[0];
|
|
|
|
open_session(user_data, (char *)(header.data + 1));
|
2018-09-19 15:10:48 +02:00
|
|
|
break;
|
|
|
|
case 0x02: //client enter a vnc password
|
|
|
|
if (!header.data)
|
|
|
|
return NULL;
|
|
|
|
return strdup((char *)header.data);
|
|
|
|
case 0x03: // client enter a credential
|
2018-09-20 00:04:32 +02:00
|
|
|
data = (uint8_t *)malloc(size);
|
2018-09-19 15:10:48 +02:00
|
|
|
memcpy(data, header.data, size);
|
|
|
|
return data;
|
|
|
|
break;
|
|
|
|
case 0x04: // ack from client
|
|
|
|
data = (uint8_t *)malloc(1);
|
|
|
|
*data = (uint8_t)header.data[0];
|
|
|
|
return data;
|
|
|
|
break;
|
|
|
|
case 0x05: //mouse event
|
2018-09-20 20:03:05 +02:00
|
|
|
//LOG("MOuse event %d\n", header.data[4]);
|
2022-08-16 23:05:52 +02:00
|
|
|
SendPointerEvent(user_data->vncl,
|
|
|
|
(header.data[0] | (header.data[1] << 8)) & 0xFFFF,
|
|
|
|
(header.data[2] | (header.data[3] << 8)) & 0xFFFF, header.data[4]);
|
2018-09-19 15:10:48 +02:00
|
|
|
break;
|
2018-09-20 20:03:05 +02:00
|
|
|
case 0x06: // key board event
|
2022-08-16 23:05:52 +02:00
|
|
|
// LOG("Key is %c %d", (header.data[0] | (header.data[1] << 8)) & 0xFFFF, header.data[2]);
|
|
|
|
SendKeyEvent(user_data->vncl, (header.data[0] | (header.data[1] << 8)) & 0xFFFF, header.data[2] ? TRUE : FALSE);
|
2018-09-20 20:03:05 +02:00
|
|
|
break;
|
2018-09-22 14:37:23 +02:00
|
|
|
case 0x07:
|
2020-12-29 20:30:16 +01:00
|
|
|
SendClientCutText(user_data->vncl, (char *)header.data, strlen((char *)header.data));
|
2018-09-22 14:37:23 +02:00
|
|
|
break;
|
2022-08-17 00:39:53 +02:00
|
|
|
case 0x08:
|
|
|
|
LOG("Receive ping message from client: %s", (char*)header.data);
|
|
|
|
break;
|
2018-09-19 15:10:48 +02:00
|
|
|
default:
|
|
|
|
return vnc_fatal(user_data, "Unknown client command");
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-22 14:37:23 +02:00
|
|
|
static void got_clipboard(rfbClient *cl, const char *text, int len)
|
|
|
|
{
|
2022-08-17 00:39:53 +02:00
|
|
|
LOG("received clipboard text '%s'", text);
|
2018-10-06 19:58:57 +02:00
|
|
|
void *data = rfbClientGetClientData(cl, cl);
|
2018-09-22 14:37:23 +02:00
|
|
|
wvnc_user_data_t *user_data = get_user_data(data);
|
2018-09-23 14:09:43 +02:00
|
|
|
uint8_t *cmd = (uint8_t *)malloc(len + 1);
|
2018-09-22 14:37:23 +02:00
|
|
|
cmd[0] = 0x85;
|
2018-09-23 14:09:43 +02:00
|
|
|
memcpy(cmd + 1, text, len);
|
2018-10-06 19:58:57 +02:00
|
|
|
ws_b(user_data->wscl->client, cmd, len + 1);
|
2018-09-22 14:37:23 +02:00
|
|
|
free(cmd);
|
|
|
|
uint8_t *ack = (uint8_t *)process(user_data, 1);
|
|
|
|
if (!ack || !(*ack))
|
|
|
|
{
|
2022-08-17 00:39:53 +02:00
|
|
|
LOG("Fail to set client clipboard");
|
2018-09-22 14:37:23 +02:00
|
|
|
if (ack)
|
|
|
|
free(ack);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
free(ack);
|
|
|
|
}
|
2018-10-19 12:38:46 +02:00
|
|
|
|
2020-12-29 20:30:16 +01:00
|
|
|
void event_loop(void *data)
|
2018-10-09 17:24:44 +02:00
|
|
|
{
|
|
|
|
wvnc_user_data_t *user_data = get_user_data(data);
|
|
|
|
rfbClient *vncl = NULL;
|
|
|
|
vncl = rfbGetClient(8, 3, 4);
|
|
|
|
vncl->MallocFrameBuffer = resize;
|
|
|
|
vncl->canHandleNewFBSize = TRUE;
|
2022-08-16 21:26:23 +02:00
|
|
|
vncl->FinishedFrameBufferUpdate = finish_update;
|
2018-10-09 17:24:44 +02:00
|
|
|
vncl->GotFrameBufferUpdate = update;
|
|
|
|
vncl->GetPassword = get_password;
|
|
|
|
//cl->HandleKeyboardLedState=kbd_leds;
|
|
|
|
//cl->HandleTextChat=text_chat;
|
|
|
|
vncl->GotXCutText = got_clipboard;
|
|
|
|
vncl->GetCredential = get_credential;
|
|
|
|
vncl->listenPort = LISTEN_PORT_OFFSET;
|
|
|
|
vncl->listen6Port = LISTEN_PORT_OFFSET;
|
|
|
|
user_data->status = READY; // 1 for ready for connect
|
|
|
|
user_data->vncl = vncl;
|
|
|
|
rfbClientSetClientData(vncl, vncl, user_data);
|
2020-12-29 20:30:16 +01:00
|
|
|
waitfor((void *)user_data);
|
2018-10-09 17:24:44 +02:00
|
|
|
// child
|
2020-12-29 20:30:16 +01:00
|
|
|
if (user_data->vncl && user_data->vncl->frameBuffer)
|
|
|
|
{
|
2018-10-09 17:24:44 +02:00
|
|
|
free(user_data->vncl->frameBuffer);
|
|
|
|
user_data->vncl->frameBuffer = NULL;
|
|
|
|
}
|
|
|
|
if (user_data->vncl)
|
|
|
|
rfbClientCleanup(user_data->vncl);
|
2018-10-19 12:38:46 +02:00
|
|
|
// close the connection before free data
|
|
|
|
destroy_request(user_data->wscl);
|
|
|
|
free(user_data);
|
2018-10-09 17:24:44 +02:00
|
|
|
}
|
2020-12-29 20:30:16 +01:00
|
|
|
void *handle(void *data)
|
2018-09-19 15:10:48 +02:00
|
|
|
{
|
2018-10-06 19:58:57 +02:00
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
2018-10-09 17:24:44 +02:00
|
|
|
pthread_t th;
|
|
|
|
antd_task_t *task = NULL;
|
2020-12-29 20:30:16 +01:00
|
|
|
void *cl = (void *)rq->client;
|
2018-10-06 19:58:57 +02:00
|
|
|
if (ws_enable(rq->request))
|
2018-09-19 15:10:48 +02:00
|
|
|
{
|
2018-10-19 12:38:46 +02:00
|
|
|
wvnc_user_data_t *user_data = (wvnc_user_data_t *)malloc(sizeof(wvnc_user_data_t));
|
|
|
|
user_data->wscl = rq;
|
2022-08-16 21:26:23 +02:00
|
|
|
user_data->last_update = current_timestamp();
|
|
|
|
user_data->ux = 0xFFFF;
|
|
|
|
user_data->uy = 0xFFFF;
|
|
|
|
user_data->uw = 0;
|
|
|
|
user_data->uh = 0;
|
2020-12-29 20:30:16 +01:00
|
|
|
if (pthread_create(&th, NULL, (void *(*)(void *))event_loop, (void *)user_data) != 0)
|
2018-10-09 17:24:44 +02:00
|
|
|
{
|
|
|
|
free(user_data);
|
|
|
|
perror("pthread_create: cannot create thread for wvnc\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pthread_detach(th);
|
2019-07-31 15:16:38 +02:00
|
|
|
task = antd_create_task(NULL, NULL, NULL, time(NULL));
|
2018-10-09 17:24:44 +02:00
|
|
|
return task;
|
|
|
|
}
|
2018-09-19 15:10:48 +02:00
|
|
|
}
|
2018-09-23 14:29:43 +02:00
|
|
|
else
|
|
|
|
{
|
2020-12-29 20:30:16 +01:00
|
|
|
antd_error(cl, 400, "Please use a websocket connection");
|
2018-09-23 14:29:43 +02:00
|
|
|
}
|
2022-08-17 00:39:53 +02:00
|
|
|
task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
2018-10-09 17:24:44 +02:00
|
|
|
return task;
|
2018-10-06 19:58:57 +02:00
|
|
|
//LOG("%s\n", "EXIT Streaming..");
|
2018-09-26 11:10:59 +02:00
|
|
|
}
|
|
|
|
void init()
|
|
|
|
{
|
2018-10-06 19:58:57 +02:00
|
|
|
}
|
|
|
|
void destroy()
|
|
|
|
{
|
2018-09-19 15:10:48 +02:00
|
|
|
}
|