存档

文章标签 ‘客户端’

采用php开发终端字符界面

2009年6月1日

众所周知,PHP通常情况下都用于动态网站的开发,抑或采用PHP_CLI进行命令行的开发。其实PHP除了能进行以上两种开发外,还可以进行终端系统的开发。

废话以后再讲,先上个图一看便知。

dev terminal by php

这个界面没见过?不会吧,各位XD去过银行吧?留心看过其操作界面没有?想起来了?对了,他们的操作界面就是类似于这个东东。当然这只是一个登录界面而已。虽然这只是一个登录界面,但其所有代码均采用PHP完成,没想到吧?

那就来看看Code:

<?php
define('MY_NCURSES_KEY_ESC',   27);
define('MY_NCURSES_KEY_NUM_MIN',	48);//0
define('MY_NCURSES_KEY_NUM_MAX',	57);//9
define('MY_NCURSES_KEY_INPUT_MIN',	65);//A
define('MY_NCURSES_KEY_INPUT_MAX',	122);//z

function login_win() {

	$login_rows = 10;
	$login_cols = 40;

	$login_y = ((25 - $login_rows) >> 1) + 1;
	$login_x = ((80 - $login_cols) >> 1) + 1;

	ncurses_init();
	$login_win = ncurses_newwin ( $login_rows, $login_cols, $login_y, $login_x );
	if (empty ( $login_win )) {
		trigger_error ( 'unable to create window' );
		return NULL;
	}
	ncurses_wborder ( $login_win, 0, 0, 0, 0, 0, 0, 0, 0 );
	ncurses_wrefresh ( $login_win );

	ncurses_wattron ( $login_win, NCURSES_A_REVERSE );
	ncurses_mvwaddstr ( $login_win, 0, 2, "登录" );
	ncurses_wattroff ( $login_win, NCURSES_A_REVERSE );

	$well_str = "请输入用户名和密码";
	$title_x = ( int ) ($login_cols - strlen ( $well_str ) >> 1) + 1;
	$title_y = 2;
	ncurses_mvwaddstr ( $login_win, $title_y, $title_x, $well_str );

	$name_var ="";
	$name_len = 0;
	$pass_var = "";
	$pass_len = 0;

	$u_cursor_y = 4;
	$u_cursor_x = 12;
	$p_cursor_y = 5;
	$p_cursor_x = 12;

	$b_y = 7;
	$b_commit_x = 8;
	$b_reset_x = 20;

	$v_current = 0;
	$h_current = 0;
	$do_loop = 1;

	$commit_str = "登录";
	$reset_str = "重置";
	ncurses_mvwaddstr ( $login_win, $b_y, $b_commit_x, $commit_str );
	ncurses_mvwaddstr ( $login_win, $b_y, $b_reset_x, $reset_str );

	//第四行第四列
	ncurses_mvwaddstr($login_win, 4, 4, "用户名:");
	//第五行第四列
	ncurses_mvwaddstr($login_win, 5, 4, "密  码:");

	ncurses_wmove ( $login_win, $u_cursor_y, $u_cursor_x );

	ncurses_wrefresh ( $login_win );
	ncurses_keypad ( $login_win, TRUE );
	ncurses_noecho ();
	ncurses_curs_set ( 1 );

	$logined = FALSE;

	while ($do_loop) {

		$key = ncurses_wgetch($login_win);

		$v_move = 0;
		$h_move = 0;

		switch ($key) {
			case NCURSES_KEY_UP:
				if ($v_current > 0) {
					$v_move = -1;
				}

				break;
			case NCURSES_KEY_DOWN:
				if ($v_current < 2) {
					$v_move = 1;
				}

				break;
			case NCURSES_KEY_LEFT:
				if ($h_current > 0) {
					$h_move = -1;
				}
				break;
			case NCURSES_KEY_RIGHT:
				if ($h_current < 1) {
					$h_move = 1;
				}
				break;
			case MY_NCURSES_KEY_LF:
			case MY_NCURSES_KEY_CR:
				//reset
				if (($h_current == 1) &&($v_current == 2)) {
					$name_var = "";
					$name_len = 0;
					$name_char= "";

					$pass_var = "";
					$pass_len = 0;
					$pass_char= "";

					$u_cursor_x = 12;
					$p_cursor_x = 12;

					$fill = str_pad(" ", 10);
					ncurses_mvwaddstr ( $login_win, $u_cursor_y, $u_cursor_x, $fill );
					ncurses_mvwaddstr ( $login_win, $p_cursor_y, $p_cursor_x, $fill );

					ncurses_mvwaddstr ( $login_win, $u_cursor_y, $u_cursor_x, "" );
					ncurses_mvwaddstr ( $login_win, $p_cursor_y, $p_cursor_x, "" );

				}elseif (($h_current == 0) && ($v_current == 2)) {
					//login

					if (($name_var == "cc") && ($pass_var == "cc")) {
						;
						$do_loop = 0;
						$logined = TRUE;
					}
				}

				break;
			case MY_NCURSES_KEY_BACKSPACE:
				if (($v_current == 0) && ($name_len  > 0)) {
					$name_len --;
					$name_var = substr ( $name_var, 0, $name_len );
					$u_cursor_x --;
					ncurses_mvwaddstr ( $login_win, $u_cursor_y, $u_cursor_x, "" );
				} else if (($v_current == 1) && ($pass_len > 0)) {
					$pass_len --;
					$pass_var = substr ( $pass_var, 0, $pass_len );
					$p_cursor_x --;
					ncurses_mvwaddstr ( $login_win, $p_cursor_y, $p_cursor_x, "" );
				}
				break;
			case MY_NCURSES_KEY_ESC:
				ncurses_flushinp ();
				$do_loop = 0;
				$logined = FALSE;
				break;
			default:
				if ((($key >= MY_NCURSES_KEY_INPUT_MIN) && ($key <= MY_NCURSES_KEY_INPUT_MAX)) ||
					(($key >= MY_NCURSES_KEY_NUM_MIN) && ($key <= MY_NCURSES_KEY_NUM_MAX))) {
					if (($v_current == 0) && ($name_len < 10)) {
						$name_len ++;
						$name_var .= $name_char = chr ( $key );
						ncurses_mvwaddstr ( $login_win, $u_cursor_y, $u_cursor_x, $name_char );
						$u_cursor_x ++;
					} else if (($v_current == 1) && ($pass_len < 10)) {
						$pass_len ++;
						$pass_var .= $pass_char = chr ( $key );
						ncurses_mvwaddstr ( $login_win, $p_cursor_y, $p_cursor_x, "*" );
						$p_cursor_x ++;
					}
				}
				break;
		}

		$v_current += $v_move;
		if ($v_current == 0) {
			ncurses_wattron ( $login_win, NCURSES_A_REVERSE );
			ncurses_mvwaddstr ( $login_win, $u_cursor_y, $u_cursor_x, "" );
			ncurses_wattroff ( $login_win, NCURSES_A_REVERSE );
		}elseif ($v_current == 1) {
			ncurses_wattron ( $login_win, NCURSES_A_REVERSE );
			ncurses_mvwaddstr ( $login_win, $p_cursor_y, $p_cursor_x, "" );
			ncurses_wattroff ( $login_win, NCURSES_A_REVERSE );
		}else {

			$h_current += $h_move;
			if ($h_current == 0) {
				ncurses_mvwaddstr ( $login_win, $b_y, $b_reset_x, $reset_str );

				ncurses_wattron ( $login_win, NCURSES_A_REVERSE );
				ncurses_mvwaddstr ( $login_win, $b_y, $b_commit_x, $commit_str );
				ncurses_wattroff ( $login_win, NCURSES_A_REVERSE );
			}else {
				ncurses_mvwaddstr ( $login_win, $b_y, $b_commit_x, $commit_str );

				ncurses_wattron ( $login_win, NCURSES_A_REVERSE );
				ncurses_mvwaddstr ( $login_win, $b_y, $b_reset_x, $reset_str );
				ncurses_wattroff ( $login_win, NCURSES_A_REVERSE );

			}

		}

		ncurses_wrefresh ( $login_win );
	}// end while

	ncurses_werase($login_win);
	ncurses_clear();
	ncurses_end();

	if (isset($logined) && $logined) {
		return TRUE;
	}else {
		return FALSE;
	}

}
?>

现在明白是怎么回事了吧?当然各位兄弟把这代码拿下去,可能你们的环境中是无法RUN的,因为这采用了其它的PHP包。

对于采用的什么包,各位XDJM,还敬请关注本博,稍后将奉上相关的PHP包的安装与配置 :)

PHP教程 , , , ,

openvpn客户端配置文件详解

2009年2月24日

# 定义是一个客户端
client

# 定义使用路由IP模式,与服务端一致
;dev tap
dev tun

# 定义Windows下使用的网卡名称,linux不需要
;dev-node 我的连接

# 定义使用的协议,与服务端一致
;proto tcp
proto udp

# 指定服务端地址和端口,可以用多行指定多台服务器
# 实现负载均衡(从上往下尝试)
remote 192.168.228.153 1194
;remote my-server-2 1194

# 若上面配置了多台服务器,让客户端随机连接
;remote-random

# 解析服务器域名
# Keep trying indefinitely to resolve the
# host name of the OpenVPN server.  Very useful
# on machines which are not permanently connected
# to the internet such as laptops.
resolv-retry infinite

# 客户端不需要绑定端口
# Most clients do not need to bind to
# a specific local port number.
nobind

# 也是为了让Openvpn也nobody运行(安全)
# 注意:Windows不能设置
;user nobody
;group nobody

# Try to preserve some state across restarts.
persist-key
persist-tun

# 若客户端通过HTTP Proxy,在这里设置
# 要使用Proxy,不能使用UDP为VPN的通讯协议
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]

# 无线网络有很多多余的头文件,设置忽略它
;mute-replay-warnings

# 重点,就是指定ca和客户端的证书
# 注意,下面的两个号是连在一起的,之间没有空格
ca “C:\\Program Files\\OpenVPN\\easy-rsa\\keys ca.crt”
cert “C:\\Program Files\\OpenVPN\\easy-rsa\\keys backup.crt”
key “C:\\Program Files\\OpenVPN\\easy-rsa\\keys backup.key”

# 如果服务端打开了PAM认证模块,客户端需要另其有效
;auth-user-pass

# 一些安全措施
# Verify server certificate by checking
# that the certicate has the nsCertType
# field set to “server”.  This is an
# important precaution to protect against
# a potential attack discussed here:
#  http://openvpn.net/howto.html#mitm
#
# To use this feature, you will need to generate
# your server certificates with the nsCertType
# field set to “server”.  The build-key-server
# script in the easy-rsa folder will do this.
;ns-cert-type server

# If a tls-auth key is used on the server
# then every client must also have the key.
;tls-auth ta.key 1

# Select a cryptographic cipher.
# If the cipher option is used on the server
# then you must also specify it here.
;cipher x

# 使用lzo压缩,与服务端一致
comp-lzo

# Set log file verbosity.
verb 3

# Silence repeating messages
;mute 20

虚拟网络 , , ,

使用OPENVPN架设VPN服务器

2009年2月24日

使用OpenVpn架设VPN服务器的过程记录如下:

说明: 架设OPENVPN服务器,服务器方面做的工作比较多,客户端相对来说就比较简单。
服务器采用RSA证书和密钥验证方式对客户端进行验证,默认情况下证书和用户是一对一的,
多个用户使用同一证书会被踢出。所以首先要做的工作就是证书的制作。

一、OpenVpn服务器端配置过程
修改  OpenVPN\easy-rsa\vars.bat.sample

set KEY_COUNTRY=CN
set KEY_PROVINCE=SiChuang
set KEY_CITY=ChengDu
set KEY_ORG=acho.org
set KEY_EMAIL=chcn001@gmail.com

一、产生证书
打开 cmd或command进入 OpenVPN\easy-rsa,依次执行以下命令
(1)、init-config
(2)、vars
(3)、clean-all
(4)、build-ca
build-ca  **这个命令用于创建根证书
Country Name (2 letter code) [CN]:
State or Province Name (full name) [AnHui]:
Locality Name (eg, city) [HeFei]:
Organization Name (eg, company) [lizhenbao]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server’s hostname) []:rootcr
Email Address [aaa@aa.com]:

(5)、build-dh
(6)、build-key-server servername

生成服务端密钥
build-key-server server  **创建服务器证书,server为机器名
Country Name (2 letter code) [CN]:
State or Province Name (full name) [AnHui]:
Locality Name (eg, city) [HeFei]:
Organization Name (eg, company) [lizhenbao]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server’s hostname) []:server
Email Address [lizhenbao@gmail.com]:
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:password
An optional company name []:

(7)、build-key clientname
生成客户端密钥
build-key client  **创建客户端证书,client为用户名
Country Name (2 letter code) [CN]:
State or Province Name (full name) [AnHui]:
Locality Name (eg, city) [HeFei]:
Organization Name (eg, company) [lizhenbao]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server’s hostname) []:client
Email Address [lizhenbao@gmail.com]:
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:password
An optional company name []:

openvpn –genkey –secret keys/ta.key
二、复制证书

1、服务器端配置

将ca.crt,dh1024.pem,server.crt,server.key,ta.key复制到 OpenVPN\config目录下

拷贝server.ovpn到config目录下,修改后,用OpenVPN GUI启动服务器

服务器端文件示例:(server.ovpn)
local 192.168.3.1 #建立VPN的IP
port 443 #端口号,根据需要,自行修改,如果是用http代理连接,请不要修改
proto tcp-server #通过TCP协议连接
dev tap #win下必须设为tap
server 192.168.0.0 255.255.255.0 # 虚拟局域网网段设置,请根据需要自行修改,不支持和拔号网卡位于同一网段
push “route 0.0.0.0 0.0.0.0″ #表示client通过VPN SERVER上网
keepalive 20 180
ca “C:\\Program Files\\OPENVPN\\KEY\\ca.crt” #CA证书存放位置,请根据实际情况自行修改
cert “C:\\Program Files\\OPENVPN\\KEY\\server.crt” #服务器证书存放位置,请根据实际情况自行修改
key “C:\\Program Files\\OPENVPN\\KEY\\server.key” #服务器密钥存放位置,请根据实际情况自行修改
dh “C:\\Program Files\\OPENVPN\\KEY\\dh1024.pem” #dh1024.pem存放位置,请根据实际情况自行修改
push “redirect-gateway def1″
push “dhcp-option DNS 219.141.140.10″ #DNS,请根据实际情况自行修改
mode server
tls-server
status “C:\\Program Files\\OPENVPN\\log\\openvpn-status.log” #LOG记录文件存放位置,请根据实际情况自行修改
comp-lzo
verb 4

至此OpenVpn服务器配置成功。如果想要OpenVpn服务器自动启动,则找到OpenVpn服务,将其启动方式设置为“自动”。

二、OpenVpn 客户端配置过程
将ca.crt client.crt client.key ta.key复制到 OpenVPN\config目录下

拷贝client.ovpn到config目录下,修改后,用OpenVPN GUI启动客户端

客户端文件示例:(client.ovpn)

client
dev tap #windows下面用tap,LINUX下用tun
proto tcp-client
remote 192.168.3.1 443 #VPN服务器的域名或IP 端口
resolv-retry infinite
nobind
#http-proxy 192.168.1.1 80 #这里填入你的代理服务器地址和端口
mute-replay-warnings
ca “C:\\Program Files\\OPENVPN\\KEY\\ca.crt”
cert “C:\\Program Files\\OPENVPN\\KEY\\client.crt” #这里改成每个客户端相应的证书
key “C:\\Program Files\\OPENVPN\\KEY\\client.key” #这里改成每个客户端相应的密钥
comp-lzo
verb 4
status openvpn-status.log

其它设置:
上面的配置拔号成功后,VPN SERVER的IP:192.168.0.1
VPN client的IP:192.168.0.2
ping 192.168.0.1 //相互之间应能ping通
三、Openvpn 服务器端发放证书方法
启动cmd或command
进入easy-rsa目录
vars
build-key username  **其中username为用户名  ,注意各证书的Common Name不能相同(http://www.cnitblog.com/lizhenbao)
附:OpenVPN安装过程

操作系统, 虚拟网络 , , , , , , , , ,