Skip to content

Commit

Permalink
端口自适应
Browse files Browse the repository at this point in the history
  • Loading branch information
lunzhiPenxil committed Dec 8, 2021
1 parent 47582cb commit 462b665
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
42 changes: 42 additions & 0 deletions OlivOS/accountAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
'''

import json
import socket
from contextlib import closing

import OlivOS

Expand Down Expand Up @@ -74,3 +76,43 @@ def save(path, logger_proc, Account_data, safe_mode = False):
tmp_total_account_data['account'].append(tmp_this_account_data)
with open(path, 'w', encoding = 'utf-8') as account_conf_f:
account_conf_f.write(json.dumps(tmp_total_account_data, indent = 4))

def accountFix(basic_conf_models, bot_info_dict, logger_proc):
res = {}
for basic_conf_models_this in basic_conf_models:
if basic_conf_models[basic_conf_models_this]['type'] == 'post':
if basic_conf_models[basic_conf_models_this]['server']['auto'] == True:
basic_conf_models[basic_conf_models_this]['server']['host'] = '0.0.0.0'
if isInuse(
'127.0.0.1',
basic_conf_models[basic_conf_models_this]['server']['port']
):
basic_conf_models[basic_conf_models_this]['server']['port'] = get_free_port()
for bot_info_dict_this in bot_info_dict:
Account_data_this = bot_info_dict[bot_info_dict_this]
if Account_data_this.platform['model'] == 'gocqhttp_show':
if Account_data_this.post_info.auto == True:
Account_data_this.post_info.type = 'post'
Account_data_this.post_info.host = 'http://127.0.0.1'
Account_data_this.post_info.port = get_free_port()
Account_data_this.post_info.access_token = bot_info_dict_this
res[bot_info_dict_this] = Account_data_this
return res

def isInuse(ip, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
flag = True
try:
s.connect((ip, port))
s.shutdown(2)
flag = True
except:
flag = False
return flag


def get_free_port():
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(('', 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
return s.getsockname()[1]
2 changes: 1 addition & 1 deletion OlivOS/libEXEModelAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def run(self):
if self.Proc_data['bot_info_dict'].platform['model'] == 'gocqhttp' or self.Proc_data['bot_info_dict'].platform['model'] == 'gocqhttp_show':
self.log(2, 'OlivOS libEXEModel server [' + self.Proc_name + '] will run under visiable mode')
subprocess.call(
"start cmd /K ..\\..\\..\\lib\\go-cqhttp.exe faststart",
'start cmd /K "title GoCqHttp For OlivOS|..\\..\\..\\lib\\go-cqhttp.exe faststart"',
shell = True,
cwd = '.\\conf\\gocqhttp\\' + self.Proc_data['bot_info_dict'].hash
)
Expand Down
5 changes: 5 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@
bot_info_dict = plugin_bot_info_dict,
logger_proc = Proc_dict[basic_conf_models_this['logger_proc']],
)
plugin_bot_info_dict = OlivOS.accountAPI.accountFix(
basic_conf_models = basic_conf_models,
bot_info_dict = plugin_bot_info_dict,
logger_proc = Proc_dict[basic_conf_models_this['logger_proc']],
)
elif basic_conf_models_this['type'] == 'qqGuild_link':
flag_need_enable = False
for bot_info_key in plugin_bot_info_dict:
Expand Down

0 comments on commit 462b665

Please sign in to comment.