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

Diff of /lbbs/src/bbs_net.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.83 by sysadm, Fri Nov 21 07:36:41 2025 UTC Revision 1.84 by sysadm, Sun Nov 30 10:05:16 2025 UTC
# Line 18  Line 18 
18  #include "login.h"  #include "login.h"
19  #include "menu.h"  #include "menu.h"
20  #include "screen.h"  #include "screen.h"
21    #include "str_process.h"
22  #include <errno.h>  #include <errno.h>
23  #include <fcntl.h>  #include <fcntl.h>
24  #include <netdb.h>  #include <netdb.h>
# Line 28  Line 29 
29  #include <time.h>  #include <time.h>
30  #include <unistd.h>  #include <unistd.h>
31  #include <arpa/inet.h>  #include <arpa/inet.h>
32    #include <ctype.h>
33  #include <libssh/libssh.h>  #include <libssh/libssh.h>
34  #include <libssh/server.h>  #include <libssh/server.h>
35  #include <libssh/callbacks.h>  #include <libssh/callbacks.h>
# Line 50  enum _bbs_net_constant_t Line 52  enum _bbs_net_constant_t
52          MAX_PROCESS_BAR_LEN = 30,          MAX_PROCESS_BAR_LEN = 30,
53          MAXSTATION = 26 * 2,          MAXSTATION = 26 * 2,
54          STATION_PER_LINE = 4,          STATION_PER_LINE = 4,
55            USERNAME_MAX_LEN = 20,
56            PASSWORD_MAX_LEN = 20,
57  };  };
58    
59  struct _bbsnet_conf  struct _bbsnet_conf
# Line 58  struct _bbsnet_conf Line 62  struct _bbsnet_conf
62          char host2[40];          char host2[40];
63          char ip[40];          char ip[40];
64          in_port_t port;          in_port_t port;
65            int8_t use_ssh;
66          char charset[CHARSET_MAX_LEN + 1];          char charset[CHARSET_MAX_LEN + 1];
67  } bbsnet_conf[MAXSTATION];  } bbsnet_conf[MAXSTATION];
68    
# Line 69  static int load_bbsnet_conf(const char * Line 74  static int load_bbsnet_conf(const char *
74          MENU *p_menu;          MENU *p_menu;
75          MENU_ITEM *p_menu_item;          MENU_ITEM *p_menu_item;
76          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
77          char t[256], *t1, *t2, *t3, *t4, *t5, *saveptr;          char t[256], *t1, *t2, *t3, *t4, *t5, *t6, *saveptr;
78    
79          fp = fopen(file_config, "r");          fp = fopen(file_config, "r");
80          if (fp == NULL)          if (fp == NULL)
# Line 108  static int load_bbsnet_conf(const char * Line 113  static int load_bbsnet_conf(const char *
113                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
114                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
115                  t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
116                    t6 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
117    
118                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t5 == NULL || t[0] == '#' || t[0] == '*')                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL ||
119                            t5 == NULL || t6 == NULL || t[0] == '#' || t[0] == '*')
120                  {                  {
121                          continue;                          continue;
122                  }                  }
# Line 121  static int load_bbsnet_conf(const char * Line 128  static int load_bbsnet_conf(const char *
128                  strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1);                  strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1);
129                  bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0';                  bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0';
130                  bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23);                  bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23);
131                  strncpy(bbsnet_conf[menu_item_id].charset, t5, sizeof(bbsnet_conf[menu_item_id].charset) - 1);                  bbsnet_conf[menu_item_id].use_ssh = (toupper(t5[0]) == 'Y');
132                    strncpy(bbsnet_conf[menu_item_id].charset, t6, sizeof(bbsnet_conf[menu_item_id].charset) - 1);
133                  bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';                  bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';
134    
135                  p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id);                  p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id);
# Line 187  static void process_bar(int n, int len) Line 195  static void process_bar(int n, int len)
195                  n = len;                  n = len;
196          }          }
197    
198          moveto(4, 0);          moveto(4, 1);
199          prints(" ------------------------------ \r\n");          prints(" ------------------------------ \r\n");
200          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);
201          memcpy(buf2, buf, (size_t)n);          memcpy(buf2, buf, (size_t)n);
# Line 244  static int bbsnet_connect(int n) Line 252  static int bbsnet_connect(int n)
252          time_t t_used = time(NULL);          time_t t_used = time(NULL);
253          struct tm *tm_used;          struct tm *tm_used;
254          int ch;          int ch;
255            char remote_user[USERNAME_MAX_LEN + 1];
256            char remote_pass[PASSWORD_MAX_LEN + 1];
257            ssh_session session = NULL;
258            ssh_channel channel = NULL;
259            int ssh_process_config = 0;
260            int ssh_log_level = SSH_LOG_PROTOCOL;
261    
262          if (user_online_update("BBS_NET") < 0)          if (user_online_update("BBS_NET") < 0)
263          {          {
264                  log_error("user_online_update(BBS_NET) error\n");                  log_error("user_online_update(BBS_NET) error\n");
265          }          }
266    
267            if (bbsnet_conf[n].use_ssh)
268            {
269                    clearscr();
270    
271                    if (!SSH_v2)
272                    {
273                            moveto(1, 1);
274                            prints("只有在以SSH方式登陆本站时,才能使用SSH站点穿梭。");
275                            press_any_key();
276                            return 0;
277                    }
278    
279                    moveto(1, 1);
280                    prints("通过SSH方式连接[%s]...", bbsnet_conf[n].host1);
281                    moveto(2, 1);
282                    prints("请输入用户名: ");
283                    iflush();
284                    if (str_input(remote_user, sizeof(remote_user), DOECHO) < 0)
285                    {
286                            return -1;
287                    }
288                    if (remote_user[0] == '\0')
289                    {
290                            return 0;
291                    }
292    
293                    moveto(3, 1);
294                    prints("请输入密码: ");
295                    iflush();
296                    if (str_input(remote_pass, sizeof(remote_pass), NOECHO) < 0)
297                    {
298                            return -1;
299                    }
300                    if (remote_pass[0] == '\0')
301                    {
302                            return 0;
303                    }
304            }
305    
306          clearscr();          clearscr();
307    
308          moveto(0, 0);          moveto(1, 1);
309          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",
310                     bbsnet_conf[n].host1, bbsnet_conf[n].ip);                     bbsnet_conf[n].host1, bbsnet_conf[n].ip);
311          iflush();          iflush();
# Line 417  static int bbsnet_connect(int n) Line 470  static int bbsnet_connect(int n)
470                                  else if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))                                  else if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
471  #endif  #endif
472                                  {                                  {
473                                          ch = igetch(0);                                          do
474                                            {
475                                                    ch = igetch(0);
476                                            } while (ch == 0);
477                                          if (ch == Ctrl('C') || ch == KEY_ESC)                                          if (ch == Ctrl('C') || ch == KEY_ESC)
478                                          {                                          {
479                                                  goto cleanup;                                                  goto cleanup;
# Line 455  static int bbsnet_connect(int n) Line 511  static int bbsnet_connect(int n)
511          local_addr[sizeof(local_addr) - 1] = '\0';          local_addr[sizeof(local_addr) - 1] = '\0';
512          local_port = ntohs(sin.sin_port);          local_port = ntohs(sin.sin_port);
513    
514            if (bbsnet_conf[n].use_ssh)
515            {
516                    session = ssh_new();
517                    if (session == NULL)
518                    {
519                            log_error("ssh_new() error\n");
520                            goto cleanup;
521                    }
522    
523                    if (ssh_options_set(session, SSH_OPTIONS_FD, &sock) < 0 ||
524                            ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &ssh_process_config) < 0 ||
525                            ssh_options_set(session, SSH_OPTIONS_HOST, bbsnet_conf[n].ip) < 0 ||
526                            ssh_options_set(session, SSH_OPTIONS_USER, remote_user) < 0 ||
527                            ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "+ssh-rsa") < 0 ||
528                            ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
529                    {
530                            log_error("Error setting SSH options: %s\n", ssh_get_error(session));
531                            goto cleanup;
532                    }
533    
534                    while (!SYS_server_exit)
535                    {
536                            ret = ssh_connect(session);
537                            if (ret == SSH_OK)
538                            {
539                                    break;
540                            }
541                            else if (ret == SSH_ERROR)
542                            {
543                                    log_error("ssh_connect() error\n");
544                                    goto cleanup;
545                            }
546                    }
547    
548                    ssh_set_blocking(session, 0);
549    
550                    for (int i = 0; !SYS_server_exit;)
551                    {
552                            ret = ssh_userauth_password(session, NULL, remote_pass);
553                            if (ret == SSH_AUTH_SUCCESS)
554                            {
555                                    break;
556                            }
557                            else if (ret == SSH_AUTH_AGAIN)
558                            {
559    #ifdef _DEBUG
560                                    log_error("ssh_userauth_password() error: SSH_AUTH_AGAIN\n");
561    #endif
562                            }
563                            else if (ret == SSH_AUTH_ERROR)
564                            {
565                                    log_error("ssh_userauth_password() error: %d\n", ret);
566                                    goto cleanup;
567                            }
568                            else // if (ret == SSH_AUTH_DENIED)
569                            {
570                                    prints("\033[1;31m身份验证失败!\033[m\r\n");
571                                    i++;
572                                    if (i < BBS_login_retry_times)
573                                    {
574                                            prints("请输入密码: ");
575                                            iflush();
576                                            if (str_input(remote_pass, sizeof(remote_pass), NOECHO) < 0)
577                                            {
578                                                    goto cleanup;
579                                            }
580                                            if (remote_pass[0] == '\0')
581                                            {
582                                                    goto cleanup;
583                                            }
584                                    }
585                                    else
586                                    {
587                                            goto cleanup;
588                                    }
589                            }
590                    }
591    
592                    channel = ssh_channel_new(session);
593                    if (channel == NULL)
594                    {
595                            log_error("ssh_channel_new() error\n");
596                            goto cleanup;
597                    }
598    
599                    while (!SYS_server_exit)
600                    {
601                            ret = ssh_channel_open_session(channel);
602                            if (ret == SSH_OK)
603                            {
604                                    break;
605                            }
606                            else if (ret == SSH_ERROR)
607                            {
608                                    log_error("ssh_channel_open_session() error\n");
609                                    goto cleanup;
610                            }
611                    }
612    
613                    while (!SYS_server_exit)
614                    {
615                            ret = ssh_channel_request_pty(channel);
616                            if (ret == SSH_OK)
617                            {
618                                    break;
619                            }
620                            else if (ret == SSH_ERROR)
621                            {
622                                    log_error("ssh_channel_request_pty() error\n");
623                                    goto cleanup;
624                            }
625                    }
626    
627                    while (!SYS_server_exit)
628                    {
629                            ret = ssh_channel_request_shell(channel);
630                            if (ret == SSH_OK)
631                            {
632                                    break;
633                            }
634                            else if (ret == SSH_ERROR)
635                            {
636                                    log_error("ssh_channel_request_shell() error\n");
637                                    goto cleanup;
638                            }
639                    }
640            }
641    
642          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
643          iflush();          iflush();
644          log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n",          log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n",
# Line 509  static int bbsnet_connect(int n) Line 693  static int bbsnet_connect(int n)
693                          break;                          break;
694                  }                  }
695    
696                    if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(channel))
697                    {
698                            log_error("Remote SSH channel is closed\n");
699                            loop = 0;
700                            break;
701                    }
702    
703  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
704                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
705                  ret = nfds;                  ret = nfds;
# Line 584  static int bbsnet_connect(int n) Line 775  static int bbsnet_connect(int n)
775                          if (events[i].data.fd == STDOUT_FILENO)                          if (events[i].data.fd == STDOUT_FILENO)
776  #else  #else
777                          if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))                          if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))
778  #endif    #endif
779                          {                          {
780                                  stdout_write_wait = 1;                                  stdout_write_wait = 1;
781                          }                          }
# Line 697  static int bbsnet_connect(int n) Line 888  static int bbsnet_connect(int n)
888    
889                          while (input_conv_offset < input_conv_len && !SYS_server_exit)                          while (input_conv_offset < input_conv_len && !SYS_server_exit)
890                          {                          {
891                                  ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset));                                  if (bbsnet_conf[n].use_ssh)
892                                    {
893                                            ret = ssh_channel_write(channel, input_conv + input_conv_offset, (uint32_t)(input_conv_len - input_conv_offset));
894                                            if (ret == SSH_ERROR)
895                                            {
896                                                    log_error("ssh_channel_write() error: %s\n", ssh_get_error(session));
897                                                    loop = 0;
898                                                    break;
899                                            }
900                                    }
901                                    else
902                                    {
903                                            ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset));
904                                    }
905                                  if (ret < 0)                                  if (ret < 0)
906                                  {                                  {
907                                          if (errno == EAGAIN || errno == EWOULDBLOCK)                                          if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 743  static int bbsnet_connect(int n) Line 947  static int bbsnet_connect(int n)
947                  {                  {
948                          while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)                          while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)
949                          {                          {
950                                  ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);                                  if (bbsnet_conf[n].use_ssh)
951                                    {
952                                            ret = ssh_channel_read_nonblocking(channel, output_buf + output_buf_len,
953                                                                                                               (uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0);
954                                            if (ret == SSH_ERROR)
955                                            {
956                                                    log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(session));
957                                                    loop = 0;
958                                                    break;
959                                            }
960                                            else if (ret == SSH_EOF)
961                                            {
962                                                    sock_read_wait = 0;
963                                                    loop = 0;
964                                                    break;
965                                            }
966                                            else if (ret == 0)
967                                            {
968                                                    sock_read_wait = 0;
969                                                    break;
970                                            }
971                                    }
972                                    else
973                                    {
974                                            ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);
975                                    }
976                                  if (ret < 0)                                  if (ret < 0)
977                                  {                                  {
978                                          if (errno == EAGAIN || errno == EWOULDBLOCK)                                          if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 860  cleanup: Line 1089  cleanup:
1089          }          }
1090  #endif  #endif
1091    
1092            if (bbsnet_conf[n].use_ssh)
1093            {
1094                    ssh_channel_free(channel);
1095                    ssh_disconnect(session);
1096                    ssh_free(session);
1097            }
1098    
1099          // Restore STDIN/STDOUT flags          // Restore STDIN/STDOUT flags
1100          fcntl(STDIN_FILENO, F_SETFL, flags_stdin);          fcntl(STDIN_FILENO, F_SETFL, flags_stdin);
1101          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);
# Line 884  cleanup: Line 1120  cleanup:
1120  static int bbsnet_refresh()  static int bbsnet_refresh()
1121  {  {
1122          clearscr();          clearscr();
1123          moveto(1, 0);  
1124          prints(" ----------------------------------------------------------------------------- ");          moveto(1, 1);
1125            prints(" ------------------------------------------------------------------------------ ");
1126          for (int i = 2; i < 19; i++)          for (int i = 2; i < 19; i++)
1127          {          {
1128                  moveto(i, 0);                  moveto(i, 1);
1129                  prints("|");                  prints("|");
1130                  moveto(i, 79);                  moveto(i, 80);
1131                  prints("|");                  prints("|");
1132          }          }
1133          moveto(19, 0);          moveto(19, 1);
1134          prints("|-----------------------------------------------------------------------------|");          prints("|------------------------------------------------------------------------------|");
1135          moveto(22, 0);          moveto(22, 1);
1136          prints(" ----------------------------------------------------------------------------- ");          prints(" ------------------------------------------------------------------------------ ");
1137          moveto(23, 0);          moveto(23, 1);
1138          prints(" [\x1b[1;32mCtrl+C\x1b[m]退出");          prints(" [\033[1;32mCtrl+C\033[m]退出");
1139    
1140          iflush();          iflush();
1141    
# Line 909  static int bbsnet_selchange() Line 1146  static int bbsnet_selchange()
1146  {  {
1147          int i = bbsnet_menu.menu_item_pos[0];          int i = bbsnet_menu.menu_item_pos[0];
1148    
1149          moveto(20, 0);          moveto(20, 1);
1150          clrtoeol();          clrtoeol();
1151          prints("|\x1b[1m单位:\x1b[1;33m%-18s\x1b[m  站名:\x1b[1;33m%s\x1b[m",          prints("|\033[1m单位: \033[1;33m%s\033[m%*s  站名: \033[1;33m%s\033[m",
1152                     bbsnet_conf[i].host2, bbsnet_conf[i].host1);                     bbsnet_conf[i].host2, 20 - str_length(bbsnet_conf[i].host2, 1), "", bbsnet_conf[i].host1);
1153          moveto(20, 79);          moveto(20, 80);
1154          prints("|");          prints("|");
1155          moveto(21, 0);          moveto(21, 1);
1156          clrtoeol();          clrtoeol();
1157          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[i].ip);          prints("|\033[1m连往: \033[1;33m%-20s\033[m  端口: \033[1;33m%-5d\033[m            类型: \033[1;33m%s\033[m",
1158          if (bbsnet_conf[i].port != 23)                     bbsnet_conf[i].ip, bbsnet_conf[i].port, (bbsnet_conf[i].use_ssh ? "SSH" : "Telnet"));
1159          {          moveto(21, 80);
                 prints("  %d", bbsnet_conf[i].port);  
         }  
         prints("\x1b[m");  
         moveto(21, 79);  
1160          prints("|");          prints("|");
1161          iflush();          iflush();
1162    
# Line 936  int bbs_net() Line 1169  int bbs_net()
1169    
1170          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
1171    
         clearscr();  
1172          bbsnet_refresh();          bbsnet_refresh();
1173          display_menu(&bbsnet_menu);          display_menu(&bbsnet_menu);
1174          bbsnet_selchange();          bbsnet_selchange();
# Line 967  int bbs_net() Line 1199  int bbs_net()
1199                          goto cleanup;                          goto cleanup;
1200                  case CR:                  case CR:
1201                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
1202                            // Force cleanup anything remaining in the output buffer
1203                            clearscr();
1204                            iflush();
1205                            // Clear screen and redraw menu
1206                          bbsnet_refresh();                          bbsnet_refresh();
1207                          display_menu(&bbsnet_menu);                          display_menu(&bbsnet_menu);
1208                          bbsnet_selchange();                          bbsnet_selchange();


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

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