Permalink
Please
sign in to comment.
Showing
with
312 additions
and 0 deletions.
- +109 −0 .gitignore
- 0 README.md
- 0 __init__.py
- +3 −0 requirements.txt
- +131 −0 server.py
- +3 −0 templates/404.html
- +33 −0 templates/index.html
- +33 −0 templates/repo.html
@@ -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 |
No changes.
No changes.
@@ -0,0 +1,3 @@ | ||
requests | ||
flask | ||
pygithub |
@@ -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("<img src='%s' alt='%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("<pre>" + file.decoded_content + "</pre>"), terms) | ||
return Markup("<a href='%s'>Download %s</a>" % (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/<id>', methods=['GET'], defaults={'path': ''}) | ||
@application.route('/repository/<id>/', methods=['GET'], defaults={'path': ''}) | ||
@application.route('/repository/<id>/<path:path>', 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() |
@@ -0,0 +1,3 @@ | ||
<!doctype html> | ||
<title>404</title> | ||
<h1>404</h1> |
@@ -0,0 +1,33 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<!-- Required meta tags --> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
|
||
<!-- Bootstrap CSS --> | ||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous"> | ||
</head> | ||
<body> | ||
<div class="container-fluid"> | ||
<h1>GihHub Anonymous</h1> | ||
<form action="" method="post"> | ||
<div class="form-group"> | ||
<label for="githubRepository">GitHub Repository</label> | ||
<input type="url" class="form-control" name="githubRepository" id="githubRepository" aria-describedby="githubRepository" placeholder="GitHub Repository"> | ||
<small id="githubRepositoryHelp" class="form-text text-muted">The github url to the repository that you want to anonymous.</small> | ||
</div> | ||
<div class="form-group"> | ||
<label for="ignoredTerms">The text to remove from the repository</label> | ||
<textarea class="form-control" name="terms" id="ignoredTerms" rows="5"></textarea> | ||
<small id="ignoredTermsHelp" class="form-text text-muted">One term per line.</small> | ||
</div> | ||
<button type="submit" class="btn btn-primary">Submit</button> | ||
</form> | ||
</div> | ||
|
||
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script> | ||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script> | ||
</body> | ||
</html> |
@@ -0,0 +1,33 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<!-- Required meta tags --> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
|
||
<!-- Bootstrap CSS --> | ||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous"> | ||
|
||
<link href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/2.8.0/github-markdown.min.css" rel="stylesheet" type="text/css" /> | ||
</head> | ||
<body> | ||
<div class="container-fluid"> | ||
<table class="table"> | ||
<tbody> | ||
{% for item in current_folder %} | ||
<tr> | ||
<td><a href="/repository/{{ current_repository }}/{{ item.path }}">{{ item.name }}</a> </td> | ||
</tr> | ||
{% endfor %} | ||
</tbody> | ||
</table> | ||
{% if current_file %} | ||
{{ current_file|file_render(terms) }} | ||
{% endif %} | ||
</div> | ||
|
||
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script> | ||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script> | ||
</body> | ||
</html> |
0 comments on commit
dd82a5a