Skip to content

Commit

Permalink
增加自动官网翻译、并且将其设置为默认
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypdncy committed Jan 9, 2023
1 parent 95a9528 commit f138fc0
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 17 deletions.
11 changes: 9 additions & 2 deletions cnf/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@

template_file = "./template/主机扫描报告模板-202104.docx"

# 翻译工具: BaiDu YouDao Tenable
# BaiDu : 百度翻译,需要配置key、secret
# YouDao : 网易翻译,需要配置key、secret
# Tenable : 官网翻译,无需配置
translate_tool = "Tenable"
translate_status = True
translate_auto_db = True # 翻译结果默认添加到db中
translate_sem = 9 # 协程限制
Expand All @@ -46,8 +51,10 @@
translate_baidu_secret = "XXXXXX"

translate_youdao_url = "https://openapi.youdao.com/api"
translate_youdao_appkey = "xxxxxxxx"
translate_youdao_appsecret = "xxxxxxxx"
translate_youdao_appkey = ""
translate_youdao_appsecret = ""

translate_tenable_url = "https://zh-cn.tenable.com/plugins/nessus/"

translate_order = {
"name_en": "name_cn",
Expand Down
1 change: 0 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
# ````':. ':::::::::' ::::..
# '.:::::' ':'````..
# ------------------------------------------------------------
from datetime import datetime

config_data = {
"user": {
Expand Down
9 changes: 7 additions & 2 deletions modle/common/translate/baidu.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import hashlib
import random

from aiohttp import ClientResponse

from cnf.const import translate_baidu_url, translate_baidu_appid, translate_baidu_secret
from cnf.const import translate_order
from modle.common.loophole.loopholes import Loopholes
Expand Down Expand Up @@ -80,8 +82,11 @@ def _make_en_reqinfos(self):

return en_reqinfos

def _analysis_cn_resinfo(self, resinfo):
async def _analysis_cn_resinfo(self, response: ClientResponse, type_cn):
"""
解析响应体中的中文数据
"""
return resinfo["trans_result"][0]["dst"]
res_json = await response.json()
return {
type_cn: res_json["trans_result"][0]["dst"]
}
15 changes: 9 additions & 6 deletions modle/common/translate/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import logging
from abc import abstractmethod

from aiohttp import ClientResponse
from aiohttp.client import ClientSession, ClientTimeout

from cnf.const import translate_sem, translate_qps, translate_status, translate_auto_db, json_loops_error, vuln_db_file
Expand Down Expand Up @@ -58,14 +59,15 @@ async def _tran_http_with_sem(self, reqinfo, sem):

async def _tran_http(self, reqinfo, sem=None):
await asyncio.sleep(1)
async with ClientSession(timeout=self.timeout) as session:
async with ClientSession(timeout=self.timeout, headers=reqinfo.get("headers", {})) as session:
try:
async with session.request(method=reqinfo["method"], url=reqinfo["url"],
**reqinfo["kwargs"]) as response:
data = await response.json()

data = await self._analysis_cn_resinfo(response, reqinfo["type_cn"])
self.tran_number += 1
print("------翻译漏洞进度:{0}/{1}".format(int(self.tran_number / 3) + 1, self.tran_count), end='\r')
return [reqinfo["plugin_id"], reqinfo["type_cn"], data]
return [reqinfo["plugin_id"], data]
except Exception as e:
print(e)

Expand Down Expand Up @@ -100,13 +102,14 @@ def _make_en_reqinfos(self):
pass

@abstractmethod
def _analysis_cn_resinfo(self, resinfo):
async def _analysis_cn_resinfo(self, response: ClientResponse, type_cn):
pass

def run(self):
cn_resinfos = asyncio.run(self._async_main())
for plugin_id, type_cn, resinfo in cn_resinfos:
self.LOOPHOLES[plugin_id][type_cn] = self._analysis_cn_resinfo(resinfo)
for plugin_id, resinfo in cn_resinfos:
for type_cn, cn_text in resinfo.items():
self.LOOPHOLES[plugin_id][type_cn] = cn_text
self._check_en2cn()
self.LOOPHOLES.dump_loops()
if translate_auto_db:
Expand Down
15 changes: 10 additions & 5 deletions modle/common/translate/youdao.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@
import time
import uuid

from aiohttp import ClientResponse

from cnf.const import translate_youdao_url, translate_youdao_appkey, translate_youdao_appsecret, translate_order
from modle.common.loophole.loopholes import Loopholes
from modle.common.translate.base import TranBase


class TranYoudao(TranBase):
class TranYouDao(TranBase):

def __init__(self, LOOPHOLES: Loopholes):
super(TranYoudao, self).__init__(LOOPHOLES)
super(TranYouDao, self).__init__(LOOPHOLES)

def _make_en_reqinfos(self):

Expand Down Expand Up @@ -88,8 +90,11 @@ def truncate(q):

return en_reqinfos

def _analysis_cn_resinfo(self, resinfo):
async def _analysis_cn_resinfo(self, response: ClientResponse, type_cn):
"""
解析响应体
解析响应体中的中文数据
"""
return resinfo["translation"]
res_json = await response.json()
return {
type_cn: res_json["translation"]
}
10 changes: 9 additions & 1 deletion modle/handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@

import logging

from cnf.const import translate_tool
from modle.common.loophole.loopholes import Loopholes
from modle.common.translate.baidu import TranBaidu
from modle.common.translate.tenable import TranTenable
from modle.common.translate.youdao import TranYouDao
from modle.data.hosts import DataHosts
from modle.data.loops import DataLoops
from modle.docx.host import DocxHost
Expand All @@ -52,7 +55,12 @@ def __init__(self, docxtype):
self.LOOPHOLES.run()

logging.info("---开始翻译数据")
TranBaidu(self.LOOPHOLES).run()
func_translate_tools = {
"BaiDu": TranBaidu,
"YouDao": TranYouDao,
"Tenable": TranTenable
}
func_translate_tools[translate_tool](self.LOOPHOLES).run()

def run_hosts(self):
"""
Expand Down

0 comments on commit f138fc0

Please sign in to comment.