Update util.py
parent
41dbb749a2
commit
0f50dd8bcc
33
util.py
33
util.py
|
@ -25,19 +25,25 @@ def get_ip_address():
|
||||||
try:
|
try:
|
||||||
gethostname = socket.gethostname()
|
gethostname = socket.gethostname()
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
print(exc)
|
print("gethostname", exc)
|
||||||
gethostname = None
|
gethostname = None
|
||||||
|
|
||||||
default_ip = "127.0.0.1"
|
default_ip = "127.0.0.1"
|
||||||
ip = default_ip
|
ip = default_ip
|
||||||
if not gethostname is None:
|
|
||||||
|
check_public_ip = True
|
||||||
|
if "macos" in platform.platform().lower():
|
||||||
|
if "arm64" in platform.platform().lower():
|
||||||
|
check_public_ip = False
|
||||||
|
|
||||||
|
if check_public_ip and not gethostname is None:
|
||||||
try:
|
try:
|
||||||
ip = [l for l in ([ip for ip in socket.gethostbyname_ex(gethostname)[2]
|
ip = [l for l in ([ip for ip in socket.gethostbyname_ex(gethostname)[2]
|
||||||
if not ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 53)),
|
if not ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 53)),
|
||||||
s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET,
|
s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET,
|
||||||
socket.SOCK_DGRAM)]][0][1]]) if l][0][0]
|
socket.SOCK_DGRAM)]][0][1]]) if l][0][0]
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
print(exc)
|
print("gethostbyname_ex", exc)
|
||||||
ip = gethostname
|
ip = gethostname
|
||||||
|
|
||||||
#print("get_ip_address:", ip)
|
#print("get_ip_address:", ip)
|
||||||
|
@ -162,8 +168,11 @@ def is_text_match_keyword(keyword_string, text):
|
||||||
|
|
||||||
def save_json(config_dict, target_path):
|
def save_json(config_dict, target_path):
|
||||||
json_str = json.dumps(config_dict, indent=4)
|
json_str = json.dumps(config_dict, indent=4)
|
||||||
|
try:
|
||||||
with open(target_path, 'w') as outfile:
|
with open(target_path, 'w') as outfile:
|
||||||
outfile.write(json_str)
|
outfile.write(json_str)
|
||||||
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
|
||||||
def write_string_to_file(filename, data):
|
def write_string_to_file(filename, data):
|
||||||
outfile = None
|
outfile = None
|
||||||
|
@ -448,8 +457,12 @@ def dump_settings_to_maxbot_plus_extension(ext, config_dict, CONST_MAXBOT_CONFIG
|
||||||
os.unlink(target_path)
|
os.unlink(target_path)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
with open(target_path, 'w') as outfile:
|
with open(target_path, 'w') as outfile:
|
||||||
json.dump(config_dict, outfile)
|
json.dump(config_dict, outfile)
|
||||||
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
|
||||||
# add host_permissions
|
# add host_permissions
|
||||||
target_path = ext
|
target_path = ext
|
||||||
|
@ -457,8 +470,11 @@ def dump_settings_to_maxbot_plus_extension(ext, config_dict, CONST_MAXBOT_CONFIG
|
||||||
|
|
||||||
manifest_dict = None
|
manifest_dict = None
|
||||||
if os.path.isfile(target_path):
|
if os.path.isfile(target_path):
|
||||||
|
try:
|
||||||
with open(target_path) as json_data:
|
with open(target_path) as json_data:
|
||||||
manifest_dict = json.load(json_data)
|
manifest_dict = json.load(json_data)
|
||||||
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
|
||||||
local_remote_url_array = []
|
local_remote_url_array = []
|
||||||
local_remote_url = config_dict["advanced"]["remote_url"]
|
local_remote_url = config_dict["advanced"]["remote_url"]
|
||||||
|
@ -482,8 +498,11 @@ def dump_settings_to_maxbot_plus_extension(ext, config_dict, CONST_MAXBOT_CONFIG
|
||||||
|
|
||||||
if is_manifest_changed:
|
if is_manifest_changed:
|
||||||
json_str = json.dumps(manifest_dict, indent=4)
|
json_str = json.dumps(manifest_dict, indent=4)
|
||||||
|
try:
|
||||||
with open(target_path, 'w') as outfile:
|
with open(target_path, 'w') as outfile:
|
||||||
outfile.write(json_str)
|
outfile.write(json_str)
|
||||||
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def dump_settings_to_maxblock_plus_extension(ext, config_dict, CONST_MAXBOT_CONFIG_FILE, CONST_MAXBLOCK_EXTENSION_FILTER):
|
def dump_settings_to_maxblock_plus_extension(ext, config_dict, CONST_MAXBOT_CONFIG_FILE, CONST_MAXBLOCK_EXTENSION_FILTER):
|
||||||
|
@ -501,9 +520,13 @@ def dump_settings_to_maxblock_plus_extension(ext, config_dict, CONST_MAXBOT_CONF
|
||||||
os.unlink(target_path)
|
os.unlink(target_path)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
with open(target_path, 'w') as outfile:
|
with open(target_path, 'w') as outfile:
|
||||||
config_dict["domain_filter"]=CONST_MAXBLOCK_EXTENSION_FILTER
|
config_dict["domain_filter"]=CONST_MAXBLOCK_EXTENSION_FILTER
|
||||||
json.dump(config_dict, outfile)
|
json.dump(config_dict, outfile)
|
||||||
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
|
||||||
# convert web string to reg pattern
|
# convert web string to reg pattern
|
||||||
def convert_string_to_pattern(my_str, dynamic_length=True):
|
def convert_string_to_pattern(my_str, dynamic_length=True):
|
||||||
|
@ -1375,8 +1398,12 @@ def get_answer_list_from_user_guess_string(config_dict, CONST_MAXBOT_ANSWER_ONLI
|
||||||
# load from internet.
|
# load from internet.
|
||||||
user_guess_string = ""
|
user_guess_string = ""
|
||||||
if os.path.exists(CONST_MAXBOT_ANSWER_ONLINE_FILE):
|
if os.path.exists(CONST_MAXBOT_ANSWER_ONLINE_FILE):
|
||||||
|
try:
|
||||||
with open(CONST_MAXBOT_ANSWER_ONLINE_FILE, "r") as text_file:
|
with open(CONST_MAXBOT_ANSWER_ONLINE_FILE, "r") as text_file:
|
||||||
user_guess_string = text_file.readline()
|
user_guess_string = text_file.readline()
|
||||||
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
|
||||||
if len(user_guess_string) > 0:
|
if len(user_guess_string) > 0:
|
||||||
user_guess_string = format_config_keyword_for_json(user_guess_string)
|
user_guess_string = format_config_keyword_for_json(user_guess_string)
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue