@@ -364,156 +364,136 @@ def main():
364364 public_ip = None
365365 if config .getboolean ("fetch_public_ip" , "enabled" ):
366366 public_ip = ext_ip .get_ext_ip (config )
367- if config .getboolean ("change_mac_addr" , "enabled" ):
368- if os .getuid () == 0 :
369- logger .info ("Attempting to change mac address." )
370- mac_addr .change_mac (config = config )
371- else :
372- logger .info ("Changing mac address require sudo permissions. Skipping" )
373367
374- # no need to fork process when we don't want to change MAC address
375- pid = 0
376- if config .getboolean ("change_mac_addr" , "enabled" ):
377- pid = gevent .fork ()
378368
379- if pid == 0 :
380- for protocol_name , server_class in protocols .name_mapping .items ():
381- protocol_template = os .path .join (
382- root_template_directory , protocol_name , "{0}.xml" .format (protocol_name )
369+
370+ for protocol_name , server_class in protocols .name_mapping .items ():
371+ protocol_template = os .path .join (
372+ root_template_directory , protocol_name , "{0}.xml" .format (protocol_name )
373+ )
374+ if os .path .isfile (protocol_template ):
375+ xsd_file = os .path .join (
376+ package_directory ,
377+ "protocols" ,
378+ protocol_name ,
379+ "{0}.xsd" .format (protocol_name ),
383380 )
384- if os .path .isfile (protocol_template ):
385- xsd_file = os .path .join (
386- package_directory ,
387- "protocols" ,
388- protocol_name ,
389- "{0}.xsd" .format (protocol_name ),
390- )
391- validate_template (protocol_template , xsd_file )
392- dom_protocol = etree .parse (protocol_template )
393- if dom_protocol .xpath ("//{0}" .format (protocol_name )):
394- if ast .literal_eval (
395- dom_protocol .xpath ("//{0}/@enabled" .format (protocol_name ))[0 ]
396- ):
397- host = dom_protocol .xpath ("//{0}/@host" .format (protocol_name ))[
398- 0
399- ]
400- # -- > Are we running on testing config?
401- if "testing.cfg" in args .config :
402- if "127." not in host :
403- if not args .force :
404- logger .error (
405- "To run conpot on a non local interface, please specify -f option"
406- )
407- sys .exit (1 )
408- port = ast .literal_eval (
409- dom_protocol .xpath ("//{0}/@port" .format (protocol_name ))[0 ]
410- )
411- server = server_class (
412- protocol_template , root_template_directory , args
413- )
414- greenlet = spawn_startable_greenlet (server , host , port )
415- greenlet .link_exception (on_unhandled_greenlet_exception )
416- servers .append ((server , greenlet ))
417- logger .info (
418- "Found and enabled {} protocol." .format (
419- protocol_name , server
420- )
421- )
422- else :
381+ validate_template (protocol_template , xsd_file )
382+ dom_protocol = etree .parse (protocol_template )
383+ if dom_protocol .xpath ("//{0}" .format (protocol_name )):
384+ if ast .literal_eval (
385+ dom_protocol .xpath ("//{0}/@enabled" .format (protocol_name ))[0 ]
386+ ):
387+ host = dom_protocol .xpath ("//{0}/@host" .format (protocol_name ))[
388+ 0
389+ ]
390+ # -- > Are we running on testing config?
391+ if "testing.cfg" in args .config :
392+ if "127." not in host :
393+ if not args .force :
394+ logger .error (
395+ "To run conpot on a non local interface, please specify -f option"
396+ )
397+ sys .exit (1 )
398+ port = ast .literal_eval (
399+ dom_protocol .xpath ("//{0}/@port" .format (protocol_name ))[0 ]
400+ )
401+ server = server_class (
402+ protocol_template , root_template_directory , args
403+ )
404+ greenlet = spawn_startable_greenlet (server , host , port )
405+ greenlet .link_exception (on_unhandled_greenlet_exception )
406+ servers .append ((server , greenlet ))
423407 logger .info (
424- "{} available but disabled by configuration ." .format (
425- protocol_name
408+ "Found and enabled {} protocol ." .format (
409+ protocol_name , server
426410 )
427411 )
428412 else :
429- logger .debug (
430- "No {} template found. Service will remain unconfigured/stopped ." .format (
413+ logger .info (
414+ "{} available but disabled by configuration ." .format (
431415 protocol_name
432416 )
433417 )
434-
435- log_worker = LogWorker (config , dom_base , session_manager , public_ip )
436- greenlet = spawn_startable_greenlet (log_worker )
437- greenlet .link_exception (on_unhandled_greenlet_exception )
438- servers .append ((log_worker , greenlet ))
439-
440- # TODO: Line up Proxy init with other protocols
441- template_proxy = os .path .join (root_template_directory , "proxy" , "proxy.xml" )
442- if os .path .isfile (template_proxy ):
443- xsd_file = os .path .join (
444- os .path .dirname (inspect .getfile (Proxy )), "proxy.xsd"
445- )
446- validate_template (template_proxy , xsd_file )
447- dom_proxy = etree .parse (template_proxy )
448- if dom_proxy .xpath ("//proxies" ):
449- if ast .literal_eval (dom_proxy .xpath ("//proxies/@enabled" )[0 ]):
450- proxies = dom_proxy .xpath ("//proxies/*" )
451- for p in proxies :
452- name = p .attrib ["name" ]
453- host = p .attrib ["host" ]
454- keyfile = None
455- certfile = None
456- if "keyfile" in p .attrib and "certfile" in p .attrib :
457- keyfile = p .attrib ["keyfile" ]
458- certfile = p .attrib ["certfile" ]
459-
460- # if path is absolute we assert that the cert and key is located in
461- # the templates ssl standard location
462-
463- if not os .path .isabs (keyfile ):
464- keyfile = os .path .join (
465- os .path .dirname (root_template_directory ),
466- "ssl" ,
467- keyfile ,
468- )
469- certfile = os .path .join (
470- os .path .dirname (root_template_directory ),
471- "ssl" ,
472- certfile ,
473- )
474- port = ast .literal_eval (p .attrib ["port" ])
475- proxy_host = p .xpath ("./proxy_host/text()" )[0 ]
476- proxy_port = ast .literal_eval (p .xpath ("./proxy_port/text()" )[0 ])
477- decoder = p .xpath ("./decoder/text()" )
478- if len (decoder ) > 0 :
479- decoder = decoder [0 ]
480- else :
481- decoder = None
482- proxy_instance = Proxy (
483- name , proxy_host , proxy_port , decoder , keyfile , certfile
484- )
485- proxy_server = proxy_instance .get_server (host , port )
486- proxy_greenlet = spawn_startable_greenlet (proxy_server )
487- proxy_greenlet .link_exception (on_unhandled_greenlet_exception )
488- servers .append ((proxy_instance , proxy_greenlet ))
489- else :
490- logger .info ("Proxy available but disabled by template." )
491418 else :
492- logger .info (
493- "No proxy template found. Service will remain unconfigured/stopped."
419+ logger .debug (
420+ "No {} template found. Service will remain unconfigured/stopped." .format (
421+ protocol_name
422+ )
494423 )
495424
496- try :
497- if len (servers ) > 0 :
498- gevent .wait ()
499- except KeyboardInterrupt :
500- logging .info ("Stopping Conpot" )
501- for server , greenlet in servers :
502- logging .debug (f"Shutting down { greenlet .name } " )
503- server .stop ()
504- greenlet .get ()
505- finally :
506- conpot_core .close_fs ()
425+ log_worker = LogWorker (config , dom_base , session_manager , public_ip )
426+ greenlet = spawn_startable_greenlet (log_worker )
427+ greenlet .link_exception (on_unhandled_greenlet_exception )
428+ servers .append ((log_worker , greenlet ))
507429
430+ # TODO: Line up Proxy init with other protocols
431+ template_proxy = os .path .join (root_template_directory , "proxy" , "proxy.xml" )
432+ if os .path .isfile (template_proxy ):
433+ xsd_file = os .path .join (
434+ os .path .dirname (inspect .getfile (Proxy )), "proxy.xsd"
435+ )
436+ validate_template (template_proxy , xsd_file )
437+ dom_proxy = etree .parse (template_proxy )
438+ if dom_proxy .xpath ("//proxies" ):
439+ if ast .literal_eval (dom_proxy .xpath ("//proxies/@enabled" )[0 ]):
440+ proxies = dom_proxy .xpath ("//proxies/*" )
441+ for p in proxies :
442+ name = p .attrib ["name" ]
443+ host = p .attrib ["host" ]
444+ keyfile = None
445+ certfile = None
446+ if "keyfile" in p .attrib and "certfile" in p .attrib :
447+ keyfile = p .attrib ["keyfile" ]
448+ certfile = p .attrib ["certfile" ]
449+
450+ # if path is absolute we assert that the cert and key is located in
451+ # the templates ssl standard location
452+
453+ if not os .path .isabs (keyfile ):
454+ keyfile = os .path .join (
455+ os .path .dirname (root_template_directory ),
456+ "ssl" ,
457+ keyfile ,
458+ )
459+ certfile = os .path .join (
460+ os .path .dirname (root_template_directory ),
461+ "ssl" ,
462+ certfile ,
463+ )
464+ port = ast .literal_eval (p .attrib ["port" ])
465+ proxy_host = p .xpath ("./proxy_host/text()" )[0 ]
466+ proxy_port = ast .literal_eval (p .xpath ("./proxy_port/text()" )[0 ])
467+ decoder = p .xpath ("./decoder/text()" )
468+ if len (decoder ) > 0 :
469+ decoder = decoder [0 ]
470+ else :
471+ decoder = None
472+ proxy_instance = Proxy (
473+ name , proxy_host , proxy_port , decoder , keyfile , certfile
474+ )
475+ proxy_server = proxy_instance .get_server (host , port )
476+ proxy_greenlet = spawn_startable_greenlet (proxy_server )
477+ proxy_greenlet .link_exception (on_unhandled_greenlet_exception )
478+ servers .append ((proxy_instance , proxy_greenlet ))
479+ else :
480+ logger .info ("Proxy available but disabled by template." )
508481 else :
509- # wait for the child to end
510- try :
511- os .waitpid (pid , 0 )
512- except KeyboardInterrupt :
513- pass
514- # Revert MAC address
515- iface = config .get ("change_mac_addr" , "iface" )
516- mac_addr .revert_mac (iface )
482+ logger .info (
483+ "No proxy template found. Service will remain unconfigured/stopped."
484+ )
485+
486+ try :
487+ if len (servers ) > 0 :
488+ gevent .wait ()
489+ except KeyboardInterrupt :
490+ logging .info ("Stopping Conpot" )
491+ for server , greenlet in servers :
492+ logging .debug (f"Shutting down { greenlet .name } " )
493+ server .stop ()
494+ greenlet .get ()
495+ finally :
496+ conpot_core .close_fs ()
517497
518498
519499if __name__ == "__main__" :
0 commit comments