diff --git a/.gitignore b/.gitignore index a826747..152948e 100644 --- a/.gitignore +++ b/.gitignore @@ -62,3 +62,11 @@ target/ env/ .idea .vagrant + +# Angular 2 +frontend/typings +frontend/system-config.js* +frontend/app/**/*.js +frontend/app/**/*.map +frontend/node_modules +frontend/npm-debug.log diff --git a/Vagrantfile b/Vagrantfile index 99a724f..bdc1655 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -1,10 +1,21 @@ # -*- mode: ruby -*- # vi: set ft=ruby : +$script = < + + + + + + + + + Loading... + + diff --git a/frontend/package.json b/frontend/package.json new file mode 100644 index 0000000..7643be3 --- /dev/null +++ b/frontend/package.json @@ -0,0 +1,37 @@ +{ + "name": "angular-quickstart", + "version": "1.0.0", + "scripts": { + "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ", + "lite": "lite-server", + "postinstall": "typings install", + "tsc": "tsc", + "tsc:w": "tsc -w", + "typings": "typings" + }, + "license": "ISC", + "dependencies": { + "@angular/common": "~2.0.1", + "@angular/compiler": "~2.0.1", + "@angular/core": "~2.0.1", + "@angular/forms": "~2.0.1", + "@angular/http": "~2.0.1", + "@angular/platform-browser": "~2.0.1", + "@angular/platform-browser-dynamic": "~2.0.1", + "@angular/router": "~3.0.1", + "@angular/upgrade": "~2.0.1", + "angular-in-memory-web-api": "~0.1.1", + "bootstrap": "^3.3.7", + "core-js": "^2.4.1", + "reflect-metadata": "^0.1.8", + "rxjs": "5.0.0-beta.12", + "systemjs": "0.19.39", + "zone.js": "^0.6.25" + }, + "devDependencies": { + "concurrently": "^3.0.0", + "lite-server": "^2.2.2", + "typescript": "^2.0.3", + "typings":"^1.4.0" + } +} diff --git a/frontend/styles.css b/frontend/styles.css new file mode 100644 index 0000000..e69de29 diff --git a/frontend/systemjs.config.js b/frontend/systemjs.config.js new file mode 100644 index 0000000..a7295ff --- /dev/null +++ b/frontend/systemjs.config.js @@ -0,0 +1,43 @@ +/** + * System configuration for Angular samples + * Adjust as necessary for your application needs. + */ +(function (global) { + System.config({ + paths: { + // paths serve as alias + 'npm:': 'node_modules/' + }, + // map tells the System loader where to look for things + map: { + // our app is within the app folder + app: 'app', + // angular bundles + '@angular/core': 'npm:@angular/core/bundles/core.umd.js', + '@angular/common': 'npm:@angular/common/bundles/common.umd.js', + '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js', + '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js', + '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js', + '@angular/http': 'npm:@angular/http/bundles/http.umd.js', + '@angular/router': 'npm:@angular/router/bundles/router.umd.js', + '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js', + // other libraries + 'rxjs': 'npm:rxjs', + 'angular-in-memory-web-api': 'npm:angular-in-memory-web-api' + }, + // packages tells the System loader how to load when no filename and/or no extension + packages: { + app: { + main: './main.js', + defaultExtension: 'js' + }, + rxjs: { + defaultExtension: 'js' + }, + 'angular-in-memory-web-api': { + main: './index.js', + defaultExtension: 'js' + } + } + }); +})(this); diff --git a/frontend/tsconfig.json b/frontend/tsconfig.json new file mode 100644 index 0000000..25c53c9 --- /dev/null +++ b/frontend/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "target": "es5", + "module": "commonjs", + "moduleResolution": "node", + "sourceMap": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "removeComments": true, + "noImplicitAny": false + } +} diff --git a/frontend/typings.json b/frontend/typings.json new file mode 100644 index 0000000..7da31ca --- /dev/null +++ b/frontend/typings.json @@ -0,0 +1,7 @@ +{ + "globalDependencies": { + "core-js": "registry:dt/core-js#0.0.0+20160725163759", + "jasmine": "registry:dt/jasmine#2.2.0+20160621224255", + "node": "registry:dt/node#6.0.0+20160909174046" + } +} diff --git a/ops/Vagrant.sh b/ops/Vagrant.sh deleted file mode 100644 index f3b7e27..0000000 --- a/ops/Vagrant.sh +++ /dev/null @@ -1,48 +0,0 @@ -#!/bin/bash -export DEBIAN_FRONTEND=noninteractive - -echo 'Updating the aptitude repository...' -apt-get -y update > /dev/null - -packages=('python' 'python-pip' 'python-dev') - -for package in "${packages[@]}" -do - if dpkg --get-selections | grep -q "^$package[[:space:]]*install$" >/dev/null - then - echo "Skipping the installation of $package" - else - echo "Installing $package..." - apt-get install -y $package > /dev/null - fi -done - -echo 'Checking the python version...' -if [ $(python -V 2>&1 | grep -c "2.7") -eq 0 ] -then - >&2 echo 'Please ensure that python 2.7 is installed and is the default python version' - exit 1 -fi - -echo 'Creating the /opt/swagip/logs directory' -mkdir /opt/swagip/logs - -if ! [ -d '/opt/swagip/env' ] -then - echo 'Installing the virtual environment...' - pip install virtualenv > /dev/null - virtualenv /opt/swagip/env > /dev/null - source /opt/swagip/env/bin/activate -else - >&2 echo '/opt/swagip/env already exists. Please make sure it is removed before rerunning the script' - exit 1 -fi - -cd /opt/swagip/git -echo 'Installing the Python packages required in the virtualenv...' -pip install -r requirements.txt > /dev/null -deactivate - -unset DEBIAN_FRONTEND - -echo 'The installation has completed!' diff --git a/swagip.wsgi b/swagip.wsgi new file mode 100644 index 0000000..b3588a4 --- /dev/null +++ b/swagip.wsgi @@ -0,0 +1,9 @@ +#!/usr/bin/python +import sys, os +sys.path.insert (0,'/opt/swagip/git') +os.chdir('/opt/swagip/git') + +activate_this = '/opt/swagip/env/bin/activate_this.py' +execfile(activate_this, dict(__file__=activate_this)) + +from swagip import app as application diff --git a/swagip/static/js/custom.js b/swagip/static/js/custom.js deleted file mode 100644 index 4c58887..0000000 --- a/swagip/static/js/custom.js +++ /dev/null @@ -1,78 +0,0 @@ -//Supply the command minus the url (i.e. 'wget -qO - ') and supply 'this' -function setHeaderTable(command, element) { - - //Reset to defaults - resetHeaderTable(element); - - //Change the table header - $('#headerTable thead tr th.key').html('Command:'); - - //Change the rows - $('#headerTable > tbody > tr > td.key').each(function () { - - var rowKey = $(this).html(); - - if (rowKey.match(/json/i)) { - $(this).html(command + HOSTNAME + '/all'); - } - else if (rowKey.match(/source-ip/i)) { - $(this).html(command + HOSTNAME + '/'); - } - else { - $(this).html(command + HOSTNAME + '/' + rowKey.toLowerCase()); - } - }); -} - -// Supply 'this' -function resetHeaderTable(element) { - - //Make the clicked nav link active - $('#headerTableNav').find('li.active').removeClass('active'); - $(element).addClass('active'); - - //Change the table header - $('#headerTable thead tr th.key').html('Item:'); - - //Set the rows to default - $('#headerTable > tbody > tr > td.key').each(function () { - - if (($(this).html()).match(/(.*)[\/]$/i)) { - $(this).html('source-ip'); - } - else if (($(this).html()).match(/(.*)[\/]all$/i)) { - $(this).html('JSON'); - } - else { - $(this).html(($(this).html()).replace(/(.*)[\/]/i, '')); - } - }); -} - -$(document).ready(function () { - - $('#browser').on('click', function (e) { - resetHeaderTable(this); - return false; - }); - - $('#wget').on('click', function (e) { - setHeaderTable('wget -qO - ', this); - return false; - }); - - $('#curl').on('click', function (e) { - setHeaderTable('curl ', this); - return false; - }); - - $('#fetch').on('click', function (e) { - setHeaderTable('fetch -qo - ', this); - return false; - }); - - $('#powershell').on('click', function (e) { - setHeaderTable('Invoke-RestMethod http://', this); - return false; - }); -}); diff --git a/swagip/templates/base.html b/swagip/templates/base.html deleted file mode 100644 index c2b3ff1..0000000 --- a/swagip/templates/base.html +++ /dev/null @@ -1,22 +0,0 @@ - - - {% if title %}{{ title }}{% endif %} - - - - - - - - - - - - -
- {% block content %}{% endblock %} -
- - diff --git a/swagip/templates/index.html b/swagip/templates/index.html deleted file mode 100644 index 8027dc7..0000000 --- a/swagip/templates/index.html +++ /dev/null @@ -1,43 +0,0 @@ -{% extends "base.html" %} - {% block content %} -
-

SwagIP

-
-
-

Your Public IP is {% if 'Source-IP' in clientInfo %} {{ clientInfo['Source-IP'] }} {% endif %}

-
- -
- {% if clientInfo %} - -
- - - - - - - - - {% for key in clientInfo %} - - - - - {% endfor %} - - - - - -
Item:Result:
{{ key.lower() }}{{ clientInfo[key] }}
JSON{{ clientInfo | tojson(indent=4) | safe }}
-
- {% endif %} -
- {% endblock %} diff --git a/swagip/utils.py b/swagip/utils.py index 97031d4..5ccf104 100644 --- a/swagip/utils.py +++ b/swagip/utils.py @@ -7,20 +7,20 @@ from collections import OrderedDict -def getClientInfo(request): +def get_client_info(request): """ Function to get client information """ - clientInfo = {} - if request: - if request.access_route: - clientInfo['Source-IP'] = request.access_route[0].split(':')[0] - elif request.remote_addr: - clientInfo['Source-IP'] = request.remote_addr + client_info = {} + if request.access_route: + client_info['source-ip'] = request.access_route[0].split(':')[0] + elif request.remote_addr: + client_info['source-ip'] = request.remote_addr - if 'REMOTE_PORT' in request.environ: - clientInfo['Source-Port'] = request.environ['REMOTE_PORT'] + if 'REMOTE_PORT' in request.environ: + client_info['source-port'] = request.environ['REMOTE_PORT'] - clientInfo.update(dict(request.headers.to_list())) - clientInfo = OrderedDict(sorted(clientInfo.items())) + client_info.update(dict(request.headers.to_list())) + client_info = dict((key.lower(), client_info.get(key)) for key in client_info.keys()) + client_info = OrderedDict(sorted(client_info.items())) - return clientInfo + return client_info diff --git a/swagip/views.py b/swagip/views.py index d892ef3..ef67808 100644 --- a/swagip/views.py +++ b/swagip/views.py @@ -4,42 +4,51 @@ Purpose: routes for the app """ -from flask import render_template, request, jsonify -from swagip import app, HOSTNAME -from swagip.utils import getClientInfo +from flask import request, jsonify, send_from_directory +import os +from swagip import app +from swagip.utils import get_client_info +BASE_PATH = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) +FRONTEND_FOLDER = os.path.join(BASE_PATH, 'frontend') -@app.route('/', methods=['GET']) -def index(): - """ Function to return index page + +@app.route('/api/') +def get_headers(header_name): + """ Function to return individual headers """ - clientInfo = getClientInfo(request) - if 'User-Agent' in clientInfo: - userAgent = clientInfo['User-Agent'] + client_info = get_client_info(request) - if "Wget" in userAgent or "fetch" in userAgent or "curl" in userAgent or "WindowsPowerShell" in userAgent: - if 'Source-IP' in clientInfo: - return clientInfo['Source-IP'] - else: - return '' + if str(header_name).lower() == 'all': + return jsonify(client_info) - return render_template('index.html', title='SwagIP', hostname=HOSTNAME, clientInfo=clientInfo) + if str(header_name).lower() in client_info: + return str(client_info[(header_name.lower())]) + return '' -@app.route('/') -def show_post(header_name): - """ Function to return individual headers - """ - clientInfo = getClientInfo(request) +@app.route('/') +def frontend_index(): + client_info = get_client_info(request) - if str(header_name).lower() == 'source-port': - if 'Source-Port' in clientInfo: - return str(clientInfo['Source-Port']) - elif str(header_name).lower() == 'all': - return jsonify(clientInfo) - elif str(header_name).title() in clientInfo: - return str(clientInfo[(header_name.title())]) + for browser in ['Wget', 'fetch', 'curl', 'WindowsPowerShell']: + if browser in client_info.get('user-agent'): + return client_info.get('source-ip') - return '' + return send_from_directory(FRONTEND_FOLDER, 'index.html') + + +@app.route('/browser') +@app.route('/curl') +@app.route('/wget') +@app.route('/fetch') +@app.route('/powershell') +def frontend_routes(): + return send_from_directory(FRONTEND_FOLDER, 'index.html') + + +@app.route('/') +def frontend_static_files(filename): + return send_from_directory(FRONTEND_FOLDER, filename)