Starting inserting protocol and ip interface in services
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from base64 import b64decode
|
||||
import sqlite3, uvicorn, sys, secrets, re, os, asyncio
|
||||
import httpx, urllib, websockets
|
||||
import sqlite3, uvicorn, sys, secrets, re
|
||||
import httpx, websockets, os, asyncio
|
||||
from typing import List, Union
|
||||
from fastapi import FastAPI, HTTPException, WebSocket, Depends
|
||||
from pydantic import BaseModel, BaseSettings
|
||||
@@ -333,6 +333,8 @@ class ServiceAddForm(BaseModel):
|
||||
name: str
|
||||
port: int
|
||||
ipv6: bool
|
||||
proto: str
|
||||
ip_int: str
|
||||
|
||||
class ServiceAddResponse(BaseModel):
|
||||
status:str
|
||||
@@ -341,22 +343,28 @@ class ServiceAddResponse(BaseModel):
|
||||
@app.post('/api/services/add', response_model=ServiceAddResponse)
|
||||
async def add_new_service(form: ServiceAddForm, auth: bool = Depends(is_loggined)):
|
||||
"""Add a new service"""
|
||||
import time
|
||||
if form.ipv6:
|
||||
if not checkIpv6(form.ip_int):
|
||||
return {"status":"Invalid IPv6 address"}
|
||||
else:
|
||||
if not checkIpv4(form.ip_int):
|
||||
return {"status":"Invalid IPv4 address"}
|
||||
if form.proto not in ["tcp", "udp"]:
|
||||
return {"status":"Invalid protocol"}
|
||||
srv_id = None
|
||||
try:
|
||||
srv_id = str(form.port)+"::"+("ipv6" if form.ipv6 else "ipv4")
|
||||
srv_id = gen_service_id(db)
|
||||
db.query("INSERT INTO services (service_id ,name, port, ipv6, status) VALUES (?, ?, ?, ?, ?)",
|
||||
srv_id, refactor_name(form.name), form.port, form.ipv6, STATUS.STOP)
|
||||
except sqlite3.IntegrityError:
|
||||
return {'status': 'Name or/and ports of the service has been already assigned'}
|
||||
await firewall.reload()
|
||||
init_t = time.time()
|
||||
await refresh_frontend()
|
||||
return {'status': 'ok', 'service_id': srv_id}
|
||||
|
||||
async def frontend_debug_proxy(path):
|
||||
httpc = httpx.AsyncClient()
|
||||
req = httpc.build_request("GET",urllib.parse.urljoin(f"http://127.0.0.1:{os.getenv('F_PORT','3000')}", path))
|
||||
req = httpc.build_request("GET",f"http://127.0.0.1:{os.getenv('F_PORT','3000')}/"+path)
|
||||
resp = await httpc.send(req, stream=True)
|
||||
return StreamingResponse(resp.aiter_bytes(),status_code=resp.status_code)
|
||||
|
||||
|
||||
@@ -114,13 +114,14 @@ class Interceptor:
|
||||
def _start_queue(self,func,n_threads):
|
||||
def func_wrap(ll_data, ll_proto_id, data, ctx, *args):
|
||||
pkt_parsed = ip6.IP6(data) if self.ipv6 else ip.IP(data)
|
||||
|
||||
try:
|
||||
level4 = None
|
||||
if self.proto == ProtoTypes.TCP: level4 = pkt_parsed[tcp.TCP].body_bytes
|
||||
elif self.proto == ProtoTypes.UDP: level4 = pkt_parsed[udp.UDP].body_bytes
|
||||
if level4:
|
||||
if func(level4):
|
||||
return pkt_parsed.bin(), interceptor.NF_ACCEPT
|
||||
return data, interceptor.NF_ACCEPT
|
||||
elif self.proto == ProtoTypes.TCP:
|
||||
pkt_parsed[tcp.TCP].flags &= 0x00
|
||||
pkt_parsed[tcp.TCP].flags |= tcp.TH_FIN | tcp.TH_ACK
|
||||
|
||||
@@ -1,11 +1,21 @@
|
||||
import traceback
|
||||
from typing import Dict
|
||||
from proxy import Filter, Proxy
|
||||
import os, sqlite3, socket, asyncio
|
||||
import os, sqlite3, socket, asyncio, re
|
||||
import secrets
|
||||
from base64 import b64decode
|
||||
|
||||
LOCALHOST_IP = socket.gethostbyname(os.getenv("LOCALHOST_IP","127.0.0.1"))
|
||||
|
||||
regex_ipv6 = r"^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$";
|
||||
regex_ipv4 = r"^([0-9]{1,3}\\.){3}[0-9]{1,3}(\\/([0-9]|[1-2][0-9]|3[0-2]))?$"
|
||||
|
||||
def checkIpv6(ip:str):
|
||||
return bool(re.match(regex_ipv6, ip))
|
||||
|
||||
def checkIpv4(ip:str):
|
||||
return bool(re.match(regex_ipv4, ip))
|
||||
|
||||
class SQLite():
|
||||
def __init__(self, db_name) -> None:
|
||||
self.conn = None
|
||||
@@ -239,4 +249,11 @@ class ProxyManager:
|
||||
def refactor_name(name:str):
|
||||
name = name.strip()
|
||||
while " " in name: name = name.replace(" "," ")
|
||||
return name
|
||||
return name
|
||||
|
||||
def gen_service_id(db):
|
||||
while True:
|
||||
res = secrets.token_hex(8)
|
||||
if len(db.query('SELECT 1 FROM services WHERE service_id = ?;', res)) == 0:
|
||||
break
|
||||
return res
|
||||
Reference in New Issue
Block a user