Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

TS-109にOpenVPNを建ててAndroidから繋げるようにした

公衆無線LANからより安全にインターネットにアクセスするために、NAS(TS-109)にOpenVPNを建ててAndroidから繋げるようにしました。

自宅のルータはWHR-HP-G300Nという機種でもともとPPTP機能が付いているのですが、あいにくspモードはPPTPに接続できません。そこで自分でNASにOpenVPNを設定することにしました。

最近のQNAPのNASはもともとOpenVPN機能が備わっていて管理画面からちょいちょいと設定できるようなのですが、うちのTS-109は古い機種なのでそのような新しいファームウェアには対応していません。ですから自分でやる必要があります。

そこで参考にしたページはこちらです。

Install OpenVPN on QNAP

Androidからのアクセスについてはこちらも。

さくらのVPSにOpenVPNを建ててAndroidから繋げるようにした

ダイナミックDNSの設定

まず、インターネットからNASにアクセスできるように、DDNSの設定が必要です。昔はQNAPのDDNSといえばDynDNSだったのですが最近有料になってしまったようです。調べてみると一旦Free Trialに申し込んでから解約すれば1ホストだけは無料で使い続けられるようなのですが、クレジットカード情報の登録が必要なのでやめました。

TS-109にはDynDNSの他にもいくつかのDDNSサービスの設定ができるようになっています。そこで、no-ip を使うことにしました。

http://www.noip.com/

ドメイン登録後、試しにアクセスしてみるとルータのログイン画面がでました。アクセスできているみたいです。

OpenVPNのインストールと設定

インストール自体は簡単です。

# ipkg update
# ipkg list | grep openvpn
openvpn – 2.2.0-1 – SSL based VPN server with Windows client support
# ipkg install openvpn

ここで、openvpn を実行してみると

# openvpn
OpenVPN 2.2.0 arm-none-linux-gnueabi [SSL] [LZO2] [EPOLL] [eurephia] built on Feb 17 2012

General Options:
–config file : Read configuration options from file.
–help : Show options.
–version : Show copyright and version information.
(以下省略)

確かにインストールはできたみたいです。次に、

# cd /opt/etc/openvpn
# mkdir log
# cd log
# touch openvpn.log
# touch status.log
# mkdir /opt/etc/openvpn/modules

そして次に行うのは、tun.ko モジュールのインストールです。以下のリンクから tun.ko モジュールをダウンロードします。

http://www.box.net/shared/68nynucztf

解凍後のtun.ko というファイルを /opt/etc/openvpn/modules へコピーします。

それから autorun.sh を編集します。

# mount -t ext2 /dev/mtdblock5 /tmp/config

/tmp/config/autorun.sh に以下の行を追加します。

# install tun.ko
mkdir /dev/net;
mknod /dev/net/tun c 10 200;
(sleep 20;insmod /opt/etc/openvpn/modules/tun.ko)&

再起動してから、

# lsmod

を実行してみて tun 8896 1 – Live 0xbf07a00 というような行が見えればOK。だめなときは sleep をもう少し増やしてみます。

鍵の生成

鍵の生成はPC上で行います。PCに OpenVPN GUI をインストールします。ダウンロードは以下のリンクから。

http://openvpn.se/

次にインストール先の easy-rsaフォルダで init-config します。

# cd C:\Program Files (x86)\OpenVPN\easy-rsa
# init-config

次に vars.bat を編集します。

set KEY_COUNTRY=JP
set KEY_PROVINCE=Kanagawa
set KEY_CITY=XXX
set KEY_ORG=XXXXXXXX
set KEY_EMAIL=XXXXXXXXXXX

編集できたら次は、

# vars
# clean-all
# build-ca

いろいろ聞かれますが、さっき編集した vars.bat の値がデフォルト値になっているのでReturnキーをたたきます。

Common Name のところはサーバー名をいれます。

次に、サーバ用の鍵を生成します。引数の のところはさっきいれたCommon Nameに置き換えます

# build-key-server

同様にクライアント用の鍵を生成します。のところは適当な名前でどうぞ。

# build-key

そういえば、鍵生成の途中でパスワードを聞かれたような気がしますが、そこは何も入力せずにReturnです。

さらに、ディフィー・ヘルマン鍵共有パラメータを生成します。

# build-dh

できあがった鍵、ca.crt, dh1024.pem, server.crt, server.key をNASの /opt/etc/openvpn/keys に置きます。
さらに、TLS-AUTH HMAC 共有鍵も使うことにしました。

$ /opt/sbin/openvpn –genkey –secret /opt/etc/openvpn/keys/ta.key

サーバーコンフィグファイルの設定

/opt/etc/openvpn/easy.conf というファイルを作りました。中身はこんな感じ。

# OpenVPN server configuration QNAP NAS
# basic settings
port 1194
proto udp
dev tun
#
# detect mtu if the connection is slow.
;mtu-test
#
# define mtu, if necessary
;tun-mtu 1280
;mssfix 1280
;fragment 1280
#
# define the ip-addresses of the underlying tunnel
server 10.8.0.0 255.255.255.0
#
# Route
push “route XXX.XXX.XXX.XXX 255.255.255.0” # <— Enter the ip-address of your home network here! push “redirect-gateway def1” push “dhcp-option DNS XXX.XXX.XXX.XXX” # DNS ip-address # # certificates & keys dh /opt/etc/openvpn/keys/dh1024.pem ca /opt/etc/openvpn/keys/ca.crt cert /opt/etc/openvpn/keys/server.crt key /opt/etc/openvpn/keys/server.key tls-auth /opt/etc/openvpn/keys/ta.key 0 # # data compression comp-lzo push “comp-lzo yes” # # allow, that several clients with the same common name log on ; duplicate-cn # # different clients can “see” each other through the tunnel. ; client-to-client # # Keepalive keepalive 15 120 # # verbosity of status messages in the console. Activate for debugging (1-9 possible) verb 5 # # Log files status /opt/etc/openvpn/log/status.log log-append /opt/etc/openvpn/log/openvpn.log # # Run as daemon (activate, after everything is set up properly) daemon # # Management Interface. Access with “telnet localhost 7505” management localhost 7505

;tun-mtu 1280
;mssfix 1280
;fragment 1280

のところはうまく調整すると高速化できるらしいのですが、デフォルトの1500のままにしてあります。1280じゃなくて 1426 とか 1435とかそんな値がいいみたい。

クライアントコンフィグファイルの設定

以下のような中身のファイル client.ovpnを作ります。

# connect to QNAP OpenVPN Server
#
proto udp
dev tun
client
tls-auth ta.key 1
remote XXX.XXX.XXX 1194 # <— enter your no-ip host name here! pull # set mtu, if necessary ;tun-mtu 1280 ;mssfix 1280 ;fragment 1280 # resolv-retry infinite nobind persist-key persist-tun # certificates and keys # Note the double \\ in the path for a windows config ca ca.crt cert client.crt key client.key # comp-lzo

ルータの設定

ルータの設定で Port 1194 へのアクセスをNASに転送するようにします。WHR-HP-G300Nの場合、管理メニューの”ゲーム&アプリ”というタブの中の”ポート変換”という設定があります。(この設定名はなかなか気づかないよ、発見するのがちょっと大変だったよ。)

テスト

サーバーを起動します。最初はテストなので手動で起動します。さきほどのサーバーコンフィグファイルの中の daemon という行をコメントアウトしてデーモンモードで起動しないようにしておきます。

# cd /opt/etc/openvpn
# openvpn easy.conf

そうするといろいろ出力がでてきて、最後に Initialization Sequence Completed となれば成功です。

次はAndroidの設定です。AndroidのSDカードに以下のファイルをコピーします。

client.ovpn
ca.crt
client1.crt
client1.key
ta.key

Androidには以下のアプリをインストールしておきます。

https://play.google.com/store/apps/details?id=de.blinkt.openvpn

アプリを起動します。左下のフォルダアイコンをタップし、SDカードにコピーした client.ovpnを選択します。
インポートが完了したらFDアイコンをタップして保存します。

インポートしたプロファイル名をタップするとVPNサーバへの接続が始まります。

Androidのターミナルアプリから10.8.0.1へpingが通れば成功です。サーバから10.8.0.X へのping もやってみましょう。

やった、つながったぁ、と思ったらどうもブラウジングできません。さんざん悩んだあげく、

echo “1” > /proc/sys/net/ipv4/ip_forward

これで解決。

速度ですが、下りはざっと3割減くらい。なぜか上りは速くなった。

サーバの自動起動

テストが済んだら、サーバを自動起動するために、autorun.sh に以下の行を追加しておきます。

(sleep 12; /opt/sbin/openvpn /opt/etc/openvpn/easy.conf)&

PAGE TOP