Saturday, August 29, 2015

Openconnect Server (ocserv) on Openwrt

I'd like to set up a VPN server on a router running Openwrt. There are several choices, L2TP, PPTP, OpenVPN and openconnect. PPTP is not very secure. L2TP must be used with IPSec for security. While OpenVPN and openconnect are both SSL-VPN. They are easy to configure and adaptive to the restrictions of ISP. Because there is a luci app for openconnect server. It's easier to set up than OpenVPN. I would set up an openconnect server, which operates in pseudo-bridge mode, meaning remote clients are on the same subnet as the computers at home.

Tuesday, July 28, 2015

Openwrt Synchronized Wifi Hotspot

There are three wireless routers at my home. All run Openwrt. One acts as gateway. The other two are APs. Sometimes, my mother wants to turn off WiFi before sleep. She would power off the gateway router. As a result, all clients will connect to the other two APs, and cannot access the Internet.

I want to make things more elegant. When the two APs detect that the gateway is down, they will turn off their wifi as well, and turn it on when the gateway is online again. Fortunately, the two APs have enough ROM for python. So I need not mess around with shell scripts.

Friday, July 24, 2015

Netgear WNR2000 v4 刷 OpenWrt 和 Gargoyle

最近淘了一个二手的WNR2000,硬件版本v4,使用AR9341单芯片方案,4MB闪存,32MB内存。发现自带固件功能太弱,可玩性不高,故刷Openwrt.

WNR2000是Openwrt支持的硬件,可以参考http://wiki.openwrt.org/toh/netgear/wnr2000,但是这个网页里的信息过时了。telnetenable已经不适用于这台WNR2000。可喜的是电路板上留有TTL焊盘,焊上排针就可以使用。离网络口隔离变压器最远的是GND,然后是RX,TX,波特率是115200。

连接上TTL即可看到终端,官方固件是基于Openwrt改造的,进入终端还有Openwrt的字符画。不过可用的指令很少。

Monday, July 6, 2015

用netcat传送文件

一些特殊情况下,比如ssh挂掉了,可以用netcat传输文件。当然,是明文传输。

首先在接收端listen,输入
nc -l 6000 > filename
然后在发送端输入
nc IP 6000 < file_to_send

IP是接收端IP地址,filename是文件保存的名字。

也可以改成在发送端Listen,适用于BusyBox里不支持-l的netcat

发送端:
cat file_to_send | pv -b | nc -l -p 3333
接收端:
nc IP 3333 > filename


还有一个办法,用Python的SimpleHTTPServer和wget

服务端:
python -m SimpleHTTPServer
会在当前目录下运行一个HTTP服务器,端口号为8000

客户端:
wget http://IP:8000/filename

Saturday, May 16, 2015

Python实现A记录查询

最近在学计算机网络,用的教材是Computer Networking: A Top Down Approach. 第二章节介绍了DNS。以前用Python实现过基于TCP和文本应答的协议,不过没有处理过DNS这样结构紧凑的协议。

由于Python本身是没有类型的,而DNS对每一项的字长有严格要求(这种协议用C实现相当直接),因此要先生成固定长度的数据。有多种实现方法,比如:
bytearray, struct.pack, binascii.unhexlify, 直接在string中使用\x转义

Wednesday, March 25, 2015

Python跨平台需要注意的问题

最近在写一个Python小程序,需要移植到windows上,遇到了几个小问题,记录一下。

可以使用sys.platform判断系统类型

ossaudiodev在Windows下不可用,用PyAudio替代

Windows在使用open打开文件时会区别'rb'和'r',而Linux的'r'和'rb'是等价的。因此在Windows下打开二进制文件必须用'rb'模式

Windows7下的Python在读取用户给定的中文路径时会出错,要先把路径转换到unicode,可以使用uni_path = unicode(path, "utf8")

PyGTK写的程序会变得很丑,暂时还不知道怎么解决。以后改用PySide好了

在Windows下最好把Python安装目录放到PATH环境变量里,这样比较方便用pip

Saturday, March 7, 2015

Gnome 3.14添加自动启动

Fedora21已经找不到gnome-session-properties了,虽然gnome-tweak-tools提供了添加子启动程序的功能,不过相当鸡肋。最后找到一个方法可以实现原来gnome-session-properties的功能

在~/.config/autostart/下新建一个xxx.desktop文件,内容如下:
[Desktop Entry]
Type=Application
Name=xscreensaver-autostart
Comment=Autostart xscreensaver
Exec=/usr/libexec/xscreensaver-autostart
Terminal=false
X-Desktop-File-Install-Version=0.22
X-GNOME-Autostart-enabled=true
OnlyShowIn=GNOME;

要注意的是,Exec后面必须是程序的完整路径。

真心想吐槽Gnome,怎么越改越麻烦了。