Skip to content

Commit

Permalink
Merge pull request #1 from SimplyVanilla/feature/init
Browse files Browse the repository at this point in the history
Init
  • Loading branch information
Netherwhal authored Jan 7, 2024
2 parents 7791156 + b02cf1f commit b37c02d
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 1 deletion.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
indent_size = 2
indent_style = space

[Makefile]
indent_size = 4
indent_style = tab

[*.py]
indent_size = 4
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2
updates:

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"

- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "daily"
53 changes: 53 additions & 0 deletions .github/workflows/build-and-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: build and publish

permissions:
id-token: write

on:
push:
branches:
- main
tags:
- '*'
pull_request:
branches:
- main

env:
TERM: xterm-256color

jobs:
build-and-publish:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']

steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Build
run: make build
env:
PYTHON_VERSION: ${{ matrix.python-version }}

- name: Test
run: make test
env:
PYTHON_VERSION: ${{ matrix.python-version }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Publish to PyPI
if: matrix.python-version == '3.11' && github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
41 changes: 41 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: "CodeQL"

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
schedule:
- cron: "21 17 * * 3"

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ python ]

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
queries: +security-and-quality

- name: Autobuild
uses: github/codeql-action/autobuild@v3

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
with:
category: "/language:${{ matrix.language }}"
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
/.idea/
/.venv/

__pycache__
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# votifier.py
# votifier.py
5 changes: 5 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from votifier import VoteV1, Client

v = VoteV1('EliteToplist.net', 'Netherwhal', 'player-ip')
c = Client('server-ip', 'public-key')
c.vote(v)
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
coveralls
flake8
setuptools
twine
wheel
rsa
28 changes: 28 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from setuptools import setup, find_packages


def find_description():
with open('README.md') as fh:
return fh.read()


setup(
name='votifier',
version='0.0.1',
author='Netherwhal',
author_email='[email protected]',
description='Votifier v1 Client.',
long_description=find_description(),
long_description_content_type='text/markdown',
url='https://github.com/SimplyVanilla/votifier.py',
packages=find_packages(exclude=['tests*']),
classifiers=[
'Programming Language :: Python :: 3',
'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
'Operating System :: OS Independent',
],
python_requires='>=3',
install_requires=[
'rsa',
],
)
1 change: 1 addition & 0 deletions votifier/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .votifier import VoteV1, Client
64 changes: 64 additions & 0 deletions votifier/votifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from datetime import datetime, timezone
import socket
import rsa


class VoteV1:
def __init__(
self,
service_name: str,
username: str,
address: str,
timestamp: datetime = datetime.now(timezone.utc),
):
self.service_name = service_name
self.username = username
self.address = address
self.timestamp = round(timestamp.timestamp() * 1000)

def __str__(self) -> str:
return f"""VOTE
{self.service_name}
{self.username}
{self.address}
{self.timestamp}
"""


def ensure_pem_format(key_str: str) -> str:
pem_header = "-----BEGIN PUBLIC KEY-----"
pem_footer = "-----END PUBLIC KEY-----"

if not key_str.startswith(pem_header):
key_str = pem_header + "\n" + key_str

if not key_str.endswith(pem_footer):
key_str = key_str + "\n" + pem_footer

return key_str


class Client:
def __init__(
self,
host: str,
public_key: str,
port: int = 8192,
timeout: int = 3,
):
self.public_key = public_key
self.address = (host, port)
self.timeout = timeout

def vote(self, vote: VoteV1):
pub_key = rsa.PublicKey.load_pkcs1_openssl_pem(ensure_pem_format(self.public_key).encode())
encrypted_message = rsa.encrypt(str(vote).encode(), pub_key)

# Send the encrypted message
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(self.address)
s.settimeout(self.timeout)
header = s.recv(64).decode().split()
if len(header) != 2:
raise Exception("Not a Votifier v1 server")
s.sendall(encrypted_message)

0 comments on commit b37c02d

Please sign in to comment.