proxymode db
move settings to db and rework code to support
This commit is contained in:
parent
bdc452b99c
commit
904167a8c5
4 changed files with 100 additions and 28 deletions
|
|
@ -171,6 +171,8 @@ xmpp_listen_port = 5223
|
|||
|
||||
|
||||
async def start():
|
||||
#config_proxyMode_deleteTable() #delete existing proxymode table
|
||||
|
||||
#Reset xmpp/mqtt to false in database for bots and clients
|
||||
bot_reset_connectionStatus()
|
||||
client_reset_connectionStatus()
|
||||
|
|
@ -237,6 +239,17 @@ async def start():
|
|||
|
||||
# Start web servers
|
||||
if bumper_proxy_mode:
|
||||
bumperlog.info("Proxy Mode Enabled")
|
||||
if config_proxyMode_countEntries() == 0: # check if proxymode servers are entered
|
||||
bumperlog.info("Proxy Mode - No Servers, Loading Defaults (US)")
|
||||
config_proxyMode_defaults() # set defaults if 0
|
||||
|
||||
configproxy = config_proxyMode_getall()
|
||||
cntentries = len(configproxy)
|
||||
proxymodelog.info(f"Loaded {cntentries} entries from proxyconfig")
|
||||
for entry in configproxy:
|
||||
proxymodelog.info(f"Config Entry {entry}")
|
||||
|
||||
conf_server.confserver_proxy_app()
|
||||
else:
|
||||
conf_server.confserver_app()
|
||||
|
|
|
|||
|
|
@ -78,15 +78,6 @@ class ConfServer:
|
|||
try:
|
||||
ecoresp = ""
|
||||
|
||||
# Ecovacs IP for proxy mode testing
|
||||
ecouser_net_ip = "47.88.66.164"
|
||||
ecovacs_com_ip = "47.252.51.29"
|
||||
eco_us_api = "47.89.135.130"
|
||||
mq_na_ip = "47.254.52.46"
|
||||
recommender_ip = "47.111.101.11"
|
||||
bigdata_international_ip = "47.88.132.151"
|
||||
bigdata_northamerica_ip = "47.88.66.111"
|
||||
|
||||
server_port = 443 #default to 443
|
||||
if "_SSLProtocolTransport" != type(request.transport).__name__ and "_SelectorSocketTransport" != type(request.transport).__name__: #check not ssl transport class
|
||||
if "_extra" in request.transport:
|
||||
|
|
@ -97,26 +88,31 @@ class ConfServer:
|
|||
return await self.handle_base(request)
|
||||
if request.raw_path == "/lookup.do":
|
||||
return await self.handle_lookup(request) #use bumper to handle lookup so bot gets Bumper IP and not Ecovacs
|
||||
elif "ecovacs.com" in request.host:
|
||||
ecorequest = f"{request.scheme}://{ecovacs_com_ip}"
|
||||
elif "ecouser.net" in request.host:
|
||||
ecorequest = f"{request.scheme}://{ecouser_net_ip}"
|
||||
elif "eco-us-api" in request.host:
|
||||
ecorequest = f"{request.scheme}://{eco_us_api}"
|
||||
elif "mq-" in request.host:
|
||||
ecorequest = f"{request.scheme}://{mq_na_ip}"
|
||||
elif "recommender" in request.host:
|
||||
ecorequest = f"{request.scheme}://{recommender_ip}"
|
||||
elif "bigdata-international" in request.host:
|
||||
ecorequest = f"{request.scheme}://{bigdata_international_ip}"
|
||||
elif "bigdata-northamerica" in request.host:
|
||||
ecorequest = f"{request.scheme}://{bigdata_northamerica_ip}"
|
||||
|
||||
matchproxy = bumper.config_proxyMode_getServerIP("app", request.host)
|
||||
|
||||
if matchproxy:
|
||||
proxymodelog.info(f"Matched {request.host} to entry in proxyconfig!")
|
||||
ecorequest = f"{request.scheme}://{matchproxy}"
|
||||
else:
|
||||
ecorequest = f"{request.scheme}://{request.host}"
|
||||
proxymodelog.info(f"No match for {request.host} in proxyconfig!")
|
||||
if "ecovacs.com" in request.host:
|
||||
proxymodelog.info(f"ecovacs.com in {request.host} defaulting to ecovacs.com IP!")
|
||||
matchproxy = bumper.config_proxyMode_getServerIP("app", "ecovacs.com")
|
||||
ecorequest = f"{request.scheme}://{matchproxy}"
|
||||
elif "ecouser.net" in request.host:
|
||||
proxymodelog.info(f"ecouser.net in {request.host} defaulting to ecouser.net IP!")
|
||||
matchproxy = bumper.config_proxyMode_getServerIP("app", "ecouser.net")
|
||||
ecorequest = f"{request.scheme}://{matchproxy}"
|
||||
else:
|
||||
proxymodelog.info(f"No matches for {request.host} defaulting to ecovacs.com IP!")
|
||||
matchproxy = bumper.config_proxyMode_getServerIP("app", "ecovacs.com")
|
||||
ecorequest = f"{request.scheme}://{matchproxy}"
|
||||
|
||||
if server_port != 443:
|
||||
ecorequest = f"{ecorequest}:{server_port}"
|
||||
|
||||
proxymodelog.info(f"{request.host} - {ecorequest}")
|
||||
ecorequest = f"{ecorequest}{request.path_qs}"
|
||||
requestheaders = {'host': request.host}
|
||||
async with aiohttp.ClientSession(headers=requestheaders, connector=aiohttp.TCPConnector(verify_ssl=False)) as session:
|
||||
|
|
@ -126,14 +122,14 @@ class ConfServer:
|
|||
jdata = json.loads(jdata)
|
||||
async with session.request(request.method, ecorequest, json=jdata) as resp:
|
||||
ecoresp = await resp.text()
|
||||
ecoresp = ecoresp.replace("portal-ww.ecouser.net", ecouser_net_ip)
|
||||
#ecoresp = ecoresp.replace("portal-ww.ecouser.net", ecouser_net_ip)
|
||||
proxymodelog.info(f"HTTP Proxy Response from EcoVacs (URL: {ecorequest}) - (Status: {resp.status}) - {ecoresp}")
|
||||
|
||||
else:
|
||||
proxymodelog.info(f"HTTP Proxy Request to EcoVacs (body=false) (host:{request.host}) - {ecorequest}")
|
||||
async with session.request(request.method, ecorequest) as resp:
|
||||
ecoresp = await asyncio.shield(resp.text())
|
||||
ecoresp = ecoresp.replace("portal-ww.ecouser.net", ecouser_net_ip)
|
||||
#ecoresp = ecoresp.replace("portal-ww.ecouser.net", ecouser_net_ip)
|
||||
proxymodelog.info(f"HTTP Proxy Response from EcoVacs (URL: {ecorequest}) - (Status: {resp.status}) - {ecoresp}")
|
||||
|
||||
if resp.status == 200:
|
||||
|
|
|
|||
56
bumper/db.py
56
bumper/db.py
|
|
@ -32,9 +32,65 @@ def db_get():
|
|||
db.table("clients", cache_size=0)
|
||||
db.table("bots", cache_size=0)
|
||||
db.table("tokens", cache_size=0)
|
||||
db.table("config_proxymode", cache_size=0)
|
||||
|
||||
return db
|
||||
|
||||
def config_proxyMode_deleteTable():
|
||||
opendb = db_get()
|
||||
opendb.purge_table("config_proxymode")
|
||||
|
||||
def config_proxyMode_defaults():
|
||||
defaults = [
|
||||
{"type":"app","host":"gl-us-api.ecovacs.com","ip":"47.252.51.29","match":"gl-us-"},
|
||||
{"type":"app","host":"gl-us-openapi.ecovacs.com","ip":"47.252.51.29"},
|
||||
{"type":"app","host":"portal-ww.ecouser.net","ip":"47.88.66.164","match":"portal-"},
|
||||
{"type":"app","host":"bigdata-northamerica.ecovacs.com","ip":"47.88.66.111"},
|
||||
{"type":"app","host":"bigdata-international.ecovacs.com","ip":"47.88.132.151"},
|
||||
{"type":"app","host":"eco-us-api.ecovacs.com","ip":"47.89.135.130","match":"eco-us-"},
|
||||
{"type":"app","host":"ecovacs.com","ip":"47.90.210.46"},
|
||||
{"type":"app","host":"ecouser.net","ip":"116.62.93.217"},
|
||||
{"type":"mqtt_server","host":"mq-ww.ecouser.net","ip":"47.254.52.46"},
|
||||
]
|
||||
opendb = db_get()
|
||||
with opendb:
|
||||
config = opendb.table("config_proxymode")
|
||||
config.insert_multiple(defaults)
|
||||
|
||||
def config_proxyMode_getServerIP(type, host):
|
||||
opendb = db_get()
|
||||
with opendb:
|
||||
proxyconfig = opendb.table("config_proxymode")
|
||||
proxy = Query()
|
||||
if type == "mqtt_server":
|
||||
entry = proxyconfig.get((proxy.type == type))
|
||||
|
||||
else:
|
||||
entry = proxyconfig.get((proxy.type == type) & (proxy.host == host))
|
||||
|
||||
if entry:
|
||||
return entry["ip"]
|
||||
|
||||
else:
|
||||
proxylist = proxyconfig.search(Query())
|
||||
for proxy in proxylist: # check for sub matches
|
||||
if "match" in proxy:
|
||||
if proxy["match"] in host:
|
||||
return proxy["ip"]
|
||||
return None
|
||||
|
||||
def config_proxyMode_countEntries():
|
||||
opendb = db_get()
|
||||
with opendb:
|
||||
config = opendb.table("config_proxymode")
|
||||
return len(config)
|
||||
|
||||
def config_proxyMode_getall():
|
||||
opendb = db_get()
|
||||
with opendb:
|
||||
config = opendb.table("config_proxymode")
|
||||
return config.search(Query())
|
||||
|
||||
|
||||
def user_add(userid):
|
||||
newuser = BumperUser()
|
||||
|
|
|
|||
|
|
@ -377,7 +377,14 @@ class BumperMQTTServer_Plugin:
|
|||
)
|
||||
mqttserverlog.info(f"Bumper Authentication Success - Bot - SN: {username} - DID: {didsplit[0]} - Class: {tmpbotdetail[0]}")
|
||||
authenticated = True
|
||||
mq_na_ip = "47.254.52.46"
|
||||
mqtt_server = bumper.config_proxyMode_getServerIP("mqtt_server","")
|
||||
if mqtt_server:
|
||||
proxymodelog.info(f"MQTT Proxy Mode - Using server {mqtt_server}")
|
||||
else:
|
||||
proxymodelog.error(f"MQTT Proxy Mode - No server found! Load defaults or set mqtt_server in config_proxymode table!")
|
||||
proxymodelog.exception(f"MQTT Proxy Mode - Exiting due to no MQTT Server configured!")
|
||||
exit(1)
|
||||
|
||||
if authenticated and bumper.bumper_proxy_mode:
|
||||
proxymodelog.info(f"MQTT Proxy Mode - Proxy Bot to MQTT - Client_id: {client_id} - Username: {username}")
|
||||
|
||||
|
|
@ -387,7 +394,7 @@ class BumperMQTTServer_Plugin:
|
|||
|
||||
try:
|
||||
await self.proxyclients[client_id].connect(
|
||||
f"mqtts://{username}:{password}@{mq_na_ip}:8883",
|
||||
f"mqtts://{username}:{password}@{mqtt_server}:8883",
|
||||
)
|
||||
except Exception as e:
|
||||
mqttserverlog.error(f"MQTT Proxy Mode - Exception connecting with proxy to ecovacs - {e}")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue