pagekite.py で localhost を公開する

Pagekite.py は、リバース プロキシを使用してローカルホストへのトンネルを作成し、ネットワークの外部からパブリックにアクセスできるようにするツールです。これはローカルホスト トンネリングとも呼ばれ、ネットワークの外部から NAT とファイアウォールの背後にあるローカルで実行中の Web アプリをすばやくテストしたりアクセスしたりしたいが、パブリック静的 IP を使用した DNS 構成用の VPS などのクラウドベースのサーバーを購入したり、AWS EC2 などのクラウドベースのインスタンスにデプロイしたりするなどの複雑なプロセスを実行したくない場合に特に便利です。

Terminology

  • Pagekite backend: A pagekite backend is your local running web app that is behind NAT or firewall and does not have publicly accessible static ip address and domain name. It is the one you want to be exposed to internet.
  • Pagekite frontend: A pagekite frontend is a proxy server that is on the internet with a publicly accessible static ip address or domain name and to which a pagekite backend is connecting to for proxy and also through which outsiders can access for further accessing your local running web app.

How to install pagekite.py

  • Install for backend (Mac, Linux)
$ curl -s https://pagekite.net/pk/ |sudo bash

# After above command succeeds, pagekite.py should be available at:
/usr/local/bin/pagekite.py

# See help doc
$ pagekite.py --help
  • Install for frontend (Linux - Ubuntu)
# Add our repository to /etc/apt/sources.list
$ echo deb http://pagekite.net/pk/deb/ pagekite main | sudo tee -a /etc/apt/sources.list

# Add the PageKite packaging key to your key-ring
$ sudo apt-key adv --recv-keys --keyserver keys.gnupg.net AED248B1C7B2CAC3

# Refresh your package sources by issuing
$ sudo apt-get update

# Install pagekite !
$ sudo apt-get install pagekite

# After above steps, pagekite command should be available at:
/usr/bin/pagekite

Note that installing with deb (apt) or rpm (yum) will install bonus scripts for auto start and upgrade and configs, so it should be preferred on linux (ubuntu or centos).

How to config pagekite.py frontend

# Remove all lines in below file:
sudo vi /etc/pagekite.d/10_account.rc

# Edit below file:
sudo vi /etc/pagekite.d/20_frontend.rc
# Add below content:
isfrontend
# ports that frontend is listening for local service to connect to
# and also for outsiders to access, comma separated, such as 80,443
ports=8080
# protocols that frontend is using for outsiders to access, such as http,https
protos=http
# domain of the frontend server and password is for
# backend authentication during pagekite connection
domain=http:xxx.com:password
# if dns is configured to allow for wildcard naming resolution, then
# using the * like below gives users free choices for kite names
domain=http:*.xxx.com:password
# if tls/ssl will be used (e.g. 443 port), then below entry is for
# specifying the pem file (containing certificate and key)
#tls_endpoint=xxx.com:/path/to/pem

How to config pagekite.py backend with rc file and make connection to expose local services

  • Create a config file at: ~/.pagekite.rc with below content
kitename=KITENAME
kitesecret=PASSWORD
frontend=FRONTEND_HOSTNAME:FRONTEND_PORT
  • Run command below to expose http://localhost:8080 for example:
$ pagekite.py 8080 KITENAME
  • Run command below to expose running web app on other known hosts (with ip: x.x.x.x):
$ pagekite.py x.x.x.x:yy KITENAME
  • Run command below to expose a local directory and files:
$ pagekite.py <directory> KITENAME +indexes
  • Run command below to expose html files:
$ pagekite.py *.html KITENAME
  • NOTE: If need different KITENAMEs for above examples, multiple kitename entries should be added in ~/.pagekite.rc file:
kitename=KITENAME1
kitename=KITENAME2
...
kitename=KITENAMEX
kitesecret=PASSWORD
frontend=FRONTEND_HOSTNAME:FRONTEND_PORT

How to connect to frontend directly from command line without using ~/.pagekite.rc

# expose a local service running on port 8080
## specify proto and port:
$ pagekite.py --clean \
  --frontend=FRONTEND-HOST:FRONTEND-PORT \
  --service_on=http/8080:KITENAME:localhost:8080:password
## specify proto and use default port frontend is listening on
$ pagekite.py --clean \
  --frontend=FRONTEND-HOST:FRONTEND-PORT \
  --service_on=http:KITENAME:localhost:8080:password

Others

  • Pagekite.py can not only be used for exposing http service, but also ssh or technically any other protocols
  • Pagekite.py can also be used to quickly expose static files or directories thanks to its built web http server
  • pagekite.me is a paid frontend where you can register a custom name like http://youname.pagekite.me for public access
  • Similar tools and services are also available, such as: ngrok, localtunnel, and etc.
  • Pagekite is open source and available here

参考文献

networking