Skip to content

Commit

Permalink
fix: Fixes to the routing, dynamic interface name, fixed disappearing…
Browse files Browse the repository at this point in the history
… public key
  • Loading branch information
blackandred committed Aug 3, 2022
1 parent 07fd1a8 commit 31edb06
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Purpose of this setup is to expose private subnets behind `Gateway node` to the
- Access to private subnets behind gateway
- Automatic client configuration generation
- Optional support for TOR (allows to hide administrator identity of a hidden service)
- Systemd support
31 changes: 27 additions & 4 deletions tasks/client.single.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## Retrieve gateway public key ###

- name: Retrieve gateway key
shell: "/usr/local/bin/key-extractor /etc/tinc/{{ network_name }}/hosts/{{ gateway_name }}"
register: gateway_key_stdout

- name: Set gateway key as fact
set_fact:
gateway_key: "{{ gateway_key_stdout.stdout }}"

### Prepare directories ###

- name: Create client temporary directory path
set_fact:
client_temp_path: "{{ local_clients_configs_path }}/{{ client.name }}/etc/tinc/{{ network_name }}"
Expand All @@ -12,17 +24,22 @@
- "{{ client_temp_path }}/"
- "{{ client_temp_path }}/hosts"

### Create client configuration ###

- name: Create tinc.conf
template:
src: client/etc/tinc/my-network/tinc.conf
dest: "{{ client_temp_path }}/tinc.conf"
mode: "u+rw,g+r,o"

- name: Create tinc-up script
- name: Create tinc scripts
template:
src: client/etc/tinc/my-network/tinc-up
dest: "{{ client_temp_path }}/tinc-up"
src: "client/etc/tinc/my-network/{{ item }}"
dest: "{{ client_temp_path }}/{{ item }}"
mode: "u+rwx,g+rx,o"
with_items:
- tinc-up
- tinc-down

- name: Create a gateway host file
template:
Expand All @@ -47,7 +64,13 @@
set_fact:
client_key: "{{ client_key_stdout.stdout }}"

# Copy key to the gateway node
### Generate the client config ###
- name: Create client (self) host file
template:
src: client/etc/tinc/my-network/hosts/client
dest: "{{ client_temp_path }}/hosts/{{ client.name }}"

### Copy key to the gateway node ###
- name: Create client host file on gateway node
template:
src: server/etc/tinc/my-network/hosts/client-template
Expand Down
2 changes: 1 addition & 1 deletion tasks/clients.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
loop: "{{ clients }}"
loop_control:
loop_var: client
include: client.single.yaml
include_tasks: client.single.yaml
17 changes: 17 additions & 0 deletions tasks/server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@
dest: "/etc/tinc/{{ network_name }}/tinc-up"
mode: "u+rwx,g+rx,o"

- set_fact:
gateway_key: ""

- name: Check if gateway host file already exists - to extract public key from it
register: public_key_exists
stat:
path: "/etc/tinc/{{ network_name }}/hosts/{{ gateway_name }}"

- name: Extract existing public key to not delete it
when: public_key_exists.stat.exists
block:
- name: Extract key
shell: "/usr/local/bin/key-extractor /etc/tinc/{{ network_name }}/hosts/{{ gateway_name }}"
register: gateway_key_stdout
- set_fact:
gateway_key: "{{ gateway_key_stdout.stdout }}"

- name: Create a gateway host file
template:
src: server/etc/tinc/my-network/hosts/gateway
Expand Down
3 changes: 2 additions & 1 deletion templates/client/etc/tinc/my-network/hosts/client
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# VPN address
# VPN address. Must be on both sides, without this line the nodes wont be able to ping each other
Subnet = {{ client.ip }}/32

# My public key, should be propagated to other VPN peers, including gateway
{{ client_key }}
2 changes: 1 addition & 1 deletion templates/client/etc/tinc/my-network/tinc-down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
{% for subnet in subnets_to_expose %}
ip -4 route del {{ subnet }} dev {{ dev_interface | default("tun0") }} || true
ip -4 route del {{ subnet }} dev $INTERFACE || true
{% endfor %}
4 changes: 2 additions & 2 deletions templates/client/etc/tinc/my-network/tinc-up
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh
ifconfig $INTERFACE hw ether {{ client.hw_ether }}
ifconfig $INTERFACE hw ether {{ client.hw_ether }} || true
ifconfig $INTERFACE {{ client.ip }} netmask {{ netmask | default("255.255.255.0") }} -arp

{% for subnet in subnets_to_expose %}
ip -4 route add {{ subnet }} dev {{ dev_interface | default("tun0") }}
ip -4 route add {{ subnet }} dev $INTERFACE
{% endfor %}
3 changes: 3 additions & 0 deletions templates/server/etc/tinc/my-network/hosts/gateway
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ Subnet = {{ gateway_ip }}/32
{% for subnet in subnets_to_expose %}
Subnet = {{ subnet }}
{% endfor %}

# Public key of the gateway
{{ gateway_key }}
2 changes: 1 addition & 1 deletion templates/server/etc/tinc/my-network/tinc-up
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh
ifconfig $INTERFACE hw ether {{ gateway_hw_ether }}
ifconfig $INTERFACE hw ether {{ gateway_hw_ether }} || true
ifconfig $INTERFACE {{ gateway_ip }} netmask {{ gateway_mask }} -arp

0 comments on commit 31edb06

Please sign in to comment.