Skip to content

Commit 2d53cfe

Browse files
committed
Initial commit
0 parents  commit 2d53cfe

File tree

10 files changed

+1409
-0
lines changed

10 files changed

+1409
-0
lines changed

.gitignore

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Object files
5+
*.o
6+
*.ko
7+
*.obj
8+
*.elf
9+
10+
# Precompiled Headers
11+
*.gch
12+
*.pch
13+
14+
# Libraries
15+
*.lib
16+
*.a
17+
*.la
18+
*.lo
19+
20+
# Shared objects (inc. Windows DLLs)
21+
*.dll
22+
*.so
23+
*.so.*
24+
*.dylib
25+
26+
# Executables
27+
*.exe
28+
*.out
29+
*.app
30+
*.i*86
31+
*.x86_64
32+
*.hex
33+
34+
# Debug files
35+
*.dSYM/
36+
*.su
37+
38+
# Cmake files
39+
CMakeCache.txt
40+
CMakeFiles
41+
CMakeScripts
42+
Makefile
43+
cmake_install.cmake
44+
install_manifest.txt
45+
CTestTestfile.cmake
46+
build
47+
48+
# Clion files
49+
.idea/
50+
51+
# Project files
52+
html.h

CMakeLists.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
cmake_minimum_required(VERSION 2.8)
2+
project(ttyd)
3+
4+
set(SOURCE_FILES server.c http.c protocol.c)
5+
6+
find_package(OpenSSL REQUIRED)
7+
find_package(Libwebsockets QUIET)
8+
9+
# libwebsockets 1.x doesn't support cmake
10+
if (NOT Libwebsockets_DIR)
11+
pkg_check_modules(Libwebsockets REQUIRED libwebsockets)
12+
find_path(LIBWEBSOCKETS_INCLUDE_DIR libwebsockets.h
13+
HINTS ${LIBWEBSOCKETS_INCLUDEDIR} ${LIBWEBSOCKETS_INCLUDE_DIRS})
14+
find_library(LIBWEBSOCKETS_LIBRARIES NAMES websockets libwebsockets
15+
HINTS ${LIBWEBSOCKETS_LIBDIR} ${LIBWEBSOCKETS_LIBRARY_DIRS})
16+
include(FindPackageHandleStandardArgs)
17+
find_package_handle_standard_args(LIBWEBSOCKETS DEFAULT_MSG LIBWEBSOCKETS_LIBRARIES LIBWEBSOCKETS_INCLUDE_DIR)
18+
mark_as_advanced(LIBWEBSOCKETS_INCLUDE_DIR LIBWEBSOCKETS_LIBRARIES)
19+
endif()
20+
21+
find_package(PkgConfig)
22+
pkg_check_modules(PC_JSON-C REQUIRED json-c)
23+
find_path(JSON-C_INCLUDE_DIR json.h
24+
HINTS ${PC_JSON-C_INCLUDEDIR} ${PC_JSON-C_INCLUDE_DIRS} PATH_SUFFIXES json-c json)
25+
find_library(JSON-C_LIBRARY NAMES json-c libjson-c
26+
HINTS ${PC_JSON-C_LIBDIR} ${PC_JSON-C_LIBRARY_DIRS})
27+
include(FindPackageHandleStandardArgs)
28+
find_package_handle_standard_args(JSON-C DEFAULT_MSG JSON-C_LIBRARY JSON-C_INCLUDE_DIR)
29+
mark_as_advanced(JSON-C_INCLUDE_DIR JSON-C_LIBRARY)
30+
31+
find_program(CMAKE_XXD NAMES xxd)
32+
add_custom_command(OUTPUT html.h
33+
COMMAND ${CMAKE_XXD} -i index.html html.h
34+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
35+
COMMENT "Generating html.h from index.html")
36+
list(APPEND SOURCE_FILES html.h)
37+
38+
set(INCLUDE_DIRS ${OPENSSL_INCLUDE_DIR} ${LIBWEBSOCKETS_INCLUDE_DIR} ${JSON-C_INCLUDE_DIR})
39+
set(LINK_LIBS util pthread ${OPENSSL_LIBRARIES} ${LIBWEBSOCKETS_LIBRARIES} ${JSON-C_LIBRARY})
40+
41+
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
42+
target_include_directories(${PROJECT_NAME} PUBLIC ${INCLUDE_DIRS})
43+
target_link_libraries(${PROJECT_NAME} ${LINK_LIBS})

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 Shuanglei Tao <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# ttyd - terminal emulator for the web
2+
3+
ttyd is a simple command line tool for sharing terminal over the web, inspired by [GoTTY](https://github.com/yudai/gotty).
4+
5+
![screenshot](screenshot.gif)
6+
7+
> **WARNING:** ttyd is still under heavily development, so features may be incomplete or expected to have bugs.
8+
9+
# Requirements
10+
11+
- [CMake](https://cmake.org)
12+
- [OpenSSL](https://www.openssl.org)
13+
- [JSON-C](https://github.com/json-c/json-c)
14+
- [Libwebsockets](https://libwebsockets.org)
15+
16+
# Installation
17+
18+
### For Mac OS X users
19+
20+
```bash
21+
brew install cmake openssl json-c libwebsockets
22+
git clone https://github.com/tsl0922/ttyd.git
23+
cd ttyd && mkdir build && cd build
24+
cmake -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl ..
25+
make
26+
```
27+
28+
### For Linux users
29+
30+
Ubuntu as example:
31+
32+
```bash
33+
sudo apt-get install cmake libwebsockets-dev libjson-c-dev libssl-dev
34+
git clone https://github.com/tsl0922/ttyd.git
35+
cd ttyd && mkdir build && cd build
36+
cmake ..
37+
make
38+
```
39+
40+
The `ttyd` executable file will be in the `build` directory.
41+
42+
# Usage
43+
44+
```
45+
Usage: ttyd command [options]
46+
```
47+
48+
ttyd will start a web server at port `7681`. When you open <http://localhost:7681>, the `command` will be started with `options` as arguments and now you can see the running command on the web! :tada:
49+
50+
# Credits
51+
52+
- [GoTTY](https://github.com/yudai/gotty): ttyd is a port of GoTTY to `C` language.
53+
- [hterm](https://chromium.googlesource.com/apps/libapps/+/HEAD/hterm): ttyd uses hterm to run a terminal emulator on the web.

http.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "server.h"
2+
#include "html.h"
3+
4+
int
5+
callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) {
6+
unsigned char buffer[4096 + LWS_PRE];
7+
unsigned char *p;
8+
unsigned char *end;
9+
char buf[256];
10+
int n;
11+
12+
switch (reason) {
13+
case LWS_CALLBACK_HTTP:
14+
lwsl_notice("lws_http_serve: %s\n", in);
15+
16+
{
17+
char name[100], rip[50];
18+
lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name,
19+
sizeof(name), rip, sizeof(rip));
20+
sprintf(buf, "%s (%s)", name, rip);
21+
lwsl_notice("HTTP connect from %s\n", buf);
22+
}
23+
24+
if (len < 1) {
25+
lws_return_http_status(wsi, HTTP_STATUS_BAD_REQUEST, NULL);
26+
goto try_to_reuse;
27+
}
28+
29+
if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI))
30+
return 0;
31+
32+
if (strcmp((const char *) in, "/")) {
33+
lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL);
34+
goto try_to_reuse;
35+
}
36+
37+
p = buffer + LWS_PRE;
38+
end = p + sizeof(buffer) - LWS_PRE;
39+
40+
if (lws_add_http_header_status(wsi, 200, &p, end))
41+
return 1;
42+
if (lws_add_http_header_by_token(wsi,
43+
WSI_TOKEN_HTTP_CONTENT_TYPE,
44+
(unsigned char *) "text/html",
45+
9, &p, end))
46+
return 1;
47+
if (lws_add_http_header_content_length(wsi, index_html_len, &p, end))
48+
return 1;
49+
if (lws_finalize_http_header(wsi, &p, end))
50+
return 1;
51+
n = lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE), LWS_WRITE_HTTP_HEADERS);
52+
if (n < 0) {
53+
return 1;
54+
}
55+
56+
n = lws_write_http(wsi, index_html, index_html_len);
57+
if (n < 0)
58+
return 1;
59+
goto try_to_reuse;
60+
case LWS_CALLBACK_HTTP_WRITEABLE:
61+
lwsl_info("LWS_CALLBACK_HTTP_WRITEABLE\n");
62+
break;
63+
default:
64+
break;
65+
}
66+
67+
return 0;
68+
69+
/* if we're on HTTP1.1 or 2.0, will keep the idle connection alive */
70+
try_to_reuse:
71+
if (lws_http_transaction_completed(wsi))
72+
return -1;
73+
74+
return 0;
75+
}

0 commit comments

Comments
 (0)