From 91b3870d2215e498929098cd0db37e6093227519 Mon Sep 17 00:00:00 2001 From: Antonin Kaplan Date: Wed, 29 Apr 2026 08:50:04 +0200 Subject: [PATCH] initial commit --- .gitea/workflows/build-container.yaml | 52 +++++++++++++++++ Dockerfile | 6 ++ README.md | 6 +- main.py | 80 +++++++++++++++++++++++++++ requirements.txt | 3 + 5 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 .gitea/workflows/build-container.yaml create mode 100644 Dockerfile create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitea/workflows/build-container.yaml b/.gitea/workflows/build-container.yaml new file mode 100644 index 0000000..79e0dbf --- /dev/null +++ b/.gitea/workflows/build-container.yaml @@ -0,0 +1,52 @@ +name: release-tag + +on: + push + +jobs: + release-image: + runs-on: ubuntu-latest + container: + image: catthehacker/ubuntu:act-latest + env: + DOCKER_ORG: angoosh + DOCKER_LATEST: latest + RUNNER_TOOL_CACHE: /toolcache + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker BuildX + uses: docker/setup-buildx-action@v2 + with: + config-inline: | + [registry."gitea.angoosh.com"] + #http = true + #insecure = true + + - name: Login to DockerHub + uses: docker/login-action@v2 + with: + registry: https://gitea.angoosh.com + username: ${{ secrets.PKG_REG_USER }} + password: ${{ secrets.PKG_REG_PASS }} + + - name: Get Meta + id: meta + run: | + echo REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F"/" '{print $2}') >> $GITHUB_OUTPUT + echo REPO_VERSION=$(git describe --tags --always | sed 's/^v//') >> $GITHUB_OUTPUT + + - name: Build and push + uses: docker/build-push-action@v4 + with: + context: . + file: ./Dockerfile + platforms: | + linux/amd64 + push: true + tags: | + gitea.angoosh.com/${{ env.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}:${{ env.DOCKER_LATEST }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7de388e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +FROM python:3-alpine +ADD requirements.txt / +RUN pip install -r requirements.txt +ADD main.py / +EXPOSE 8000 +CMD [ "python", "./main.py" ] diff --git a/README.md b/README.md index 3b76636..23bcc9d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ # mktxp-extension -Extends mktxp with additional features. \ No newline at end of file +Extends mktxp with additional features. + +## Aditional features +1. Wireguard +2. Mangle diff --git a/main.py b/main.py new file mode 100644 index 0000000..7aa32cf --- /dev/null +++ b/main.py @@ -0,0 +1,80 @@ +import routeros_api +from prometheus_client import start_http_server, Gauge +import time +import re +import os + +APP_NAME = 'mktxp-extension' +RTR_ADDRESS = os.environ.get('RTR_ADDRESS') +RTR_USER = os.environ.get('RTR_USER') +RTR_PASS = os.environ.get('RTR_PASS') + +gauge_wg_rx = Gauge(APP_NAME+'_wireguard_rx', 'Rx amount on peer', ['peer','interface']) +gauge_wg_tx = Gauge(APP_NAME+'_wireguard_tx', 'Tx amount on peer', ['peer','interface']) +gauge_wg_handshake = Gauge(APP_NAME+'_wireguard_handshake', 'Last peer handshake', ['peer','interface']) +gauge_wg_active = Gauge(APP_NAME+'_wireguard_active', 'If peer is active, is set to inactive after 5 minutes of last connection', ['peer','interface']) +gauge_mangle_rx = Gauge(APP_NAME+'_manle_rx', 'Rx amount on rule', ['comment']) +gauge_mangle_tx = Gauge(APP_NAME+'_mangle_tx', 'Tx amount on rule', ['comment']) + +connection = routeros_api.RouterOsApiPool(RTR_ADDRESS, username=RTR_USER, password=RTR_PASS, plaintext_login=True) +api = connection.get_api() + +def get_wg(): + + list = api.get_resource('/interface/wireguard/peers') + peers = list.get() + for peer in peers: + seconds = 0 + + gauge_wg_rx.labels(peer=peer['name'], interface=peer['interface']).set(int(peer['rx'])) + gauge_wg_tx.labels(peer=peer['name'], interface=peer['interface']).set(int(peer['tx'])) + lasthandshake = '' + try: + lasthandshake = peer['last-handshake'] + + tm = re.split(r'[dhms]',lasthandshake) + tm_rev = [] + multiply = [] + for i in range(0,len(tm)-1): + tm_rev.append(tm[len(tm)-2-i]) + if 's' in lasthandshake: + multiply.append(1) + if 'm' in lasthandshake: + multiply.append(60) + if 'h' in lasthandshake: + multiply.append(3600) + if 'd' in lasthandshake: + multiply.append(86400) + + for i in range(0, len(tm_rev)): + seconds += int(tm_rev[i]) * multiply[i] + + except: + seconds = -1 + gauge_wg_handshake.labels(peer=peer['name'], interface=peer['interface']).set(seconds) + if (seconds == -1) or (seconds > 300): + gauge_wg_active.labels(peer=peer['name'], interface=peer['interface']).set(0) + else: + gauge_wg_active.labels(peer=peer['name'], interface=peer['interface']).set(1) + +def get_mangle(): + + list = api.get_resource('/ip/firewall/mangle') + rules = list.get() + for rule in rules: + seconds = 0 + if "Download" in rule['comment']: + gauge_mangle_rx.labels(comment=rule['comment']).set(int(rule['bytes'])) + elif "Upload" in rule['comment']: + gauge_mangle_tx.labels(comment=rule['comment']).set(int(rule['bytes'])) + else: + pass + + +if __name__ == '__main__': + start_http_server(8000) + + while True: + get_wg() + get_mangle() + time.sleep(5) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..24b6624 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +regex +prometheus_client +routeros_api