solyaris
Posts: 77
Joined: Thu Mar 27, 2014 11:03 am
Location: Genova, Italy
Contact: Website

open reverse proxy on a raspberry with ngrok!

Thu Jun 05, 2014 8:01 am

Just to advertise about excellent ngrok open source project! To show you what exactly do ngrok, let me quot the http://www.ngrok.com website:

1. Demo your latest code
ngrok creates a tunnel from the public internet (http://subdomain.ngrok.com) to a port on your local machine. You can give this URL to anyone to allow them to try out a web site you're developing without doing any deployment.

2. Understand HTTP services
ngrok captures all traffic through the tunnel. It displays information about the HTTP traffic for your inspection. Raw request/response bytes, parsed headers and form data, JSON/XML syntax checking and more are included.

3. Replay Requests
Developing services which consume webhooks can be challenging if the hooks are labor-intensive to generate. Use ngrok's replay request feature to iterate quickly on a new feature without switching contexts to generate new requests.

Download and Installation (for Raspberry)
ngrok is easy to install. Download a single binary with zero run-time dependencies for any major platform. Unzip it and then run it from the command line. Ngrok run like a charm on a Raspberry, already complied for the ARM processor so, just download it:
https://dl.ngrok.com/linux_arm/ngrok.zip

Pricing & Features
ngrok.com is a pay-what-you-want service offered with no signup required. Many features are available for FREE to everyone, without signing up. Just download and run!

As said ngrok is an opensource project (https://github.com/inconshreveable/ngrok). You can run install and host and run your ngrok server, or SIMPLY you can use ngrok.com cloud server for basic usage; here you have many for free and some features are available under pay-what-you-want model. For my needs is essential the feature that allow to configure your Custom Subdomains: Pick a custom subdomain so your app appears on https://myapp.ngrok.com instead of http://a1b2c3d4.ngrok.com
To configure your subdomain, please above all do it here: https://ngrok.com/dashboard

Run ngrok as a Raspian daemon (my small contribute/tips):
for my project, a cloud server need to communicate with remote "terminals" (terminals are Raspberry HW with Raspian OS), I choose to implement "webhooks" to "push" data (ecommerce orders) to terminals. In this scenario each terminal:

A) run a webserver (Sinatra above Ruby in my case) that reply to HTTP REST APIs as

HTTP POST http://localhost:NNNN/neworder
HTTP GET http://localhost:NNNN/status

etc.

BTW, Please note the webserver in the case above reply from port number NNNN
In my case, as Rubyst, the API webserver is a simple Sinatra app, that do something like:

# myapp.rb
# require 'sinatra'
get '/status' do
# doing something
end

post '/neworder' do
# doing something
end


B) run a ngrok daemon that allow to publish to a internet address. This is possible using the (previously configured) subdomain GREAT feature of ngrok, and above mentioned endpoints become (please note that HTTPS is allowed too!):

HTTP POST https://myapp.ngrok.com/neworder
HTTP GET https://myapp.ngrok.com/status

etc.

A minor note about a possible ngrok client daemon script; I use this one:

#!/bin/bash
# this file: /your/path/ngrok.sh
TZ='Europe/Rome' /your/path/ngrok -config=/your/path/.ngrok -log=stdout -subdomain=myapp NNNN > /your/path/ngrok.log 2>&1 &


minor / stupid notes for beginners (as me):

TZ='Europe/Rome'
set the date inyour local time (I'm from Italy); that's necessary in case you run the daemon at startup (see below for details); otherwise yyou get UTC time.

/your/path/ngrok
run the ngrok client on your raspberry. The client connect to the ngrok.com server

-config=/your/path/.ngrok
.ngrok is the ngrok configuration file; it allow many things. polease read documentation: https://ngrok.com/usage

-subdomain=myapp NNNN
run ngrok with subdomain: myapp mapping localhost port number: NNNN

-log=stdout > /your/path/ngrok.log 2>&1
this let ngrok to NOT use default terminal mode mut instead force to write to log file: /your/path/ngrok.log eventually doing nothing > /dev/null

Important: I have the need to start @reboot the script above in a raspberry connected with a WiFI dongle.
The connectivity need some seconds (almost more than 20 in my case) so putting the script in crontab with @reboot was not a good idea (ngrok go down when start, if do not found networking). The solution come from rapdom (I thank you), here:
http://www.raspberrypi.org/forums/viewt ... 40#p559840

so let put in your /etc/network/interfaces config file this:

# this file: /etc/network/interfaces
# start ngrok after networking is up
post-up /your/path/ngrok.sh


Final notes
Forwarding non-HTTP services! Ngrok run also with TCP mode! that allow by example to SSH to your remote Rasberry ;-)

I come to ngrok after tested other localhost reverse tunnel sw as original Ruby localtunnel (ther are some realizations available also as open source projects). When discovered ngrok, I decided to use this one. I think is an excellent piece of software, written in Go language. It's a open source project.

Above all the ngrok's author, Alan Shreeve (contact@ngrok.com), is a very smart and generous person,
and if you enjoy using ngrok, let him know laso here: https://twitter.com/inconshreveable

I hope this help, i just my small contribute to thank you people in this forum often stand my questions and helped to solve things :-)

giorgio

User avatar
dentex
Posts: 180
Joined: Sun May 20, 2012 7:58 am
Location: Italy
Contact: Website

Re: open reverse proxy on a raspberry with ngrok!

Thu Oct 09, 2014 2:50 pm

Thanks for the info, I was reading some docs also on the grok website and it looks really cool.

I have the problem that I use a Wireless ISP, so I'm always behind a NAT (the roof antenna).
To log-in via SSH to my PC from outside, in the past I used a RasPi as "man in the middle", connecting my desktop to this RasPi with a reverse-SSH-tunnel. Now that the RasPi is behind the NAT too (soon it will serve as a hardware/monitoring project), I lost this man-in-the-middle and I think that grok will do the job.

Saluti. ;)
One of my projects with the Raspberry Pi:
--------------------------------------------------------
(Raspberry) Pi Aquarium Controller: https://github.com/dentex/piac

solyaris
Posts: 77
Joined: Thu Mar 27, 2014 11:03 am
Location: Genova, Italy
Contact: Website

Re: open reverse proxy on a raspberry with ngrok!

Thu Oct 09, 2014 3:41 pm

yes dentex
as you can read in documentation, ngrok support SSH. I used successfully SSH "allowed by" ngrok. just a bit slow (of course).
ngrok ...just ROCK with HTTP (and also TCP ;-))
giorgio

User avatar
dentex
Posts: 180
Joined: Sun May 20, 2012 7:58 am
Location: Italy
Contact: Website

Re: open reverse proxy on a raspberry with ngrok!

Fri Oct 10, 2014 11:57 am

Hello again solyaris,
I tried ngrok on my RPi and also the desktop, amazing with HTTP, you're right.
But how exactly do you set up the SSH tunnel?
At the moment I'm at issuing:

Code: Select all

./ngrok -proto=tcp 22
but then, what do you use to connect remotely?
On the usage page from the ngrok site there's not much info on this.
Thanks.

Edit:
OK, found this one; it's

Code: Select all

ssh $REMOTE_USER@ngrok.com -p $SPECIFIED_PORT 
As you say above, to keep it running in background, closing your local ssh shell:

Code: Select all

./ngrok -log=stdout -proto=tcp 22 > ngrok.log &
If you just use the "&" at the end of a command, it goes in background, but it won't actually connect until you "fg". Then it doesn't like CTRL-Z to pause/go again in background. So we need "-log=stdout".
Ref.: https://github.com/inconshreveable/ngrok/issues/57
One of my projects with the Raspberry Pi:
--------------------------------------------------------
(Raspberry) Pi Aquarium Controller: https://github.com/dentex/piac

Return to “Networking and servers”