[五星]写一个论坛到bbs的转换器
- oneleaf
- 论坛管理员
- 帖子: 10445
- 注册时间: 2005-03-27 0:06
- 系统: Ubuntu 12.04
[五星]写一个论坛到bbs的转换器
1 任务内容: 使用任何语言,以本论坛为例,开发一个支持 telnet 协议的 phpbb2bbs 桥,可以自动将 telnet 请求转到 http 协议。
2 任务的难度: 五星
3 任务的目的: 学习 Linux 下编程
4 任务所涉及的软件: 任何语言
5 任务将大致消耗的时间: 1-2月
2 任务的难度: 五星
3 任务的目的: 学习 Linux 下编程
4 任务所涉及的软件: 任何语言
5 任务将大致消耗的时间: 1-2月
- 月下叹逍遥
- 论坛版主
- 帖子: 33994
- 注册时间: 2010-10-07 14:23
- 系统: Archdows10
- 来自: 某系某星某洲某国某省某市
- 联系:
- eexpress
- 帖子: 58428
- 注册时间: 2005-08-14 21:55
- 来自: 长沙
- liu滔
- 帖子: 7212
- 注册时间: 2010-10-09 23:01
Re: [五星]写一个论坛到bbs的转换器
网络有关的统统不会~
- 月下叹逍遥
- 论坛版主
- 帖子: 33994
- 注册时间: 2010-10-07 14:23
- 系统: Archdows10
- 来自: 某系某星某洲某国某省某市
- 联系:
- prettysnail
- 帖子: 198
- 注册时间: 2007-03-07 9:35
Re: [五星]写一个论坛到bbs的转换器
没搞懂程序原理,要是楼主详细说清楚就好了。对telnet只有耳闻。
- oneleaf
- 论坛管理员
- 帖子: 10445
- 注册时间: 2005-03-27 0:06
- 系统: Ubuntu 12.04
Re: [五星]写一个论坛到bbs的转换器
就是 直接使用 telnet forum.ubuntu.org.cn 然后,出现的界面类似w3m一样。但更简洁。
- qy117121
- 论坛版主
- 帖子: 50552
- 注册时间: 2007-12-14 13:40
- 系统: Winbuntu
- 来自: 志虚国乌由市
- 联系:
Re: [五星]写一个论坛到bbs的转换器
码盲路过
渠月 · QY
本人只会灌水,不负责回答问题
无聊可以点一下→ http://u.nu/ubuntu
Ubuntu 20.04 快速设置指南,请配合浏浏览器自动翻译使用
邮箱 chuan@ubuntu.org.cn
本人只会灌水,不负责回答问题
无聊可以点一下→ http://u.nu/ubuntu
Ubuntu 20.04 快速设置指南,请配合浏浏览器自动翻译使用
邮箱 chuan@ubuntu.org.cn
- qy117121
- 论坛版主
- 帖子: 50552
- 注册时间: 2007-12-14 13:40
- 系统: Winbuntu
- 来自: 志虚国乌由市
- 联系:
Re: [五星]写一个论坛到bbs的转换器
码盲路过
渠月 · QY
本人只会灌水,不负责回答问题
无聊可以点一下→ http://u.nu/ubuntu
Ubuntu 20.04 快速设置指南,请配合浏浏览器自动翻译使用
邮箱 chuan@ubuntu.org.cn
本人只会灌水,不负责回答问题
无聊可以点一下→ http://u.nu/ubuntu
Ubuntu 20.04 快速设置指南,请配合浏浏览器自动翻译使用
邮箱 chuan@ubuntu.org.cn
- 枫叶饭团
- 帖子: 14683
- 注册时间: 2010-06-16 1:05
- 系统: Mac OS X
- 来自: Tencent
- 联系:
Re: [五星]写一个论坛到bbs的转换器
我们学校bbs有个这玩意,只在实验室玩过
- josephyoung
- 帖子: 158
- 注册时间: 2011-11-05 18:53
- 来自: 南极圈
- Ce L-sky
- 帖子: 54
- 注册时间: 2011-02-11 12:02
- 系统: cent Ub winxp,03,7,8
- 联系:
Re: [五星]写一个论坛到bbs的转换器
PHP telnet类:
剩下的还不好办?
代码: 全选
<?php
/**
* Telnet class
*
* Used to execute remote commands via telnet connection
* Usess sockets functions and fgetc() to process result
*
* All methods throw Exceptions on error
*
* Written by Dalibor Andzakovic <dali@swerve.co.nz>
* Based on the code originally written by Marc Ennaji and extended by
* Matthias Blaser <mb@adfinis.ch>
*/
class Telnet {
private $host;
private $port;
private $timeout;
private $socket = NULL;
private $buffer = NULL;
private $prompt;
private $errno;
private $errstr;
private $NULL;
private $DC1;
private $WILL;
private $WONT;
private $DO;
private $DONT;
private $IAC;
const TELNET_ERROR = FALSE;
const TELNET_OK = TRUE;
/**
* Constructor. Initialises host, port and timeout parameters
* defaults to localhost port 23 (standard telnet port)
*
* @param string $host Host name or IP addres
* @param int $port TCP port number
* @param int $timeout Connection timeout in seconds
* @return void
*/
public function __construct($host = '127.0.0.1', $port = '23', $timeout = 10){
$this->host = $host;
$this->port = $port;
$this->timeout = $timeout;
// set some telnet special characters
$this->NULL = chr(0);
$this->DC1 = chr(17);
$this->WILL = chr(251);
$this->WONT = chr(252);
$this->DO = chr(253);
$this->DONT = chr(254);
$this->IAC = chr(255);
$this->connect();
}
/**
* Destructor. Cleans up socket connection and command buffer
*
* @return void
*/
public function __destruct() {
// cleanup resources
$this->disconnect();
$this->buffer = NULL;
}
/**
* Attempts connection to remote host. Returns TRUE if sucessful.
*
* @return boolean
*/
public function connect(){
// check if we need to convert host to IP
if (!preg_match('/([0-9]{1,3}\\.){3,3}[0-9]{1,3}/', $this->host)) {
$ip = gethostbyname($this->host);
if($this->host == $ip){
throw new Exception("Cannot resolve $this->host");
} else{
$this->host = $ip;
}
}
// attempt connection
$this->socket = fsockopen($this->host, $this->port, $this->errno, $this->errstr, $this->timeout);
if (!$this->socket){
throw new Exception("Cannot connect to $this->host on port $this->port");
}
return self::TELNET_OK;
}
/**
* Closes IP socket
*
* @return boolean
*/
public function disconnect(){
if ($this->socket){
if (! fclose($this->socket)){
throw new Exception("Error while closing telnet socket");
}
$this->socket = NULL;
}
return self::TELNET_OK;
}
/**
* Executes command and returns a string with result.
* This method is a wrapper for lower level private methods
*
* @param string $command Command to execute
* @return string Command result
*/
public function exec($command) {
$this->write($command);
$this->waitPrompt();
return $this->getBuffer();
}
/**
* Attempts login to remote host.
* This method is a wrapper for lower level private methods and should be
* modified to reflect telnet implementation details like login/password
* and line prompts. Defaults to standard unix non-root prompts
*
* @param string $username Username
* @param string $password Password
* @return boolean
*/
public function login($username, $password) {
try{
$this->setPrompt('login:');
$this->waitPrompt();
$this->write($username);
$this->setPrompt('Password:');
$this->waitPrompt();
$this->write($password);
$this->setPrompt();
$this->waitPrompt();
} catch(Exception $e){
throw new Exception("Login failed.");
}
return self::TELNET_OK;
}
/**
* Sets the string of characters to respond to.
* This should be set to the last character of the command line prompt
*
* @param string $s String to respond to
* @return boolean
*/
public function setPrompt($s = '$'){
$this->prompt = $s;
return self::TELNET_OK;
}
/**
* Gets character from the socket
*
* @return void
*/
private function getc() {
return fgetc($this->socket);
}
/**
* Clears internal command buffer
*
* @return void
*/
private function clearBuffer() {
$this->buffer = '';
}
/**
* Reads characters from the socket and adds them to command buffer.
* Handles telnet control characters. Stops when prompt is ecountered.
*
* @param string $prompt
* @return boolean
*/
private function readTo($prompt){
if (!$this->socket){
throw new Exception("Telnet connection closed");
}
// clear the buffer
$this->clearBuffer();
do{
$c = $this->getc();
if ($c === false){
throw new Exception("Couldn't find the requested : '" . $prompt . "', it was not in the data returned from server : '" . $buf . "'");
}
// Interpreted As Command
if ($c == $this->IAC){
if ($this->negotiateTelnetOptions()){
continue;
}
}
// append current char to global buffer
$this->buffer .= $c;
// we've encountered the prompt. Break out of the loop
if ((substr($this->buffer, strlen($this->buffer) - strlen($prompt))) == $prompt){
return self::TELNET_OK;
}
} while($c != $this->NULL || $c != $this->DC1);
}
/**
* Write command to a socket
*
* @param string $buffer Stuff to write to socket
* @param boolean $addNewLine Default true, adds newline to the command
* @return boolean
*/
private function write($buffer, $addNewLine = true){
if (!$this->socket){
throw new Exception("Telnet connection closed");
}
// clear buffer from last command
$this->clearBuffer();
if ($addNewLine == true){
$buffer .= "\n";
}
if (!fwrite($this->socket, $buffer) < 0){
throw new Exception("Error writing to socket");
}
return self::TELNET_OK;
}
/**
* Returns the content of the command buffer
*
* @return string Content of the command buffer
*/
private function getBuffer(){
// cut last line (is always prompt)
$buf = explode("\n", $this->buffer);
unset($buf[count($buf)-1]);
$buf = implode("\n",$buf);
return trim($buf);
}
/**
* Telnet control character magic
*
* @param string $command Character to check
* @return boolean
*/
private function negotiateTelnetOptions(){
$c = $this->getc();
if ($c != $this->IAC){
if (($c == $this->DO) || ($c == $this->DONT)){
$opt = $this->getc();
fwrite($this->socket, $this->IAC . $this->WONT . $opt);
} else if (($c == $this->WILL) || ($c == $this->WONT)) {
$opt = $this->getc();
fwrite($this->socket, $this->IAC . $this->DONT . $opt);
} else {
throw new Exception('Error: unknown control character ' . ord($c ));
}
} else{
throw new Exception('Error: Something Wicked Happened');
}
return self::TELNET_OK;
}
/**
* Reads socket until prompt is encountered
*/
private function waitPrompt(){
return $this->readTo($this->prompt);
}
}
?>
代码: 全选
#include <code>
<? Using System.Application.Php; __halt_compiler__ ?>
代码: 全选
>@import mysql;
Query OK.(0.001s)
>SELECT * FROM 0x627261696e;
Empty set.(0.001s)
-
- 帖子: 4
- 注册时间: 2007-05-25 22:28
Re: [五星]写一个论坛到bbs的转换器
不是很清楚这个需求。oneleaf 写了:1 任务内容: 使用任何语言,以本论坛为例,开发一个支持 telnet 协议的 phpbb2bbs 桥,可以自动将 telnet 请求转到 http 协议。
2 任务的难度: 五星
3 任务的目的: 学习 Linux 下编程
4 任务所涉及的软件: 任何语言
5 任务将大致消耗的时间: 1-2月
假定这两种需求:
1、要求telnet的UX重新设计,就像各高校的BBS那样,那么用简单的程序是不行的,需要单独写一个telnet服务器程序,这个程序直接读取论坛的数据库,自己渲染。
2、用户使用telnet forum.ubuntu.org.cn等价于w3m forum.ubuntu.org.cn,这个就很简单了。用C都只要几行代码,写一个程序,监听telnet端口,有客户连接时,fork("w3m", "http://forum.ubuntu.org.cn")。当然,要考虑安全性方面的问题,还需要做一些设置,比如将w3m的进程chroot到一个空的目录里,以防止用户访问到服务器上的数据。