diff --git a/src/kit/shell/inc/shell.h b/src/kit/shell/inc/shell.h index d85354c1019aab68f546469fade7aa751cb47415..5c48728d72793567c138fd6040aadbe13cac74d0 100644 --- a/src/kit/shell/inc/shell.h +++ b/src/kit/shell/inc/shell.h @@ -70,6 +70,9 @@ typedef struct SShellArguments { char* netTestRole; char* cloudDsn; bool cloud; + char* cloudHost; + char* cloudPort; + char* cloudToken; } SShellArguments; typedef enum WS_ACTION_TYPE_S { WS_CONN, WS_QUERY, WS_FETCH, WS_FETCH_BLOCK } WS_ACTION_TYPE; @@ -91,7 +94,6 @@ void shellCheck(TAOS* con, SShellArguments* args); void get_history_path(char* history); void shellCheck(TAOS* con, SShellArguments* args); void cleanup_handler(void* arg); -char *last_strstr(const char *haystack, const char *needle); void exitShell(); int shellDumpResult(TAOS_RES* con, char* fname, int* error_no, bool printMode); void shellGetGrantInfo(void* con); @@ -99,7 +101,8 @@ int isCommentLine(char* line); int wsclient_handshake(); int wsclient_conn(); void wsclient_query(char* command); -int tcpConnect(); +int tcpConnect(char* host, int port); +int parse_cloud_dsn(); /**************** Global variable declarations ****************/ extern char PROMPT_HEADER[]; diff --git a/src/kit/shell/src/shellDarwin.c b/src/kit/shell/src/shellDarwin.c index d73970123aff65f9ed66b37a5803b1ccc9a4eb7f..3866b1166bc7924d89dbe022d1770dc3d08565b2 100644 --- a/src/kit/shell/src/shellDarwin.c +++ b/src/kit/shell/src/shellDarwin.c @@ -579,23 +579,23 @@ void exitShell() { exit(EXIT_SUCCESS); } -int tcpConnect() { +int tcpConnect(char* host, int port) { struct sockaddr_in serv_addr; - if (args.port == 0) { - args.port = 6041; + if (port == 0) { + port = 6041; } - if (NULL == args.host) { - args.host = "localhost"; + if (NULL == host) { + host = "localhost"; } - struct hostent *server = gethostbyname(args.host); + struct hostent *server = gethostbyname(host); if ((server == NULL) || (server->h_addr == NULL)) { - fprintf(stderr, "no such host: %s\n", args.host); + fprintf(stderr, "no such host: %s\n", host); return -1; } memset(&serv_addr, 0, sizeof(struct sockaddr_in)); serv_addr.sin_family = AF_INET; - serv_addr.sin_port = htons(args.port); + serv_addr.sin_port = htons(port); memcpy(&(serv_addr.sin_addr.s_addr), server->h_addr, server->h_length); args.socket = socket(AF_INET, SOCK_STREAM, 0); if (args.socket < 0) { diff --git a/src/kit/shell/src/shellEngine.c b/src/kit/shell/src/shellEngine.c index b099f71fd6e22505e2c6d8d8cd279d365f8ad931..0d9176fd1396f540ce44ecfa0bbe6d337a39b1c8 100644 --- a/src/kit/shell/src/shellEngine.c +++ b/src/kit/shell/src/shellEngine.c @@ -1133,18 +1133,39 @@ int taos_base64_encode(unsigned char *source, size_t sourcelen, char *target, si return 1; } -char *last_strstr(const char *haystack, const char *needle) { - if (*needle == '\0') - return (char *) haystack; - - char *res = NULL; - for (;;) { - char *p = strstr(haystack, needle); - if (p == NULL) break; - res = p; - haystack = p + 1; +int parse_cloud_dsn() { + if (args.cloudDsn == NULL) { + fprintf(stderr, "Cannot read cloud service info\n"); + return 1; + } else { + char *start = strstr(args.cloudDsn, "http://"); + if (start != NULL) { + args.cloudHost = start + strlen("http://"); + } else { + start = strstr(args.cloudDsn, "https://"); + if (start != NULL) { + args.cloudHost = start + strlen("https://"); + } else { + args.cloudHost = args.cloudDsn; + } + } + char *port = strstr(args.cloudHost, ":"); + if ((port == NULL) || (port + strlen(":")) == NULL) { + fprintf(stderr, "Invalid format in TDengine cloud dsn: %s\n", args.cloudDsn); + return 1; + } + char *token = strstr(port + strlen(":"), "?token="); + if ((token == NULL) || (token + strlen("?token=")) == NULL || + (strlen(token + strlen("?token=")) == 0)) { + fprintf(stderr, "Invalid format in TDengine cloud dsn: %s\n", args.cloudDsn); + return -1; + } + port[0] = '\0'; + args.cloudPort = port + strlen(":"); + token[0] = '\0'; + args.cloudToken = token + strlen("?token="); } - return res; + return 0; } int wsclient_handshake() { @@ -1161,38 +1182,11 @@ int wsclient_handshake() { } taos_base64_encode(key_nonce, 16, websocket_key, 256); if (args.cloud) { - if (args.cloudDsn == NULL) { - fprintf(stderr, "Cannot read cloud service info\n"); - return -1; - } else { - char* start = strstr(args.cloudDsn, "http://"); - if (start != NULL) { - args.cloudDsn = start + strlen("http://"); - } else { - start = strstr(args.cloudDsn, "https://"); - if (start != NULL) { - args.cloudDsn = start + strlen("https://"); - } - } - char* port = strstr(args.cloudDsn, ":"); - if ((port == NULL) || (port + 1) == NULL) { - fprintf(stderr, "Invalid format in TDengine cloud dsn: %s\n", args.cloudDsn); - return -1; - } - port[0] = '\0'; - char* token = strstr(port+ strlen(":"), "?token="); - if ((token == NULL) || (token + strlen("?token=")) == NULL) { - fprintf(stderr, "Invalid format in TDengine cloud dsn: %s\n", args.cloudDsn); - return -1; - } - token[0] = '\0'; snprintf(request_header, 1024, "GET /rest/ws?token=%s HTTP/1.1\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nHost: " "%s:%s\r\nSec-WebSocket-Key: " "%s\r\nSec-WebSocket-Version: 13\r\n\r\n", - token + strlen("?token="), args.cloudDsn, port + strlen(":"), websocket_key); - } - + args.cloudToken, args.cloudHost, args.cloudPort, websocket_key); } else { snprintf(request_header, 1024, "GET /rest/ws HTTP/1.1\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nHost: %s:%d\r\nSec-WebSocket-Key: " @@ -1349,9 +1343,9 @@ int wsclient_conn() { if (code->valueint == 0) { cJSON_Delete(root); if (args.cloud) { - fprintf(stdout, "Successfully connect to cloud service in restful mode\n"); + fprintf(stdout, "Successfully connect to %s:%s in restful mode\n\n", args.cloudHost, args.cloudPort); } else { - fprintf(stdout, "Successfully connect to %s in restful mode\n", args.host); + fprintf(stdout, "Successfully connect to %s:%d in restful mode\n\n", args.host, args.port); } return 0; diff --git a/src/kit/shell/src/shellLinux.c b/src/kit/shell/src/shellLinux.c index 9843a24dadb0dd753c66c89ba2c6e94a24da2673..b2bfaceeb38ff011dab79e204aa6bdd572bfcccd 100644 --- a/src/kit/shell/src/shellLinux.c +++ b/src/kit/shell/src/shellLinux.c @@ -609,23 +609,23 @@ void exitShell() { exit(EXIT_SUCCESS); } -int tcpConnect() { +int tcpConnect(char* host, int port) { struct sockaddr_in serv_addr; - if (args.port == 0) { - args.port = 6041; + if (port == 0) { + port = 6041; } - if (NULL == args.host) { - args.host = "localhost"; + if (NULL == host) { + host = "localhost"; } - struct hostent *server = gethostbyname(args.host); + struct hostent *server = gethostbyname(host); if ((server == NULL) || (server->h_addr == NULL)) { - fprintf(stderr, "no such host: %s\n", args.host); + fprintf(stderr, "no such host: %s\n", host); return -1; } memset(&serv_addr, 0, sizeof(struct sockaddr_in)); serv_addr.sin_family = AF_INET; - serv_addr.sin_port = htons(args.port); + serv_addr.sin_port = htons(port); memcpy(&(serv_addr.sin_addr.s_addr), server->h_addr, server->h_length); args.socket = socket(AF_INET, SOCK_STREAM, 0); if (args.socket < 0) { diff --git a/src/kit/shell/src/shellMain.c b/src/kit/shell/src/shellMain.c index 6fc27dde6aef17dc7f9f62d3eb1705257a42e116..05ffb8b4d1adc6596cdcc17a0667c3425b125710 100644 --- a/src/kit/shell/src/shellMain.c +++ b/src/kit/shell/src/shellMain.c @@ -89,6 +89,9 @@ SShellArguments args = {.host = NULL, .netTestRole = NULL, .cloudDsn = NULL, .cloud = true, + .cloudHost = NULL, + .cloudPort = NULL, + .cloudToken = NULL, }; /* @@ -128,10 +131,17 @@ int main(int argc, char* argv[]) { exit(0); } - if (args.restful || args.cloud) { - if (tcpConnect()) { - exit(EXIT_FAILURE); - } + if (args.cloud) { + if (parse_cloud_dsn()) { + exit(EXIT_FAILURE); + } + if (tcpConnect(args.cloudHost, atoi(args.cloudPort))) { + exit(EXIT_FAILURE); + } + } else if (args.restful) { + if (tcpConnect(args.host, args.port)) { + exit(EXIT_FAILURE); + } } /* Initialize the shell */ diff --git a/src/kit/shell/src/shellWindows.c b/src/kit/shell/src/shellWindows.c index 2fdbcfd5c2fbea3d36d0ba85b720d0e334589e94..0578611cd74bf9259f88df47682db259d791c824 100644 --- a/src/kit/shell/src/shellWindows.c +++ b/src/kit/shell/src/shellWindows.c @@ -369,21 +369,21 @@ void get_history_path(char *history) { void exitShell() { exit(EXIT_SUCCESS); } -int tcpConnect() { +int tcpConnect(char* host, int iport) { int iResult; WSADATA wsaData; struct addrinfo *aResult = NULL, *ptr = NULL, hints; - if (args.port == 0) { - args.port = 6041; + if (iport == 0) { + iport = 6041; } - if (NULL == args.host) { - args.host = "localhost"; + if (NULL == host) { + host = "localhost"; } char port[10] = {0}; - sprintf_s(port, 10, "%d", args.port); + sprintf_s(port, 10, "%d", iport); iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != 0) { @@ -394,7 +394,7 @@ int tcpConnect() { hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; - iResult = getaddrinfo(args.host, port, &hints, &aResult); + iResult = getaddrinfo(host, port, &hints, &aResult); if ( iResult != 0 ) { printf("getaddrinfo failed with error: %d\n", iResult); WSACleanup();