Category: Just about Code

  • Install OpenVPN with IPv6 on Ubuntu

    This article gives out the process of install OpenVPN with IPv6, and to be more specific, it will foucus on the difference with the Ubuntu official document about OpenVPN installation [1]. Before start, You need to check whether your server has IPv6 address. And if you are looking for tips about OpenVPN both with IPv4 and IPv6, you are supposed to read another post about 《OpenVPN both with IPv4 and IPv6》.

    Currently the lastest stable version of OpenVPN is 2.2.2 and for Ubuntu repo is still 2.2.1, both of them are only IPv4 supported. But thankfully the 2.3 version which supports IPv6 is already in RC stage and there is an official apt repo [2] so that we can achieve it easily.

    Most of commands need superuser privilege, so just use root to make everything easy.

    1) Install OpenVPN 2.3_rc1 (Both on server and client)
    [sh]
    wget -O – http://repos.openvpn.net/repos/repo-public.gpg | apt-key add –
    cd /etc/apt/sources.list.d
    wget http://repos.openvpn.net/repos/apt/conf/repos.openvpn.net-precise-snapshots.list
    apt-get update && apt-get install openvpn easy-rsa
    [/sh]
    Note that we install easy-rsa more than openvpn only because easy-rsa examples used to be in OpenVPN doc directory never exist in OpenVPN 2.3_rc1.

    2) Generate Certificate Authority
    [sh]
    cp -r /usr/share/easy-rsa/ /etc/openvpn/
    cd /etc/openvpn/easy-rsa/
    [/sh]
    Note that the location of easy-rsa directory has changed.

    Edit the “vars” file by vim or nano or something else (I will use vim for following examples), be sure to change following variables:
    [sh]
    export KEY_COUNTRY=”US”
    export KEY_PROVINCE=”CA”
    export KEY_CITY=”SanFrancisco”
    export KEY_ORG=”Fort-Funston”
    export KEY_EMAIL=”me@myhost.mydomain”
    export KEY_CNc=changeme
    export KEY_NAME=changeme
    export KEY_OU=changeme
    [/sh]
    Note that there are two duplicate KEY_EMAIL, feel free to delete one. All these variables are required when building all crts and keys, make sure to change it which will make them as default value so that you can press Enter all the way.
    [sh]
    source vars
    ./clean-all
    ./build-ca
    [/sh]

    3) Generate Server Certificates
    [sh]
    ./build-key-server servername
    # change servername to whatever you like, just make it corresponding with configuration.
    ./build-dh
    cd /etc/openvpn/easy-rsa/keys
    cp servername.crt servername.key ca.crt dh1024.pem /etc/openvpn/
    [/sh]

    4) Generate tls auth key (Add more security to OpenVPN port)
    [sh]
    cd /etc/openvpn
    openvpn –genkey –secret ta.key
    [/sh]

    5) Server Configuration
    [sh]
    cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/
    gzip -d /etc/openvpn/server.conf.gz
    vim /etc/openvpn/server.conf
    [/sh]
    Following is the change list by the default configure file. “A -> B # C” meaning change A to B, while C is the comment for more easy reading and you can just ignore it.
    [sh]
    port 1194 -> port XXXX # Choose one not fucked by GFW
    proto udp -> proto udp6 # IPv6
    cert server.crt -> cert servername.crt # Same with the name of Server Certificates
    key server.key -> key server.key # ditto
    ;push “redirect-gateway def1 bypass-dhcp” -> push “redirect-gateway def1 bypass-dhcp” # IP redirect
    ;tls-auth ta.key 0 -> tls-auth ta.key 0 # tls auth
    [/sh]

    6) iptables Configuration
    [sh]
    iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
    iptables-save > /etc/iptables.rules
    vim /etc/network/if-up.d/iptables
    [/sh]
    This will be a new file and you should press following in it.
    [sh]
    #!/bin/sh
    iptables-restore < /etc/iptables.rules [/sh] After saving the file you need to make it executeable. [sh] chmod +x /etc/network/if-up.d/iptables vim /etc/sysctl.conf [/sh] All following listings are commented in default configure file, just uncomment them. [sh] net.ipv4.ip_forward=1 net.ipv6.conf.all.forwarding=1 net.ipv4.conf.all.accept_redirects = 0 net.ipv6.conf.all.accept_redirects = 0 net.ipv4.conf.all.send_redirects = 0 [/sh] Note that the important difference is here. which make IP forward also work for IPv6. the parameter is different from IPv4, but fortunately it has been already listed in default configure file. At last, just make all related restart. [sh] sysctl -p /etc/init.d/openvpn restart /etc/init.d/networking restart [/sh] Now the server side should be working, a simple confirmation is to check whether tun0 or tun1 exists in ifconfig, then let's go for clients. 7) Generate Client Certificates [sh] cd /etc/openvpn/easy-rsa/ source vars ./build-key clientname # change clientname to whatever you like, just make it corresponding with configuration. [/sh] 8) Client Configuration Following are needed files for client, be careful to download them by scp or something else from server. And this is the last thing you need to do on the server. all things left should be done on the cliet besides step 1). Make sure to copy all needed files under the /etc/openvpn/ so that you can run it as daemon easily. [sh] /etc/openvpn/ca.crt /etc/openvpn/ta.key /etc/openvpn/easy-rsa/keys/clientname.crt /etc/openvpn/easy-rsa/keys/clientname.key [/sh] Copy the default client configure file to /etc/openvpn/ [sh] cd /etc/openvpn cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf . [/sh] Following is the change list by the default configure file. Just the same with Server Configuration in step 5) [sh] proto udp -> proto udp6
    remote my-server-1 1194 -> remote ::1 XXXX # Same IPv6 Address and Port Number
    cert client.crt -> cert clientname.crt # Same with the name of Client Certificates
    key client.key -> key clientname.key # ditto
    ;tls-auth ta.key 1 -> tls-auth ta.key 1
    [/sh]

    9) Confirmation
    Now you can just start the openvpn by
    [sh]
    opevpn /etc/openvpn/client.conf
    [/sh]
    if you can ping 10.8.0.1 successfully then everything should be ok. after that you can make it working as daemon by
    [sh]
    update-rc.d openvpn defaults
    [/sh]

    10) Reference:
    [1] https://help.ubuntu.com/12.04/serverguide/openvpn.html
    [2] https://community.openvpn.net/openvpn/wiki/OpenvpnSoftwareRepos
    [3] http://www.vpser.net/build/linode-install-openvpn.html

  • Linux下Texlive的ctex包中文字体问题

    不得不先赞一下现在texlive在linux上的安装已经非常方便了,刚更新2012不久
    发现在2011就中一直困扰的ctex包中文字体问题还是没有解决,只好自己动手了

    错误信息如下:
    [shell]
    Invalid fontname `[SIMKAI.TTF]/ICU’, contains ‘[‘

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !
    ! fontspec error: “font-not-found”
    !
    ! The font “[SIMKAI.TTF]” cannot be found.
    !
    ! See the fontspec documentation for further information.
    !
    ! For immediate help type H <return>.
    !………………………………………..
    [/shell]

    错误说的很清楚,字体名错误,字体找不到

    解决方法如下:
    修改ctex-xecjk-winfonts.def
    Texlive 2012下的相对路径为 /texmf-dist/tex/latex/ctex/fontset/ctex-xecjk-winfonts.def
    如果是默认安装位置,完整路径应为 /usr/local/texlive/2012/texmf-dist/tex/latex/ctex/fontset/ctex-xecjk-winfonts.def

    原始文件如下:
    [tex]
    % ctex-xecjk-winfonts.def: Windows 的 xeCJK 字体设置,默认为六种中易字体
    % vim:ft=tex

    \setCJKmainfont[BoldFont={SimHei},ItalicFont={[SIMKAI.TTF]}]
    {SimSun}
    \setCJKsansfont{SimHei}
    \setCJKmonofont{[SIMFANG.TTF]}

    \setCJKfamilyfont{zhsong}{SimSun}
    \setCJKfamilyfont{zhhei}{SimHei}
    \setCJKfamilyfont{zhkai}{[SIMKAI.TTF]}
    \setCJKfamilyfont{zhfs}{[SIMFANG.TTF]}
    % \setCJKfamilyfont{zhli}{LiSu}
    % \setCJKfamilyfont{zhyou}{YouYuan}

    \newcommand*{\songti}{\CJKfamily{zhsong}} % 宋体
    \newcommand*{\heiti}{\CJKfamily{zhhei}} % 黑体
    \newcommand*{\kaishu}{\CJKfamily{zhkai}} % 楷书
    \newcommand*{\fangsong}{\CJKfamily{zhfs}} % 仿宋
    % \newcommand*{\lishu}{\CJKfamily{zhli}} % 隶书
    % \newcommand*{\youyuan}{\CJKfamily{zhyou}} % 幼圆

    \endinput
    [/tex]

    其中带中括号的字体名都是需要修改的,这时需运行
    [shell]
    fc-list :lang=zh-cn
    [/shell]
    来查看系统中的中文字体,记下楷体仿宋对应的名称,即显示信息中第一个英文
    在我的系统中楷体是 KaiTi,仿宋是 FangSong
    不过会因为安装的字体版本不同而有所差异

    接下来只要将对应的字体修改即可,即
    把[SIMKAI.TTF]修改为KaiTi
    把[SIMFANG.TTF]修改为FangSong
    需要注意不止一处

    vim下的替换命令
    [shell]
    :%s/\[SIMKAI.TTF\]/KaiTi/g
    :%s/\[SIMFANG.TTF\]/FangSong/g
    [/shell]

    其他一些废话:
    如果自己安装过ctex包(可能安装在了texmf-local目录下),一定要删去
    另一个解决方案是使用Adobe的字体,调用ctex包时增加adobefonts参数即可

    关于ctex包这个错误的原因开发者在这里已经说的很清楚,也就不再赘述。
    虽然作者不推荐修改,但是确实没有找到更方便的解决办法。欢迎讨论交流。

  • 北航软院本科毕设答辩演示高仿LaTeX模板

    今天答辩,不想用学院给的PPT模板,就写了这个高仿的LaTeX模板,今天展示下来还不错,所以发出来了,如果后几个小班的人有人想用,非常欢迎尝试。需要提醒的是,答辩教室的机器上没有pdf阅读器,需要自带一个,推荐用smartrapdf的portable版。

    代码托管在了Github上,这里是传送门,更详细的信息可以看Github上的README。