1+ import socket , sys , os , threading , inspect , asyncio , subprocess
2+ try :
3+ import tornado , debugpy
4+ except ImportError :
5+ print ("[IDACode] Dependencies missing, run: python3 -m pip install --user debugpy tornado" )
6+ sys .exit ()
7+ import idaapi
8+ import idacode_utils .dbg as dbg
9+ import idacode_utils .hooks as hooks
10+ import idacode_utils .settings as settings
11+ from idacode_utils .socket_handler import SocketHandler
12+
13+ VERSION = "0.1.2"
14+ initialized = False
15+
16+ def setup_patches ():
17+ hooks .install ()
18+ sys .executable = settings .PYTHON
19+
20+ def create_socket_handler ():
21+ asyncio .set_event_loop (asyncio .new_event_loop ())
22+ app = tornado .web .Application ([
23+ (r"/ws" , SocketHandler ),
24+ ])
25+ server = tornado .httpserver .HTTPServer (app )
26+ print (f"[IDACode] listening on { settings .HOST } :{ settings .PORT } " )
27+ server .listen (address = settings .HOST , port = settings .PORT )
28+
29+ def start_server ():
30+ setup_patches ()
31+ create_socket_handler ()
32+ tornado .ioloop .IOLoop .current ().start ()
33+
34+ def get_python_versions ():
35+ settings_version = subprocess .check_output ([settings .PYTHON , "-c" , "import sys; print(sys.version + sys.platform)" ])
36+ settings_version = settings_version .decode ("utf-8" , "ignore" ).strip ()
37+ ida_version = f"{ sys .version } { sys .platform } "
38+ return (settings_version , ida_version )
39+
40+ class IDACode (idaapi .plugin_t ):
41+ def __init__ (self ):
42+ self .flags = idaapi .PLUGIN_UNL
43+ self .comment = "IDACode"
44+ self .help = "IDACode"
45+ self .wanted_name = "IDACode"
46+ self .wanted_hotkey = ""
47+
48+ def init (self ):
49+ global initialized
50+ if not initialized :
51+ initialized = True
52+ if os .path .isfile (settings .PYTHON ):
53+ settings_version , ida_version = get_python_versions ()
54+ if settings_version != ida_version :
55+ print ("[IDACode] settings.PYTHON version mismatch, aborting load:" )
56+ print (f"[IDACode] IDA interpreter: { ida_version } " )
57+ print (f"[IDACode] settings.PYTHON: { settings_version } " )
58+ return idaapi .PLUGIN_SKIP
59+ else :
60+ print (f"[IDACode] settings.PYTHON ({ settings .PYTHON } ) does not exist, aborting load" )
61+ print ("[IDACode] To fix this issue, modify idacode_utils/settings.py to point to the python executable" )
62+ return idaapi .PLUGIN_SKIP
63+ print (f"[IDACode] Plugin version { VERSION } " )
64+ print ("[IDACode] Plugin loaded, use Edit -> Plugins -> IDACode to start the server" )
65+ return idaapi .PLUGIN_OK
66+
67+ def run (self , args ):
68+ thread = threading .Thread (target = start_server )
69+ thread .daemon = True
70+ thread .start ()
71+
72+ def term (self ):
73+ pass
0 commit comments