def main(): shell.check_python() # fix py2exe if hasattr(sys, "frozen") and sys.frozen in \ ("windows_exe", "console_exe"): p = os.path.dirname(os.path.abspath(sys.executable)) os.chdir(p) config = shell.get_config(True) daemon.daemon_exec(config) logging.info("starting local at %s:%d" % (config['local_address'], config['local_port'])) dns_resolver = asyncdns.DNSResolver() tcp_server = tcprelay.TCPRelay(config, dns_resolver, True) udp_server = udprelay.UDPRelay(config, dns_resolver, True) loop = eventloop.EventLoop() dns_resolver.add_to_loop(loop) tcp_server.add_to_loop(loop) udp_server.add_to_loop(loop) def handler(signum, _): logging.warn('received SIGQUIT, doing graceful shutting down..') tcp_server.close(next_tick=True) udp_server.close(next_tick=True) signal.signal(getattr(signal, 'SIGQUIT', signal.SIGTERM), handler) def int_handler(signum, _): sys.exit(1) signal.signal(signal.SIGINT, int_handler) daemon.set_user(config.get('user', None)) loop.run()
def main(): shell.check_python() config = shell.get_config(False) daemon.daemon_exec(config) daemon.set_user(config.get('user', None)) thread.start_new_thread(db_transfer.DbTransfer.thread_db, ()) while True: time.sleep(99999)
def main(): #check python shell.check_python() #get config print('get config') config = shell.get_config(True) #start daemon process print('daemon_exec') daemon.daemon_exec(config)
def main(): shell.check_python() # fix py2exe if hasattr(sys, "frozen") and sys.frozen in \ ("windows_exe", "console_exe"): p = os.path.dirname(os.path.abspath(sys.executable)) os.chdir(p) config = shell.get_config(True) if not config.get('dns_ipv6', False): asyncdns.IPV6_CONNECTION_SUPPORT = False daemon.daemon_exec(config) logging.info( "local start with protocol[%s] password [%s] method [%s] obfs [%s] obfs_param [%s]" % (config['protocol'], config['password'], config['method'], config['obfs'], config['obfs_param'])) try: logging.info("starting local at %s:%d" % (config['local_address'], config['local_port'])) dns_resolver = asyncdns.DNSResolver() tcp_server = tcprelay.TCPRelay(config, dns_resolver, True) udp_server = udprelay.UDPRelay(config, dns_resolver, True) loop = eventloop.EventLoop() dns_resolver.add_to_loop(loop) tcp_server.add_to_loop(loop) udp_server.add_to_loop(loop) def handler(signum, _): logging.warn('received SIGQUIT, doing graceful shutting down..') tcp_server.close(next_tick=True) udp_server.close(next_tick=True) signal.signal(getattr(signal, 'SIGQUIT', signal.SIGTERM), handler) def int_handler(signum, _): sys.exit(1) signal.signal(signal.SIGINT, int_handler) daemon.set_user(config.get('user', None)) loop.run() except Exception as e: shell.print_exception(e) sys.exit(1)
else: result = "[error]argvs error" else: conn.close() return logging.info(result) conn.send(result) conn.close() if len(sys.argv) < 2: print "参数错误" exit(0) else: daemon.daemon_exec(sys.argv[1]) logging.basicConfig(level=logging.INFO) s = sockets.createSocket() while True: try: conn, addr = s.accept() thread.start_new_thread(dealClient, (conn, addr)) except SystemExit: logging.info("exit...........") sys.exit(0) except: import traceback logging.error(traceback.format_exc()) if conn is not None: conn.close()
return r.content def main(): m = Minitor() while True: resp_payload = json.loads(m.connect()) for item in resp_payload: queue_name = item['name'] # 队列名 messages_ready = item['messages_ready'] # 准备好的消息数 msg_cnt_total = item["messages"] messages_unacknowledged = item[ "messages_unacknowledged"] # 未确认的消息数 consumers = item["consumers"] # print "队列名:{queue_name},准备好的消息数:{messages_ready},消费者数:{consumers},待处理的消息:{msg_cnt_total}".format( # queue_name=queue_name, messages_ready=messages_ready, consumers=consumers, msg_cnt_total=msg_cnt_total # ) if messages_ready > 1 or consumers < 1: # 队列准备好的数据大于1或者消费者小于1 msg = "error 队列名={queue_name} 积压消息数={messages_ready},消费者数量={consumers}".format( queue_name=queue_name, messages_ready=messages_ready, consumers=consumers) logger.error("%s" % msg) time.sleep(3) if __name__ == "__main__": RUN['daemon'] = sys.argv[1] print RUN daemon.daemon_exec(RUN) main()
def main(): shell.check_python() # fix py2exe if hasattr(sys, "frozen") and sys.frozen in ("windows_exe", "console_exe"): p = os.path.dirname(os.path.abspath(sys.executable)) os.chdir(p) config = shell.get_config(True) #added by cloud for local random choose a server and the port and the port_password if config['port_password']: if config['password']: logging.warn( 'warning: port_password should not be used with server_port and password. server_port and password will be ignored' ) # config['server_port'] = int(random.choice(config['port_password'].items())[0]) if config.has_key('server_port'): if type(config['server_port']) == list and config['server_port']: config['server_port'] = random.choice( config.get('server_port', 8388)) elif config['server_port']: config['server_port'] == int( common.to_str(config.get('server_port', 8388))) else: config['server_port'] = int( random.choice(config['port_password'].items())[0]) else: config['server_port'] = int( random.choice(config['port_password'].items())[0]) if not config['password'] or str(config['password']) == "": config['password'] = common.to_str( config['port_password']["%s" % config['server_port']]) else: if type(config['server_port']) == list and config['server_port']: config['server_port'] = random.choice( config.get('server_port', 8388)) else: config['server_port'] == int( common.to_str(config.get('server_port', 8388))) config["password"] = str( config["port_password"]["%s" % config["server_port"]]).strip() logging.warn('!' * 30) logging.info( "OK.. I choose this guy to help me f**k the GFW.. [ %s : %s : %s : %s : %s]" % (config['server'], config['server_port'], config['password'], config['server_info']["%s" % config['server']], config['method'])) logging.warn('!' * 30) time.sleep(1) daemon.daemon_exec(config) try: logging.info("starting local at %s:%d" % (config['local_address'], config['local_port'])) dns_resolver = asyncdns.DNSResolver(config) tcp_server = tcprelay.TCPRelay(config, dns_resolver, True) udp_server = udprelay.UDPRelay(config, dns_resolver, True) loop = eventloop.EventLoop(config) dns_resolver.add_to_loop(loop) tcp_server.add_to_loop(loop) udp_server.add_to_loop(loop) def handler(signum, _): logging.warn('received SIGQUIT, doing graceful shutting down..') tcp_server.close(next_tick=True) udp_server.close(next_tick=True) signal.signal(getattr(signal, 'SIGQUIT', signal.SIGTERM), handler) def int_handler(signum, _): sys.exit(1) signal.signal(signal.SIGINT, int_handler) daemon.set_user(config.get('user', None)) loop.run() except Exception as e: shell.print_exception(e) sys.exit(1)
def main(): shell.check_python() # fix py2exe if hasattr(sys, "frozen") and sys.frozen in ("windows_exe", "console_exe"): p = os.path.dirname(os.path.abspath(sys.executable)) os.chdir(p) config = shell.get_config(True) #added by tib for local random choose a server and the port and the port_password if config['port_password']: if config['password']: logging.warn('warning: port_password should not be used with server_port and password. server_port and password will be ignored') # config['server_port'] = int(random.choice(config['port_password'].items())[0]) if config.has_key('server_port'): if type(config['server_port']) == list and config['server_port']: config['server_port'] = random.choice(config.get('server_port', 8388)) elif config['server_port']: config['server_port'] == int(common.to_str(config.get('server_port',8388))) else: config['server_port'] = int(random.choice(config['port_password'].items())[0]) else: config['server_port'] = int(random.choice(config['port_password'].items())[0]) config['password'] = common.to_str(config['port_password']["%s" % config['server_port']]) else: if type(config['server_port']) == list and config['server_port']: config['server_port'] = random.choice(config.get('server_port', 8388)) else: config['server_port'] == int(common.to_str(config.get('server_port',8388))) config["password"] = str(config["port_password"]["%s" % config["server_port"]]).strip() logging.warn('!' * 30) logging.info("OK.. I choose this guy to help me f**k the GFW.. [ %s : %s : %s : %s : %s]" % (config['server'],config['server_port'],config['password'],config['server_info']["%s" % config['server']],config['method'])) logging.warn('!' * 30) time.sleep(1) daemon.daemon_exec(config) try: logging.info("starting local at %s:%d" % (config['local_address'], config['local_port'])) dns_resolver = asyncdns.DNSResolver(config) tcp_server = tcprelay.TCPRelay(config, dns_resolver, True) udp_server = udprelay.UDPRelay(config, dns_resolver, True) loop = eventloop.EventLoop(config) dns_resolver.add_to_loop(loop) tcp_server.add_to_loop(loop) udp_server.add_to_loop(loop) def handler(signum, _): logging.warn('received SIGQUIT, doing graceful shutting down..') tcp_server.close(next_tick=True) udp_server.close(next_tick=True) signal.signal(getattr(signal, 'SIGQUIT', signal.SIGTERM), handler) def int_handler(signum, _): sys.exit(1) signal.signal(signal.SIGINT, int_handler) daemon.set_user(config.get('user', None)) loop.run() except Exception as e: shell.print_exception(e) sys.exit(1)
def main(): import sys import os import signal import resource # -- import from shadowsockesr-v import logger import conf import exit import shell, daemon, eventloop, tcprelay, udprelay, asyncdns, common # get ssr configurations ssr_conf = shell.get_ssr_conf(is_local=False, ssr_conf_path=conf.configuration_path()) # start daemon daemon.daemon_exec(ssr_conf) logger.info('current process open files[cmd\'ulimit -n\'] resource: soft %d hard %d' % resource.getrlimit(resource.RLIMIT_NOFILE)) # todo Multiport -> Single port if not ssr_conf['port_password']: ssr_conf['port_password'] = {} server_port = ssr_conf['server_port'] if type(server_port) == list: for a_server_port in server_port: ssr_conf['port_password'][a_server_port] = ssr_conf['password'] else: ssr_conf['port_password'][str(server_port)] = ssr_conf['password'] if not ssr_conf.get('dns_ipv6', False): asyncdns.IPV6_CONNECTION_SUPPORT = False # no used # if config.get('manager_address', 0): # logging.info('entering manager mode') # manager.run(config) # return tcp_servers = [] udp_servers = [] dns_resolver = asyncdns.DNSResolver() if int(ssr_conf['workers']) > 1: stat_counter_dict = None else: stat_counter_dict = {} port_password = ssr_conf['port_password'] config_password = ssr_conf.get('password', 'm') del ssr_conf['port_password'] for port, password_obfs in port_password.items(): method = ssr_conf["method"] protocol = ssr_conf.get("protocol", 'origin') protocol_param = ssr_conf.get("protocol_param", '') obfs = ssr_conf.get("obfs", 'plain') obfs_param = ssr_conf.get("obfs_param", '') bind = ssr_conf.get("out_bind", '') bindv6 = ssr_conf.get("out_bindv6", '') if type(password_obfs) == list: password = password_obfs[0] obfs = common.to_str(password_obfs[1]) if len(password_obfs) > 2: protocol = common.to_str(password_obfs[2]) elif type(password_obfs) == dict: password = password_obfs.get('password', config_password) method = common.to_str(password_obfs.get('method', method)) protocol = common.to_str(password_obfs.get('protocol', protocol)) protocol_param = common.to_str(password_obfs.get('protocol_param', protocol_param)) obfs = common.to_str(password_obfs.get('obfs', obfs)) obfs_param = common.to_str(password_obfs.get('obfs_param', obfs_param)) bind = password_obfs.get('out_bind', bind) bindv6 = password_obfs.get('out_bindv6', bindv6) else: password = password_obfs a_config = ssr_conf.copy() ipv6_ok = False logger.info("server start with protocol[%s] password [%s] method [%s] obfs [%s] obfs_param [%s]" % (protocol, password, method, obfs, obfs_param)) if 'server_ipv6' in a_config: try: if len(a_config['server_ipv6']) > 2 and a_config['server_ipv6'][0] == "[" and a_config['server_ipv6'][-1] == "]": a_config['server_ipv6'] = a_config['server_ipv6'][1:-1] a_config['server_port'] = int(port) a_config['password'] = password a_config['method'] = method a_config['protocol'] = protocol a_config['protocol_param'] = protocol_param a_config['obfs'] = obfs a_config['obfs_param'] = obfs_param a_config['out_bind'] = bind a_config['out_bindv6'] = bindv6 a_config['server'] = a_config['server_ipv6'] logger.info("starting server at [%s]:%d" % (a_config['server'], int(port))) tcp_servers.append(tcprelay.TCPRelay(a_config, dns_resolver, False, stat_counter=stat_counter_dict)) udp_servers.append(udprelay.UDPRelay(a_config, dns_resolver, False, stat_counter=stat_counter_dict)) if a_config['server_ipv6'] == '::': ipv6_ok = True except Exception as e: logger.error(e) try: a_config = ssr_conf.copy() a_config['server_port'] = int(port) a_config['password'] = password a_config['method'] = method a_config['protocol'] = protocol a_config['protocol_param'] = protocol_param a_config['obfs'] = obfs a_config['obfs_param'] = obfs_param a_config['out_bind'] = bind a_config['out_bindv6'] = bindv6 logger.info("starting server at %s:%d" % (a_config['server'], int(port))) tcp_servers.append(tcprelay.TCPRelay(a_config, dns_resolver, False, stat_counter=stat_counter_dict)) udp_servers.append(udprelay.UDPRelay(a_config, dns_resolver, False, stat_counter=stat_counter_dict)) except Exception as e: if not ipv6_ok: logger.error(e) def run_server(): def child_handler(signum, _): logger.warning('received SIGQUIT, doing graceful shutting down..') list(map(lambda s: s.close(next_tick=True), tcp_servers + udp_servers)) signal.signal(getattr(signal, 'SIGQUIT', signal.SIGTERM), child_handler) def int_handler(signum, _): sys.exit(1) signal.signal(signal.SIGINT, int_handler) try: loop = eventloop.EventLoop() dns_resolver.add_to_loop(loop) list(map(lambda s: s.add_to_loop(loop), tcp_servers + udp_servers)) daemon.set_user(ssr_conf.get('user', None)) loop.run() except Exception as e: exit.error(e) if int(ssr_conf['workers']) > 1: if os.name == 'posix': children = [] is_child = False for i in range(0, int(ssr_conf['workers'])): r = os.fork() if r == 0: logger.info('worker started') is_child = True run_server() break else: children.append(r) if not is_child: def handler(signum, _): for pid in children: try: os.kill(pid, signum) os.waitpid(pid, 0) except OSError: # child may already exited pass sys.exit() signal.signal(signal.SIGTERM, handler) signal.signal(signal.SIGQUIT, handler) signal.signal(signal.SIGINT, handler) # master for a_tcp_server in tcp_servers: a_tcp_server.close() for a_udp_server in udp_servers: a_udp_server.close() dns_resolver.close() for child in children: os.waitpid(child, 0) else: logger.warning('worker is only available on Unix/Linux') run_server() else: run_server()
from bottle import static_file from bottle import request from bottle import template from bottle import install from bottle_sqlite import SQLitePlugin config = { 'log-file': './www.log', 'pid-file': './.www.pid', 'db-file': '../db/imgs.db', 'img-path': '../db/imgs', 'daemon': sys.argv[1] if len(sys.argv) > 1 else None } if config['daemon']: import daemon daemon.daemon_exec(config) install(SQLitePlugin(dbfile=config['db-file'])) templatestr = """<!DOCTYPE html> <html> <head> <title>BING IMAGE</title> <style> body{ max-width: 90%; margin: auto; padding-bottom:2em; background-color: #333333; color: #eeeeee; font-family: "Helvetica";
def main(): shell.check_python() config = shell.get_config(False) daemon.daemon_exec(config) if config['port_password']: if config['password']: logging.warn('warning: port_password should not be used with ' 'server_port and password. server_port and password ' 'will be ignored') else: config['port_password'] = {} server_port = config['server_port'] if type(server_port) == list: for a_server_port in server_port: config['port_password'][a_server_port] = config['password'] else: config['port_password'][str(server_port)] = config['password'] if config.get('manager_address', 0): logging.info('entering manager mode') manager.run(config) return tcp_servers = [] udp_servers = [] if 'dns_server' in config: # allow override settings in resolv.conf dns_resolver = asyncdns.DNSResolver(config['dns_server'], config['prefer_ipv6']) else: dns_resolver = asyncdns.DNSResolver(prefer_ipv6=config['prefer_ipv6']) port_password = config['port_password'] del config['port_password'] for port, password in port_password.items(): a_config = config.copy() a_config['server_port'] = int(port) a_config['password'] = password logging.info("starting server at %s:%d" % (a_config['server'], int(port))) tcp_servers.append(tcprelay.TCPRelay(a_config, dns_resolver, False)) udp_servers.append(udprelay.UDPRelay(a_config, dns_resolver, False)) def run_server(): def child_handler(signum, _): logging.warn('received SIGQUIT, doing graceful shutting down..') list( map(lambda s: s.close(next_tick=True), tcp_servers + udp_servers)) signal.signal(getattr(signal, 'SIGQUIT', signal.SIGTERM), child_handler) def int_handler(signum, _): sys.exit(1) signal.signal(signal.SIGINT, int_handler) try: loop = eventloop.EventLoop() dns_resolver.add_to_loop(loop) list(map(lambda s: s.add_to_loop(loop), tcp_servers + udp_servers)) daemon.set_user(config.get('user', None)) loop.run() except Exception as e: shell.print_exception(e) sys.exit(1) if int(config['workers']) > 1: if os.name == 'posix': children = [] is_child = False for i in range(0, int(config['workers'])): r = os.fork() if r == 0: logging.info('worker started') is_child = True run_server() break else: children.append(r) if not is_child: def handler(signum, _): for pid in children: try: os.kill(pid, signum) os.waitpid(pid, 0) except OSError: # child may already exited pass sys.exit() signal.signal(signal.SIGTERM, handler) signal.signal(signal.SIGQUIT, handler) signal.signal(signal.SIGINT, handler) # master for a_tcp_server in tcp_servers: a_tcp_server.close() for a_udp_server in udp_servers: a_udp_server.close() dns_resolver.close() for child in children: os.waitpid(child, 0) else: logging.warn('worker is only available on Unix/Linux') run_server() else: run_server()
ChatHandler._instances[sock] = ChatHandler(server, sock) return ChatHandler._instances[sock] def handle_ERR_NO_SUPPORT(self, type_, message): self.send_message(comm.copy_protocol('ERR_NO_SUPPORT')) def handle_C2S_VERSION(self, message): print('handle_C2S_VERSION=========' + message) if message == conf.VERSION: self.send_message(comm.copy_protocol('S2C_VERSION')) else: self.send_message(comm.copy_protocol('ERR_VERSION')) def handle_C2S_HEARTBEAT(self, message): print('handle_C2S_HEARTBEAT=========' + message) p = comm.copy_protocol('S2C_HEARTBEAT') p.set_data(int(time.time())) self.send_message(p) if __name__ == '__main__': config = {'daemon': 'start', 'pid-file': '/tmp/chat-server.pid', 'log-file':'log.log', 'user': '******'} daemon.daemon_exec(config) daemon.set_user(config.get('user', None)) ChatServer().runserver(config.get('host', '0.0.0.0'), config.get('port', 5005))