/[LeafOK_CVS]/lbbs/src/bbs_net.c
ViewVC logotype

Contents of /lbbs/src/bbs_net.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.104 - (show annotations)
Fri Dec 19 06:16:26 2025 UTC (2 months, 3 weeks ago) by sysadm
Branch: MAIN
Changes since 1.103: +82 -82 lines
Content type: text/x-csrc
Append \n to the end of logging message by log_...()
Remove ending \n from each logging message

1 /* SPDX-License-Identifier: GPL-3.0-or-later */
2 /*
3 * bbs_net
4 * - user interactive feature of site shuttle
5 *
6 * Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com>
7 */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include "bbs.h"
14 #include "bbs_net.h"
15 #include "common.h"
16 #include "io.h"
17 #include "log.h"
18 #include "login.h"
19 #include "menu.h"
20 #include "screen.h"
21 #include "str_process.h"
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <netdb.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include <arpa/inet.h>
32 #include <ctype.h>
33 #include <libssh/libssh.h>
34 #include <libssh/server.h>
35 #include <libssh/callbacks.h>
36 #include <netinet/in.h>
37 #include <netinet/ip.h>
38 #include <sys/select.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41
42 #ifdef HAVE_SYS_EPOLL_H
43 #include <sys/epoll.h>
44 #else
45 #include <poll.h>
46 #endif
47
48 static const char MENU_CONF_DELIM[] = " \t\r\n";
49
50 enum _bbs_net_constant_t
51 {
52 MAXSTATION = 26 * 2,
53 STATION_PER_LINE = 4,
54 USERNAME_MAX_LEN = 20,
55 PASSWORD_MAX_LEN = 20,
56 REMOTE_CONNECT_TIMEOUT = 10, // seconds
57 SSH_CONNECT_TIMEOUT = 5, // seconds
58 PROGRESS_BAR_LEN = 30,
59 };
60
61 struct _bbsnet_conf
62 {
63 char org_name[40];
64 char site_name[40];
65 char host_name[IP_ADDR_LEN];
66 char port[6];
67 int8_t use_ssh;
68 char charset[CHARSET_MAX_LEN + 1];
69 } bbsnet_conf[MAXSTATION];
70
71 static MENU_SET bbsnet_menu;
72
73 static void unload_bbsnet_conf(void);
74
75 static int load_bbsnet_conf(const char *file_config)
76 {
77 FILE *fp;
78 MENU *p_menu;
79 MENU_ITEM *p_menu_item;
80 MENU_ITEM_ID menu_item_id;
81 char line[LINE_BUFFER_LEN], *t1, *t2, *t3, *t4, *t5, *t6, *saveptr;
82 long port;
83 char *endptr;
84
85 unload_bbsnet_conf();
86
87 bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU));
88 if (bbsnet_menu.p_menu_pool == NULL)
89 {
90 log_error("calloc(p_menu_pool) error");
91 return -1;
92 }
93 bbsnet_menu.menu_count = 1;
94
95 bbsnet_menu.p_menu_item_pool = calloc(MAXSTATION, sizeof(MENU_ITEM));
96 if (bbsnet_menu.p_menu_item_pool == NULL)
97 {
98 log_error("calloc(p_menu_item_pool) error");
99 unload_bbsnet_conf();
100 return -1;
101 }
102 bbsnet_menu.menu_item_count = MAXSTATION;
103
104 p_menu = (MENU *)get_menu_by_id(&bbsnet_menu, 0);
105
106 strncpy(p_menu->name, "BBSNET", sizeof(p_menu->name) - 1);
107 p_menu->name[sizeof(p_menu->name) - 1] = '\0';
108 p_menu->title.show = 0;
109 p_menu->screen_show = 0;
110
111 fp = fopen(file_config, "r");
112 if (fp == NULL)
113 {
114 unload_bbsnet_conf();
115 return -2;
116 }
117
118 menu_item_id = 0;
119 while (fgets(line, sizeof(line), fp) && menu_item_id < MAXSTATION)
120 {
121 t1 = strtok_r(line, MENU_CONF_DELIM, &saveptr);
122 t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
123 t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
124 t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
125 t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
126 t6 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
127
128 if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL ||
129 t5 == NULL || t6 == NULL || t1[0] == '#')
130 {
131 continue;
132 }
133
134 strncpy(bbsnet_conf[menu_item_id].site_name, t2, sizeof(bbsnet_conf[menu_item_id].site_name) - 1);
135 bbsnet_conf[menu_item_id].site_name[sizeof(bbsnet_conf[menu_item_id].site_name) - 1] = '\0';
136 strncpy(bbsnet_conf[menu_item_id].org_name, t1, sizeof(bbsnet_conf[menu_item_id].org_name) - 1);
137 bbsnet_conf[menu_item_id].org_name[sizeof(bbsnet_conf[menu_item_id].org_name) - 1] = '\0';
138 strncpy(bbsnet_conf[menu_item_id].host_name, t3, sizeof(bbsnet_conf[menu_item_id].host_name) - 1);
139 bbsnet_conf[menu_item_id].host_name[sizeof(bbsnet_conf[menu_item_id].host_name) - 1] = '\0';
140 port = strtol(t4, &endptr, 10);
141 if (*endptr != '\0' || port <= 0 || port > 65535)
142 {
143 log_error("Invalid port value %ld of menu item %d", port, menu_item_id);
144 fclose(fp);
145 unload_bbsnet_conf();
146 return -3;
147 }
148 strncpy(bbsnet_conf[menu_item_id].port, t4, sizeof(bbsnet_conf[menu_item_id].port) - 1);
149 bbsnet_conf[menu_item_id].port[sizeof(bbsnet_conf[menu_item_id].port) - 1] = '\0';
150 bbsnet_conf[menu_item_id].use_ssh = (toupper(t5[0]) == 'Y');
151 strncpy(bbsnet_conf[menu_item_id].charset, t6, sizeof(bbsnet_conf[menu_item_id].charset) - 1);
152 bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';
153
154 p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id);
155 if (p_menu_item == NULL)
156 {
157 log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id);
158 fclose(fp);
159 unload_bbsnet_conf();
160 return -3;
161 }
162
163 p_menu_item->row = (int16_t)(2 + menu_item_id / STATION_PER_LINE);
164 p_menu_item->col = (int16_t)(5 + menu_item_id % STATION_PER_LINE * 20);
165 snprintf(p_menu_item->action, sizeof(p_menu_item->action), "%d", (int16_t)menu_item_id);
166 p_menu_item->submenu = 0;
167 p_menu_item->priv = 0;
168 p_menu_item->level = 0;
169 p_menu_item->name[0] =
170 (char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id - MAXSTATION / 2);
171 p_menu_item->name[1] = '\0';
172 snprintf(p_menu_item->text, sizeof(p_menu_item->text), "\033[1;36m%c.\033[m %s",
173 p_menu_item->name[0], bbsnet_conf[menu_item_id].site_name);
174
175 p_menu->items[p_menu->item_count] = menu_item_id;
176 p_menu->item_count++;
177 menu_item_id++;
178 }
179
180 bbsnet_menu.menu_item_count = (int16_t)menu_item_id;
181 bbsnet_menu.menu_id_path[0] = 0;
182 bbsnet_menu.menu_item_pos[0] = 0;
183 bbsnet_menu.choose_step = 0;
184
185 fclose(fp);
186
187 return 0;
188 }
189
190 static void unload_bbsnet_conf(void)
191 {
192 bbsnet_menu.menu_count = 0;
193 bbsnet_menu.menu_item_count = 0;
194
195 if (bbsnet_menu.p_menu_pool)
196 {
197 free(bbsnet_menu.p_menu_pool);
198 bbsnet_menu.p_menu_pool = NULL;
199 }
200
201 if (bbsnet_menu.p_menu_item_pool)
202 {
203 free(bbsnet_menu.p_menu_item_pool);
204 bbsnet_menu.p_menu_item_pool = NULL;
205 }
206 }
207
208 static void progress_bar(int percent, int len)
209 {
210 char line[LINE_BUFFER_LEN];
211 char buf[LINE_BUFFER_LEN];
212 char buf2[LINE_BUFFER_LEN];
213 int pos;
214
215 if (len < 4)
216 {
217 len = 4;
218 }
219 else if (len + 2 > LINE_BUFFER_LEN)
220 {
221 len = LINE_BUFFER_LEN - 3;
222 }
223 if (percent < 0)
224 {
225 percent = 0;
226 }
227 else if (percent > 100)
228 {
229 percent = 100;
230 }
231
232 pos = len * percent / 100;
233
234 line[0] = ' ';
235 for (int i = 1; i <= len; i++)
236 {
237 line[i] = '-';
238 }
239 line[len + 1] = ' ';
240 line[len + 2] = '\0';
241
242 snprintf(buf, sizeof(buf), "%*s%3d%%%*s",
243 (len - 4) / 2, "", percent, (len - 4 + 1) / 2, "");
244 memcpy(buf2, buf, (size_t)pos);
245 buf2[pos] = '\0';
246
247 moveto(4, 1);
248 prints("%s\r\n", line);
249 prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + pos);
250 prints("%s\r\n", line);
251 iflush();
252 }
253
254 static int bbsnet_connect(int n)
255 {
256 int sock = -1;
257 int ret = 0;
258 int loop;
259 int error;
260 int sock_connected = 0;
261 int flags_sock = -1;
262 int flags_stdin = -1;
263 int flags_stdout = -1;
264 struct sockaddr_in sin;
265 char input_buf[LINE_BUFFER_LEN];
266 char output_buf[LINE_BUFFER_LEN];
267 int input_buf_len = 0;
268 int output_buf_len = 0;
269 int input_buf_offset = 0;
270 int output_buf_offset = 0;
271 char input_conv[LINE_BUFFER_LEN * 2];
272 char output_conv[LINE_BUFFER_LEN * 2];
273 int input_conv_len = 0;
274 int output_conv_len = 0;
275 int input_conv_offset = 0;
276 int output_conv_offset = 0;
277 iconv_t input_cd = (iconv_t)(-1);
278 iconv_t output_cd = (iconv_t)(-1);
279 char tocode[CHARSET_MAX_LEN + 20];
280
281 #ifdef HAVE_SYS_EPOLL_H
282 struct epoll_event ev, events[MAX_EVENTS];
283 int epollfd = -1;
284 #else
285 struct pollfd pfds[3];
286 #endif
287
288 int nfds;
289 int stdin_read_wait = 0;
290 int stdout_write_wait = 0;
291 int sock_read_wait = 0;
292 int sock_write_wait = 0;
293 struct addrinfo hints, *res = NULL;
294 int tos;
295 char remote_addr[INET_ADDRSTRLEN];
296 int remote_port;
297 char local_addr[INET_ADDRSTRLEN];
298 int local_port;
299 socklen_t sock_len;
300 time_t t_begin, t_used;
301 struct timespec ts_begin, ts_now;
302 int progress, progress_last;
303 int ch;
304 char remote_user[USERNAME_MAX_LEN + 1];
305 char remote_pass[PASSWORD_MAX_LEN + 1];
306 ssh_session outbound_session = NULL;
307 ssh_channel outbound_channel = NULL;
308 int ssh_process_config = 0;
309 int ssh_log_level = SSH_LOG_NOLOG;
310
311 if (user_online_update("BBS_NET") < 0)
312 {
313 log_error("user_online_update(BBS_NET) error");
314 }
315
316 if (bbsnet_conf[n].use_ssh)
317 {
318 clearscr();
319
320 if (!SSH_v2)
321 {
322 moveto(1, 1);
323 prints("只有在以SSH方式登陆本站时,才能使用SSH站点穿梭。");
324 press_any_key();
325 return 0;
326 }
327
328 moveto(1, 1);
329 prints("通过SSH方式连接[%s]...", bbsnet_conf[n].site_name);
330 moveto(2, 1);
331 prints("请输入用户名: ");
332 iflush();
333 if (str_input(remote_user, sizeof(remote_user), DOECHO) < 0)
334 {
335 return -1;
336 }
337 if (remote_user[0] == '\0')
338 {
339 return 0;
340 }
341
342 moveto(3, 1);
343 prints("请输入密码: ");
344 iflush();
345 if (str_input(remote_pass, sizeof(remote_pass), NOECHO) < 0)
346 {
347 return -1;
348 }
349 if (remote_pass[0] == '\0')
350 {
351 return 0;
352 }
353 }
354
355 clearscr();
356
357 moveto(1, 1);
358 prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",
359 bbsnet_conf[n].site_name, bbsnet_conf[n].host_name);
360 iflush();
361
362 memset(&hints, 0, sizeof(hints));
363 hints.ai_family = AF_INET;
364 hints.ai_socktype = SOCK_STREAM;
365 hints.ai_protocol = IPPROTO_TCP;
366
367 if ((ret = getaddrinfo(BBS_address, NULL, &hints, &res)) != 0)
368 {
369 log_error("getaddrinfo() error (%d): %s", ret, gai_strerror(ret));
370 ret = -1;
371 goto cleanup;
372 }
373
374 if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr), local_addr, sizeof(local_addr)) == NULL)
375 {
376 log_error("inet_ntop() error (%d)", errno);
377 ret = -1;
378 goto cleanup;
379 }
380 local_port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
381
382 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
383 if (sock < 0)
384 {
385 log_error("socket() error (%d)", errno);
386 ret = -1;
387 goto cleanup;
388 }
389
390 if (bind(sock, res->ai_addr, res->ai_addrlen) < 0)
391 {
392 log_error("bind(%s:%u) error (%d)", local_addr, local_port, errno);
393 ret = -1;
394 goto cleanup;
395 }
396
397 freeaddrinfo(res);
398 res = NULL;
399
400 memset(&hints, 0, sizeof(hints));
401 hints.ai_family = AF_INET;
402 hints.ai_flags = AI_NUMERICSERV;
403 hints.ai_socktype = SOCK_STREAM;
404 hints.ai_protocol = IPPROTO_TCP;
405
406 if ((ret = getaddrinfo(bbsnet_conf[n].host_name, bbsnet_conf[n].port, &hints, &res)) != 0)
407 {
408 log_error("getaddrinfo() error (%d): %s", ret, gai_strerror(ret));
409 prints("\033[1;31m查找主机名失败!\033[m\r\n");
410 press_any_key();
411 ret = -1;
412 goto cleanup;
413 }
414
415 if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr), remote_addr, sizeof(remote_addr)) == NULL)
416 {
417 log_error("inet_ntop() error (%d)", errno);
418 ret = -1;
419 goto cleanup;
420 }
421 remote_port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
422
423 // Set socket as non-blocking
424 if ((flags_sock = fcntl(sock, F_GETFL, 0)) == -1)
425 {
426 log_error("fcntl(F_GETFL) error (%d)", errno);
427 ret = -1;
428 goto cleanup;
429 }
430 if ((fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK)) == -1)
431 {
432 log_error("fcntl(F_SETFL) error (%d)", errno);
433 ret = -1;
434 goto cleanup;
435 }
436
437 // Set STDIN/STDOUT as non-blocking
438 if ((flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1)
439 {
440 log_error("fcntl(F_GETFL) error (%d)", errno);
441 ret = -1;
442 goto cleanup;
443 }
444 if ((flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1)
445 {
446 log_error("fcntl(F_GETFL) error (%d)", errno);
447 ret = -1;
448 goto cleanup;
449 }
450 if ((fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK)) == -1)
451 {
452 log_error("fcntl(F_SETFL) error (%d)", errno);
453 ret = -1;
454 goto cleanup;
455 }
456 if ((fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK)) == -1)
457 {
458 log_error("fcntl(F_SETFL) error (%d)", errno);
459 ret = -1;
460 goto cleanup;
461 }
462
463 #ifdef HAVE_SYS_EPOLL_H
464 epollfd = epoll_create1(0);
465 if (epollfd < 0)
466 {
467 log_error("epoll_create1() error (%d)", errno);
468 ret = -1;
469 goto cleanup;
470 }
471
472 ev.events = EPOLLOUT | EPOLLET;
473 ev.data.fd = sock;
474 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
475 {
476 log_error("epoll_ctl(socket) error (%d)", errno);
477 ret = -1;
478 goto cleanup;
479 }
480
481 ev.events = EPOLLIN | EPOLLET;
482 ev.data.fd = STDIN_FILENO;
483 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
484 {
485 log_error("epoll_ctl(STDIN_FILENO) error (%d)", errno);
486 ret = -1;
487 goto cleanup;
488 }
489 #endif
490
491 while (!SYS_server_exit)
492 {
493 if ((ret = connect(sock, res->ai_addr, res->ai_addrlen)) == 0)
494 {
495 sock_connected = 1;
496 break;
497 }
498 else if (ret < 0)
499 {
500 if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS)
501 {
502 // Use select / epoll to check writability of the socket,
503 // then use getsockopt to check the status of the socket.
504 // See man connect(2)
505 break;
506 }
507 else if (errno == EINTR)
508 {
509 continue;
510 }
511 else
512 {
513 log_error("connect(socket) error (%d)", errno);
514 prints("\033[1;31m连接失败!\033[m\r\n");
515 press_any_key();
516 ret = -1;
517 goto cleanup;
518 }
519 }
520 }
521
522 progress = progress_last = 0;
523 prints("\033[1;32m连接进行中,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");
524 progress_bar(0, PROGRESS_BAR_LEN);
525
526 if (clock_gettime(CLOCK_REALTIME, &ts_begin) == -1)
527 {
528 log_error("clock_gettime() error (%d)", errno);
529 ret = -1;
530 goto cleanup;
531 }
532 ts_now = ts_begin;
533
534 while ((ts_now.tv_sec - ts_begin.tv_sec) * 1000 +
535 (ts_now.tv_nsec - ts_begin.tv_nsec) / 1000 / 1000 <
536 REMOTE_CONNECT_TIMEOUT * 1000 &&
537 !sock_connected && !SYS_server_exit)
538 {
539 #ifdef HAVE_SYS_EPOLL_H
540 nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
541 ret = nfds;
542 #else
543 pfds[0].fd = sock;
544 pfds[0].events = POLLOUT;
545 pfds[1].fd = STDIN_FILENO;
546 pfds[1].events = POLLIN;
547 nfds = 2;
548 ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second
549 #endif
550
551 if (ret < 0)
552 {
553 if (errno != EINTR)
554 {
555 #ifdef HAVE_SYS_EPOLL_H
556 log_error("epoll_wait() error (%d)", errno);
557 #else
558 log_error("poll() error (%d)", errno);
559 #endif
560 break;
561 }
562 }
563 else if (ret == 0) // timeout
564 {
565 if (clock_gettime(CLOCK_REALTIME, &ts_now) == -1)
566 {
567 log_error("clock_gettime() error (%d)", errno);
568 ret = -1;
569 goto cleanup;
570 }
571
572 progress = (int)((ts_now.tv_sec - ts_begin.tv_sec) * 1000 +
573 (ts_now.tv_nsec - ts_begin.tv_nsec) / 1000 / 1000) /
574 REMOTE_CONNECT_TIMEOUT / 10 +
575 1;
576 if (progress < 0)
577 {
578 progress = 0;
579 }
580 if (progress > 100)
581 {
582 progress = 100;
583 }
584
585 if (progress > progress_last)
586 {
587 progress_last = progress;
588 progress_bar(progress, PROGRESS_BAR_LEN);
589 }
590 }
591 else // ret > 0
592 {
593 for (int i = 0; i < nfds; i++)
594 {
595 #ifdef HAVE_SYS_EPOLL_H
596 if (events[i].data.fd == sock)
597 #else
598 if (pfds[i].fd == sock && (pfds[i].revents & POLLOUT))
599 #endif
600 {
601 socklen_t len = sizeof(error);
602 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
603 {
604 log_error("getsockopt() error (%d) !", errno);
605 ret = -1;
606 goto cleanup;
607 }
608 if (error == 0)
609 {
610 sock_connected = 1;
611 break;
612 }
613 }
614 #ifdef HAVE_SYS_EPOLL_H
615 else if (events[i].data.fd == STDIN_FILENO)
616 #else
617 else if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
618 #endif
619 {
620 do
621 {
622 ch = igetch(0);
623 } while (ch == 0);
624 if (ch == Ctrl('C') || ch == KEY_ESC)
625 {
626 ret = 0;
627 goto cleanup;
628 }
629 }
630 }
631 }
632 }
633 if (SYS_server_exit)
634 {
635 ret = 0;
636 goto cleanup;
637 }
638 if (!sock_connected)
639 {
640 progress_bar(100, PROGRESS_BAR_LEN);
641 prints("\033[1;31m连接失败!\033[m\r\n");
642 press_any_key();
643 ret = -1;
644 goto cleanup;
645 }
646
647 tos = IPTOS_LOWDELAY;
648 if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
649 {
650 log_error("setsockopt IP_TOS=%d error (%d)", tos, errno);
651 }
652
653 sock_len = sizeof(sin);
654 if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0)
655 {
656 log_error("getsockname() error: %d", errno);
657 ret = -1;
658 goto cleanup;
659 }
660
661 if (inet_ntop(AF_INET, &(sin.sin_addr), local_addr, sizeof(local_addr)) == NULL)
662 {
663 log_error("inet_ntop() error (%d)", errno);
664 ret = -1;
665 goto cleanup;
666 }
667 local_port = ntohs(sin.sin_port);
668
669 if (bbsnet_conf[n].use_ssh)
670 {
671 outbound_session = ssh_new();
672 if (outbound_session == NULL)
673 {
674 log_error("ssh_new() error");
675 ret = -1;
676 goto cleanup;
677 }
678
679 if (ssh_options_set(outbound_session, SSH_OPTIONS_FD, &sock) < 0 ||
680 ssh_options_set(outbound_session, SSH_OPTIONS_PROCESS_CONFIG, &ssh_process_config) < 0 ||
681 ssh_options_set(outbound_session, SSH_OPTIONS_KNOWNHOSTS, SSH_KNOWN_HOSTS_FILE) < 0 ||
682 ssh_options_set(outbound_session, SSH_OPTIONS_HOST, bbsnet_conf[n].host_name) < 0 ||
683 ssh_options_set(outbound_session, SSH_OPTIONS_USER, remote_user) < 0 ||
684 ssh_options_set(outbound_session, SSH_OPTIONS_HOSTKEYS, "+ssh-ed25519,ecdsa-sha2-nistp256,ssh-rsa") < 0 ||
685 ssh_options_set(outbound_session, SSH_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
686 {
687 log_error("Error setting SSH options: %s", ssh_get_error(outbound_session));
688 ret = -1;
689 goto cleanup;
690 }
691
692 ssh_set_blocking(outbound_session, 0);
693
694 t_begin = time(NULL);
695 ret = SSH_ERROR;
696 while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
697 {
698 ret = ssh_connect(outbound_session);
699 if (ret == SSH_OK)
700 {
701 break;
702 }
703 else if (ret == SSH_AGAIN)
704 {
705 // log_debug("ssh_connect() error: SSH_AGAIN");
706 }
707 else // if (ret == SSH_ERROR)
708 {
709 log_error("ssh_connect() error: SSH_ERROR");
710 ret = -1;
711 goto cleanup;
712 }
713 }
714 if (ret != SSH_OK)
715 {
716 prints("\033[1;31m连接超时!\033[m\r\n");
717 press_any_key();
718 ret = -1;
719 goto cleanup;
720 }
721
722 ret = ssh_session_is_known_server(outbound_session);
723 switch (ret)
724 {
725 case SSH_KNOWN_HOSTS_NOT_FOUND:
726 case SSH_KNOWN_HOSTS_UNKNOWN:
727 if (ssh_session_update_known_hosts(outbound_session) != SSH_OK)
728 {
729 log_error("ssh_session_update_known_hosts(%s) error", bbsnet_conf[n].host_name);
730 prints("\033[1;31m无法添加服务器证书\033[m\r\n");
731 press_any_key();
732 ret = -1;
733 goto cleanup;
734 }
735 log_common("SSH key of (%s) is added into %s", bbsnet_conf[n].host_name, SSH_KNOWN_HOSTS_FILE);
736 case SSH_KNOWN_HOSTS_OK:
737 break;
738 case SSH_KNOWN_HOSTS_CHANGED:
739 case SSH_KNOWN_HOSTS_OTHER:
740 log_error("ssh_session_is_known_server(%s) error: %d", bbsnet_conf[n].host_name, ret);
741 prints("\033[1;31m服务器证书已变更\033[m\r\n");
742 press_any_key();
743 ret = -1;
744 goto cleanup;
745 }
746
747 ret = SSH_AUTH_ERROR;
748 while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
749 {
750 ret = ssh_userauth_password(outbound_session, NULL, remote_pass);
751 if (ret == SSH_AUTH_SUCCESS)
752 {
753 break;
754 }
755 else if (ret == SSH_AUTH_AGAIN)
756 {
757 // log_debug("ssh_userauth_password() error: SSH_AUTH_AGAIN");
758 }
759 else if (ret == SSH_AUTH_ERROR)
760 {
761 log_error("ssh_userauth_password() error: SSH_AUTH_ERROR");
762 ret = -1;
763 goto cleanup;
764 }
765 else // if (ret == SSH_AUTH_DENIED)
766 {
767 log_error("ssh_userauth_password() error: SSH_AUTH_DENIED");
768 prints("\033[1;31m身份验证失败!\033[m\r\n");
769 press_any_key();
770 ret = -1;
771 goto cleanup;
772 }
773 }
774 if (ret != SSH_AUTH_SUCCESS)
775 {
776 prints("\033[1;31m连接超时!\033[m\r\n");
777 press_any_key();
778 ret = -1;
779 goto cleanup;
780 }
781
782 outbound_channel = ssh_channel_new(outbound_session);
783 if (outbound_channel == NULL)
784 {
785 log_error("ssh_channel_new() error");
786 ret = -1;
787 goto cleanup;
788 }
789
790 ret = SSH_ERROR;
791 while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
792 {
793 ret = ssh_channel_open_session(outbound_channel);
794 if (ret == SSH_OK)
795 {
796 break;
797 }
798 else if (ret == SSH_AGAIN)
799 {
800 // log_debug("ssh_channel_open_session() error: SSH_AGAIN");
801 }
802 else // if (ret == SSH_ERROR)
803 {
804 log_error("ssh_channel_open_session() error: SSH_ERROR");
805 ret = -1;
806 goto cleanup;
807 }
808 }
809 if (ret != SSH_OK)
810 {
811 prints("\033[1;31m连接超时!\033[m\r\n");
812 press_any_key();
813 ret = -1;
814 goto cleanup;
815 }
816
817 ret = SSH_ERROR;
818 while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
819 {
820 ret = ssh_channel_request_pty(outbound_channel);
821 if (ret == SSH_OK)
822 {
823 break;
824 }
825 else if (ret == SSH_AGAIN)
826 {
827 // log_debug("ssh_channel_request_pty() error: SSH_AGAIN");
828 }
829 else // if (ret == SSH_ERROR)
830 {
831 log_error("ssh_channel_request_pty() error: SSH_ERROR");
832 ret = -1;
833 goto cleanup;
834 }
835 }
836 if (ret != SSH_OK)
837 {
838 prints("\033[1;31m连接超时!\033[m\r\n");
839 press_any_key();
840 ret = -1;
841 goto cleanup;
842 }
843
844 ret = SSH_ERROR;
845 while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
846 {
847 ret = ssh_channel_request_shell(outbound_channel);
848 if (ret == SSH_OK)
849 {
850 break;
851 }
852 else if (ret == SSH_AGAIN)
853 {
854 // log_debug("ssh_channel_request_shell() error: SSH_AGAIN");
855 }
856 else // if (ret == SSH_ERROR)
857 {
858 log_error("ssh_channel_request_shell() error: SSH_ERROR");
859 ret = -1;
860 goto cleanup;
861 }
862 }
863 if (ret != SSH_OK)
864 {
865 prints("\033[1;31m连接超时!\033[m\r\n");
866 press_any_key();
867 ret = -1;
868 goto cleanup;
869 }
870 }
871
872 prints("\033[1;31m连接成功!\033[m\r\n");
873 iflush();
874 log_common("BBSNET connect to %s:%d from %s:%d by [%s]",
875 remote_addr, remote_port, local_addr, local_port, BBS_username);
876
877 snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset,
878 (strcasecmp(stdio_charset, bbsnet_conf[n].charset) == 0 ? "" : "//IGNORE"));
879 input_cd = iconv_open(tocode, stdio_charset);
880 if (input_cd == (iconv_t)(-1))
881 {
882 log_error("iconv_open(%s->%s) error: %d", stdio_charset, tocode, errno);
883 ret = -1;
884 goto cleanup;
885 }
886
887 snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
888 (strcasecmp(bbsnet_conf[n].charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
889 output_cd = iconv_open(tocode, bbsnet_conf[n].charset);
890 if (output_cd == (iconv_t)(-1))
891 {
892 log_error("iconv_open(%s->%s) error: %d", bbsnet_conf[n].charset, tocode, errno);
893 ret = -1;
894 goto cleanup;
895 }
896
897 #ifdef HAVE_SYS_EPOLL_H
898 ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
899 ev.data.fd = sock;
900 if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)
901 {
902 log_error("epoll_ctl(socket) error (%d)", errno);
903 ret = -1;
904 goto cleanup;
905 }
906
907 ev.events = EPOLLOUT | EPOLLET;
908 ev.data.fd = STDOUT_FILENO;
909 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
910 {
911 log_error("epoll_ctl(STDOUT_FILENO) error (%d)", errno);
912 ret = -1;
913 goto cleanup;
914 }
915 #endif
916
917 BBS_last_access_tm = t_begin = time(NULL);
918 loop = 1;
919
920 while (loop && !SYS_server_exit)
921 {
922 if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
923 {
924 log_debug("SSH channel is closed");
925 loop = 0;
926 break;
927 }
928
929 if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(outbound_channel))
930 {
931 log_debug("Outbound channel is closed");
932 loop = 0;
933 break;
934 }
935
936 #ifdef HAVE_SYS_EPOLL_H
937 nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
938 ret = nfds;
939 #else
940 pfds[0].fd = STDIN_FILENO;
941 pfds[0].events = POLLIN;
942 pfds[1].fd = sock;
943 pfds[1].events = POLLIN | POLLOUT;
944 pfds[2].fd = STDOUT_FILENO;
945 pfds[2].events = POLLOUT;
946 nfds = 3;
947 ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second
948 #endif
949
950 if (ret < 0)
951 {
952 if (errno != EINTR)
953 {
954 #ifdef HAVE_SYS_EPOLL_H
955 log_error("epoll_wait() error (%d)", errno);
956 #else
957 log_error("poll() error (%d)", errno);
958 #endif
959 break;
960 }
961 continue;
962 }
963 else if (ret == 0) // timeout
964 {
965 if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
966 {
967 log_debug("User input timeout");
968 break;
969 }
970 }
971
972 for (int i = 0; i < nfds; i++)
973 {
974 #ifdef HAVE_SYS_EPOLL_H
975 if (events[i].events & (EPOLLHUP | EPOLLERR))
976 #else
977 if (pfds[i].revents & (POLLHUP | POLLERR))
978 #endif
979 {
980 #ifdef HAVE_SYS_EPOLL_H
981 log_debug("FD (%d) error events (%d)", events[i].data.fd, events[i].events);
982 #else
983 log_debug("FD (%d) error events (%d)", pfds[i].fd, pfds[i].revents);
984 #endif
985 loop = 0;
986 break;
987 }
988
989 #ifdef HAVE_SYS_EPOLL_H
990 if (events[i].data.fd == STDIN_FILENO && (events[i].events & EPOLLIN))
991 #else
992 if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
993 #endif
994 {
995 stdin_read_wait = 1;
996 }
997
998 #ifdef HAVE_SYS_EPOLL_H
999 if (events[i].data.fd == sock)
1000 #else
1001 if (pfds[i].fd == sock)
1002 #endif
1003 {
1004 #ifdef HAVE_SYS_EPOLL_H
1005 if (events[i].events & EPOLLIN)
1006 #else
1007 if (pfds[i].revents & POLLIN)
1008 #endif
1009 {
1010 sock_read_wait = 1;
1011 }
1012
1013 #ifdef HAVE_SYS_EPOLL_H
1014 if (events[i].events & EPOLLOUT)
1015 #else
1016 if (pfds[i].revents & POLLOUT)
1017 #endif
1018 {
1019 sock_write_wait = 1;
1020 }
1021 }
1022
1023 #ifdef HAVE_SYS_EPOLL_H
1024 if (events[i].data.fd == STDOUT_FILENO && (events[i].events & EPOLLOUT))
1025 #else
1026 if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))
1027 #endif
1028 {
1029 stdout_write_wait = 1;
1030 }
1031 }
1032
1033 if (stdin_read_wait)
1034 {
1035 while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
1036 {
1037 if (SSH_v2)
1038 {
1039 ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);
1040 if (ret == SSH_ERROR)
1041 {
1042 log_debug("ssh_channel_read_nonblocking() error: %s", ssh_get_error(SSH_session));
1043 loop = 0;
1044 break;
1045 }
1046 else if (ret == SSH_EOF)
1047 {
1048 stdin_read_wait = 0;
1049 loop = 0;
1050 break;
1051 }
1052 else if (ret == 0)
1053 {
1054 // Send NO-OP to remote server
1055 input_buf[input_buf_len] = '\0';
1056 input_buf_len++;
1057
1058 BBS_last_access_tm = time(NULL);
1059 stdin_read_wait = 0;
1060 break; // Check whether channel is still open
1061 }
1062 }
1063 else
1064 {
1065 ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);
1066 }
1067 if (ret < 0)
1068 {
1069 if (errno == EAGAIN || errno == EWOULDBLOCK)
1070 {
1071 stdin_read_wait = 0;
1072 break;
1073 }
1074 else if (errno == EINTR)
1075 {
1076 continue;
1077 }
1078 else
1079 {
1080 log_error("read(STDIN) error (%d)", errno);
1081 loop = 0;
1082 break;
1083 }
1084 }
1085 else if (ret == 0) // broken pipe
1086 {
1087 log_debug("read(STDIN) EOF");
1088 stdin_read_wait = 0;
1089 loop = 0;
1090 break;
1091 }
1092 else
1093 {
1094 input_buf_len += ret;
1095 BBS_last_access_tm = time(NULL);
1096
1097 // Refresh current action while user input
1098 if (user_online_update("BBS_NET") < 0)
1099 {
1100 log_error("user_online_update(BBS_NET) error");
1101 }
1102
1103 continue;
1104 }
1105 }
1106 }
1107
1108 if (sock_write_wait)
1109 {
1110 if (input_buf_offset < input_buf_len)
1111 {
1112 // For debug
1113 #ifdef _DEBUG
1114 for (int j = input_buf_offset; j < input_buf_len; j++)
1115 {
1116 log_debug("input: <--[%u]", (input_buf[j] + 256) % 256);
1117 }
1118 #endif
1119
1120 ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);
1121 if (ret < 0)
1122 {
1123 log_error("io_buf_conv(input, %d, %d, %d) error", input_buf_len, input_buf_offset, input_conv_len);
1124 input_buf_len = input_buf_offset; // Discard invalid sequence
1125 }
1126
1127 // For debug
1128 #ifdef _DEBUG
1129 for (int j = input_conv_offset; j < input_conv_len; j++)
1130 {
1131 log_debug("input_conv: <--[%u]", (input_conv[j] + 256) % 256);
1132 }
1133 #endif
1134 }
1135
1136 while (input_conv_offset < input_conv_len && !SYS_server_exit)
1137 {
1138 if (bbsnet_conf[n].use_ssh)
1139 {
1140 ret = ssh_channel_write(outbound_channel, input_conv + input_conv_offset, (uint32_t)(input_conv_len - input_conv_offset));
1141 if (ret == SSH_ERROR)
1142 {
1143 log_debug("ssh_channel_write() error: %s", ssh_get_error(outbound_session));
1144 loop = 0;
1145 break;
1146 }
1147 }
1148 else
1149 {
1150 ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset));
1151 }
1152 if (ret < 0)
1153 {
1154 if (errno == EAGAIN || errno == EWOULDBLOCK)
1155 {
1156 sock_write_wait = 0;
1157 break;
1158 }
1159 else if (errno == EINTR)
1160 {
1161 continue;
1162 }
1163 else
1164 {
1165 log_debug("write(socket) error (%d)", errno);
1166 loop = 0;
1167 break;
1168 }
1169 }
1170 else if (ret == 0) // broken pipe
1171 {
1172 log_debug("write(socket) EOF");
1173 sock_write_wait = 0;
1174 loop = 0;
1175 break;
1176 }
1177 else
1178 {
1179 input_conv_offset += ret;
1180 if (input_conv_offset >= input_conv_len) // Output buffer complete
1181 {
1182 input_conv_offset = 0;
1183 input_conv_len = 0;
1184 break;
1185 }
1186 continue;
1187 }
1188 }
1189 }
1190
1191 if (sock_read_wait)
1192 {
1193 while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)
1194 {
1195 if (bbsnet_conf[n].use_ssh)
1196 {
1197 ret = ssh_channel_read_nonblocking(outbound_channel, output_buf + output_buf_len,
1198 (uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0);
1199 if (ret == SSH_ERROR)
1200 {
1201 log_debug("ssh_channel_read_nonblocking() error: %s", ssh_get_error(outbound_session));
1202 loop = 0;
1203 break;
1204 }
1205 else if (ret == SSH_EOF)
1206 {
1207 sock_read_wait = 0;
1208 loop = 0;
1209 break;
1210 }
1211 else if (ret == 0)
1212 {
1213 sock_read_wait = 0;
1214 break;
1215 }
1216 }
1217 else
1218 {
1219 ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);
1220 }
1221 if (ret < 0)
1222 {
1223 if (errno == EAGAIN || errno == EWOULDBLOCK)
1224 {
1225 sock_read_wait = 0;
1226 break;
1227 }
1228 else if (errno == EINTR)
1229 {
1230 continue;
1231 }
1232 else
1233 {
1234 log_debug("read(socket) error (%d)", errno);
1235 loop = 0;
1236 break;
1237 }
1238 }
1239 else if (ret == 0) // broken pipe
1240 {
1241 log_debug("read(socket) EOF");
1242 sock_read_wait = 0;
1243 loop = 0;
1244 break;
1245 }
1246 else
1247 {
1248 output_buf_len += ret;
1249 continue;
1250 }
1251 }
1252 }
1253
1254 if (stdout_write_wait)
1255 {
1256 if (output_buf_offset < output_buf_len)
1257 {
1258 ret = io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len);
1259 if (ret < 0)
1260 {
1261 log_error("io_buf_conv(output, %d, %d, %d) error", output_buf_len, output_buf_offset, output_conv_len);
1262 output_buf_len = output_buf_offset; // Discard invalid sequence
1263 }
1264 }
1265
1266 while (output_conv_offset < output_conv_len && !SYS_server_exit)
1267 {
1268 if (SSH_v2)
1269 {
1270 ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset));
1271 if (ret == SSH_ERROR)
1272 {
1273 log_debug("ssh_channel_write() error: %s", ssh_get_error(SSH_session));
1274 loop = 0;
1275 break;
1276 }
1277 }
1278 else
1279 {
1280 ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset));
1281 }
1282 if (ret < 0)
1283 {
1284 if (errno == EAGAIN || errno == EWOULDBLOCK)
1285 {
1286 stdout_write_wait = 0;
1287 break;
1288 }
1289 else if (errno == EINTR)
1290 {
1291 continue;
1292 }
1293 else
1294 {
1295 log_debug("write(STDOUT) error (%d)", errno);
1296 loop = 0;
1297 break;
1298 }
1299 }
1300 else if (ret == 0) // broken pipe
1301 {
1302 log_debug("write(STDOUT) EOF");
1303 stdout_write_wait = 0;
1304 loop = 0;
1305 break;
1306 }
1307 else
1308 {
1309 output_conv_offset += ret;
1310 if (output_conv_offset >= output_conv_len) // Output buffer complete
1311 {
1312 output_conv_offset = 0;
1313 output_conv_len = 0;
1314 break;
1315 }
1316 continue;
1317 }
1318 }
1319 }
1320 }
1321
1322 ret = 1; // Normal disconnect
1323 BBS_last_access_tm = time(NULL);
1324 t_used = BBS_last_access_tm - t_begin;
1325 log_common("BBSNET disconnect, %ld days %ld hours %ld minutes %ld seconds used",
1326 t_used / 86400, t_used % 86400 / 3600, t_used % 3600 / 60, t_used % 60);
1327
1328 cleanup:
1329 // Clear sensitive data
1330 memset(remote_pass, 0, sizeof(remote_pass));
1331 memset(remote_user, 0, sizeof(remote_user));
1332
1333 if (input_cd != (iconv_t)(-1))
1334 {
1335 iconv_close(input_cd);
1336 }
1337 if (output_cd != (iconv_t)(-1))
1338 {
1339 iconv_close(output_cd);
1340 }
1341
1342 #ifdef HAVE_SYS_EPOLL_H
1343 if (epollfd != -1 && close(epollfd) < 0)
1344 {
1345 log_error("close(epoll) error (%d)");
1346 }
1347 #endif
1348
1349 if (bbsnet_conf[n].use_ssh)
1350 {
1351 if (outbound_channel != NULL)
1352 {
1353 ssh_channel_send_eof(outbound_channel);
1354 ssh_channel_close(outbound_channel);
1355 ssh_channel_free(outbound_channel);
1356 }
1357 if (outbound_session != NULL)
1358 {
1359 ssh_disconnect(outbound_session);
1360 ssh_free(outbound_session);
1361 }
1362 }
1363
1364 // Restore STDIN/STDOUT flags
1365 if (flags_stdin != -1 && fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1)
1366 {
1367 log_error("fcntl(F_SETFL) error (%d)", errno);
1368 }
1369 if (flags_stdout != -1 && fcntl(STDOUT_FILENO, F_SETFL, flags_stdout) == -1)
1370 {
1371 log_error("fcntl(F_SETFL) error (%d)", errno);
1372 }
1373
1374 if (sock != -1 && close(sock) == -1)
1375 {
1376 log_error("close(socket) error (%d)", errno);
1377 }
1378
1379 if (res)
1380 {
1381 freeaddrinfo(res);
1382 }
1383
1384 return ret;
1385 }
1386
1387 static int bbsnet_refresh()
1388 {
1389 clearscr();
1390
1391 moveto(1, 1);
1392 prints(" ------------------------------------------------------------------------------ ");
1393 for (int i = 2; i < 19; i++)
1394 {
1395 moveto(i, 1);
1396 prints("|");
1397 moveto(i, 80);
1398 prints("|");
1399 }
1400 moveto(19, 1);
1401 prints("|------------------------------------------------------------------------------|");
1402 moveto(22, 1);
1403 prints(" ------------------------------------------------------------------------------ ");
1404 moveto(23, 1);
1405 prints(" [\033[1;32mCtrl+C\033[m]退出");
1406
1407 iflush();
1408
1409 return 0;
1410 }
1411
1412 static int bbsnet_selchange()
1413 {
1414 int i = bbsnet_menu.menu_item_pos[0];
1415
1416 moveto(20, 1);
1417 clrtoeol();
1418 prints("|\033[1m单位: \033[1;33m%s\033[m%*s 站名: \033[1;33m%s\033[m%*s 类型: \033[1;33m%s\033[m",
1419 bbsnet_conf[i].org_name, 20 - str_length(bbsnet_conf[i].org_name, 1), "",
1420 bbsnet_conf[i].site_name, 20 - str_length(bbsnet_conf[i].site_name, 1), "",
1421 (bbsnet_conf[i].use_ssh ? "SSH" : "Telnet"));
1422 moveto(20, 80);
1423 prints("|");
1424 moveto(21, 1);
1425 clrtoeol();
1426 prints("|\033[1m连往: \033[1;33m%-20s\033[m 端口: \033[1;33m%-5s\033[m 编码: \033[1;33m%s\033[m",
1427 bbsnet_conf[i].host_name, bbsnet_conf[i].port, bbsnet_conf[i].charset);
1428 moveto(21, 80);
1429 prints("|");
1430 iflush();
1431
1432 return 0;
1433 }
1434
1435 int bbs_net()
1436 {
1437 int ch;
1438
1439 if (load_bbsnet_conf(CONF_BBSNET) < 0)
1440 {
1441 clearscr();
1442 moveto(1, 1);
1443 prints("加载穿梭配置失败!");
1444 press_any_key();
1445 return -1;
1446 }
1447
1448 bbsnet_refresh();
1449 display_menu(&bbsnet_menu);
1450 bbsnet_selchange();
1451
1452 while (!SYS_server_exit)
1453 {
1454 ch = igetch(100);
1455
1456 if (ch != KEY_NULL && ch != KEY_TIMEOUT)
1457 {
1458 BBS_last_access_tm = time(NULL);
1459 }
1460
1461 switch (ch)
1462 {
1463 case KEY_NULL: // broken pipe
1464 log_debug("KEY_NULL");
1465 goto cleanup;
1466 case KEY_TIMEOUT:
1467 if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
1468 {
1469 log_debug("User input timeout");
1470 goto cleanup;
1471 }
1472 continue;
1473 case KEY_ESC:
1474 case Ctrl('C'): // user cancel
1475 goto cleanup;
1476 case CR:
1477 if (bbsnet_connect(bbsnet_menu.menu_item_pos[0]) < 0)
1478 {
1479 log_debug("bbsnet_connect() error");
1480 }
1481 // Force cleanup anything remaining in the output buffer
1482 clearscr();
1483 iflush();
1484 // Clear screen and redraw menu
1485 bbsnet_refresh();
1486 display_menu(&bbsnet_menu);
1487 bbsnet_selchange();
1488 break;
1489 case KEY_UP:
1490 for (int i = 0; i < STATION_PER_LINE; i++)
1491 {
1492 menu_control(&bbsnet_menu, KEY_UP);
1493 }
1494 bbsnet_selchange();
1495 break;
1496 case KEY_DOWN:
1497 for (int i = 0; i < STATION_PER_LINE; i++)
1498 {
1499 menu_control(&bbsnet_menu, KEY_DOWN);
1500 }
1501 bbsnet_selchange();
1502 break;
1503 case KEY_LEFT:
1504 menu_control(&bbsnet_menu, KEY_UP);
1505 bbsnet_selchange();
1506 break;
1507 case KEY_RIGHT:
1508 menu_control(&bbsnet_menu, KEY_DOWN);
1509 bbsnet_selchange();
1510 break;
1511 case KEY_HOME:
1512 case KEY_PGUP:
1513 menu_control(&bbsnet_menu, KEY_PGUP);
1514 bbsnet_selchange();
1515 break;
1516 case KEY_END:
1517 case KEY_PGDN:
1518 menu_control(&bbsnet_menu, KEY_PGDN);
1519 bbsnet_selchange();
1520 break;
1521 default:
1522 menu_control(&bbsnet_menu, ch);
1523 bbsnet_selchange();
1524 break;
1525 }
1526 }
1527
1528 cleanup:
1529 unload_bbsnet_conf();
1530
1531 return 0;
1532 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1