未验证 提交 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 {
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[];
......
......@@ -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) {
......
......@@ -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;
......
......@@ -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) {
......
......@@ -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 */
......
......@@ -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();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册