fix websocket problem via ssh

This commit is contained in:
Xuan Sang LE 2018-02-10 13:44:25 +01:00
parent 708e54989d
commit 902d4ce494
6 changed files with 61 additions and 92 deletions

View File

@ -183,14 +183,10 @@ void catb(void* client, FILE* ptr)
void cat(void* client, FILE *resource)
{
char buf[1024];
int _ssl = 0;
#ifdef USE_OPENSSL
_ssl = usessl();
#endif
//fgets(buf, sizeof(buf), resource);
while (fgets(buf, sizeof(buf), resource) != NULL)
{
antd_send(client, buf, strlen(buf), _ssl);
antd_send(client, buf, strlen(buf));
//fgets(buf, sizeof(buf), resource);
}
@ -387,11 +383,7 @@ char* post_url_decode(void* client,int len)
{
char *query = (char*) malloc((len+1)*sizeof(char));
for (int i = 0; i < len; i++) {
#ifdef USE_OPENSSL
antd_recv(client, (query+i), 1, server_config.usessl);
#else
antd_recv(client, (query+i), 1, 0);
#endif
antd_recv(client, (query+i), 1);
}
query[len]='\0';
//query = url_decode(query);
@ -581,9 +573,7 @@ void ws_confirm_request(void* client, const char* key)
strcpy(rkey,key);
strcat(rkey,WS_MAGIC_STRING);
//printf("RESPONDKEY '%s'\n", rkey);
int _ssl = 0;
#ifdef USE_OPENSSL
_ssl = usessl();
SHA_CTX context;
#else
SHA1_CTX context;
@ -596,15 +586,15 @@ int _ssl = 0;
//printf("Base 64 '%s'\n", base64);
// send accept to client
sprintf(buf, "HTTP/1.1 101 Switching Protocols\r\n");
antd_send(client, buf, strlen(buf), _ssl);
antd_send(client, buf, strlen(buf));
sprintf(buf, "Upgrade: websocket\r\n");
antd_send(client, buf, strlen(buf), _ssl);
antd_send(client, buf, strlen(buf));
sprintf(buf, "Connection: Upgrade\r\n");
antd_send(client, buf, strlen(buf), _ssl);
antd_send(client, buf, strlen(buf));
sprintf(buf, "Sec-WebSocket-Accept: %s\r\n",base64);
antd_send(client, buf, strlen(buf), _ssl);
antd_send(client, buf, strlen(buf));
sprintf(buf, "\r\n");
antd_send(client, buf, strlen(buf), _ssl);
antd_send(client, buf, strlen(buf));
LOG("%s\n", "Websocket is now enabled for plugin");
}
@ -809,13 +799,9 @@ dictionary decode_url_request(const char* query)
*/
char* json_data_decode(void* client,int len)
{
int _ssl = 0;
#ifdef USE_OPENSSL
_ssl = usessl();
#endif
char *query = (char*) malloc((len+1)*sizeof(char));
for (int i = 0; i < len; i++) {
antd_recv(client, (query+i), 1, _ssl);
antd_recv(client, (query+i), 1);
}
query[len]='\0';
//query = url_decode(query);

View File

@ -61,19 +61,16 @@ int response(void* client, const char* data)
buf[size] = '\r';
buf[size+1] = '\n';
buf[size+2] = '\0';
int _ssl = 0;
#ifdef USE_OPENSSL
_ssl = usessl();
#endif
nbytes = antd_send(client, buf, strlen(buf), _ssl);
nbytes = antd_send(client, buf, strlen(buf));
return (nbytes ==-1?0:1);
}
int antd_send(const void *src, const void* data, int len, int _ssl)
int antd_send(const void *src, const void* data, int len)
{
if(!src) return -1;
antd_client_t * source = (antd_client_t *) src;
#ifdef USE_OPENSSL
if(_ssl)
if(usessl())
{
//LOG("SSL WRITE\n");
return SSL_write((SSL*) source->ssl, data, len);
@ -86,12 +83,12 @@ int antd_send(const void *src, const void* data, int len, int _ssl)
}
#endif
}
int antd_recv(const void *src, void* data, int len, int _ssl)
int antd_recv(const void *src, void* data, int len)
{
if(!src) return -1;
antd_client_t * source = (antd_client_t *) src;
#ifdef USE_OPENSSL
if(_ssl)
if(usessl())
{
//LOG("SSL READ\n");
return SSL_read((SSL*) source->ssl, data, len);
@ -138,10 +135,6 @@ int __t(void* client, const char* fstring,...)
va_start( arguments, fstring);
dlen = vsnprintf(0,0,fstring,arguments) + 1;
va_end(arguments);
int _ssl = 0;
#ifdef USE_OPENSSL
_ssl = usessl();
#endif
if ((data = (char*)malloc(dlen*sizeof(char))) != 0)
{
va_start(arguments, fstring);
@ -164,12 +157,12 @@ int _ssl = 0;
//chunk[buflen-1] = '\0';
//response(client,chunk);
sent += buflen;
nbytes = antd_send(client, chunk, buflen, _ssl);
nbytes = antd_send(client, chunk, buflen);
free(chunk);
if(nbytes == -1) return 0;
}
chunk = "\r\n";
antd_send(client, chunk, strlen(chunk), _ssl);
antd_send(client, chunk, strlen(chunk));
}
free(data);
}
@ -182,13 +175,10 @@ int __b(void* client, const unsigned char* data, int size)
int sent = 0;
int buflen = 0;
int nbytes;
int _ssl = 0;
#ifdef USE_OPENSSL
_ssl = usessl();
#endif
if(size <= BUFFLEN)
{
nbytes = antd_send(client,data,size,_ssl);
nbytes = antd_send(client,data,size);
return (nbytes==-1?0:1);
}
else
@ -200,7 +190,7 @@ int _ssl = 0;
else
buflen = size - sent;
memcpy(buf,data+sent,buflen);
nbytes = antd_send(client,buf,buflen,_ssl);
nbytes = antd_send(client,buf,buflen);
sent += buflen;
if(nbytes == -1) return 0;
}
@ -238,13 +228,9 @@ int __f(void* client, const char* file)
LOG("Cannot read : %s\n", file);
return 0;
}
int _ssl = 0;
#ifdef USE_OPENSSL
_ssl = usessl();
#endif
while(fgets(buf, sizeof(buf), ptr) != NULL)
{
nbytes = antd_send(client, buf, strlen(buf), _ssl);
nbytes = antd_send(client, buf, strlen(buf));
if(nbytes == -1) return 0;
//LOG("READ : %s\n", buf);
//fgets(buf, sizeof(buf), ptr);
@ -312,13 +298,9 @@ int read_buf(void* sock, char*buf,int size)
int i = 0;
char c = '\0';
int n;
int _ssl = 0;
#ifdef USE_OPENSSL
_ssl = usessl();
#endif
while ((i < size - 1) && (c != '\n'))
{
n = antd_recv(sock, &c, 1, _ssl);
n = antd_recv(sock, &c, 1);
if (n > 0)
{
buf[i] = c;

View File

@ -74,7 +74,7 @@ void unknow(void*);
int ws_enable(dictionary);
char* read_line(void* sock);
int read_buf(void* sock,char* buf,int i);
int antd_send(const void *source, const void* data, int len, int usessl);
int antd_recv(const void *source, void* data, int len, int usessl);
int antd_send(const void *source, const void* data, int len);
int antd_recv(const void *source, void* data, int len);
int antd_close(void* source);
#endif

View File

@ -12,7 +12,7 @@ static void ws_gen_mask_key(ws_msg_header_t * header)
* based on this header, we'll decide
* the appropriate handle for frame data
*/
ws_msg_header_t * ws_read_header(int client)
ws_msg_header_t * ws_read_header(void* client)
{
uint8_t byte;
@ -20,7 +20,7 @@ ws_msg_header_t * ws_read_header(int client)
ws_msg_header_t* header = (ws_msg_header_t*) malloc(sizeof(*header));
// get first byte
if(recv(client, &byte, sizeof(byte), 0) <0) goto fail;
if(antd_recv(client, &byte, sizeof(byte)) <0) goto fail;
if(BITV(byte,6) || BITV(byte,5) || BITV(byte,4)) goto fail;// all RSV bit must be 0
//printf("FIN: %d, RSV1: %d, RSV2: %d, RSV3:%d, opcode:%d\n", BITV(byte,7), BITV(byte,6), BITV(byte,5), BITV(byte,4),(byte & 0x0F) );
@ -29,7 +29,7 @@ ws_msg_header_t * ws_read_header(int client)
header->opcode = (byte & 0x0F);
// get next byte
if(recv(client, &byte, sizeof(byte), 0) <0) goto fail;
if(antd_recv(client, &byte, sizeof(byte)) <0) goto fail;
//printf("MASK: %d paylen:%d\n", BITV(byte,7), (byte & 0x7F));
// check mask bit, should be 1
@ -47,19 +47,19 @@ ws_msg_header_t * ws_read_header(int client)
header->plen = len;
} else if(len == 126)
{
if(recv(client,bytes, 2*sizeof(uint8_t), 0) <0) goto fail;
if(antd_recv(client,bytes, 2*sizeof(uint8_t)) <0) goto fail;
header->plen = (bytes[0]<<8) + bytes[1];
} else
{
//read only last 4 byte
if(recv(client,bytes, 8*sizeof(uint8_t), 0) <0) goto fail;
if(antd_recv(client,bytes, 8*sizeof(uint8_t)) <0) goto fail;
header->plen = (bytes[4]<<24) + (bytes[5]<<16) + (bytes[6] << 8) + bytes[7] ;
}
//printf("len: %d\n", header->plen);
// last step is to get the maskey
if(header->mask)
if(recv(client,header->mask_key, 4*sizeof(uint8_t), 0) <0) goto fail;
if(antd_recv(client,header->mask_key, 4*sizeof(uint8_t)) <0) goto fail;
//printf("key 0: %d key 1: %d key2:%d, key3: %d\n",header->mask_key[0],header->mask_key[1],header->mask_key[2], header->mask_key[3] );
// check wheather it is a ping or a close message
@ -91,12 +91,12 @@ ws_msg_header_t * ws_read_header(int client)
* Read data from client
* and unmask data using the key
*/
int ws_read_data(int client, ws_msg_header_t* header, int len, uint8_t* data)
int ws_read_data(void* client, ws_msg_header_t* header, int len, uint8_t* data)
{
// if len == -1 ==> read all remaining data to 'data';
if(header->plen == 0) return 0;
int dlen = (len==-1 || len > header->plen)?header->plen:len;
if((dlen = recv(client,data, dlen, 0)) <0) return -1;
if((dlen = antd_recv(client,data, dlen)) <0) return -1;
header->plen = header->plen - dlen;
// unmask received data
if(header->mask)
@ -105,7 +105,7 @@ int ws_read_data(int client, ws_msg_header_t* header, int len, uint8_t* data)
data[dlen] = '\0';
return dlen;
}
void _send_header(int client, ws_msg_header_t header)
void _send_header(void* client, ws_msg_header_t header)
{
uint8_t byte = 0;
uint8_t bytes[8];
@ -113,7 +113,7 @@ void _send_header(int client, ws_msg_header_t header)
//first byte |FIN|000|opcode|
byte = (header.fin << 7) + header.opcode;
//printf("BYTE: %d\n", byte);
send(client, &byte, 1, 0);
antd_send(client, &byte, 1);
// second byte, payload length
// mask may be 0 or 1
//if(header.mask == 1)
@ -121,15 +121,15 @@ void _send_header(int client, ws_msg_header_t header)
if(header.plen <= 125)
{
byte = (header.mask << 7) + header.plen;
send(client, &byte, 1, 0);
antd_send(client, &byte, 1);
}
else if(header.plen < 65536) // 16 bits
{
byte = (header.mask << 7) + 126;
bytes[0] = (header.plen) >> 8;
bytes[1] = (header.plen) & 0x00FF;
send(client, &byte, 1, 0);
send(client, &bytes, 2, 0);
antd_send(client, &byte, 1);
antd_send(client, &bytes, 2);
}
else // > 16 bits
{
@ -138,19 +138,19 @@ void _send_header(int client, ws_msg_header_t header)
bytes[5] = ((header.plen)>>16) & 0x00FF;
bytes[6] = ((header.plen)>>8) & 0x00FF;
bytes[7] = (header.plen) & 0x00FF;
send(client, &byte, 1, 0);
send(client, &bytes, 8, 0);
antd_send(client, &byte, 1);
antd_send(client, &bytes, 8);
}
// send mask key
if(header.mask)
{
send(client, header.mask_key,4,0);
antd_send(client, header.mask_key,4);
}
}
/**
* Send a frame to client
*/
void ws_send_frame(int client, uint8_t* data, ws_msg_header_t header)
void ws_send_frame(void* client, uint8_t* data, ws_msg_header_t header)
{
uint8_t * masked;
masked = data;
@ -163,16 +163,16 @@ void ws_send_frame(int client, uint8_t* data, ws_msg_header_t header)
}
_send_header(client, header);
if(header.opcode == WS_TEXT)
send(client,(char*)masked,header.plen,0);
antd_send(client,(char*)masked,header.plen);
else
send(client,(uint8_t*)masked,header.plen,0);
antd_send(client,(uint8_t*)masked,header.plen);
if(masked && header.mask)
free(masked);
}
/**
* send a text data frame to client
*/
void ws_send_text(int client, const char* data,int mask)
void ws_send_text(void* client, const char* data,int mask)
{
ws_msg_header_t header;
header.fin = 1;
@ -187,7 +187,7 @@ void ws_send_text(int client, const char* data,int mask)
* send a single binary data fram to client
* not tested yet, but should work
*/
void ws_send_binary(int client, uint8_t* data, int l, int mask)
void ws_send_binary(void* client, uint8_t* data, int l, int mask)
{
ws_msg_header_t header;
header.fin = 1;
@ -201,7 +201,7 @@ void ws_send_binary(int client, uint8_t* data, int l, int mask)
/*
* send a file as binary data
*/
void ws_send_file(int client, const char* file, int mask)
void ws_send_file(void* client, const char* file, int mask)
{
uint8_t buff[1024];
FILE *ptr;
@ -245,7 +245,7 @@ void ws_send_file(int client, const char* file, int mask)
* Not tested yet
* but should work
*/
void pong(int client, int len)
void pong(void* client, int len)
{
//printf("PONG\n");
ws_msg_header_t pheader;
@ -254,7 +254,7 @@ void pong(int client, int len)
pheader.plen = len;
pheader.mask = 0;
uint8_t data[len];
if(recv(client,data, len, 0) < 0) return;
if(antd_recv(client,data, len) < 0) return;
ws_send_frame(client,data,pheader);
//_send_header(client, pheader);
//send(client, data, len, 0);
@ -262,7 +262,7 @@ void pong(int client, int len)
/*
* Not tested yet, but should work
*/
void ws_send_close(int client, unsigned int status, int mask)
void ws_send_close(void* client, unsigned int status, int mask)
{
//printf("CLOSED\n");
ws_msg_header_t header;

View File

@ -37,19 +37,19 @@ typedef struct{
uint8_t mask_key[4];
} ws_msg_header_t;
ws_msg_header_t * ws_read_header(int);
void ws_send_frame(int , uint8_t* , ws_msg_header_t );
void pong(int client, int len);
ws_msg_header_t * ws_read_header(void*);
void ws_send_frame(void* , uint8_t* , ws_msg_header_t );
void pong(void* client, int len);
void ws_send_text(int client, const char* data,int mask);
void ws_send_close(int client, unsigned int status, int mask);
void ws_send_file(int client, const char* file, int mask);
void ws_send_binary(int client, uint8_t* data, int l, int mask);
void ws_send_text(void* client, const char* data,int mask);
void ws_send_close(void* client, unsigned int status, int mask);
void ws_send_file(void* client, const char* file, int mask);
void ws_send_binary(void* client, uint8_t* data, int l, int mask);
int ws_read_data(int , ws_msg_header_t*, int, uint8_t*);
int ws_read_data(void* , ws_msg_header_t*, int, uint8_t*);
int request_socket(const char* ip, int port);
int ip_from_hostname(const char * hostname , char* ip);
int sock_read_buf(int sock, char*buf,int size);
int sock_read_buf(void* sock, char*buf,int size);
int ws_open_hand_shake(const char* host, int port, const char* resource);
char* get_ip_address();
#endif

View File

@ -61,13 +61,14 @@ void handler(void* cl, const char* m, const char* rqp, dictionary rq)
// Close the slave side of the PTY
close(fds);
int max_fdm;
int cl_fd = ((antd_client_t*)cl)->sock;
while (1)
{
FD_ZERO(&fd_in);
//FD_SET(0, &fd_in);
FD_SET(fdm, &fd_in);
FD_SET(cl,&fd_in);
max_fdm = fdm>cl?fdm:cl;
FD_SET(cl_fd,&fd_in);
max_fdm = fdm>cl_fd?fdm:cl_fd;
rc = select(max_fdm + 1, &fd_in, NULL, NULL, NULL);
switch(rc)
{
@ -79,7 +80,7 @@ void handler(void* cl, const char* m, const char* rqp, dictionary rq)
default :
{
// If data is on websocket side
if (FD_ISSET(cl, &fd_in))
if (FD_ISSET(cl_fd, &fd_in))
{
h = ws_read_header(cl);
if(h)