From bbe82d29b8f97bda48ea27e76d0a60d1caa99b85 Mon Sep 17 00:00:00 2001
From: kwadronaut <kwadronaut@autistici.org>
Date: Tue, 23 Feb 2021 15:44:34 +0100
Subject: [PATCH] [feat] add eip v4

- Closes: #16
---
 Makefile                                      | 10 ++++--
 config/CONFIG                                 |  1 +
 main.go                                       |  9 +++--
 .../templates/{ => v3}/eip-service.json.jinja |  0
 scripts/templates/v4/eip-service.json.jinja   | 34 +++++++++++++++++++
 5 files changed, 49 insertions(+), 5 deletions(-)
 rename scripts/templates/{ => v3}/eip-service.json.jinja (100%)
 create mode 100644 scripts/templates/v4/eip-service.json.jinja

diff --git a/Makefile b/Makefile
index a435bcd..209e71c 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,8 @@
 CONFIG=config/demo.yaml
-EIP_TEMPLATE=scripts/templates/eip-service.json.jinja
-EIP_SERVICE=deploy/public/3/eip-service.json
+EIP_TEMPLATE_3=scripts/templates/3/eip-service.json.jinja
+EIP_TEMPLATE_4=scripts/templates/4/eip-service.json.jinja
+EIP_SERVICE_3=deploy/public/3/eip-service.json
+EIP_SERVICE_4=deploy/public/4/eip-service.json
 PROVIDER_TEMPLATE=scripts/templates/provider.json.jinja
 PROVIDER=deploy/public/provider.json
 
@@ -17,7 +19,9 @@ gen-shapeshifter:
 	scripts/gen-shapeshifter-state.py deploy/shapeshifter-state
 gen-provider:
 	mkdir -p deploy/public/3
-	@python3 scripts/simplevpn.py --file=eip --config=$(CONFIG) --template=$(EIP_TEMPLATE) --obfs4_state deploy/shapeshifter-state > $(EIP_SERVICE) || echo "ERROR: see $(EIP_SERVICE) for output"
+	mkdir -p deploy/public/4
+	@python3 scripts/simplevpn.py --file=eip --config=$(CONFIG) --template=$(EIP_TEMPLATE_3) --obfs4_state deploy/shapeshifter-state > $(EIP_SERVICE_3) || echo "ERROR: see $(EIP_SERVICE_3) for output"
+	@python3 scripts/simplevpn.py --file=eip --config=$(CONFIG) --template=$(EIP_TEMPLATE_4) --obfs4_state deploy/shapeshifter-state > $(EIP_SERVICE_4) || echo "ERROR: see $(EIP_SERVICE_4) for output"
 	@python3 scripts/simplevpn.py --file=provider --config=$(CONFIG) --template=$(PROVIDER_TEMPLATE) > $(PROVIDER) || echo "ERROR: see $(PROVIDER) for output"
 	rsync -ra deploy/public/ public/
 populate:
diff --git a/config/CONFIG b/config/CONFIG
index e1a31c9..6919143 100755
--- a/config/CONFIG
+++ b/config/CONFIG
@@ -1,6 +1,7 @@
 # general variables
 
 export VPNWEB_API_PATH="./public"
+export VPNWEB_API_VERSION="4"
 export VPNWEB_PROVIDER_CA="./public/ca.crt"
 export VPNWEB_AUTH=sip2
 export VPNWEB_CAKEY=test/files/ca.key
diff --git a/main.go b/main.go
index 4f223a2..2bfd94d 100644
--- a/main.go
+++ b/main.go
@@ -29,11 +29,16 @@ func main() {
 
 	/* static files */
 
+	web.HttpFileHandler(srv, "/provider.json", filepath.Join(opts.ApiPath, "provider.json"))
+	web.HttpFileHandler(srv, "/ca.crt", opts.ProviderCaPath)
 	web.HttpFileHandler(srv, "/3/configs.json", filepath.Join(opts.ApiPath, "3", "configs.json"))
 	web.HttpFileHandler(srv, "/3/service.json", filepath.Join(opts.ApiPath, "3", "service.json"))
 	web.HttpFileHandler(srv, "/3/config/eip-service.json", filepath.Join(opts.ApiPath, "3", "eip-service.json"))
-	web.HttpFileHandler(srv, "/provider.json", filepath.Join(opts.ApiPath, "provider.json"))
-	web.HttpFileHandler(srv, "/ca.crt", opts.ProviderCaPath)
+
+	web.HttpFileHandler(srv, "/4/configs.json", filepath.Join(opts.ApiPath, "4", "configs.json"))
+	web.HttpFileHandler(srv, "/4/service.json", filepath.Join(opts.ApiPath, "4", "service.json"))
+	web.HttpFileHandler(srv, "/4/config/eip-service.json", filepath.Join(opts.ApiPath, "4", "eip-service.json"))
+	web.HttpFileHandler(srv, "/4/ca.crt", opts.ProviderCaPath)
 
 	mtr := http.NewServeMux()
 	mtr.Handle("/metrics", promhttp.Handler())
diff --git a/scripts/templates/eip-service.json.jinja b/scripts/templates/v3/eip-service.json.jinja
similarity index 100%
rename from scripts/templates/eip-service.json.jinja
rename to scripts/templates/v3/eip-service.json.jinja
diff --git a/scripts/templates/v4/eip-service.json.jinja b/scripts/templates/v4/eip-service.json.jinja
new file mode 100644
index 0000000..9dd5498
--- /dev/null
+++ b/scripts/templates/v4/eip-service.json.jinja
@@ -0,0 +1,34 @@
+{
+    "serial": 4,
+    "version": 4,
+    "locations": { {% for loc in locations %}
+        "{{loc}}": {
+            "name": "{{ locations[loc]['name'] }}",
+            "country_code": "{{ locations[loc]['country_code'] }}",
+            "hemisphere": "{{ locations[loc]['hemisphere'] }}",
+            "timezone": "{{ locations[loc]['timezone'] }}"
+        }{{ "," if not loop.last }}{% endfor %}
+    },
+    "gateways": [ {% for gw in gateways %}
+        {
+            "host": "{{ gateways[gw]["host"] }}",
+            "ip_address": "{{ gateways[gw]["ip_address"] }}",
+            "ip_address6": "{{ gateways[gw]["ip_address6"] }}",
+            "location": "{{ gateways[gw]["location"] }}",
+            "capabilities": {
+                "adblock": false,
+                "filter_dns": false,
+                "limited": false,
+                "transport": [ {% for tr, proto, port, options in gateways[gw]["transports"] %}
+                    {"type": "{{ tr }}",
+                     "protocols": ["{{ proto }}"],{% if options %}
+                     "options": {{ options | tojson }},{% endif %}
+                     "ports": ["{{ port }}"]
+                    }{{ "," if not loop.last }}{% endfor %}
+                ]
+            }
+        }{{ "," if not loop.last }}{% endfor %}
+    ],
+    "auth": "{{ auth }}",
+    "openvpn_configuration": {{ openvpn|tojson(indent=8) }}
+}
-- 
GitLab