diff --git a/src/agent.c b/src/agent.c index 4174e0f..ebfa05e 100644 --- a/src/agent.c +++ b/src/agent.c @@ -26,10 +26,14 @@ static struct termios ttystate_backup; uv_tty_t agent_stdout_tty; uv_tty_t agent_stdin_tty; static int64_t pending_send = 0; //记录待转发的字节,用于tty 流控 +int max_suggested_size = 10240; void agent_read_stdin(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf); static void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) { + if (suggested_size > max_suggested_size) { + suggested_size = max_suggested_size; + } buf->base = (char *)malloc(suggested_size); buf->len = suggested_size; } @@ -137,20 +141,19 @@ void agent_read_stdin(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) { } static char *data = NULL; - // TODO 动态大小 if (data == NULL) { - data = (char *)malloc(9999); + data = (char *)malloc(2 * max_suggested_size); } static int data_size = 0; - CHECK(nread < 9999, "nread<9999"); + CHECK(nread < max_suggested_size, "nread<2*max_suggested_size"); memcpy(data + data_size, buf->base, nread); data_size += nread; - CHECK(data_size < 9999, "data_size>=9999"); + CHECK(data_size < max_suggested_size, "data_size>=max_suggested_size"); int used_data_size = process_stdin(data, data_size); int unused_data_size = data_size - used_data_size; if (unused_data_size > 0) { - CHECK(unused_data_size < 9999, "erorr"); + CHECK(unused_data_size < max_suggested_size, "erorr"); memmove(data, data + used_data_size, unused_data_size); } data_size = unused_data_size; diff --git a/src/entry.c b/src/entry.c index 8a312a0..99dca29 100644 --- a/src/entry.c +++ b/src/entry.c @@ -93,8 +93,8 @@ int main(int argc, const char *argv[]) { #endif log_set_quiet(true); - char *verbose_env_str = getenv("VERBOSE"); - if (getenv("VERBOSE") != NULL) { + char *verbose_env_str = getenv("TERMTUNNEL_VERBOSE"); + if (verbose_env_str != NULL) { int verbose_level = atoi(verbose_env_str); if (verbose_level < 0 || verbose_level > 9) { verbose_level = 0; diff --git a/src/fileexchange.c b/src/fileexchange.c index d3ca4d1..0578ecf 100644 --- a/src/fileexchange.c +++ b/src/fileexchange.c @@ -148,16 +148,6 @@ static int file_send_request(path_exchange_t *pe) { return 0; } -#if 0 -// from libuv -void exchange_notify_handler(uv_async_t *handle) -{ - log_info("exchange_notify_handler\n"); - //comm_write_packet_to_cli(COMMAND_TTY_PING, NULL, 0); - return; -} -#endif 0 - int file_send_start(char *src_path, char *dst_path) { path_exchange_t *pe = (path_exchange_t *)malloc(sizeof(path_exchange_t)); strcpy(pe->src_path, src_path); diff --git a/src/pipe.c b/src/pipe.c index 586e436..3b0155c 100644 --- a/src/pipe.c +++ b/src/pipe.c @@ -362,11 +362,10 @@ static void common_read_tty(uv_stream_t *stream, ssize_t nread, } fsm_append_input(global_fsm_context, buf->base, nread); fsm_run(global_fsm_context); - // TODO:可能因为单frame过大导致2000不够用 - char *dst = (char *)malloc(2000); + char *dst = (char *)malloc(5 * VIR_MTU); int sz = 0; while (true) { - sz = fsm_pop_output(global_fsm_context, dst, 2000); + sz = fsm_pop_output(global_fsm_context, dst, 5 * VIR_MTU); if (sz > 0) { server_handle_green_packet(dst, sz); } else { diff --git a/src/pty.c b/src/pty.c index 54b78ef..a811772 100644 --- a/src/pty.c +++ b/src/pty.c @@ -47,10 +47,8 @@ int get_pty_fd() { return pty_fd; } static int do_exec(const char *name, int ptypt, char **argv, int argc) { struct winsize ttysize; - if (ioctl(myself_fd, TIOCGWINSZ, &ttysize) == 0) { - // pass - // sleep(5); - } else { + if (ioctl(myself_fd, TIOCGWINSZ, &ttysize) != 0) { + // fallback ttysize.ws_col = 20; ttysize.ws_row = 20; ttysize.ws_xpixel = 0; @@ -69,15 +67,15 @@ static int do_exec(const char *name, int ptypt, char **argv, int argc) { int slavept = open(name, O_RDWR); if (slavept < 0) { - printf("open slave error\n"); + fprintf(stderr, "open slave error\n"); exit(EXIT_FAILURE); } ioctl(slavept, TIOCSWINSZ, &ttysize); + close(0); close(1); close(2); - dup2(slavept, 0); dup2(slavept, 1); dup2(slavept, 2); @@ -85,15 +83,14 @@ static int do_exec(const char *name, int ptypt, char **argv, int argc) { close(slavept); close(pty_fd); - int fdlimit = (int)sysconf(_SC_OPEN_MAX); + int32_t fdlimit = (int32_t)sysconf(_SC_OPEN_MAX); for (int i = STDERR_FILENO + 1; i < fdlimit; i++) { close(i); } - - execvp(new_argv[0], new_argv); //不会传递环境变量 + //unsetenv("TERMTUNNEL_VERBOSE"); + execvp(new_argv[0], new_argv); fprintf(stderr, "%s: %s\n", new_argv[0], strerror(errno)); exit(EXIT_FAILURE); - // printf("kkk2k\n"); return 0; } diff --git a/src/utils.c b/src/utils.c index 564c9e7..00789ad 100644 --- a/src/utils.c +++ b/src/utils.c @@ -60,19 +60,16 @@ void *memdup(const void *src, size_t n) { bool stdin_is_raw() { return _stdin_is_raw; } int writen(int fd, void *buf, int n) { - int nwrite, left = n; + int nwrite = 0; + int left = n; while (left > 0) { if ((nwrite = write(fd, buf, left)) == -1) { if (errno == EINTR || errno == EAGAIN) { continue; } } else { - if (nwrite == n) { - return 0; - } else { left -= nwrite; buf += nwrite; - } } } return n;