summaryrefslogtreecommitdiff
path: root/app.py
blob: e39ab24d7ecbf501fef0359d997ff1cf1a141780 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from flask import Flask, request
from os import environ
from termcolor import colored

from whois import whois

app = Flask(__name__)

env = 'PROD' if 'PROD' in environ else 'DEV'

TEMPLATE = '''
/---------------------------------------------\\
| {ip:44}|
|                                             |
| {route:44}|
| {origin:44}|
| {descr:44}|
|                                             |
|---------------------------------------------|
| Created by Yuval Adam                       |
| https://github.com/yuvadm/ipcli.app         |
| Comments welcome at hello@ipcli.app         |
\\---------------------------------------------/
'''

def get_ip(request):
    FOWARDED_FOR_HEADER = 'X-Forwarded-For'
    if FOWARDED_FOR_HEADER in request.headers:
        # Routing layers and proxies along the way might mask the original IP
        # So fetch it from the custom header
        # And make sure we just take the first (source) IP address
        ips = request.headers.get(FOWARDED_FOR_HEADER)
        return ips.split(',')[0]
    return request.remote_addr

@app.route('/')
def index():
    ip = get_ip(request)
    w = whois(ip)
    return TEMPLATE.format(ip=ip, route=w['route'], origin=w['origin'], descr=w['descr'])

@app.route('/p')
def plain():
    return get_ip(request)