Skip to content

Commit

Permalink
Add support for the --browser option
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Feb 5, 2017
1 parent 82148cd commit e4c4fea
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Expand Up @@ -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})
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion src/server.c
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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] = "";
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions src/utils.c
Expand Up @@ -7,6 +7,11 @@
#include <string.h>
#include <signal.h>

#if defined(_WIN32) || defined(__CYGWIN__)
#include <windows.h>
#include <shellapi.h>
#endif

void *
xmalloc(size_t size) {
if (size == 0)
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/utils.h
Expand Up @@ -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);
Expand Down

0 comments on commit e4c4fea

Please sign in to comment.