def authorize (self, channel, username, password): if username in ('ftp', 'anonymous'): channel.persona = -1, -1 channel.read_only = 1 return 1, 'Ok.', filesys.os_filesystem (self.root) else: return 0, 'Password invalid.', None
def authorize (self, channel, userName, passWord): self.AdjustPrivilege( ntsecuritycon.SE_CHANGE_NOTIFY_NAME ) self.AdjustPrivilege( ntsecuritycon.SE_ASSIGNPRIMARYTOKEN_NAME ) self.AdjustPrivilege( ntsecuritycon.SE_TCB_NAME ) try: logonHandle = win32security.LogonUser( userName, None, passWord, win32con.LOGON32_LOGON_INTERACTIVE, win32con.LOGON32_PROVIDER_DEFAULT ) except pywintypes.error as ErrorMsg: return 0, ErrorMsg[ 2 ], None userInfo = win32net.NetUserGetInfo( None, userName, 1 ) return 1, 'Login successful', filesys.os_filesystem( userInfo[ 'home_dir' ] )
def replace_default_handler(self): """ This method replaces Supervisor web ui with Supvisors web ui. """ # create default handler pointing on Supvisors ui directory here = os.path.abspath(os.path.dirname(__file__)) templatedir = os.path.join(here, 'ui') filesystem = filesys.os_filesystem(templatedir) defaulthandler = default_handler.default_handler(filesystem) # deal with authentication if self.username: # wrap the xmlrpc handler and tailhandler in an authentication handler users = {self.username: self.password} defaulthandler = supervisor_auth_handler(users, defaulthandler) else: self.supervisord.supvisors.logger.warn('Server running without any HTTP authentication checking') # replace Supervisor default handler at the end of the list self.httpserver.handlers.pop() self.httpserver.install_handler(defaulthandler, True)
def handle_request (self, request): # get the user name m = user_dir.match (request.uri) user = m.group(1) rest = m.group(2) # special hack to catch those lazy URL typers if not rest: request['Location'] = '/~%s/' % user request.error (301) return # have we already built a userdir fs for this user? if user in self.fs_cache: fs = self.fs_cache[user] else: # no, well then, let's build one. # first, find out where the user directory is try: info = pwd.getpwnam (user) except KeyError: request.error (404) return ud = info[5] + '/' + self.public_html if os.path.isdir (ud): fs = filesys.os_filesystem (ud) self.fs_cache[user] = fs else: request.error (404) return # fake out default_handler self.filesystem = fs # massage the request URI request.uri = '/' + rest return default_handler.default_handler.handle_request (self, request)
def make_http_servers(options, supervisord): servers = [] class LogWrapper: def log(self, msg): if msg.endswith('\n'): msg = msg[:-1] options.logger.trace(msg) wrapper = LogWrapper() for config in options.server_configs: family = config['family'] if family == socket.AF_INET: host, port = config['host'], config['port'] hs = supervisor_af_inet_http_server(host, port, logger_object=wrapper) elif family == socket.AF_UNIX: socketname = config['file'] sockchmod = config['chmod'] sockchown = config['chown'] hs = supervisor_af_unix_http_server(socketname,sockchmod, sockchown, logger_object=wrapper) else: raise ValueError('Cannot determine socket type %r' % family) from xmlrpc import supervisor_xmlrpc_handler from xmlrpc import SystemNamespaceRPCInterface from web import supervisor_ui_handler subinterfaces = [] for name, factory, d in options.rpcinterface_factories: try: inst = factory(supervisord, **d) except: import traceback; traceback.print_exc() raise ValueError('Could not make %s rpc interface' % name) subinterfaces.append((name, inst)) options.logger.info('RPC interface %r initialized' % name) subinterfaces.append(('system', SystemNamespaceRPCInterface(subinterfaces))) xmlrpchandler = supervisor_xmlrpc_handler(supervisord, subinterfaces) tailhandler = logtail_handler(supervisord) maintailhandler = mainlogtail_handler(supervisord) uihandler = supervisor_ui_handler(supervisord) here = os.path.abspath(os.path.dirname(__file__)) templatedir = os.path.join(here, 'ui') filesystem = filesys.os_filesystem(templatedir) defaulthandler = default_handler.default_handler(filesystem) username = config['username'] password = config['password'] if username: # wrap the xmlrpc handler and tailhandler in an authentication # handler users = {username:password} xmlrpchandler = supervisor_auth_handler(users, xmlrpchandler) tailhandler = supervisor_auth_handler(users, tailhandler) maintailhandler = supervisor_auth_handler(users, maintailhandler) uihandler = supervisor_auth_handler(users, uihandler) defaulthandler = supervisor_auth_handler(users, defaulthandler) else: options.logger.critical( 'Server %r running without any HTTP ' 'authentication checking' % config['section']) # defaulthandler must be consulted last as its match method matches # everything, so it's first here (indicating last checked) hs.install_handler(defaulthandler) hs.install_handler(uihandler) hs.install_handler(maintailhandler) hs.install_handler(tailhandler) hs.install_handler(xmlrpchandler) # last for speed (first checked) servers.append((config, hs)) return servers
# to view these log entries. # # If you decide to comment this out, be sure to remove the # logger object from the list of status objects below. # lg = status_handler.logger_for_status (lg) # =========================================================================== # Filesystem Object. # =========================================================================== # An abstraction for the file system. Filesystem objects can be # combined and implemented in interesting ways. The default type # simply remaps a directory to root. fs = filesys.os_filesystem (PUBLISHING_ROOT) # =========================================================================== # Default HTTP handler # =========================================================================== # The 'default' handler for the HTTP server is one that delivers # files normally - this is the expected behavior of a web server. # Note that you needn't use it: Your web server might not want to # deliver files! # This default handler uses the filesystem object we just constructed. dh = default_handler.default_handler (fs) # ===========================================================================
import sys if len(sys.argv) < 2: print('usage: %s <root> <port>' % (sys.argv[0])) else: import supervisor.medusa.monitor as monitor import supervisor.medusa.filesys as filesys import supervisor.medusa.default_handler as default_handler import supervisor.medusa.status_handler as status_handler import supervisor.medusa.ftp_server as ftp_server import supervisor.medusa.chat_server as chat_server import supervisor.medusa.resolver as resolver import supervisor.medusa.logger as logger rs = resolver.caching_resolver ('127.0.0.1') lg = logger.file_logger (sys.stdout) ms = monitor.secure_monitor_server ('fnord', '127.0.0.1', 9999) fs = filesys.os_filesystem (sys.argv[1]) dh = default_handler.default_handler (fs) hs = http_server ('', int(sys.argv[2]), rs, lg) hs.install_handler (dh) ftp = ftp_server.ftp_server ( ftp_server.dummy_authorizer(sys.argv[1]), port=8021, resolver=rs, logger_object=lg ) cs = chat_server.chat_server ('', 7777) sh = status_handler.status_extension([hs,ms,ftp,cs,rs]) hs.install_handler (sh) if '-p' in sys.argv: def profile_loop (): try:
def authorize (self, channel, username, password): channel.persona = -1, -1 channel.read_only = 1 return 1, 'Ok.', filesys.os_filesystem (self.root)
def make_http_servers(options, supervisord): servers = [] class LogWrapper: def log(self, msg): if msg.endswith('\n'): msg = msg[:-1] options.logger.trace(msg) wrapper = LogWrapper() for config in options.server_configs: family = config['family'] if family == socket.AF_INET: host, port = config['host'], config['port'] hs = supervisor_af_inet_http_server(host, port, logger_object=wrapper) elif family == socket.AF_UNIX: socketname = config['file'] sockchmod = config['chmod'] sockchown = config['chown'] hs = supervisor_af_unix_http_server(socketname, sockchmod, sockchown, logger_object=wrapper) else: raise ValueError('Cannot determine socket type %r' % family) from xmlrpc import supervisor_xmlrpc_handler from xmlrpc import SystemNamespaceRPCInterface from web import supervisor_ui_handler subinterfaces = [] for name, factory, d in options.rpcinterface_factories: try: inst = factory(supervisord, **d) except: import traceback traceback.print_exc() raise ValueError('Could not make %s rpc interface' % name) subinterfaces.append((name, inst)) options.logger.info('RPC interface %r initialized' % name) subinterfaces.append( ('system', SystemNamespaceRPCInterface(subinterfaces))) xmlrpchandler = supervisor_xmlrpc_handler(supervisord, subinterfaces) tailhandler = logtail_handler(supervisord) maintailhandler = mainlogtail_handler(supervisord) uihandler = supervisor_ui_handler(supervisord) here = os.path.abspath(os.path.dirname(__file__)) templatedir = os.path.join(here, 'ui') filesystem = filesys.os_filesystem(templatedir) defaulthandler = default_handler.default_handler(filesystem) username = config['username'] password = config['password'] if username: # wrap the xmlrpc handler and tailhandler in an authentication # handler users = {username: password} xmlrpchandler = supervisor_auth_handler(users, xmlrpchandler) tailhandler = supervisor_auth_handler(users, tailhandler) maintailhandler = supervisor_auth_handler(users, maintailhandler) uihandler = supervisor_auth_handler(users, uihandler) defaulthandler = supervisor_auth_handler(users, defaulthandler) else: options.logger.critical('Server %r running without any HTTP ' 'authentication checking' % config['section']) # defaulthandler must be consulted last as its match method matches # everything, so it's first here (indicating last checked) hs.install_handler(defaulthandler) hs.install_handler(uihandler) hs.install_handler(maintailhandler) hs.install_handler(tailhandler) hs.install_handler(xmlrpchandler) # last for speed (first checked) servers.append((config, hs)) return servers
from supervisor.medusa import asyncore_25 as asyncore from supervisor.medusa import default_handler from supervisor.medusa import http_server from supervisor.medusa import put_handler from supervisor.medusa import auth_handler from supervisor.medusa import filesys # For this demo, we'll just use a dictionary of usernames/passwords. # You can of course use anything that supports the mapping interface, # and it would be pretty easy to set this up to use the crypt module # on unix. users = { 'mozart' : 'jupiter', 'beethoven' : 'pastoral' } # The filesystem we will be giving access to fs = filesys.os_filesystem('/home/medusa') # The 'default' handler - delivers files for the HTTP GET method. dh = default_handler.default_handler(fs) # Supports the HTTP PUT method... ph = put_handler.put_handler(fs, '/.*') # ... but be sure to wrap it with an auth handler: ah = auth_handler.auth_handler(users, ph) # Create a Web Server hs = http_server.http_server(ip='', port=8080) # install the handlers we created:
if __name__ == '__main__': if len(sys.argv) < 2: print('usage: %s <root> <port>' % (sys.argv[0])) else: import supervisor.medusa.monitor as monitor import supervisor.medusa.filesys as filesys import supervisor.medusa.default_handler as default_handler import supervisor.medusa.ftp_server as ftp_server import supervisor.medusa.chat_server as chat_server import supervisor.medusa.resolver as resolver rs = resolver.caching_resolver('127.0.0.1') lg = logger.file_logger(sys.stdout) ms = monitor.secure_monitor_server('fnord', '127.0.0.1', 9999) fs = filesys.os_filesystem(sys.argv[1]) dh = default_handler.default_handler(fs) hs = http_server('', int(sys.argv[2]), rs, lg) hs.install_handler(dh) ftp = ftp_server.ftp_server(ftp_server.dummy_authorizer(sys.argv[1]), port=8021, resolver=rs, logger_object=lg) cs = chat_server.chat_server('', 7777) if '-p' in sys.argv: def profile_loop(): try: asyncore.loop() except KeyboardInterrupt: pass
def make_http_servers(options, supervisord): servers = [] # options.logger:Logger # 这种想扩展但是不创建子列而是把原类当做参数传进新类的设计方法很好 wrapper = LogWrapper(options.logger) for config in options.server_configs: family = config['family'] if family == socket.AF_INET: host, port = config['host'], config['port'] hs = supervisor_af_inet_http_server(host, port, logger_object=wrapper) elif family == socket.AF_UNIX: #典型的本地IPC,类似于管道,依赖路径名标识发送方和接收方。即发送数据时,指定接收方绑定的路径名,操作系统根据 # 该路径名可以直接找到对应的接收方,并将原始数据直接拷贝到接收方的内核缓冲区中,并上报给接收方进程进行处理。 # 同样的接收方可以从收到的数据包中获取到发送方的路径名,并通过此路径名向其发送数据。 # 我们适应的是unix 'family': 1, 'section': 'unix_http_server', 'chmod': 448, 'chown': (-1, -1), 'file': '/tmp/supervisor.sock', socketname = config['file'] sockchmod = config['chmod'] sockchown = config['chown'] # hs是实例对象,在该对象中建立了sock文件和连接 hs = supervisor_af_unix_http_server(socketname, sockchmod, sockchown, logger_object=wrapper) # 在supervisor_af_unix_http_server 设置了socket_map else: raise ValueError('Cannot determine socket type %r' % family) from supervisor.xmlrpc import supervisor_xmlrpc_handler from supervisor.xmlrpc import SystemNamespaceRPCInterface from supervisor.web import supervisor_ui_handler subinterfaces = [] # self.rpcinterface_factories = section.rpcinterface_factories # [('supervisor', <function make_main_rpcinterface at 0x7fcaea23a140>, {})] for name, factory, d in options.rpcinterface_factories: # print(name,factory,d) try: # inst就是supervisord的实例对象 # rpcinterface 的1026行 #def make_main_rpcinterface(supervisord): #return SupervisorNamespaceRPCInterface(supervisord) inst = factory(supervisord, **d) # factory 是函数,factory(supervisord, **d)调用函数 # inst是SupervisorNamespaceRPCInterface(supervisord)实例 # 这一步是创建rpc,但是我不知怎么做到的 except: tb = traceback.format_exc() options.logger.warn(tb) raise ValueError('Could not make %s rpc interface' % name) subinterfaces.append((name, inst)) options.logger.info('RPC interface %r initialized' % name) # 注册xml接口 # ('supervisor', <supervisor.rpcinterface.SupervisorNamespaceRPCInterface instance at 0x7f6e2f40d758>) supervisor实例 subinterfaces.append( ('system', SystemNamespaceRPCInterface(subinterfaces))) # print(subinterfaces) # [('supervisor', < supervisor.rpcinterface.SupervisorNamespaceRPCInterface instance at 0x7f350f874b48 >), # ('system', < supervisor.xmlrpc.SystemNamespaceRPCInterface instance at 0x7f350f874b90 >)] # Supervisor XML-RPC Handler 和 path # 创建 xmlrpchandler xmlrpchandler = supervisor_xmlrpc_handler(supervisord, subinterfaces) tailhandler = logtail_handler(supervisord) maintailhandler = mainlogtail_handler(supervisord) uihandler = supervisor_ui_handler(supervisord) here = os.path.abspath(os.path.dirname(__file__)) templatedir = os.path.join(here, 'ui') filesystem = filesys.os_filesystem(templatedir) defaulthandler = default_handler.default_handler(filesystem) username = config['username'] password = config['password'] if username: # wrap the xmlrpc handler and tailhandler in an authentication # handler users = {username: password} xmlrpchandler = supervisor_auth_handler(users, xmlrpchandler) tailhandler = supervisor_auth_handler(users, tailhandler) maintailhandler = supervisor_auth_handler(users, maintailhandler) uihandler = supervisor_auth_handler(users, uihandler) defaulthandler = supervisor_auth_handler(users, defaulthandler) else: options.logger.critical('Server %r running without any HTTP ' 'authentication checking' % config['section']) # defaulthandler must be consulted last as its match method matches # everything, so it's first here (indicating last checked) hs.install_handler(defaulthandler) hs.install_handler(uihandler) hs.install_handler(maintailhandler) hs.install_handler(tailhandler) hs.install_handler( xmlrpchandler) # last for speed (first checked) 最前面 servers.append((config, hs)) return servers
class sample_input_collector: def __init__ (self, request, length): self.request = request self.length = length def collect_incoming_data (self, data): print(('data from %s: <%s>' % (self.request, repr(data)))) class post_script_handler (script_handler.script_handler): def handle_request (self, request): if request.command == 'post': cl = default_handler.get_header(CONTENT_LENGTH, request.header) ic = sample_input_collector(request, cl) request.collector = ic print((request.header)) return script_handler.script_handler.handle_request (self, request) lg = logger.file_logger (sys.stdout) fs = filesys.os_filesystem (PUBLISHING_ROOT) dh = default_handler.default_handler (fs) ph = post_script_handler (fs) hs = http_server.http_server ('', 8081, logger_object = lg) hs.install_handler (dh) hs.install_handler (ph) asyncore.loop()