Skip to content

Commit

Permalink
Move new logic to /api/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
DasSkelett committed Aug 27, 2022
1 parent 0a41399 commit cbde730
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- [WireGuard Key Exchange](#wireguard-key-exchange)
* [Overview](#overview)
+ [Frontend broker](#frontend-broker)
- [POST /api/v1/wg/key/exchange](#post--api-v1-wg-key-exchange)
- [POST /api/v2/wg/key/exchange](#post--api-v2-wg-key-exchange)
+ [Backend worker](#backend-worker)
* [Installation](#installation)
* [Configuration](#configuration)
Expand Down Expand Up @@ -34,10 +34,10 @@ an MQTT bus for all workers to consume.
The frontend broker exposes the following API endpoints for use:

```
/api/v1/wg/key/exchange
/api/v2/wg/key/exchange
```

#### POST /api/v1/wg/key/exchange
#### POST /api/v2/wg/key/exchange

JSON POST'd to this endpoint should be in this format:

Expand Down Expand Up @@ -124,7 +124,7 @@ The test can be run using `bazel test ... --test_output=all` or `python3 -m unit

The client can be used via CLI:
```
$ wget -q -O- --post-data='{"domain": "ffmuc_welt","public_key": "o52Ge+Rpj4CUSitVag9mS7pSXUesNM0ESnvj/wwehkg="}' --header='Content-Type:application/json' 'http://127.0.0.1:5000/api/v1/wg/key/exchange'
$ wget -q -O- --post-data='{"domain": "ffmuc_welt","public_key": "o52Ge+Rpj4CUSitVag9mS7pSXUesNM0ESnvj/wwehkg="}' --header='Content-Type:application/json' 'http://127.0.0.1:5000/api/v2/wg/key/exchange'
{
"Endpoint": {
"Address": "gw04.ext.ffmuc.net:40011",
Expand All @@ -140,7 +140,7 @@ Or via python:
import requests
key_data = {"domain": "ffmuc_welt","public_key": "o52Ge+Rpj4CUSitVag9mS7pSXUesNM0ESnvj/wwehkg="}
broker_url = "http://127.0.0.1:5000"
push_key = requests.get(f'{broker_url}/api/v1/wg/key/exchange', json=key_data)
push_key = requests.get(f'{broker_url}/api/v2/wg/key/exchange', json=key_data)
print(f'Key push was: {push_key.json().get("Message")]}')
```

Expand Down
27 changes: 24 additions & 3 deletions wgkex/broker/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ def index() -> str:


@app.route("/api/v1/wg/key/exchange", methods=["POST"])
def wg_key_exchange() -> Tuple[Response, int]:
def wg_api_v1_key_exchange() -> Tuple[Response, int]:
"""Retrieves a new key and validates.
Returns:
Status message.
"""
Expand All @@ -96,7 +95,29 @@ def wg_key_exchange() -> Tuple[Response, int]:
domain = data.domain
# in case we want to decide here later we want to publish it only to dedicated gateways
gateway = "all"
logger.info(f"wg_key_exchange: Domain: {domain}, Key:{key}")
logger.info(f"wg_api_v1_key_exchange: Domain: {domain}, Key:{key}")

mqtt.publish(f"wireguard/{domain}/{gateway}", key)
return jsonify({"Message": "OK"}), 200


@app.route("/api/v2/wg/key/exchange", methods=["POST"])
def wg_api_v2_key_exchange() -> Tuple[Response, int]:
"""Retrieves a new key, validates it and responds with a worker/gateway the client should connect to.
Returns:
Status message, Endpoint with address/domain, port pubic key and link address.
"""
try:
data = KeyExchange.from_dict(request.get_json(force=True))
except TypeError as ex:
return abort(400, jsonify({"error": {"message": str(ex)}}))

key = data.public_key
domain = data.domain
# in case we want to decide here later we want to publish it only to dedicated gateways
gateway = "all"
logger.info(f"wg_api_v2_key_exchange: Domain: {domain}, Key:{key}")

mqtt.publish(f"wireguard/{domain}/{gateway}", key)

Expand Down

0 comments on commit cbde730

Please sign in to comment.