Skip to content

Instantly share code, notes, and snippets.

@giuseppeM99
Last active October 17, 2020 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giuseppeM99/db3453cf595955c94f0026c7bf10e7e8 to your computer and use it in GitHub Desktop.
Save giuseppeM99/db3453cf595955c94f0026c7bf10e7e8 to your computer and use it in GitHub Desktop.
Wireguard telegram bot
'''
BSD 2-Clause License
Copyright (c) 2020, Giuseppe Marino
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''
import botogram
from wgnlpy import WireGuard
from base64 import b64encode
from ipaddress import IPv4Address, IPv4Network
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey
from cryptography.hazmat.primitives import serialization
wg = WireGuard()
bot = botogram.create(BOT_TOKEN)
ADMINS = [68972553]
INTERFACE = 'wg0'
@bot.command("peers")
def peers(chat, message, args):
"""Lista dei peer di wireguard"""
if chat.id in ADMINS:
list = wg.get_interface(INTERFACE).peers
text = ''
for p in list:
text += '<pre>'+b64encode(p).decode('utf-8') + '</pre>: ' + str(list[p]['allowedips'][0]) + '\n'
chat.send(text, syntax='HTML')
@bot.command("addpeer")
def newpeer(chat, args):
"""Aggiunge un peer con l'IP passato, ritorna la private key"""
if chat.id in ADMINS:
ip = IPv4Network(args[0])
key = X25519PrivateKey.generate()
private = b64encode(key.private_bytes(encoding=serialization.Encoding.Raw, format=serialization.PrivateFormat.Raw, encryption_algorithm=serialization.NoEncryption())).decode('utf-8')
public = b64encode(key.public_key().public_bytes()).decode('utf-8')
print(wg.set_peer(INTERFACE, public, allowedips=[ip]))
chat.send('<pre>' + private + '</pre>', syntax = 'HTML')
@bot.command("delpeer")
def delpeer(chat, args):
"""Rimuove il peer con la public key passata"""
if chat.id in ADMINS:
peer = args[0]
print(wg.remove_peers(INTERFACE, peer))
chat.send('ok?')
@bot.command("pubkey")
def pubkey(chat):
"""Manda la public key del server"""
if chat.id in ADMINS:
chat.send(b64encode(wg.get_interface(INTERFACE).public_key).decode('utf-8'))
if __name__ == "__main__":
bot.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment