add gatewayfinder and root api endpoint
This commit is contained in:
parent
8663c98410
commit
e162948cb8
@ -5,6 +5,7 @@ env > .env
|
|||||||
if [ "$API" = true ]; then
|
if [ "$API" = true ]; then
|
||||||
python3.12 api_endpoints.py &
|
python3.12 api_endpoints.py &
|
||||||
sleep 1
|
sleep 1
|
||||||
|
python3.12 find_gateways.py &
|
||||||
fi
|
fi
|
||||||
|
|
||||||
python3.12 main.py
|
python3.12 main.py
|
||||||
@ -4,3 +4,4 @@ bs4
|
|||||||
requests
|
requests
|
||||||
flask
|
flask
|
||||||
flask_cors
|
flask_cors
|
||||||
|
FindMyIP
|
||||||
@ -17,6 +17,18 @@ class API:
|
|||||||
self.app.config['CORS_HEADERS'] = 'Content-Type'
|
self.app.config['CORS_HEADERS'] = 'Content-Type'
|
||||||
|
|
||||||
# --------Static Routes-------
|
# --------Static Routes-------
|
||||||
|
@self.app.route('/')
|
||||||
|
@cross_origin()
|
||||||
|
def serve_root():
|
||||||
|
root_dict = {
|
||||||
|
"version": "24-07-03",
|
||||||
|
"mode": 1,
|
||||||
|
"info": {
|
||||||
|
"files_size_sum": self.get_file_size()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return jsonify(root_dict)
|
||||||
|
|
||||||
@self.app.route('/charts')
|
@self.app.route('/charts')
|
||||||
@cross_origin()
|
@cross_origin()
|
||||||
def serve_index():
|
def serve_index():
|
||||||
@ -31,9 +43,19 @@ class API:
|
|||||||
@self.app.route('/json/<path:path>')
|
@self.app.route('/json/<path:path>')
|
||||||
@cross_origin()
|
@cross_origin()
|
||||||
def serve_json(path):
|
def serve_json(path):
|
||||||
workdir, filename = os.path.split(os.path.abspath(__file__))
|
|
||||||
return send_from_directory(f'{workdir}/data', path)
|
return send_from_directory(f'{workdir}/data', path)
|
||||||
|
|
||||||
|
def get_file_size(self):
|
||||||
|
workdir, filename = os.path.split(os.path.abspath(__file__))
|
||||||
|
files = os.listdir(f'{workdir}/data')
|
||||||
|
sizes = 0.0
|
||||||
|
for file in files:
|
||||||
|
if file.endswith('.json'):
|
||||||
|
sizes += round(int(os.path.getsize(f'{workdir}/data/{file}')), 2)
|
||||||
|
|
||||||
|
return sizes
|
||||||
|
|
||||||
|
|
||||||
api = API()
|
api = API()
|
||||||
api.app.run(host='0.0.0.0', port=8000)
|
api.app.run(host='0.0.0.0', port=8000)
|
||||||
|
|||||||
55
python/src/find_gateways.py
Normal file
55
python/src/find_gateways.py
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
from time import sleep
|
||||||
|
import socket
|
||||||
|
import FindMyIP
|
||||||
|
import ipaddress
|
||||||
|
import threading
|
||||||
|
import requests
|
||||||
|
|
||||||
|
max_threads = 50
|
||||||
|
final = {}
|
||||||
|
|
||||||
|
|
||||||
|
def check_port(ip, port):
|
||||||
|
try:
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # TCP
|
||||||
|
# sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
|
||||||
|
socket.setdefaulttimeout(2.0) # seconds (float)
|
||||||
|
result = sock.connect_ex((ip, port))
|
||||||
|
if result == 0:
|
||||||
|
# print("Port is open")
|
||||||
|
final[ip] = "OPEN"
|
||||||
|
else:
|
||||||
|
# print("Port is closed/filtered")
|
||||||
|
final[ip] = "CLOSED"
|
||||||
|
sock.close()
|
||||||
|
except:
|
||||||
|
final[ip] = "EXCEPTION"
|
||||||
|
|
||||||
|
|
||||||
|
port = 8000
|
||||||
|
local_ip = FindMyIP.internal()
|
||||||
|
local_ip = local_ip.split('.')[:-1]
|
||||||
|
local_ip.append("0")
|
||||||
|
local_ip = '.'.join(local_ip)
|
||||||
|
|
||||||
|
print(f"Scan on {local_ip}/24 for port {port}")
|
||||||
|
|
||||||
|
for ip in ipaddress.IPv4Network(f'{local_ip}/24'):
|
||||||
|
threading.Thread(target=check_port, args=[str(ip), port]).start()
|
||||||
|
# sleep(0.1)
|
||||||
|
|
||||||
|
# limit the number of threads.
|
||||||
|
while threading.active_count() > max_threads:
|
||||||
|
sleep(1)
|
||||||
|
|
||||||
|
sorted_ips = dict(sorted(final.items(), key=lambda item: tuple(map(int, item[0].split('.')))))
|
||||||
|
|
||||||
|
for ip, state in sorted_ips.items():
|
||||||
|
if state == "OPEN":
|
||||||
|
try:
|
||||||
|
response = requests.get(f'http://{ip}:{port}/')
|
||||||
|
if response.status_code == 200:
|
||||||
|
print(ip, state)
|
||||||
|
print(response.text)
|
||||||
|
except:pass
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user