commit dd82a5a4e06b7415e8b9236d7f5cd6ff194f52bc Author: tdurieux Date: Thu Sep 28 15:51:45 2017 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed5daa0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,109 @@ +github_anonymous/ +repositories/ + +# Created by https://www.gitignore.io/api/python + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +# End of https://www.gitignore.io/api/python diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..18cde73 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +requests +flask +pygithub diff --git a/server.py b/server.py new file mode 100644 index 0000000..318eaf2 --- /dev/null +++ b/server.py @@ -0,0 +1,131 @@ +import argparse +import uuid +import json +import socket +import os + + +# non standards, in requirements.txt +from flask import Flask, request, Markup, render_template, redirect, url_for +import requests +import github + + + + +class Anonymous_Github: + def __init__(self, github_token="0e8f5af6801d89e533f5c045920e928b4535d41e", host="127.0.0.1", port=5000, + config_dir='./repositories'): + self.github_token = github_token if github_token != "" else os.environ["GITHUB_AUTH_TOKEN"] + self.host = host + self.port = port + self.config_dir = config_dir + if config_dir[0:2] == "./": + self.config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), config_dir[2:]) + if not os.path.exists(self.config_dir): + os.makedirs(self.config_dir) + self.application = self.create_flask_application() + self.set_public_url() + self.github = github.Github(login_or_token=self.github_token) + + def set_public_url(self): + if self.host == "0.0.0.0": + self.public_url = "http://" + socket.getfqdn() + ":" + str(self.port) + else: + self.public_url = "http://" + self.host + ":" + str(self.port) + + + + def create_flask_application(self): + application = Flask(__name__) + application.log = {} + application.killurl = str(uuid.uuid4()) + + @application.template_filter('file_render', ) + def file_render(file, terms): + def removeTerms(content, terms): + for term in terms: + content = content.replace(term, "XXX") + return content + if ".md" in file.name: + return removeTerms(Markup(self.github.render_markdown(file.decoded_content)), terms) + if ".jpg" in file.name or ".png" in file.name or ".png" in file.name or ".gif" in file.name: + return Markup("%s" % (file.url, file.name)) + if ".html" in file.name: + return removeTerms(Markup(file.decoded_content), terms) + if ".txt" in file.name: + return removeTerms(Markup("
" + file.decoded_content + "
"), terms) + return Markup("Download %s" % (file.url, file.name)) + + @application.route('/' + application.killurl, methods=['POST']) + def seriouslykill(): + func = request.environ.get('werkzeug.server.shutdown') + func() + return "Shutting down..." + + @application.route('/repository/', methods=['GET'], defaults={'path': ''}) + @application.route('/repository//', methods=['GET'], defaults={'path': ''}) + @application.route('/repository//', methods=['GET']) + def repository(id, path): + config_path = self.config_dir + "/" + str(id) + "/config.json" + if not os.path.exists(config_path): + return render_template('404.html'), 404 + with open(config_path) as f: + data = json.load(f) + repo = data['repository']\ + .replace("http://github.com/", "")\ + .replace("https://github.com/", "") + if repo[-1] == '/': + repo = repo[:-1] + g_repo = self.github.get_repo(repo) + current_folder = g_repo.get_contents(path) + current_file = None + if type(current_folder) is github.ContentFile.ContentFile: + current_file = current_folder + current_folder = g_repo.get_contents(os.path.dirname(path)) + else: + for f in current_folder: + if f.name.lower() == "readme.md" or f.name.lower() == "index.html": + current_file = f + break + return render_template('repo.html', terms=data["terms"], current_repository=id, current_file=current_file, current_folder=current_folder) + + @application.route('/', methods=['GET']) + def index(): + return render_template('index.html') + + @application.route('/', methods=['POST']) + def add_repository(): + id = str(uuid.uuid4()) + repo = request.form['githubRepository'] + terms = request.form['terms'] + + config_path = self.config_dir + "/" + str(id) + os.mkdir(config_path) + with open(config_path + "/config.json", 'w') as outfile: + json.dump({ + "id": id, + "repository": repo, + "terms": terms.split("\n") + }, outfile) + return redirect(url_for('repository', id=id)) + + return application + + def run(self, **keywords): + self.application.run(host=self.host, port=self.port, **keywords) + + +def initParser(): + parser = argparse.ArgumentParser(description='Start Anonymous Github') + parser.add_argument('-token', required=True, help='GitHuh token') + parser.add_argument('-host', help='The hostname', default="127.0.0.1") + parser.add_argument('-port', help='The port of the application', default=5000) + parser.add_argument('-config_dir', help='The repository that will contains the configuration files', + default='./repositories') + return parser.parse_args() + + +if __name__ == "__main__": + args = initParser() + Anonymous_Github(github_token=args.token, host=args.host, port=args.port, config_dir=args.config_dir).run() diff --git a/templates/404.html b/templates/404.html new file mode 100644 index 0000000..9b535f5 --- /dev/null +++ b/templates/404.html @@ -0,0 +1,3 @@ + +404 +

404

\ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..11536f0 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,33 @@ + + + + + + + + + + + +
+

GihHub Anonymous

+
+
+ + + The github url to the repository that you want to anonymous. +
+
+ + + One term per line. +
+ +
+
+ + + + + + \ No newline at end of file diff --git a/templates/repo.html b/templates/repo.html new file mode 100644 index 0000000..a68cb06 --- /dev/null +++ b/templates/repo.html @@ -0,0 +1,33 @@ + + + + + + + + + + + + + +
+ + + {% for item in current_folder %} + + + + {% endfor %} + +
{{ item.name }}
+ {% if current_file %} + {{ current_file|file_render(terms) }} + {% endif %} +
+ + + + + + \ No newline at end of file