| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
bbs_net.c - description |
/* |
| 3 |
------------------- |
* bbs_net |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - user interactive feature of site shuttle |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
| 8 |
/*************************************************************************** |
|
| 9 |
* * |
#ifdef HAVE_CONFIG_H |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
#include "config.h" |
| 11 |
* it under the terms of the GNU General Public License as published by * |
#endif |
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 12 |
|
|
| 13 |
#include "bbs.h" |
#include "bbs.h" |
| 14 |
|
#include "bbs_net.h" |
| 15 |
#include "common.h" |
#include "common.h" |
| 16 |
#include "io.h" |
#include "io.h" |
| 17 |
#include "log.h" |
#include "log.h" |
| 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> |
| 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> |
| 38 |
#include <sys/select.h> |
#include <sys/select.h> |
| 39 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
| 40 |
#include <sys/socket.h> |
#include <sys/socket.h> |
| 41 |
|
|
| 42 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 43 |
#include <sys/epoll.h> |
#include <sys/epoll.h> |
| 44 |
|
#else |
| 45 |
|
#include <poll.h> |
| 46 |
|
#endif |
| 47 |
|
|
| 48 |
#define MENU_CONF_DELIM " \t\r\n" |
static const char MENU_CONF_DELIM[] = " \t\r\n"; |
| 49 |
|
|
| 50 |
#define MAX_PROCESS_BAR_LEN 30 |
enum _bbs_net_constant_t |
| 51 |
#define MAXSTATION 26 * 2 |
{ |
| 52 |
#define STATION_PER_LINE 4 |
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 |
struct _bbsnet_conf |
| 62 |
{ |
{ |
| 63 |
char host1[20]; |
char org_name[40]; |
| 64 |
char host2[40]; |
char site_name[40]; |
| 65 |
char ip[40]; |
char host_name[IP_ADDR_LEN]; |
| 66 |
in_port_t port; |
char port[6]; |
| 67 |
|
int8_t use_ssh; |
| 68 |
|
char charset[CHARSET_MAX_LEN + 1]; |
| 69 |
} bbsnet_conf[MAXSTATION]; |
} bbsnet_conf[MAXSTATION]; |
| 70 |
|
|
| 71 |
MENU_SET bbsnet_menu; |
static MENU_SET bbsnet_menu; |
| 72 |
|
|
| 73 |
|
static void unload_bbsnet_conf(void); |
| 74 |
|
|
| 75 |
int load_bbsnet_conf(const char *file_config) |
static int load_bbsnet_conf(const char *file_config) |
| 76 |
{ |
{ |
| 77 |
FILE *fp; |
FILE *fp; |
| 78 |
MENU *p_menu; |
MENU *p_menu; |
| 79 |
MENU_ITEM *p_menu_item; |
MENU_ITEM *p_menu_item; |
| 80 |
MENU_ITEM_ID menu_item_id; |
MENU_ITEM_ID menu_item_id; |
| 81 |
char t[256], *t1, *t2, *t3, *t4, *saveptr; |
char line[LINE_BUFFER_LEN], *t1, *t2, *t3, *t4, *t5, *t6, *saveptr; |
| 82 |
|
long port; |
| 83 |
|
char *endptr; |
| 84 |
|
|
| 85 |
fp = fopen(file_config, "r"); |
unload_bbsnet_conf(); |
|
if (fp == NULL) |
|
|
{ |
|
|
return -1; |
|
|
} |
|
| 86 |
|
|
| 87 |
bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU)); |
bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU)); |
| 88 |
if (bbsnet_menu.p_menu_pool == NULL) |
if (bbsnet_menu.p_menu_pool == NULL) |
| 89 |
{ |
{ |
| 90 |
log_error("calloc(p_menu_pool) error\n"); |
log_error("calloc(p_menu_pool) error\n"); |
| 91 |
return -3; |
return -1; |
| 92 |
} |
} |
| 93 |
bbsnet_menu.menu_count = 1; |
bbsnet_menu.menu_count = 1; |
| 94 |
|
|
| 96 |
if (bbsnet_menu.p_menu_item_pool == NULL) |
if (bbsnet_menu.p_menu_item_pool == NULL) |
| 97 |
{ |
{ |
| 98 |
log_error("calloc(p_menu_item_pool) error\n"); |
log_error("calloc(p_menu_item_pool) error\n"); |
| 99 |
return -3; |
unload_bbsnet_conf(); |
| 100 |
|
return -1; |
| 101 |
} |
} |
| 102 |
bbsnet_menu.menu_item_count = MAXSTATION; |
bbsnet_menu.menu_item_count = MAXSTATION; |
| 103 |
|
|
| 108 |
p_menu->title.show = 0; |
p_menu->title.show = 0; |
| 109 |
p_menu->screen_show = 0; |
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; |
menu_item_id = 0; |
| 119 |
while (fgets(t, 255, fp) && menu_item_id < MAXSTATION) |
while (fgets(line, sizeof(line), fp) && menu_item_id < MAXSTATION) |
| 120 |
{ |
{ |
| 121 |
t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr); |
t1 = strtok_r(line, MENU_CONF_DELIM, &saveptr); |
| 122 |
t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 123 |
t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
| 124 |
t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); |
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 || t[0] == '#' || t[0] == '*') |
if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || |
| 129 |
|
t5 == NULL || t6 == NULL || t1[0] == '#') |
| 130 |
{ |
{ |
| 131 |
continue; |
continue; |
| 132 |
} |
} |
| 133 |
|
|
| 134 |
strncpy(bbsnet_conf[menu_item_id].host1, t2, sizeof(bbsnet_conf[menu_item_id].host1) - 1); |
strncpy(bbsnet_conf[menu_item_id].site_name, t2, sizeof(bbsnet_conf[menu_item_id].site_name) - 1); |
| 135 |
bbsnet_conf[menu_item_id].host1[sizeof(bbsnet_conf[menu_item_id].host1) - 1] = '\0'; |
bbsnet_conf[menu_item_id].site_name[sizeof(bbsnet_conf[menu_item_id].site_name) - 1] = '\0'; |
| 136 |
strncpy(bbsnet_conf[menu_item_id].host2, t1, sizeof(bbsnet_conf[menu_item_id].host2) - 1); |
strncpy(bbsnet_conf[menu_item_id].org_name, t1, sizeof(bbsnet_conf[menu_item_id].org_name) - 1); |
| 137 |
bbsnet_conf[menu_item_id].host2[sizeof(bbsnet_conf[menu_item_id].host2) - 1] = '\0'; |
bbsnet_conf[menu_item_id].org_name[sizeof(bbsnet_conf[menu_item_id].org_name) - 1] = '\0'; |
| 138 |
strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1); |
strncpy(bbsnet_conf[menu_item_id].host_name, t3, sizeof(bbsnet_conf[menu_item_id].host_name) - 1); |
| 139 |
bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0'; |
bbsnet_conf[menu_item_id].host_name[sizeof(bbsnet_conf[menu_item_id].host_name) - 1] = '\0'; |
| 140 |
bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23); |
port = strtol(t4, &endptr, 10); |
| 141 |
|
if (*endptr != '\0' || port <= 0 || port > 65535) |
| 142 |
|
{ |
| 143 |
|
log_error("Invalid port value %d of menu item %d\n", 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); |
p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id); |
| 155 |
if (p_menu_item == NULL) |
if (p_menu_item == NULL) |
| 156 |
{ |
{ |
| 157 |
log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id); |
log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id); |
| 158 |
return -1; |
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); |
p_menu_item->row = (int16_t)(2 + menu_item_id / STATION_PER_LINE); |
| 167 |
p_menu_item->priv = 0; |
p_menu_item->priv = 0; |
| 168 |
p_menu_item->level = 0; |
p_menu_item->level = 0; |
| 169 |
p_menu_item->name[0] = |
p_menu_item->name[0] = |
| 170 |
(char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id); |
(char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id - MAXSTATION / 2); |
| 171 |
p_menu_item->name[1] = '\0'; |
p_menu_item->name[1] = '\0'; |
| 172 |
snprintf(p_menu_item->text, sizeof(p_menu_item->text), "[1;36m%c.[m %s", |
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].host1); |
p_menu_item->name[0], bbsnet_conf[menu_item_id].site_name); |
| 174 |
|
|
| 175 |
p_menu->items[p_menu->item_count] = menu_item_id; |
p_menu->items[p_menu->item_count] = menu_item_id; |
| 176 |
p_menu->item_count++; |
p_menu->item_count++; |
| 187 |
return 0; |
return 0; |
| 188 |
} |
} |
| 189 |
|
|
| 190 |
void unload_bbsnet_conf(void) |
static void unload_bbsnet_conf(void) |
| 191 |
{ |
{ |
| 192 |
bbsnet_menu.menu_count = 0; |
bbsnet_menu.menu_count = 0; |
| 193 |
bbsnet_menu.menu_item_count = 0; |
bbsnet_menu.menu_item_count = 0; |
| 194 |
|
|
| 195 |
free(bbsnet_menu.p_menu_pool); |
if (bbsnet_menu.p_menu_pool) |
| 196 |
bbsnet_menu.p_menu_pool = NULL; |
{ |
| 197 |
free(bbsnet_menu.p_menu_item_pool); |
free(bbsnet_menu.p_menu_pool); |
| 198 |
bbsnet_menu.p_menu_item_pool = NULL; |
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 |
void process_bar(int n, int len) |
static void progress_bar(int percent, int len) |
| 209 |
{ |
{ |
| 210 |
|
char line[LINE_BUFFER_LEN]; |
| 211 |
char buf[LINE_BUFFER_LEN]; |
char buf[LINE_BUFFER_LEN]; |
| 212 |
char buf2[LINE_BUFFER_LEN]; |
char buf2[LINE_BUFFER_LEN]; |
| 213 |
|
int pos; |
| 214 |
|
|
| 215 |
if (len > LINE_BUFFER_LEN) |
if (len <= 0) |
| 216 |
|
{ |
| 217 |
|
len = 1; |
| 218 |
|
} |
| 219 |
|
else if (len > LINE_BUFFER_LEN) |
| 220 |
{ |
{ |
| 221 |
len = LINE_BUFFER_LEN - 1; |
len = LINE_BUFFER_LEN - 1; |
| 222 |
} |
} |
| 223 |
if (n < 0) |
if (percent < 0) |
| 224 |
{ |
{ |
| 225 |
n = 0; |
percent = 0; |
| 226 |
} |
} |
| 227 |
else if (n > len) |
else if (percent > 100) |
| 228 |
{ |
{ |
| 229 |
n = len; |
percent = 100; |
| 230 |
} |
} |
| 231 |
|
|
| 232 |
moveto(4, 0); |
pos = len * percent / 100; |
| 233 |
prints(" ------------------------------ \r\n"); |
|
| 234 |
snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len); |
line[0] = ' '; |
| 235 |
strncpy(buf2, buf, (size_t)n); |
for (int i = 1; i <= len; i++) |
| 236 |
buf2[n] = '\0'; |
{ |
| 237 |
prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n); |
line[i] = '-'; |
| 238 |
prints(" ------------------------------ \r\n"); |
} |
| 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(); |
iflush(); |
| 252 |
} |
} |
| 253 |
|
|
| 254 |
int bbsnet_connect(int n) |
static int bbsnet_connect(int n) |
| 255 |
{ |
{ |
| 256 |
int sock, ret, loop, error; |
int sock = -1; |
| 257 |
|
int ret = 0; |
| 258 |
|
int loop; |
| 259 |
|
int error; |
| 260 |
int sock_connected = 0; |
int sock_connected = 0; |
| 261 |
int flags_sock; |
int flags_sock = -1; |
| 262 |
int flags_stdin; |
int flags_stdin = -1; |
| 263 |
int flags_stdout; |
int flags_stdout = -1; |
|
int len; |
|
| 264 |
struct sockaddr_in sin; |
struct sockaddr_in sin; |
| 265 |
char input_buf[LINE_BUFFER_LEN]; |
char input_buf[LINE_BUFFER_LEN]; |
| 266 |
char output_buf[LINE_BUFFER_LEN]; |
char output_buf[LINE_BUFFER_LEN]; |
| 268 |
int output_buf_len = 0; |
int output_buf_len = 0; |
| 269 |
int input_buf_offset = 0; |
int input_buf_offset = 0; |
| 270 |
int output_buf_offset = 0; |
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]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 283 |
int nfds, epollfd; |
int epollfd = -1; |
| 284 |
|
#else |
| 285 |
|
struct pollfd pfds[3]; |
| 286 |
|
#endif |
| 287 |
|
|
| 288 |
|
int nfds; |
| 289 |
int stdin_read_wait = 0; |
int stdin_read_wait = 0; |
| 290 |
int stdout_write_wait = 0; |
int stdout_write_wait = 0; |
| 291 |
int sock_read_wait = 0; |
int sock_read_wait = 0; |
| 292 |
int sock_write_wait = 0; |
int sock_write_wait = 0; |
| 293 |
struct hostent *p_host = NULL; |
struct addrinfo hints, *res = NULL; |
| 294 |
int tos; |
int tos; |
| 295 |
char remote_addr[IP_ADDR_LEN]; |
char remote_addr[INET_ADDRSTRLEN]; |
| 296 |
int remote_port; |
int remote_port; |
| 297 |
time_t t_used = time(NULL); |
char local_addr[INET_ADDRSTRLEN]; |
| 298 |
struct tm *tm_used; |
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; |
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) |
if (user_online_update("BBS_NET") < 0) |
| 312 |
{ |
{ |
| 313 |
log_error("user_online_update(BBS_NET) error\n"); |
log_error("user_online_update(BBS_NET) error\n"); |
| 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(); |
clearscr(); |
| 356 |
|
|
| 357 |
moveto(0, 0); |
moveto(1, 1); |
| 358 |
prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n", |
prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n", |
| 359 |
bbsnet_conf[n].host1, bbsnet_conf[n].ip); |
bbsnet_conf[n].site_name, bbsnet_conf[n].host_name); |
| 360 |
iflush(); |
iflush(); |
| 361 |
|
|
| 362 |
p_host = gethostbyname(bbsnet_conf[n].ip); |
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 (p_host == NULL) |
if ((ret = getaddrinfo(BBS_address, NULL, &hints, &res)) != 0) |
| 368 |
{ |
{ |
| 369 |
prints("\033[1;31m查找主机名失败!\033[m\r\n"); |
log_error("getaddrinfo() error (%d): %s\n", ret, gai_strerror(ret)); |
| 370 |
press_any_key(); |
ret = -1; |
| 371 |
return -1; |
goto cleanup; |
| 372 |
} |
} |
| 373 |
|
|
| 374 |
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
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)\n", 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) |
if (sock < 0) |
| 384 |
{ |
{ |
| 385 |
prints("\033[1;31m无法创建socket!\033[m\r\n"); |
log_error("socket() error (%d)\n", errno); |
| 386 |
press_any_key(); |
ret = -1; |
| 387 |
return -1; |
goto cleanup; |
| 388 |
} |
} |
| 389 |
|
|
| 390 |
sin.sin_family = AF_INET; |
if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) |
|
sin.sin_addr.s_addr = (BBS_address[0] != '\0' ? inet_addr(BBS_address) : INADDR_ANY); |
|
|
sin.sin_port = 0; |
|
|
|
|
|
if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) |
|
| 391 |
{ |
{ |
| 392 |
log_error("Bind address %s:%u failed (%d)\n", |
log_error("bind(%s:%u) error (%d)\n", local_addr, local_port, errno); |
| 393 |
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno); |
ret = -1; |
| 394 |
return -2; |
goto cleanup; |
| 395 |
} |
} |
| 396 |
|
|
| 397 |
memset(&sin, 0, sizeof(sin)); |
freeaddrinfo(res); |
| 398 |
sin.sin_family = AF_INET; |
res = NULL; |
| 399 |
sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0]; |
|
| 400 |
sin.sin_port = htons(bbsnet_conf[n].port); |
memset(&hints, 0, sizeof(hints)); |
| 401 |
|
hints.ai_family = AF_INET; |
| 402 |
strncpy(remote_addr, inet_ntoa(sin.sin_addr), sizeof(remote_addr) - 1); |
hints.ai_flags = AI_NUMERICSERV; |
| 403 |
remote_addr[sizeof(remote_addr) - 1] = '\0'; |
hints.ai_socktype = SOCK_STREAM; |
| 404 |
remote_port = ntohs(sin.sin_port); |
hints.ai_protocol = IPPROTO_TCP; |
| 405 |
|
|
| 406 |
prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n"); |
if ((ret = getaddrinfo(bbsnet_conf[n].host_name, bbsnet_conf[n].port, &hints, &res)) != 0) |
| 407 |
process_bar(0, MAX_PROCESS_BAR_LEN); |
{ |
| 408 |
|
log_error("getaddrinfo() error (%d): %s\n", 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)\n", 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 |
// Set socket as non-blocking |
| 424 |
flags_sock = fcntl(sock, F_GETFL, 0); |
if ((flags_sock = fcntl(sock, F_GETFL, 0)) == -1) |
| 425 |
fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK); |
{ |
| 426 |
|
log_error("fcntl(F_GETFL) error (%d)\n", 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)\n", errno); |
| 433 |
|
ret = -1; |
| 434 |
|
goto cleanup; |
| 435 |
|
} |
| 436 |
|
|
| 437 |
// Set STDIN/STDOUT as non-blocking |
// Set STDIN/STDOUT as non-blocking |
| 438 |
flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0); |
if ((flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1) |
| 439 |
flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0); |
{ |
| 440 |
fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK); |
log_error("fcntl(F_GETFL) error (%d)\n", errno); |
| 441 |
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK); |
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)\n", 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)\n", 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)\n", errno); |
| 459 |
|
ret = -1; |
| 460 |
|
goto cleanup; |
| 461 |
|
} |
| 462 |
|
|
| 463 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 464 |
epollfd = epoll_create1(0); |
epollfd = epoll_create1(0); |
| 465 |
if (epollfd < 0) |
if (epollfd < 0) |
| 466 |
{ |
{ |
| 467 |
log_error("epoll_create1() error (%d)\n", errno); |
log_error("epoll_create1() error (%d)\n", errno); |
| 468 |
return -1; |
ret = -1; |
| 469 |
|
goto cleanup; |
| 470 |
} |
} |
| 471 |
|
|
| 472 |
ev.events = EPOLLOUT | EPOLLET; |
ev.events = EPOLLOUT | EPOLLET; |
| 474 |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1) |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1) |
| 475 |
{ |
{ |
| 476 |
log_error("epoll_ctl(socket) error (%d)\n", errno); |
log_error("epoll_ctl(socket) error (%d)\n", errno); |
| 477 |
|
ret = -1; |
| 478 |
goto cleanup; |
goto cleanup; |
| 479 |
} |
} |
| 480 |
|
|
| 483 |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) |
| 484 |
{ |
{ |
| 485 |
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); |
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); |
| 486 |
|
ret = -1; |
| 487 |
goto cleanup; |
goto cleanup; |
| 488 |
} |
} |
| 489 |
|
#endif |
| 490 |
|
|
| 491 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 492 |
{ |
{ |
| 493 |
if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0) |
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) |
if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS) |
| 501 |
{ |
{ |
| 511 |
else |
else |
| 512 |
{ |
{ |
| 513 |
log_error("connect(socket) error (%d)\n", errno); |
log_error("connect(socket) error (%d)\n", errno); |
|
|
|
| 514 |
prints("\033[1;31m连接失败!\033[m\r\n"); |
prints("\033[1;31m连接失败!\033[m\r\n"); |
| 515 |
press_any_key(); |
press_any_key(); |
| 516 |
|
ret = -1; |
| 517 |
goto cleanup; |
goto cleanup; |
| 518 |
} |
} |
| 519 |
} |
} |
| 520 |
} |
} |
| 521 |
|
|
| 522 |
for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++) |
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 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second |
log_error("clock_gettime() error (%d)\n", errno); |
| 529 |
|
ret = -1; |
| 530 |
|
goto cleanup; |
| 531 |
|
} |
| 532 |
|
ts_now = ts_begin; |
| 533 |
|
|
| 534 |
if (nfds < 0) |
while ((ts_now.tv_sec - ts_begin.tv_sec) + |
| 535 |
|
(ts_now.tv_nsec - ts_begin.tv_nsec) / 1000 / 1000 / 1000 < |
| 536 |
|
REMOTE_CONNECT_TIMEOUT && |
| 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) |
if (errno != EINTR) |
| 554 |
{ |
{ |
| 555 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 556 |
log_error("epoll_wait() error (%d)\n", errno); |
log_error("epoll_wait() error (%d)\n", errno); |
| 557 |
|
#else |
| 558 |
|
log_error("poll() error (%d)\n", errno); |
| 559 |
|
#endif |
| 560 |
break; |
break; |
| 561 |
} |
} |
| 562 |
} |
} |
| 563 |
else if (nfds == 0) // timeout |
else if (ret == 0) // timeout |
| 564 |
{ |
{ |
| 565 |
process_bar(j + 1, MAX_PROCESS_BAR_LEN); |
if (clock_gettime(CLOCK_REALTIME, &ts_now) == -1) |
| 566 |
|
{ |
| 567 |
|
log_error("clock_gettime() error (%d)\n", 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 |
|
|
| 577 |
|
if (progress > progress_last) |
| 578 |
|
{ |
| 579 |
|
progress_last = progress; |
| 580 |
|
progress_bar(progress, PROGRESS_BAR_LEN); |
| 581 |
|
} |
| 582 |
} |
} |
| 583 |
else // ret > 0 |
else // ret > 0 |
| 584 |
{ |
{ |
| 585 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 586 |
{ |
{ |
| 587 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 588 |
if (events[i].data.fd == sock) |
if (events[i].data.fd == sock) |
| 589 |
|
#else |
| 590 |
|
if (pfds[i].fd == sock && (pfds[i].revents & POLLOUT)) |
| 591 |
|
#endif |
| 592 |
{ |
{ |
| 593 |
len = sizeof(error); |
socklen_t len = sizeof(error); |
| 594 |
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0) |
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) |
| 595 |
{ |
{ |
| 596 |
log_error("getsockopt() error (%d) !\n", error); |
log_error("getsockopt() error (%d) !\n", errno); |
| 597 |
|
ret = -1; |
| 598 |
goto cleanup; |
goto cleanup; |
| 599 |
} |
} |
| 600 |
if (error == 0) |
if (error == 0) |
| 601 |
{ |
{ |
| 602 |
sock_connected = 1; |
sock_connected = 1; |
| 603 |
|
break; |
| 604 |
} |
} |
| 605 |
} |
} |
| 606 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 607 |
else if (events[i].data.fd == STDIN_FILENO) |
else if (events[i].data.fd == STDIN_FILENO) |
| 608 |
|
#else |
| 609 |
|
else if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) |
| 610 |
|
#endif |
| 611 |
{ |
{ |
| 612 |
ch = igetch(0); |
do |
| 613 |
|
{ |
| 614 |
|
ch = igetch(0); |
| 615 |
|
} while (ch == 0); |
| 616 |
if (ch == Ctrl('C') || ch == KEY_ESC) |
if (ch == Ctrl('C') || ch == KEY_ESC) |
| 617 |
{ |
{ |
| 618 |
|
ret = 0; |
| 619 |
goto cleanup; |
goto cleanup; |
| 620 |
} |
} |
| 621 |
} |
} |
| 624 |
} |
} |
| 625 |
if (SYS_server_exit) |
if (SYS_server_exit) |
| 626 |
{ |
{ |
| 627 |
|
ret = 0; |
| 628 |
goto cleanup; |
goto cleanup; |
| 629 |
} |
} |
| 630 |
if (!sock_connected) |
if (!sock_connected) |
| 631 |
{ |
{ |
| 632 |
|
progress_bar(100, PROGRESS_BAR_LEN); |
| 633 |
prints("\033[1;31m连接失败!\033[m\r\n"); |
prints("\033[1;31m连接失败!\033[m\r\n"); |
| 634 |
press_any_key(); |
press_any_key(); |
| 635 |
|
ret = -1; |
| 636 |
goto cleanup; |
goto cleanup; |
| 637 |
} |
} |
| 638 |
|
|
| 642 |
log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno); |
log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno); |
| 643 |
} |
} |
| 644 |
|
|
| 645 |
|
sock_len = sizeof(sin); |
| 646 |
|
if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0) |
| 647 |
|
{ |
| 648 |
|
log_error("getsockname() error: %d", errno); |
| 649 |
|
ret = -1; |
| 650 |
|
goto cleanup; |
| 651 |
|
} |
| 652 |
|
|
| 653 |
|
if (inet_ntop(AF_INET, &(sin.sin_addr), local_addr, sizeof(local_addr)) == NULL) |
| 654 |
|
{ |
| 655 |
|
log_error("inet_ntop() error (%d)\n", errno); |
| 656 |
|
ret = -1; |
| 657 |
|
goto cleanup; |
| 658 |
|
} |
| 659 |
|
local_port = ntohs(sin.sin_port); |
| 660 |
|
|
| 661 |
|
if (bbsnet_conf[n].use_ssh) |
| 662 |
|
{ |
| 663 |
|
outbound_session = ssh_new(); |
| 664 |
|
if (outbound_session == NULL) |
| 665 |
|
{ |
| 666 |
|
log_error("ssh_new() error\n"); |
| 667 |
|
ret = -1; |
| 668 |
|
goto cleanup; |
| 669 |
|
} |
| 670 |
|
|
| 671 |
|
if (ssh_options_set(outbound_session, SSH_OPTIONS_FD, &sock) < 0 || |
| 672 |
|
ssh_options_set(outbound_session, SSH_OPTIONS_PROCESS_CONFIG, &ssh_process_config) < 0 || |
| 673 |
|
ssh_options_set(outbound_session, SSH_OPTIONS_KNOWNHOSTS, SSH_KNOWN_HOSTS_FILE) < 0 || |
| 674 |
|
ssh_options_set(outbound_session, SSH_OPTIONS_HOST, bbsnet_conf[n].host_name) < 0 || |
| 675 |
|
ssh_options_set(outbound_session, SSH_OPTIONS_USER, remote_user) < 0 || |
| 676 |
|
ssh_options_set(outbound_session, SSH_OPTIONS_HOSTKEYS, "+ssh-ed25519,ecdsa-sha2-nistp256,ssh-rsa") < 0 || |
| 677 |
|
ssh_options_set(outbound_session, SSH_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0) |
| 678 |
|
{ |
| 679 |
|
log_error("Error setting SSH options: %s\n", ssh_get_error(outbound_session)); |
| 680 |
|
ret = -1; |
| 681 |
|
goto cleanup; |
| 682 |
|
} |
| 683 |
|
|
| 684 |
|
ssh_set_blocking(outbound_session, 0); |
| 685 |
|
|
| 686 |
|
t_begin = time(NULL); |
| 687 |
|
ret = SSH_ERROR; |
| 688 |
|
while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) |
| 689 |
|
{ |
| 690 |
|
ret = ssh_connect(outbound_session); |
| 691 |
|
if (ret == SSH_OK) |
| 692 |
|
{ |
| 693 |
|
break; |
| 694 |
|
} |
| 695 |
|
else if (ret == SSH_AGAIN) |
| 696 |
|
{ |
| 697 |
|
// log_debug("ssh_connect() error: SSH_AGAIN\n"); |
| 698 |
|
} |
| 699 |
|
else // if (ret == SSH_ERROR) |
| 700 |
|
{ |
| 701 |
|
log_error("ssh_connect() error: SSH_ERROR\n"); |
| 702 |
|
ret = -1; |
| 703 |
|
goto cleanup; |
| 704 |
|
} |
| 705 |
|
} |
| 706 |
|
if (ret != SSH_OK) |
| 707 |
|
{ |
| 708 |
|
prints("\033[1;31m连接超时!\033[m\r\n"); |
| 709 |
|
press_any_key(); |
| 710 |
|
ret = -1; |
| 711 |
|
goto cleanup; |
| 712 |
|
} |
| 713 |
|
|
| 714 |
|
ret = ssh_session_is_known_server(outbound_session); |
| 715 |
|
switch (ret) |
| 716 |
|
{ |
| 717 |
|
case SSH_KNOWN_HOSTS_NOT_FOUND: |
| 718 |
|
case SSH_KNOWN_HOSTS_UNKNOWN: |
| 719 |
|
if (ssh_session_update_known_hosts(outbound_session) != SSH_OK) |
| 720 |
|
{ |
| 721 |
|
log_error("ssh_session_update_known_hosts(%s) error\n", bbsnet_conf[n].host_name); |
| 722 |
|
prints("\033[1;31m无法添加服务器证书\033[m\r\n"); |
| 723 |
|
press_any_key(); |
| 724 |
|
ret = -1; |
| 725 |
|
goto cleanup; |
| 726 |
|
} |
| 727 |
|
log_common("SSH key of (%s) is added into %s\n", bbsnet_conf[n].host_name, SSH_KNOWN_HOSTS_FILE); |
| 728 |
|
case SSH_KNOWN_HOSTS_OK: |
| 729 |
|
break; |
| 730 |
|
case SSH_KNOWN_HOSTS_CHANGED: |
| 731 |
|
case SSH_KNOWN_HOSTS_OTHER: |
| 732 |
|
log_error("ssh_session_is_known_server(%s) error: %d\n", bbsnet_conf[n].host_name, ret); |
| 733 |
|
prints("\033[1;31m服务器证书已变更\033[m\r\n"); |
| 734 |
|
press_any_key(); |
| 735 |
|
ret = -1; |
| 736 |
|
goto cleanup; |
| 737 |
|
} |
| 738 |
|
|
| 739 |
|
ret = SSH_AUTH_ERROR; |
| 740 |
|
while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) |
| 741 |
|
{ |
| 742 |
|
ret = ssh_userauth_password(outbound_session, NULL, remote_pass); |
| 743 |
|
if (ret == SSH_AUTH_SUCCESS) |
| 744 |
|
{ |
| 745 |
|
break; |
| 746 |
|
} |
| 747 |
|
else if (ret == SSH_AUTH_AGAIN) |
| 748 |
|
{ |
| 749 |
|
// log_debug("ssh_userauth_password() error: SSH_AUTH_AGAIN\n"); |
| 750 |
|
} |
| 751 |
|
else if (ret == SSH_AUTH_ERROR) |
| 752 |
|
{ |
| 753 |
|
log_error("ssh_userauth_password() error: SSH_AUTH_ERROR\n"); |
| 754 |
|
ret = -1; |
| 755 |
|
goto cleanup; |
| 756 |
|
} |
| 757 |
|
else // if (ret == SSH_AUTH_DENIED) |
| 758 |
|
{ |
| 759 |
|
log_error("ssh_userauth_password() error: SSH_AUTH_DENIED\n"); |
| 760 |
|
prints("\033[1;31m身份验证失败!\033[m\r\n"); |
| 761 |
|
press_any_key(); |
| 762 |
|
ret = -1; |
| 763 |
|
goto cleanup; |
| 764 |
|
} |
| 765 |
|
} |
| 766 |
|
if (ret != SSH_AUTH_SUCCESS) |
| 767 |
|
{ |
| 768 |
|
prints("\033[1;31m连接超时!\033[m\r\n"); |
| 769 |
|
press_any_key(); |
| 770 |
|
ret = -1; |
| 771 |
|
goto cleanup; |
| 772 |
|
} |
| 773 |
|
|
| 774 |
|
outbound_channel = ssh_channel_new(outbound_session); |
| 775 |
|
if (outbound_channel == NULL) |
| 776 |
|
{ |
| 777 |
|
log_error("ssh_channel_new() error\n"); |
| 778 |
|
ret = -1; |
| 779 |
|
goto cleanup; |
| 780 |
|
} |
| 781 |
|
|
| 782 |
|
ret = SSH_ERROR; |
| 783 |
|
while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) |
| 784 |
|
{ |
| 785 |
|
ret = ssh_channel_open_session(outbound_channel); |
| 786 |
|
if (ret == SSH_OK) |
| 787 |
|
{ |
| 788 |
|
break; |
| 789 |
|
} |
| 790 |
|
else if (ret == SSH_AGAIN) |
| 791 |
|
{ |
| 792 |
|
// log_debug("ssh_channel_open_session() error: SSH_AGAIN\n"); |
| 793 |
|
} |
| 794 |
|
else // if (ret == SSH_ERROR) |
| 795 |
|
{ |
| 796 |
|
log_error("ssh_channel_open_session() error: SSH_ERROR\n"); |
| 797 |
|
ret = -1; |
| 798 |
|
goto cleanup; |
| 799 |
|
} |
| 800 |
|
} |
| 801 |
|
if (ret != SSH_OK) |
| 802 |
|
{ |
| 803 |
|
prints("\033[1;31m连接超时!\033[m\r\n"); |
| 804 |
|
press_any_key(); |
| 805 |
|
ret = -1; |
| 806 |
|
goto cleanup; |
| 807 |
|
} |
| 808 |
|
|
| 809 |
|
ret = SSH_ERROR; |
| 810 |
|
while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) |
| 811 |
|
{ |
| 812 |
|
ret = ssh_channel_request_pty(outbound_channel); |
| 813 |
|
if (ret == SSH_OK) |
| 814 |
|
{ |
| 815 |
|
break; |
| 816 |
|
} |
| 817 |
|
else if (ret == SSH_AGAIN) |
| 818 |
|
{ |
| 819 |
|
// log_debug("ssh_channel_request_pty() error: SSH_AGAIN\n"); |
| 820 |
|
} |
| 821 |
|
else // if (ret == SSH_ERROR) |
| 822 |
|
{ |
| 823 |
|
log_error("ssh_channel_request_pty() error: SSH_ERROR\n"); |
| 824 |
|
ret = -1; |
| 825 |
|
goto cleanup; |
| 826 |
|
} |
| 827 |
|
} |
| 828 |
|
if (ret != SSH_OK) |
| 829 |
|
{ |
| 830 |
|
prints("\033[1;31m连接超时!\033[m\r\n"); |
| 831 |
|
press_any_key(); |
| 832 |
|
ret = -1; |
| 833 |
|
goto cleanup; |
| 834 |
|
} |
| 835 |
|
|
| 836 |
|
ret = SSH_ERROR; |
| 837 |
|
while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) |
| 838 |
|
{ |
| 839 |
|
ret = ssh_channel_request_shell(outbound_channel); |
| 840 |
|
if (ret == SSH_OK) |
| 841 |
|
{ |
| 842 |
|
break; |
| 843 |
|
} |
| 844 |
|
else if (ret == SSH_AGAIN) |
| 845 |
|
{ |
| 846 |
|
// log_debug("ssh_channel_request_shell() error: SSH_AGAIN\n"); |
| 847 |
|
} |
| 848 |
|
else // if (ret == SSH_ERROR) |
| 849 |
|
{ |
| 850 |
|
log_error("ssh_channel_request_shell() error: SSH_ERROR\n"); |
| 851 |
|
ret = -1; |
| 852 |
|
goto cleanup; |
| 853 |
|
} |
| 854 |
|
} |
| 855 |
|
if (ret != SSH_OK) |
| 856 |
|
{ |
| 857 |
|
prints("\033[1;31m连接超时!\033[m\r\n"); |
| 858 |
|
press_any_key(); |
| 859 |
|
ret = -1; |
| 860 |
|
goto cleanup; |
| 861 |
|
} |
| 862 |
|
} |
| 863 |
|
|
| 864 |
prints("\033[1;31m连接成功!\033[m\r\n"); |
prints("\033[1;31m连接成功!\033[m\r\n"); |
| 865 |
iflush(); |
iflush(); |
| 866 |
log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port); |
log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n", |
| 867 |
|
remote_addr, remote_port, local_addr, local_port, BBS_username); |
| 868 |
|
|
| 869 |
|
snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset, |
| 870 |
|
(strcasecmp(stdio_charset, bbsnet_conf[n].charset) == 0 ? "" : "//IGNORE")); |
| 871 |
|
input_cd = iconv_open(tocode, stdio_charset); |
| 872 |
|
if (input_cd == (iconv_t)(-1)) |
| 873 |
|
{ |
| 874 |
|
log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno); |
| 875 |
|
ret = -1; |
| 876 |
|
goto cleanup; |
| 877 |
|
} |
| 878 |
|
|
| 879 |
|
snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset, |
| 880 |
|
(strcasecmp(bbsnet_conf[n].charset, stdio_charset) == 0 ? "" : "//TRANSLIT")); |
| 881 |
|
output_cd = iconv_open(tocode, bbsnet_conf[n].charset); |
| 882 |
|
if (output_cd == (iconv_t)(-1)) |
| 883 |
|
{ |
| 884 |
|
log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno); |
| 885 |
|
ret = -1; |
| 886 |
|
goto cleanup; |
| 887 |
|
} |
| 888 |
|
|
| 889 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 890 |
ev.events = EPOLLIN | EPOLLOUT | EPOLLET; |
ev.events = EPOLLIN | EPOLLOUT | EPOLLET; |
| 891 |
ev.data.fd = sock; |
ev.data.fd = sock; |
| 892 |
if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1) |
if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1) |
| 893 |
{ |
{ |
| 894 |
log_error("epoll_ctl(socket) error (%d)\n", errno); |
log_error("epoll_ctl(socket) error (%d)\n", errno); |
| 895 |
|
ret = -1; |
| 896 |
goto cleanup; |
goto cleanup; |
| 897 |
} |
} |
| 898 |
|
|
| 901 |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) |
| 902 |
{ |
{ |
| 903 |
log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); |
log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); |
| 904 |
|
ret = -1; |
| 905 |
goto cleanup; |
goto cleanup; |
| 906 |
} |
} |
| 907 |
|
#endif |
| 908 |
|
|
| 909 |
BBS_last_access_tm = t_used = time(NULL); |
BBS_last_access_tm = t_begin = time(NULL); |
| 910 |
loop = 1; |
loop = 1; |
| 911 |
|
|
| 912 |
while (loop && !SYS_server_exit) |
while (loop && !SYS_server_exit) |
| 913 |
{ |
{ |
| 914 |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
| 915 |
{ |
{ |
| 916 |
log_error("SSH channel is closed\n"); |
log_debug("SSH channel is closed\n"); |
| 917 |
loop = 0; |
loop = 0; |
| 918 |
break; |
break; |
| 919 |
} |
} |
| 920 |
|
|
| 921 |
|
if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(outbound_channel)) |
| 922 |
|
{ |
| 923 |
|
log_debug("Outbound channel is closed\n"); |
| 924 |
|
loop = 0; |
| 925 |
|
break; |
| 926 |
|
} |
| 927 |
|
|
| 928 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 929 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
| 930 |
|
ret = nfds; |
| 931 |
|
#else |
| 932 |
|
pfds[0].fd = STDIN_FILENO; |
| 933 |
|
pfds[0].events = POLLIN; |
| 934 |
|
pfds[1].fd = sock; |
| 935 |
|
pfds[1].events = POLLIN | POLLOUT; |
| 936 |
|
pfds[2].fd = STDOUT_FILENO; |
| 937 |
|
pfds[2].events = POLLOUT; |
| 938 |
|
nfds = 3; |
| 939 |
|
ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second |
| 940 |
|
#endif |
| 941 |
|
|
| 942 |
if (nfds < 0) |
if (ret < 0) |
| 943 |
{ |
{ |
| 944 |
if (errno != EINTR) |
if (errno != EINTR) |
| 945 |
{ |
{ |
| 946 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 947 |
log_error("epoll_wait() error (%d)\n", errno); |
log_error("epoll_wait() error (%d)\n", errno); |
| 948 |
|
#else |
| 949 |
|
log_error("poll() error (%d)\n", errno); |
| 950 |
|
#endif |
| 951 |
break; |
break; |
| 952 |
} |
} |
| 953 |
continue; |
continue; |
| 954 |
} |
} |
| 955 |
else if (nfds == 0) // timeout |
else if (ret == 0) // timeout |
| 956 |
{ |
{ |
| 957 |
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) |
| 958 |
{ |
{ |
| 959 |
break; |
break; |
| 960 |
} |
} |
| 962 |
|
|
| 963 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 964 |
{ |
{ |
| 965 |
if (events[i].data.fd == STDIN_FILENO) |
#ifdef HAVE_SYS_EPOLL_H |
| 966 |
|
if (events[i].events & (EPOLLHUP | EPOLLERR)) |
| 967 |
|
#else |
| 968 |
|
if (pfds[i].revents & (POLLHUP | POLLERR)) |
| 969 |
|
#endif |
| 970 |
|
{ |
| 971 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 972 |
|
log_debug("FD (%d) error events (%d)\n", events[i].data.fd, events[i].events); |
| 973 |
|
#else |
| 974 |
|
log_debug("FD (%d) error events (%d)\n", pfds[i].fd, pfds[i].revents); |
| 975 |
|
#endif |
| 976 |
|
loop = 0; |
| 977 |
|
break; |
| 978 |
|
} |
| 979 |
|
|
| 980 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 981 |
|
if (events[i].data.fd == STDIN_FILENO && (events[i].events & EPOLLIN)) |
| 982 |
|
#else |
| 983 |
|
if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) |
| 984 |
|
#endif |
| 985 |
{ |
{ |
| 986 |
stdin_read_wait = 1; |
stdin_read_wait = 1; |
| 987 |
} |
} |
| 988 |
|
|
| 989 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 990 |
if (events[i].data.fd == sock) |
if (events[i].data.fd == sock) |
| 991 |
|
#else |
| 992 |
|
if (pfds[i].fd == sock) |
| 993 |
|
#endif |
| 994 |
{ |
{ |
| 995 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 996 |
if (events[i].events & EPOLLIN) |
if (events[i].events & EPOLLIN) |
| 997 |
|
#else |
| 998 |
|
if (pfds[i].revents & POLLIN) |
| 999 |
|
#endif |
| 1000 |
{ |
{ |
| 1001 |
sock_read_wait = 1; |
sock_read_wait = 1; |
| 1002 |
} |
} |
| 1003 |
|
|
| 1004 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 1005 |
if (events[i].events & EPOLLOUT) |
if (events[i].events & EPOLLOUT) |
| 1006 |
|
#else |
| 1007 |
|
if (pfds[i].revents & POLLOUT) |
| 1008 |
|
#endif |
| 1009 |
{ |
{ |
| 1010 |
sock_write_wait = 1; |
sock_write_wait = 1; |
| 1011 |
} |
} |
| 1012 |
} |
} |
| 1013 |
|
|
| 1014 |
if (events[i].data.fd == STDOUT_FILENO) |
#ifdef HAVE_SYS_EPOLL_H |
| 1015 |
|
if (events[i].data.fd == STDOUT_FILENO && (events[i].events & EPOLLOUT)) |
| 1016 |
|
#else |
| 1017 |
|
if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT)) |
| 1018 |
|
#endif |
| 1019 |
{ |
{ |
| 1020 |
stdout_write_wait = 1; |
stdout_write_wait = 1; |
| 1021 |
} |
} |
| 1030 |
ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0); |
ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0); |
| 1031 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 1032 |
{ |
{ |
| 1033 |
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
log_debug("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
| 1034 |
loop = 0; |
loop = 0; |
| 1035 |
break; |
break; |
| 1036 |
} |
} |
| 1042 |
} |
} |
| 1043 |
else if (ret == 0) |
else if (ret == 0) |
| 1044 |
{ |
{ |
| 1045 |
|
// Send NO-OP to remote server |
| 1046 |
|
input_buf[input_buf_len] = '\0'; |
| 1047 |
|
input_buf_len++; |
| 1048 |
|
|
| 1049 |
|
BBS_last_access_tm = time(NULL); |
| 1050 |
stdin_read_wait = 0; |
stdin_read_wait = 0; |
| 1051 |
break; // Check whether channel is still open |
break; // Check whether channel is still open |
| 1052 |
} |
} |
| 1075 |
} |
} |
| 1076 |
else if (ret == 0) // broken pipe |
else if (ret == 0) // broken pipe |
| 1077 |
{ |
{ |
| 1078 |
#ifdef _DEBUG |
log_debug("read(STDIN) EOF\n"); |
|
log_error("read(STDIN) EOF\n"); |
|
|
#endif |
|
| 1079 |
stdin_read_wait = 0; |
stdin_read_wait = 0; |
| 1080 |
loop = 0; |
loop = 0; |
| 1081 |
break; |
break; |
| 1098 |
|
|
| 1099 |
if (sock_write_wait) |
if (sock_write_wait) |
| 1100 |
{ |
{ |
| 1101 |
while (input_buf_offset < input_buf_len && !SYS_server_exit) |
if (input_buf_offset < input_buf_len) |
| 1102 |
|
{ |
| 1103 |
|
// For debug |
| 1104 |
|
#ifdef _DEBUG |
| 1105 |
|
for (int j = input_buf_offset; j < input_buf_len; j++) |
| 1106 |
|
{ |
| 1107 |
|
log_debug("input: <--[%u]\n", (input_buf[j] + 256) % 256); |
| 1108 |
|
} |
| 1109 |
|
#endif |
| 1110 |
|
|
| 1111 |
|
ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len); |
| 1112 |
|
if (ret < 0) |
| 1113 |
|
{ |
| 1114 |
|
log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len); |
| 1115 |
|
input_buf_len = input_buf_offset; // Discard invalid sequence |
| 1116 |
|
} |
| 1117 |
|
|
| 1118 |
|
// For debug |
| 1119 |
|
#ifdef _DEBUG |
| 1120 |
|
for (int j = input_conv_offset; j < input_conv_len; j++) |
| 1121 |
|
{ |
| 1122 |
|
log_debug("input_conv: <--[%u]\n", (input_conv[j] + 256) % 256); |
| 1123 |
|
} |
| 1124 |
|
#endif |
| 1125 |
|
} |
| 1126 |
|
|
| 1127 |
|
while (input_conv_offset < input_conv_len && !SYS_server_exit) |
| 1128 |
{ |
{ |
| 1129 |
ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset)); |
if (bbsnet_conf[n].use_ssh) |
| 1130 |
|
{ |
| 1131 |
|
ret = ssh_channel_write(outbound_channel, input_conv + input_conv_offset, (uint32_t)(input_conv_len - input_conv_offset)); |
| 1132 |
|
if (ret == SSH_ERROR) |
| 1133 |
|
{ |
| 1134 |
|
log_debug("ssh_channel_write() error: %s\n", ssh_get_error(outbound_session)); |
| 1135 |
|
loop = 0; |
| 1136 |
|
break; |
| 1137 |
|
} |
| 1138 |
|
} |
| 1139 |
|
else |
| 1140 |
|
{ |
| 1141 |
|
ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset)); |
| 1142 |
|
} |
| 1143 |
if (ret < 0) |
if (ret < 0) |
| 1144 |
{ |
{ |
| 1145 |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 1153 |
} |
} |
| 1154 |
else |
else |
| 1155 |
{ |
{ |
| 1156 |
log_error("write(socket) error (%d)\n", errno); |
log_debug("write(socket) error (%d)\n", errno); |
| 1157 |
loop = 0; |
loop = 0; |
| 1158 |
break; |
break; |
| 1159 |
} |
} |
| 1160 |
} |
} |
| 1161 |
else if (ret == 0) // broken pipe |
else if (ret == 0) // broken pipe |
| 1162 |
{ |
{ |
| 1163 |
#ifdef _DEBUG |
log_debug("write(socket) EOF\n"); |
|
log_error("write(socket) EOF\n"); |
|
|
#endif |
|
| 1164 |
sock_write_wait = 0; |
sock_write_wait = 0; |
| 1165 |
loop = 0; |
loop = 0; |
| 1166 |
break; |
break; |
| 1167 |
} |
} |
| 1168 |
else |
else |
| 1169 |
{ |
{ |
| 1170 |
input_buf_offset += ret; |
input_conv_offset += ret; |
| 1171 |
if (input_buf_offset >= input_buf_len) // Output buffer complete |
if (input_conv_offset >= input_conv_len) // Output buffer complete |
| 1172 |
{ |
{ |
| 1173 |
input_buf_offset = 0; |
input_conv_offset = 0; |
| 1174 |
input_buf_len = 0; |
input_conv_len = 0; |
| 1175 |
break; |
break; |
| 1176 |
} |
} |
| 1177 |
continue; |
continue; |
| 1183 |
{ |
{ |
| 1184 |
while (output_buf_len < sizeof(output_buf) && !SYS_server_exit) |
while (output_buf_len < sizeof(output_buf) && !SYS_server_exit) |
| 1185 |
{ |
{ |
| 1186 |
ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len); |
if (bbsnet_conf[n].use_ssh) |
| 1187 |
|
{ |
| 1188 |
|
ret = ssh_channel_read_nonblocking(outbound_channel, output_buf + output_buf_len, |
| 1189 |
|
(uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0); |
| 1190 |
|
if (ret == SSH_ERROR) |
| 1191 |
|
{ |
| 1192 |
|
log_debug("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(outbound_session)); |
| 1193 |
|
loop = 0; |
| 1194 |
|
break; |
| 1195 |
|
} |
| 1196 |
|
else if (ret == SSH_EOF) |
| 1197 |
|
{ |
| 1198 |
|
sock_read_wait = 0; |
| 1199 |
|
loop = 0; |
| 1200 |
|
break; |
| 1201 |
|
} |
| 1202 |
|
else if (ret == 0) |
| 1203 |
|
{ |
| 1204 |
|
sock_read_wait = 0; |
| 1205 |
|
break; |
| 1206 |
|
} |
| 1207 |
|
} |
| 1208 |
|
else |
| 1209 |
|
{ |
| 1210 |
|
ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len); |
| 1211 |
|
} |
| 1212 |
if (ret < 0) |
if (ret < 0) |
| 1213 |
{ |
{ |
| 1214 |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 1222 |
} |
} |
| 1223 |
else |
else |
| 1224 |
{ |
{ |
| 1225 |
log_error("read(socket) error (%d)\n", errno); |
log_debug("read(socket) error (%d)\n", errno); |
| 1226 |
loop = 0; |
loop = 0; |
| 1227 |
break; |
break; |
| 1228 |
} |
} |
| 1229 |
} |
} |
| 1230 |
else if (ret == 0) // broken pipe |
else if (ret == 0) // broken pipe |
| 1231 |
{ |
{ |
| 1232 |
#ifdef _DEBUG |
log_debug("read(socket) EOF\n"); |
|
log_error("read(socket) EOF\n"); |
|
|
#endif |
|
| 1233 |
sock_read_wait = 0; |
sock_read_wait = 0; |
| 1234 |
loop = 0; |
loop = 0; |
| 1235 |
break; |
break; |
| 1244 |
|
|
| 1245 |
if (stdout_write_wait) |
if (stdout_write_wait) |
| 1246 |
{ |
{ |
| 1247 |
while (output_buf_offset < output_buf_len && !SYS_server_exit) |
if (output_buf_offset < output_buf_len) |
| 1248 |
|
{ |
| 1249 |
|
ret = io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len); |
| 1250 |
|
if (ret < 0) |
| 1251 |
|
{ |
| 1252 |
|
log_error("io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len); |
| 1253 |
|
output_buf_len = output_buf_offset; // Discard invalid sequence |
| 1254 |
|
} |
| 1255 |
|
} |
| 1256 |
|
|
| 1257 |
|
while (output_conv_offset < output_conv_len && !SYS_server_exit) |
| 1258 |
{ |
{ |
| 1259 |
if (SSH_v2) |
if (SSH_v2) |
| 1260 |
{ |
{ |
| 1261 |
ret = ssh_channel_write(SSH_channel, output_buf + output_buf_offset, (uint32_t)(output_buf_len - output_buf_offset)); |
ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset)); |
| 1262 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 1263 |
{ |
{ |
| 1264 |
log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); |
log_debug("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); |
| 1265 |
loop = 0; |
loop = 0; |
| 1266 |
break; |
break; |
| 1267 |
} |
} |
| 1268 |
} |
} |
| 1269 |
else |
else |
| 1270 |
{ |
{ |
| 1271 |
ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset)); |
ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset)); |
| 1272 |
} |
} |
| 1273 |
if (ret < 0) |
if (ret < 0) |
| 1274 |
{ |
{ |
| 1283 |
} |
} |
| 1284 |
else |
else |
| 1285 |
{ |
{ |
| 1286 |
log_error("write(STDOUT) error (%d)\n", errno); |
log_debug("write(STDOUT) error (%d)\n", errno); |
| 1287 |
loop = 0; |
loop = 0; |
| 1288 |
break; |
break; |
| 1289 |
} |
} |
| 1290 |
} |
} |
| 1291 |
else if (ret == 0) // broken pipe |
else if (ret == 0) // broken pipe |
| 1292 |
{ |
{ |
| 1293 |
#ifdef _DEBUG |
log_debug("write(STDOUT) EOF\n"); |
|
log_error("write(STDOUT) EOF\n"); |
|
|
#endif |
|
| 1294 |
stdout_write_wait = 0; |
stdout_write_wait = 0; |
| 1295 |
loop = 0; |
loop = 0; |
| 1296 |
break; |
break; |
| 1297 |
} |
} |
| 1298 |
else |
else |
| 1299 |
{ |
{ |
| 1300 |
output_buf_offset += ret; |
output_conv_offset += ret; |
| 1301 |
if (output_buf_offset >= output_buf_len) // Output buffer complete |
if (output_conv_offset >= output_conv_len) // Output buffer complete |
| 1302 |
{ |
{ |
| 1303 |
output_buf_offset = 0; |
output_conv_offset = 0; |
| 1304 |
output_buf_len = 0; |
output_conv_len = 0; |
| 1305 |
break; |
break; |
| 1306 |
} |
} |
| 1307 |
continue; |
continue; |
| 1310 |
} |
} |
| 1311 |
} |
} |
| 1312 |
|
|
| 1313 |
|
ret = 1; // Normal disconnect |
| 1314 |
|
BBS_last_access_tm = time(NULL); |
| 1315 |
|
t_used = BBS_last_access_tm - t_begin; |
| 1316 |
|
log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n", |
| 1317 |
|
t_used / 86400, t_used % 86400 / 3600, t_used % 3600 / 60, t_used % 60); |
| 1318 |
|
|
| 1319 |
cleanup: |
cleanup: |
| 1320 |
if (close(epollfd) < 0) |
// Clear sensitive data |
| 1321 |
|
memset(remote_pass, 0, sizeof(remote_pass)); |
| 1322 |
|
memset(remote_user, 0, sizeof(remote_user)); |
| 1323 |
|
|
| 1324 |
|
if (input_cd != (iconv_t)(-1)) |
| 1325 |
|
{ |
| 1326 |
|
iconv_close(input_cd); |
| 1327 |
|
} |
| 1328 |
|
if (output_cd != (iconv_t)(-1)) |
| 1329 |
|
{ |
| 1330 |
|
iconv_close(output_cd); |
| 1331 |
|
} |
| 1332 |
|
|
| 1333 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 1334 |
|
if (epollfd != -1 && close(epollfd) < 0) |
| 1335 |
{ |
{ |
| 1336 |
log_error("close(epoll) error (%d)\n"); |
log_error("close(epoll) error (%d)\n"); |
| 1337 |
} |
} |
| 1338 |
|
#endif |
| 1339 |
|
|
| 1340 |
// Restore STDIN/STDOUT flags |
if (bbsnet_conf[n].use_ssh) |
| 1341 |
fcntl(STDIN_FILENO, F_SETFL, flags_stdin); |
{ |
| 1342 |
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout); |
if (outbound_channel != NULL) |
| 1343 |
|
{ |
| 1344 |
|
ssh_channel_send_eof(outbound_channel); |
| 1345 |
|
ssh_channel_close(outbound_channel); |
| 1346 |
|
ssh_channel_free(outbound_channel); |
| 1347 |
|
} |
| 1348 |
|
if (outbound_session != NULL) |
| 1349 |
|
{ |
| 1350 |
|
ssh_disconnect(outbound_session); |
| 1351 |
|
ssh_free(outbound_session); |
| 1352 |
|
} |
| 1353 |
|
} |
| 1354 |
|
|
| 1355 |
// Restore socket flags |
// Restore STDIN/STDOUT flags |
| 1356 |
fcntl(sock, F_SETFL, flags_sock); |
if (flags_stdin != -1 && fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1) |
| 1357 |
|
{ |
| 1358 |
|
log_error("fcntl(F_SETFL) error (%d)\n", errno); |
| 1359 |
|
} |
| 1360 |
|
if (flags_stdout != -1 && fcntl(STDOUT_FILENO, F_SETFL, flags_stdout) == -1) |
| 1361 |
|
{ |
| 1362 |
|
log_error("fcntl(F_SETFL) error (%d)\n", errno); |
| 1363 |
|
} |
| 1364 |
|
|
| 1365 |
if (close(sock) == -1) |
if (sock != -1 && close(sock) == -1) |
| 1366 |
{ |
{ |
| 1367 |
log_error("Close socket failed\n"); |
log_error("Close socket failed\n"); |
| 1368 |
} |
} |
| 1369 |
|
|
| 1370 |
t_used = time(NULL) - t_used; |
if (res) |
| 1371 |
tm_used = gmtime(&t_used); |
{ |
| 1372 |
|
freeaddrinfo(res); |
| 1373 |
log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n", |
} |
|
tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min, |
|
|
tm_used->tm_sec); |
|
| 1374 |
|
|
| 1375 |
return 0; |
return ret; |
| 1376 |
} |
} |
| 1377 |
|
|
| 1378 |
static int |
static int bbsnet_refresh() |
|
bbsnet_refresh() |
|
| 1379 |
{ |
{ |
| 1380 |
clearscr(); |
clearscr(); |
| 1381 |
moveto(1, 0); |
|
| 1382 |
prints(" ----------------------------------------------------------------------------- "); |
moveto(1, 1); |
| 1383 |
|
prints(" ------------------------------------------------------------------------------ "); |
| 1384 |
for (int i = 2; i < 19; i++) |
for (int i = 2; i < 19; i++) |
| 1385 |
{ |
{ |
| 1386 |
moveto(i, 0); |
moveto(i, 1); |
| 1387 |
prints("|"); |
prints("|"); |
| 1388 |
moveto(i, 79); |
moveto(i, 80); |
| 1389 |
prints("|"); |
prints("|"); |
| 1390 |
} |
} |
| 1391 |
moveto(19, 0); |
moveto(19, 1); |
| 1392 |
prints("|-----------------------------------------------------------------------------|"); |
prints("|------------------------------------------------------------------------------|"); |
| 1393 |
moveto(22, 0); |
moveto(22, 1); |
| 1394 |
prints(" ----------------------------------------------------------------------------- "); |
prints(" ------------------------------------------------------------------------------ "); |
| 1395 |
moveto(23, 0); |
moveto(23, 1); |
| 1396 |
prints(" [\x1b[1;32mCtrl+C\x1b[m]退出"); |
prints(" [\033[1;32mCtrl+C\033[m]退出"); |
| 1397 |
|
|
| 1398 |
iflush(); |
iflush(); |
| 1399 |
|
|
| 1400 |
return 0; |
return 0; |
| 1401 |
} |
} |
| 1402 |
|
|
| 1403 |
int bbsnet_selchange() |
static int bbsnet_selchange() |
| 1404 |
{ |
{ |
| 1405 |
int i = bbsnet_menu.menu_item_pos[0]; |
int i = bbsnet_menu.menu_item_pos[0]; |
| 1406 |
|
|
| 1407 |
moveto(20, 0); |
moveto(20, 1); |
| 1408 |
clrtoeol(); |
clrtoeol(); |
| 1409 |
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%*s 类型: \033[1;33m%s\033[m", |
| 1410 |
bbsnet_conf[i].host2, bbsnet_conf[i].host1); |
bbsnet_conf[i].org_name, 20 - str_length(bbsnet_conf[i].org_name, 1), "", |
| 1411 |
moveto(20, 79); |
bbsnet_conf[i].site_name, 20 - str_length(bbsnet_conf[i].site_name, 1), "", |
| 1412 |
|
(bbsnet_conf[i].use_ssh ? "SSH" : "Telnet")); |
| 1413 |
|
moveto(20, 80); |
| 1414 |
prints("|"); |
prints("|"); |
| 1415 |
moveto(21, 0); |
moveto(21, 1); |
| 1416 |
clrtoeol(); |
clrtoeol(); |
| 1417 |
prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[i].ip); |
prints("|\033[1m连往: \033[1;33m%-20s\033[m 端口: \033[1;33m%-5s\033[m 编码: \033[1;33m%s\033[m", |
| 1418 |
if (bbsnet_conf[i].port != 23) |
bbsnet_conf[i].host_name, bbsnet_conf[i].port, bbsnet_conf[i].charset); |
| 1419 |
{ |
moveto(21, 80); |
|
prints(" %d", bbsnet_conf[i].port); |
|
|
} |
|
|
prints("\x1b[m"); |
|
|
moveto(21, 79); |
|
| 1420 |
prints("|"); |
prints("|"); |
| 1421 |
iflush(); |
iflush(); |
| 1422 |
|
|
| 1427 |
{ |
{ |
| 1428 |
int ch, i; |
int ch, i; |
| 1429 |
|
|
| 1430 |
load_bbsnet_conf(CONF_BBSNET); |
if (load_bbsnet_conf(CONF_BBSNET) < 0) |
| 1431 |
|
{ |
| 1432 |
BBS_last_access_tm = time(NULL); |
clearscr(); |
| 1433 |
|
moveto(1, 1); |
| 1434 |
|
prints("加载穿梭配置失败!"); |
| 1435 |
|
press_any_key(); |
| 1436 |
|
return -1; |
| 1437 |
|
} |
| 1438 |
|
|
|
clearscr(); |
|
| 1439 |
bbsnet_refresh(); |
bbsnet_refresh(); |
| 1440 |
display_menu(&bbsnet_menu); |
display_menu(&bbsnet_menu); |
| 1441 |
bbsnet_selchange(); |
bbsnet_selchange(); |
| 1444 |
{ |
{ |
| 1445 |
ch = igetch(100); |
ch = igetch(100); |
| 1446 |
|
|
| 1447 |
|
if (ch != KEY_NULL && ch != KEY_TIMEOUT) |
| 1448 |
|
{ |
| 1449 |
|
BBS_last_access_tm = time(NULL); |
| 1450 |
|
} |
| 1451 |
|
|
| 1452 |
switch (ch) |
switch (ch) |
| 1453 |
{ |
{ |
| 1454 |
case KEY_NULL: // broken pipe |
case KEY_NULL: // broken pipe |
| 1455 |
case KEY_ESC: |
log_debug("KEY_NULL\n"); |
|
case Ctrl('C'): // user cancel |
|
| 1456 |
goto cleanup; |
goto cleanup; |
| 1457 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 1458 |
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) |
| 1459 |
{ |
{ |
| 1460 |
|
log_error("User input timeout\n"); |
| 1461 |
goto cleanup; |
goto cleanup; |
| 1462 |
} |
} |
| 1463 |
continue; |
continue; |
| 1464 |
|
case KEY_ESC: |
| 1465 |
|
case Ctrl('C'): // user cancel |
| 1466 |
|
goto cleanup; |
| 1467 |
case CR: |
case CR: |
|
igetch_reset(); |
|
| 1468 |
bbsnet_connect(bbsnet_menu.menu_item_pos[0]); |
bbsnet_connect(bbsnet_menu.menu_item_pos[0]); |
| 1469 |
|
// Force cleanup anything remaining in the output buffer |
| 1470 |
|
clearscr(); |
| 1471 |
|
iflush(); |
| 1472 |
|
// Clear screen and redraw menu |
| 1473 |
bbsnet_refresh(); |
bbsnet_refresh(); |
| 1474 |
display_menu(&bbsnet_menu); |
display_menu(&bbsnet_menu); |
| 1475 |
bbsnet_selchange(); |
bbsnet_selchange(); |
| 1511 |
bbsnet_selchange(); |
bbsnet_selchange(); |
| 1512 |
break; |
break; |
| 1513 |
} |
} |
|
BBS_last_access_tm = time(NULL); |
|
| 1514 |
} |
} |
| 1515 |
|
|
| 1516 |
cleanup: |
cleanup: |