-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
44 lines (36 loc) · 1.04 KB
/
server.py
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
#!/usr/bin/env python3
import logging
import socket
import requests
from flask import Flask, request
import furl
logging.basicConfig(level=logging.INFO)
app = Flask(__name__)
@app.route("/api/internal/secret")
def internal_secret_api():
if request.remote_addr != "127.0.0.1":
return "your ip " + request.remote_addr + " is not allowed"
else:
return "secret{4l0DyXxz8WBRQfIwZEQ}"
@app.route("/")
def index():
url = request.args.get('url')
if not url:
return "url parameter is required"
try:
hostname = furl.furl(url).host
if not hostname:
raise ValueError("empty hostname")
ip = socket.gethostbyname(hostname)
logging.info("resolve %s ip is %s", hostname, ip)
except Exception as e:
return str(e)
if ip == "127.0.0.1":
return "127.0.0.1 is forbidden"
try:
resp = requests.get(url, timeout=2)
return resp.text
except Exception as e:
return str(e)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000)