druidencrypt.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2021/05/06
  3. # @Author : ricky
  4. # @File : druidencrypt.py
  5. # @Software: vscode
  6. """druid秘钥生成界面
  7. 这里是执行jar命令生成的
  8. """
  9. import wx
  10. import subprocess
  11. import pyperclip
  12. import ui.druid.druid_encrypt_dialog as dialog
  13. from utils import pathutil
  14. def show_message(message):
  15. """统一显示对话框
  16. 根据传入对话框内容显示对话框
  17. 参数:
  18. message (str): 对话框内容
  19. """
  20. wx.MessageDialog(None, message, '操作提醒', wx.OK).ShowModal()
  21. class DruidEncrypt(dialog.DruidEncryptDialog):
  22. def __init__(self, parent):
  23. dialog.DruidEncryptDialog.__init__(self, parent)
  24. self.m_text_ctrl_original_password.SetHint('请输入原始密码(最长不超过32位)')
  25. self.Centre()
  26. def OnClickEventGenerate(self, event):
  27. """生成按钮事件"""
  28. # 校验原始密码是否输入
  29. original_password = self.m_text_ctrl_original_password.GetValue()
  30. if len(original_password) == 0:
  31. show_message('请输入原始密码')
  32. self.m_text_ctrl_original_password.SetFocus()
  33. return
  34. # 测试是否安装java环境
  35. subp = subprocess.Popen('java -version',
  36. shell=True,
  37. stdin=subprocess.PIPE,
  38. stdout=subprocess.PIPE,
  39. stderr=subprocess.STDOUT)
  40. out = subp.stdout.readlines()
  41. if (str(out[0]).find('java version') == -1):
  42. show_message('检查到您电脑上没有安装jdk,请安装jdk并且配置环境变量')
  43. return
  44. # 执行秘钥生成的命令
  45. jar_file = pathutil.resource_path('libs/jar/druid-1.2.4.jar')
  46. original_password = self.m_text_ctrl_original_password.GetValue()
  47. cmd = 'java -cp ' + jar_file + ' com.alibaba.druid.filter.config.ConfigTools ' + original_password
  48. subp = subprocess.Popen(cmd,
  49. shell=True,
  50. stdin=subprocess.PIPE,
  51. stdout=subprocess.PIPE,
  52. stderr=subprocess.STDOUT,
  53. encoding='gbk')
  54. out = subp.stdout.readlines()
  55. if (out[0].find('错误') > -1):
  56. show_message(','.join(out))
  57. return
  58. # 绑定值
  59. self.m_text_ctrl_private_key.SetValue(out[0].replace(
  60. '\n', '').split('privateKey:')[1])
  61. self.m_text_ctrl_public_key.SetValue(out[1].replace(
  62. '\n', '').split('publicKey:')[1])
  63. self.m_text_ctrl_encrypt_password.SetValue(out[2].replace(
  64. '\n', '').split('password:')[1])
  65. def OnClickEventPrivateKeyCopy(self, event):
  66. """复制私钥按钮事件"""
  67. text = self.m_text_ctrl_private_key.GetValue()
  68. if len(text) > 0:
  69. pyperclip.copy(text)
  70. show_message('私钥已复制到剪切板')
  71. else:
  72. show_message('请生成秘钥')
  73. def OnClickEventPublicKeyCopy(self, event):
  74. """复制公钥按钮事件"""
  75. text = self.m_text_ctrl_public_key.GetValue()
  76. if len(text) > 0:
  77. pyperclip.copy(text)
  78. show_message('公钥已复制到剪切板')
  79. else:
  80. show_message('请生成秘钥')
  81. def OnClickEventEncryptPasswordCopy(self, event):
  82. """复制密码按钮事件"""
  83. text = self.m_text_ctrl_encrypt_password.GetValue()
  84. if len(text) > 0:
  85. pyperclip.copy(text)
  86. show_message('密码已复制到剪切板')
  87. else:
  88. show_message('请生成密码')
  89. def OnClickEventClear(self, event):
  90. """清空按钮事件"""
  91. self.m_text_ctrl_original_password.SetValue('')
  92. self.m_text_ctrl_original_password.SetFocus()
  93. self.m_text_ctrl_private_key.SetValue('')
  94. self.m_text_ctrl_public_key.SetValue('')
  95. self.m_text_ctrl_encrypt_password.SetValue('')