mirror of
https://github.com/tsl0922/ttyd.git
synced 2025-12-30 15:34:24 +01:00
Add support for the --browser option
This commit is contained in:
13
src/server.c
13
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
|
||||
|
||||
23
src/utils.c
23
src/utils.c
@@ -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)
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user