未验证 提交 db2c4364 编写于 作者: Y Yang Zhao 提交者: GitHub

enh: taos shell support cloud (#13879)

* enh: support url in commandline taos shell

* fix: normal host logic

* enh: add default -R if env var set

* fix: refine taos shell cloud service

* fix: improve user friendly

* fix: add cloud tcp connection

* fix: header file
上级 1739712f
...@@ -70,6 +70,9 @@ typedef struct SShellArguments { ...@@ -70,6 +70,9 @@ typedef struct SShellArguments {
char* netTestRole; char* netTestRole;
char* cloudDsn; char* cloudDsn;
bool cloud; bool cloud;
char* cloudHost;
char* cloudPort;
char* cloudToken;
} SShellArguments; } SShellArguments;
typedef enum WS_ACTION_TYPE_S { WS_CONN, WS_QUERY, WS_FETCH, WS_FETCH_BLOCK } WS_ACTION_TYPE; 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); ...@@ -91,7 +94,6 @@ void shellCheck(TAOS* con, SShellArguments* args);
void get_history_path(char* history); void get_history_path(char* history);
void shellCheck(TAOS* con, SShellArguments* args); void shellCheck(TAOS* con, SShellArguments* args);
void cleanup_handler(void* arg); void cleanup_handler(void* arg);
char *last_strstr(const char *haystack, const char *needle);
void exitShell(); void exitShell();
int shellDumpResult(TAOS_RES* con, char* fname, int* error_no, bool printMode); int shellDumpResult(TAOS_RES* con, char* fname, int* error_no, bool printMode);
void shellGetGrantInfo(void* con); void shellGetGrantInfo(void* con);
...@@ -99,7 +101,8 @@ int isCommentLine(char* line); ...@@ -99,7 +101,8 @@ int isCommentLine(char* line);
int wsclient_handshake(); int wsclient_handshake();
int wsclient_conn(); int wsclient_conn();
void wsclient_query(char* command); void wsclient_query(char* command);
int tcpConnect(); int tcpConnect(char* host, int port);
int parse_cloud_dsn();
/**************** Global variable declarations ****************/ /**************** Global variable declarations ****************/
extern char PROMPT_HEADER[]; extern char PROMPT_HEADER[];
......
...@@ -579,23 +579,23 @@ void exitShell() { ...@@ -579,23 +579,23 @@ void exitShell() {
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
int tcpConnect() { int tcpConnect(char* host, int port) {
struct sockaddr_in serv_addr; struct sockaddr_in serv_addr;
if (args.port == 0) { if (port == 0) {
args.port = 6041; port = 6041;
} }
if (NULL == args.host) { if (NULL == host) {
args.host = "localhost"; host = "localhost";
} }
struct hostent *server = gethostbyname(args.host); struct hostent *server = gethostbyname(host);
if ((server == NULL) || (server->h_addr == NULL)) { 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; return -1;
} }
memset(&serv_addr, 0, sizeof(struct sockaddr_in)); memset(&serv_addr, 0, sizeof(struct sockaddr_in));
serv_addr.sin_family = AF_INET; 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); memcpy(&(serv_addr.sin_addr.s_addr), server->h_addr, server->h_length);
args.socket = socket(AF_INET, SOCK_STREAM, 0); args.socket = socket(AF_INET, SOCK_STREAM, 0);
if (args.socket < 0) { if (args.socket < 0) {
......
...@@ -1133,18 +1133,39 @@ int taos_base64_encode(unsigned char *source, size_t sourcelen, char *target, si ...@@ -1133,18 +1133,39 @@ int taos_base64_encode(unsigned char *source, size_t sourcelen, char *target, si
return 1; return 1;
} }
char *last_strstr(const char *haystack, const char *needle) { int parse_cloud_dsn() {
if (*needle == '\0') if (args.cloudDsn == NULL) {
return (char *) haystack; fprintf(stderr, "Cannot read cloud service info\n");
return 1;
char *res = NULL; } else {
for (;;) { char *start = strstr(args.cloudDsn, "http://");
char *p = strstr(haystack, needle); if (start != NULL) {
if (p == NULL) break; args.cloudHost = start + strlen("http://");
res = p; } else {
haystack = p + 1; start = strstr(args.cloudDsn, "https://");
if (start != NULL) {
args.cloudHost = start + strlen("https://");
} else {
args.cloudHost = args.cloudDsn;
} }
return res; }
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 0;
} }
int wsclient_handshake() { int wsclient_handshake() {
...@@ -1161,38 +1182,11 @@ int wsclient_handshake() { ...@@ -1161,38 +1182,11 @@ int wsclient_handshake() {
} }
taos_base64_encode(key_nonce, 16, websocket_key, 256); taos_base64_encode(key_nonce, 16, websocket_key, 256);
if (args.cloud) { 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, snprintf(request_header, 1024,
"GET /rest/ws?token=%s HTTP/1.1\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nHost: " "GET /rest/ws?token=%s HTTP/1.1\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nHost: "
"%s:%s\r\nSec-WebSocket-Key: " "%s:%s\r\nSec-WebSocket-Key: "
"%s\r\nSec-WebSocket-Version: 13\r\n\r\n", "%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 { } else {
snprintf(request_header, 1024, snprintf(request_header, 1024,
"GET /rest/ws HTTP/1.1\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nHost: %s:%d\r\nSec-WebSocket-Key: " "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() { ...@@ -1349,9 +1343,9 @@ int wsclient_conn() {
if (code->valueint == 0) { if (code->valueint == 0) {
cJSON_Delete(root); cJSON_Delete(root);
if (args.cloud) { 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 { } 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; return 0;
......
...@@ -609,23 +609,23 @@ void exitShell() { ...@@ -609,23 +609,23 @@ void exitShell() {
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
int tcpConnect() { int tcpConnect(char* host, int port) {
struct sockaddr_in serv_addr; struct sockaddr_in serv_addr;
if (args.port == 0) { if (port == 0) {
args.port = 6041; port = 6041;
} }
if (NULL == args.host) { if (NULL == host) {
args.host = "localhost"; host = "localhost";
} }
struct hostent *server = gethostbyname(args.host); struct hostent *server = gethostbyname(host);
if ((server == NULL) || (server->h_addr == NULL)) { 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; return -1;
} }
memset(&serv_addr, 0, sizeof(struct sockaddr_in)); memset(&serv_addr, 0, sizeof(struct sockaddr_in));
serv_addr.sin_family = AF_INET; 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); memcpy(&(serv_addr.sin_addr.s_addr), server->h_addr, server->h_length);
args.socket = socket(AF_INET, SOCK_STREAM, 0); args.socket = socket(AF_INET, SOCK_STREAM, 0);
if (args.socket < 0) { if (args.socket < 0) {
......
...@@ -89,6 +89,9 @@ SShellArguments args = {.host = NULL, ...@@ -89,6 +89,9 @@ SShellArguments args = {.host = NULL,
.netTestRole = NULL, .netTestRole = NULL,
.cloudDsn = NULL, .cloudDsn = NULL,
.cloud = true, .cloud = true,
.cloudHost = NULL,
.cloudPort = NULL,
.cloudToken = NULL,
}; };
/* /*
...@@ -128,8 +131,15 @@ int main(int argc, char* argv[]) { ...@@ -128,8 +131,15 @@ int main(int argc, char* argv[]) {
exit(0); exit(0);
} }
if (args.restful || args.cloud) { if (args.cloud) {
if (tcpConnect()) { 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); exit(EXIT_FAILURE);
} }
} }
......
...@@ -369,21 +369,21 @@ void get_history_path(char *history) { ...@@ -369,21 +369,21 @@ void get_history_path(char *history) {
void exitShell() { exit(EXIT_SUCCESS); } void exitShell() { exit(EXIT_SUCCESS); }
int tcpConnect() { int tcpConnect(char* host, int iport) {
int iResult; int iResult;
WSADATA wsaData; WSADATA wsaData;
struct addrinfo *aResult = NULL, struct addrinfo *aResult = NULL,
*ptr = NULL, *ptr = NULL,
hints; hints;
if (args.port == 0) { if (iport == 0) {
args.port = 6041; iport = 6041;
} }
if (NULL == args.host) { if (NULL == host) {
args.host = "localhost"; host = "localhost";
} }
char port[10] = {0}; char port[10] = {0};
sprintf_s(port, 10, "%d", args.port); sprintf_s(port, 10, "%d", iport);
iResult = WSAStartup(MAKEWORD(2,2), &wsaData); iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) { if (iResult != 0) {
...@@ -394,7 +394,7 @@ int tcpConnect() { ...@@ -394,7 +394,7 @@ int tcpConnect() {
hints.ai_family = AF_UNSPEC; hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP; hints.ai_protocol = IPPROTO_TCP;
iResult = getaddrinfo(args.host, port, &hints, &aResult); iResult = getaddrinfo(host, port, &hints, &aResult);
if ( iResult != 0 ) { if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult); printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup(); WSACleanup();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册