Skip to content

Commit

Permalink
Added first working version - 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
blackandred committed Mar 2, 2017
1 parent 4f3fa24 commit c2ad56d
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/var/pids/*
!/var/pids/.gitkeep
/conf.d/*
!/conf.d/config-example.sh.md
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
# private-networking
Networking solution written in Bash, based on reverse proxy
Allows to create multiple reverse tunnels from inside of NAT to the external server.

## Setup

- Put your configuration files into `conf.d`

```
1. File must be with ".sh" extension
2. File must be executable (eg. chmod +x "webserver.sh")
3. File must be in a proper syntax and implement proper configuration variables
described as an example in the "config-example.sh.md"
```

Send public key to all servers described in your configuration
so the communication could be without a password using a ssh key.

Run: `./bin/send-public-key.sh`

- Bind your ports to the external server

Run: `./bin/bind-network.sh`

That's all!
Your local services should be exposed to the remote server and be
visible on eg. http://localhost:1234, so you need an internal proxy or
a load balancer like nginx to forward the traffic to the internet.
36 changes: 36 additions & 0 deletions bin/bind-network.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

#--------------------------------------------
# Bind network ports to the remote server
# using a reverse proxy strategy
#
# @author Wolnościowiec Team
# @see https://wolnosciowiec.net
#--------------------------------------------

cd "$( dirname "${BASH_SOURCE[0]}" )"
DIR=$(pwd)

./kill-previous-sessions.sh

for config_file_name in ../conf.d/*.sh
do
echo " >> Reading $config_file_name"
source "$config_file_name"

for forward_ports in ${PORTS[*]}
do
IFS='>' read -r -a parts <<< "$forward_ports"
source_port=${parts[0]}
dest_port=${parts[1]}

echo " --> Forwarding $source_port:$PN_HOST:$dest_port"
autossh -M 0 -N -f -o "PubkeyAuthentication=yes" -o "PasswordAuthentication=no" -R "$source_port:$PN_HOST:$dest_port" "$PN_USER@$PN_HOST" -p $PN_PORT
echo $! >> ../var/pids/ssh-servers.pid

if [[ $? != 0 ]]; then
echo " ~ The port forwarding failed, please verify if your SSH keys are well installed"
exit 1
fi
done
done
26 changes: 26 additions & 0 deletions bin/kill-previous-sessions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

#--------------------------------------------
# Kill all previously opened ssh sessions
#
# @author Wolnościowiec Team
# @see https://wolnosciowiec.net
#--------------------------------------------

cd "$( dirname "${BASH_SOURCE[0]}" )"
DIR=$(pwd)

for config_file_name in ../conf.d/*.sh
do
source "$config_file_name"

for forward_ports in ${PORTS[*]}
do
pid=$(ps aux |grep autossh|grep "$source_port:$PN_HOST:$dest_port"|grep -v "grep"|awk '{print $2}')

if [[ $pid ]]; then
echo " >> Killing $pid"
kill $pid
fi
done
done
18 changes: 18 additions & 0 deletions bin/send-public-key.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

cd "$( dirname "${BASH_SOURCE[0]}" )"
DIR=$(pwd)

if [[ ! -f ~/.ssh/id_rsa.pub ]]; then
echo " >> RSA key not found, generating"
ssh-keygen -t rsa -f ~/.ssh/id_rsa
fi

for config_file_name in ../conf.d/*.sh
do
echo " >> Reading $config_file_name"
source "$config_file_name"

echo " >> Copying your ID to the $PN_USER@$PN_HOST:$PN_PORT, please log in"
ssh-copy-id -i ~/.ssh/id_rsa "$PN_USER@$PN_HOST" -p $PN_PORT
done
9 changes: 9 additions & 0 deletions conf.d/config-example.sh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
```
PN_USER=xxx
PN_PORT=22
PN_HOST=mydomain.org
# local port => destination port
PORTS[0]="8000>80"
PORTS[1]="22>2222"
```
Empty file added var/pids/.gitkeep
Empty file.

0 comments on commit c2ad56d

Please sign in to comment.