def parse_http_request_parameters(args): parameters = dict() if args.interface: parameters["interface"] = args.interface parameters["allow_redirects"] = args.allow_redirects parameters["max_redirects"] = args.max_redirects if args.auth_type: parameters["auth_type"] = args.auth_type if args.auth_user: parameters["auth_user"] = args.auth_user auths = args.auth_user.split(":") if len(auths) != 2: logger.error("Error: auth_user format should be like username:password") exit(0) parameters["timeout"] = args.timeout parameters["connect_timeout"] = args.connect_timeout # parameters["request_timeout"] = args.request_timeout parameters["dns_cache_timeout"] = args.dns_cache_timeout parameters["fresh_connect"] = args.fresh_connect if args.http_header: parameters["http_header"] = args.http_header if args.http_body: parameters["http_body"] = args.http_body parameters["save_response_header"] = args.save_response_header parameters["save_response_body"] = args.save_response_body parameters["terminal_request_receive_time"] = time.time() # logger parameters["logger"] = ln return parameters
def parse_traceroute(args): destination = args.destination request_parameters = dict() if args.traceroute_interface: request_parameters["interface"] = args.traceroute_interface if args.traceroute_icmp: request_parameters["protocol"] = const.TracerouteProtocol.ICMP elif args.traceroute_tcp: request_parameters["protocol"] = const.TracerouteProtocol.ICMP elif args.traceroute_udp: request_parameters["protocol"] = const.TracerouteProtocol.ICMP else: logger.error("ERROR: traceroute just support protocol icmp/udp/tcp") exit(1) if args.traceroute_max_hops: request_parameters["max_hops"] = args.traceroute_max_hops # terminal_request_receive_time request_parameters["terminal_request_receive_time"] = time.time() # request logger request_parameters["logger"] = ln response_parameters = dict() response_parameters["storage_type"] = const.StorageMode.CONSOLE # response logger response_parameters["logger"] = ln traceroute_execute(destination, request_parameters, response_parameters)
def parse_service_start(args): logger.info("=" * SEPARATOR_COUNT) logger.info("starting natrixclient services ......") # systemd service files logger.info("\n1. checking natrixclient systemd service files ......") systemd_paths = ["/etc/systemd/system/natrixclient.service"] for systemd_path in systemd_paths: logger.info("checking natrixclient systemd file {}".format(systemd_path)) if os.path.isfile(systemd_path): logger.debug("natrixclient systemd file {} exist".format(systemd_path)) else: logger.error("natrixclient systemd file {} not exist, please execute \"natrixclient service init\" first!!!") exit(201) # systemd daemon files logger.info("\n2. checking natrixclient systemd daemon files ......") # TODO: CHANGE daemon_paths = ["/etc/natrixclient/natrixclient.daemon"] for daemon_path in daemon_paths: logger.info("checking natrixclient daemon service file {}".format(daemon_path)) if os.path.isfile(daemon_path): logger.debug("natrixclient daemon service file {} exist".format(daemon_path)) else: logger.error("natrixclient daemon service file {} not exist, " "please execute \"natrixclient service init\" first!!!".format(daemon_path)) exit(202) # start systemd service logger.info("\n3. starting natrixclient services ......") services = ["natrixclient.service"] for service in services: logger.debug("starting systemd service {}".format(service)) # must add shell=True service_start_command = "systemctl start " + service service_start_process = subprocess.Popen(service_start_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) # communicate() returns a tuple (stdout, stderr) service_start_result = service_start_process.communicate() logger.debug("service start command: \"{}\" \nresult: \"{}\"".format(service_start_command, service_start_result)) # # add crontab job # # add keep alive basic crontab job # logger.info("\n4. adding keep alive basic crontab job ......") # basic_minutes = const.CRONTAB_BASIC_MINUTES # create_crontab_basic(basic_minutes) # # add keep alive advance crontab job # logger.info("\n5. adding keep alive advance crontab job ......") # advance_minutes = const.CRONTAB_ADVANCE_MINUTES # create_crontab_advance(advance_minutes) # add reboot at midnight crontab job logger.info("\n4. adding reboot at midnight crontab job ......") reboot_hours = const.CRONTAB_REBOOT_HOURS reboot_minutes = const.CRONTAB_REBOOT_MINUTES create_crontab_reboot(reboot_hours, reboot_minutes) logger.info("\nsuccessfully started natrix client services") logger.info("=" * SEPARATOR_COUNT)
def parse_service_init(args): logger.info("=" * SEPARATOR_COUNT) logger.info("initializing natrixclient services ......") # 提升到root权限 if os.geteuid(): logger.error("must be root or have sudo authorization") exit(101) init_service_etc() init_service_log() init_service_systemctl() logger.info("\nsuccessfully initialized natrix client services") logger.info("=" * SEPARATOR_COUNT)
def parse_ping(args): # need sudo if os.geteuid(): logger.error("ERROR: natrix ping need root authorization, please use sudo") exit() destination = args.destination request_parameters = dict() if args.interface: request_parameters["interface"] = args.interface request_parameters["count"] = args.count request_parameters["timeout"] = args.timeout # terminal_request_receive_time request_parameters["terminal_request_receive_time"] = time.time() request_parameters["logger"] = ln response_parameters = dict() response_parameters["storage_type"] = const.StorageMode.CONSOLE response_parameters["logger"] = ln ping_execute(destination, request_parameters, response_parameters)