|
| 1 | +import argparse |
| 2 | +import logging |
| 3 | +try: |
| 4 | + import wx |
| 5 | +except ImportError: |
| 6 | + wx = None |
| 7 | + |
| 8 | + |
| 9 | +def ask_pass(primary_message, secondary_message=None): |
| 10 | + dialog_text = primary_message |
| 11 | + if secondary_message: |
| 12 | + dialog_text = "\n".join([primary_message, secondary_message]) |
| 13 | + app = wx.App() |
| 14 | + dlg = wx.PasswordEntryDialog(None, dialog_text, 'SSH-MITM - Askpass', "", wx.OK | wx.CANCEL) |
| 15 | + |
| 16 | + response = dlg.ShowModal() |
| 17 | + if response == wx.ID_OK: |
| 18 | + return dlg.GetValue() |
| 19 | + return None |
| 20 | + |
| 21 | + |
| 22 | +def confirm(primary_message, secondary_message=None): |
| 23 | + dialog_text = primary_message |
| 24 | + if secondary_message: |
| 25 | + dialog_text = "\n".join([primary_message, secondary_message]) |
| 26 | + app = wx.App() |
| 27 | + dlg = wx.MessageDialog(None, dialog_text, 'SSH-MITM - Askpass', wx.YES | wx.NO) |
| 28 | + response = dlg.ShowModal() |
| 29 | + if response == wx.ID_YES: |
| 30 | + return True |
| 31 | + return False |
| 32 | + |
| 33 | + |
| 34 | +def main(): |
| 35 | + if wx is False: |
| 36 | + logging.error("wxPython not installed! please install!") |
| 37 | + exit(1) |
| 38 | + |
| 39 | + parser = argparse.ArgumentParser() |
| 40 | + parser.add_argument('messages', nargs='*') |
| 41 | + args = parser.parse_args() |
| 42 | + |
| 43 | + lines = " ".join(args.messages).split("\n") |
| 44 | + primary_message = lines[0].strip() |
| 45 | + if primary_message == "": |
| 46 | + primary_message = "ssh-askpass" |
| 47 | + |
| 48 | + secondary_message = "\n".join(lines[1:]).strip() |
| 49 | + if secondary_message == "": |
| 50 | + secondary_message = None |
| 51 | + |
| 52 | + if primary_message.endswith("?"): |
| 53 | + ok = confirm(primary_message, secondary_message) |
| 54 | + if not ok: |
| 55 | + exit(1) |
| 56 | + else: |
| 57 | + result = ask_pass(primary_message, secondary_message) |
| 58 | + if result is None: |
| 59 | + exit(1) |
| 60 | + else: |
| 61 | + print(result) |
| 62 | + exit(0) |
| 63 | + |
| 64 | +if __name__ == '__main__': |
| 65 | + main() |
0 commit comments