Example #1
0
int main(int argc, char** argv)
{
	boost::asio::io_service io;
	std::string post_url = "http://bbs.tianya.cn/post-culture-520191-1.shtml"; // http://bbs.tianya.cn/list.jsp?item=culture&grade=1&order=1 http://bbs.tianya.cn/post-culture-354078-1.shtml
	if (argc == 2)
		post_url = std::string(argv[1]);

	std::locale::global(std::locale(""));

	auto obj = std::make_shared<tianya_context>(boost::ref(io));
	obj->start(post_url);
	obj->filter_reply(true);

	boost::asio::signal_set terminator_signal(io);
	terminator_signal.add(SIGINT);
	terminator_signal.add(SIGTERM);
#if defined(SIGQUIT)
	terminator_signal.add(SIGQUIT);
#endif // defined(SIGQUIT)
	terminator_signal.async_wait(boost::bind(&terminator, boost::ref(io), boost::ref(obj)));

	io.run();

	// 输出到文件.
	std::string name = time_to_string(aux::gettime()) + ".txt";
	obj->serialize_to_file(name);

	return 0;
}
static void filter_child(int c, struct in_addr dest_ip)
{
	int s;

	/* we have a connection from a new client, now connect to the server */
	s = open_socket_out(SOCK_STREAM, &dest_ip, 445, LONG_CONNECT_TIMEOUT);

	if (s == -1) {
		d_printf("Unable to connect to %s\n", inet_ntoa(dest_ip));
		exit(1);
	}

	while (c != -1 || s != -1) {
		fd_set fds;
		int num;
		
		FD_ZERO(&fds);
		if (s != -1) FD_SET(s, &fds);
		if (c != -1) FD_SET(c, &fds);

		num = sys_select_intr(MAX(s+1, c+1),&fds,NULL,NULL,NULL);
		if (num <= 0) continue;
		
		if (c != -1 && FD_ISSET(c, &fds)) {
			if (!receive_smb(c, packet, BUFFER_SIZE, 0)) {
				d_printf("client closed connection\n");
				exit(0);
			}
			filter_request(packet);
			if (!send_smb(s, packet)) {
				d_printf("server is dead\n");
				exit(1);
			}			
		}
		if (s != -1 && FD_ISSET(s, &fds)) {
			if (!receive_smb(s, packet, BUFFER_SIZE, 0)) {
				d_printf("server closed connection\n");
				exit(0);
			}
			filter_reply(packet);
			if (!send_smb(c, packet)) {
				d_printf("client is dead\n");
				exit(1);
			}			
		}
	}
	d_printf("Connection closed\n");
	exit(0);
}
static void filter_child(int c, struct sockaddr_storage *dest_ss)
{
	NTSTATUS status;
	int s = -1;
	char packet[128*1024];

	/* we have a connection from a new client, now connect to the server */
	status = open_socket_out(dest_ss, TCP_SMB_PORT, LONG_CONNECT_TIMEOUT, &s);
	if (!NT_STATUS_IS_OK(status)) {
		char addr[INET6_ADDRSTRLEN];
		if (dest_ss) {
			print_sockaddr(addr, sizeof(addr), dest_ss);
		}

		d_printf("Unable to connect to %s (%s)\n",
			 dest_ss?addr:"NULL", nt_errstr(status));
		exit(1);
	}

	while (c != -1 || s != -1) {
		struct pollfd fds[2];
		int num_fds, ret;

		memset(fds, 0, sizeof(struct pollfd) * 2);
		fds[0].fd = -1;
		fds[1].fd = -1;
		num_fds = 0;

		if (s != -1) {
			fds[num_fds].fd = s;
			fds[num_fds].events = POLLIN|POLLHUP;
			num_fds += 1;
		}
		if (c != -1) {
			fds[num_fds].fd = c;
			fds[num_fds].events = POLLIN|POLLHUP;
			num_fds += 1;
		}

		ret = sys_poll_intr(fds, num_fds, -1);
		if (ret <= 0) {
			continue;
		}

		/*
		 * find c in fds and see if it's readable
		 */
		if ((c != -1) &&
		    (((fds[0].fd == c)
		      && (fds[0].revents & (POLLIN|POLLHUP|POLLERR))) ||
		     ((fds[1].fd == c)
		      && (fds[1].revents & (POLLIN|POLLHUP|POLLERR))))) {
			size_t len;
			if (!NT_STATUS_IS_OK(receive_smb_raw(
							c, packet, sizeof(packet),
							0, 0, &len))) {
				d_printf("client closed connection\n");
				exit(0);
			}
			filter_request(packet, len);
			if (!send_smb(s, packet)) {
				d_printf("server is dead\n");
				exit(1);
			}			
		}

		/*
		 * find s in fds and see if it's readable
		 */
		if ((s != -1) &&
		    (((fds[0].fd == s)
		      && (fds[0].revents & (POLLIN|POLLHUP|POLLERR))) ||
		     ((fds[1].fd == s)
		      && (fds[1].revents & (POLLIN|POLLHUP|POLLERR))))) {
			size_t len;
			if (!NT_STATUS_IS_OK(receive_smb_raw(
							s, packet, sizeof(packet),
							0, 0, &len))) {
				d_printf("server closed connection\n");
				exit(0);
			}
			filter_reply(packet);
			if (!send_smb(c, packet)) {
				d_printf("client is dead\n");
				exit(1);
			}			
		}
	}
	d_printf("Connection closed\n");
	exit(0);
}