From: Shuanglei Tao Date: Sun, 5 Feb 2017 09:36:19 +0000 (+0800) Subject: Add support for the --browser option X-Git-Url: http://git.prime8.dev/?a=commitdiff_plain;h=e4c4fea1cb973c5a00405097ae15df2d85bbe005;p=ttyd.git Add support for the --browser option --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 71714fe..3eee8ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,6 +55,10 @@ list(APPEND SOURCE_FILES html.h) set(INCLUDE_DIRS ${OPENSSL_INCLUDE_DIR} ${LIBWEBSOCKETS_INCLUDE_DIR} ${JSON-C_INCLUDE_DIR}) set(LINK_LIBS util pthread ${OPENSSL_LIBRARIES} ${LIBWEBSOCKETS_LIBRARIES} ${JSON-C_LIBRARY}) +if(WIN32) + list(APPEND LINK_LIBS shell32) +endif() + add_executable(${PROJECT_NAME} ${SOURCE_FILES}) target_include_directories(${PROJECT_NAME} PUBLIC ${INCLUDE_DIRS}) target_link_libraries(${PROJECT_NAME} ${LINK_LIBS}) diff --git a/README.md b/README.md index 9700adf..efb7ad7 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ OPTIONS: --client-option, -t Send option to client (format: key=value), repeat to add more options --check-origin, -O Do not allow websocket connection from different origin --once, -o Accept only one client and exit on disconnection + --browser, -B Open terminal with the default system browser --index, -I Custom index.html path --ssl, -S Enable SSL --ssl-cert, -C SSL certificate file path diff --git a/src/server.c b/src/server.c index 367ced9..8979012 100644 --- a/src/server.c +++ b/src/server.c @@ -37,12 +37,13 @@ static const struct option options[] = { {"readonly", no_argument, NULL, 'R'}, {"check-origin", no_argument, NULL, 'O'}, {"once", no_argument, NULL, 'o'}, + {"browser", no_argument, NULL, 'B'}, {"debug", required_argument, NULL, 'd'}, {"version", no_argument, NULL, 'v'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, 0, 0} }; -static const char *opt_string = "p:i:c:u:g:s:r:I:aSC:K:A:Rt:Ood:vh"; +static const char *opt_string = "p:i:c:u:g:s:r:I:aSC:K:A:Rt:OoBd:vh"; void print_help() { fprintf(stderr, "ttyd is a tool for sharing terminal over the web\n\n" @@ -62,6 +63,7 @@ void print_help() { " --client-option, -t Send option to client (format: key=value), repeat to add more options\n" " --check-origin, -O Do not allow websocket connection from different origin\n" " --once, -o Accept only one client and exit on disconnection\n" + " --browser, -B Open terminal with the default system browser\n" " --index, -I Custom index.html path\n" " --ssl, -S Enable SSL\n" " --ssl-cert, -C SSL certificate file path\n" @@ -218,6 +220,7 @@ main(int argc, char **argv) { int debug_level = LLL_ERR | LLL_WARN | LLL_NOTICE; char iface[128] = ""; + bool browser = false; bool ssl = false; char cert_path[1024] = ""; char key_path[1024] = ""; @@ -247,6 +250,9 @@ main(int argc, char **argv) { case 'o': server->once = true; break; + case 'B': + browser = true; + break; case 'p': info.port = atoi(optarg); if (info.port < 0) { @@ -426,6 +432,11 @@ main(int argc, char **argv) { lwsl_notice("listening on socket %s\n", server->socket_path); } else { lwsl_notice("listening on port %d\n", info.port); + if (browser) { + char url[30]; + sprintf(url, "%s://localhost:%d", ssl ? "https" : "http", info.port); + open_uri(url); + } } // libwebsockets main loop diff --git a/src/utils.c b/src/utils.c index 180d495..b0bef4c 100644 --- a/src/utils.c +++ b/src/utils.c @@ -7,6 +7,11 @@ #include #include +#if defined(_WIN32) || defined(__CYGWIN__) +#include +#include +#endif + void * xmalloc(size_t size) { if (size == 0) @@ -63,6 +68,24 @@ get_sig(const char *sig_name) { return -1; } +int +open_uri(char *uri) { +#ifdef __APPLE__ + char command[256]; + sprintf(command, "open %s > /dev/null 2>&1", uri); + return system(command); +#elif defined(_WIN32) || defined(__CYGWIN__) + return ShellExecute(0, 0, uri, 0, 0 , SW_SHOW) > 32 ? 0 : 1; +#else + // check if X server is running + if (system("xset -q > /dev/null 2>&1")) + return 1; + char command[256]; + sprintf(command, "xdg-open %s > /dev/null 2>&1", uri); + return system(command); +#endif +} + // https://github.com/darkk/redsocks/blob/master/base64.c char * base64_encode(const unsigned char *buffer, size_t length) { diff --git a/src/utils.h b/src/utils.h index ba75252..7ac0008 100644 --- a/src/utils.h +++ b/src/utils.h @@ -25,6 +25,10 @@ get_sig_name(int sig, char *buf); int get_sig(const char *sig_name); +// Open uri with the default application of system +int +open_uri(char *uri); + // Encode text to base64, the caller should free the returned string char * base64_encode(const unsigned char *buffer, size_t length);