本文整理汇总了Python中xpra.net.protocol.Protocol.set_cipher_out方法的典型用法代码示例。如果您正苦于以下问题:Python Protocol.set_cipher_out方法的具体用法?Python Protocol.set_cipher_out怎么用?Python Protocol.set_cipher_out使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xpra.net.protocol.Protocol
的用法示例。
在下文中一共展示了Protocol.set_cipher_out方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: XpraClientBase
# 需要导入模块: from xpra.net.protocol import Protocol [as 别名]
# 或者: from xpra.net.protocol.Protocol import set_cipher_out [as 别名]
#.........这里部分代码省略.........
if p is None or p._closed:
self.quit(exit_code)
return
def protocol_closed():
log("disconnect_and_quit: protocol_closed()")
self.idle_add(self.quit, exit_code)
if p:
p.flush_then_close(["disconnect", reason], done_callback=protocol_closed)
self.timeout_add(1000, self.quit, exit_code)
def exit(self):
sys.exit()
def client_type(self):
#overriden in subclasses!
return "Python"
def get_scheduler(self):
raise NotImplementedError()
def setup_connection(self, conn):
netlog("setup_connection(%s) timeout=%s", conn, conn.timeout)
self._protocol = Protocol(self.get_scheduler(), conn, self.process_packet, self.next_packet)
self._protocol.large_packets.append("keymap-changed")
self._protocol.large_packets.append("server-settings")
self._protocol.large_packets.append("logging")
self._protocol.set_compression_level(self.compression_level)
self._protocol.receive_aliases.update(self._aliases)
self._protocol.enable_default_encoder()
self._protocol.enable_default_compressor()
if self.encryption and ENCRYPT_FIRST_PACKET:
key = self.get_encryption_key()
self._protocol.set_cipher_out(self.encryption, DEFAULT_IV, key, DEFAULT_SALT, DEFAULT_ITERATIONS, INITIAL_PADDING)
self.have_more = self._protocol.source_has_more
if conn.timeout>0:
self.timeout_add((conn.timeout + EXTRA_TIMEOUT) * 1000, self.verify_connected)
netlog("setup_connection(%s) protocol=%s", conn, self._protocol)
def remove_packet_handlers(self, *keys):
for k in keys:
for d in (self._packet_handlers, self._ui_packet_handlers):
try:
del d[k]
except:
pass
def set_packet_handlers(self, to, defs):
""" configures the given packet handlers,
and make sure we remove any existing ones with the same key
(which can be useful for subclasses, not here)
"""
log("set_packet_handlers(%s, %s)", to, defs)
self.remove_packet_handlers(*defs.keys())
for k,v in defs.items():
to[k] = v
def init_packet_handlers(self):
self._packet_handlers = {}
self._ui_packet_handlers = {}
self.set_packet_handlers(self._packet_handlers, {"hello" : self._process_hello})
self.set_packet_handlers(self._ui_packet_handlers, {
"challenge": self._process_challenge,
"disconnect": self._process_disconnect,
"set_deflate": self._process_set_deflate,
示例2: XpraClientBase
# 需要导入模块: from xpra.net.protocol import Protocol [as 别名]
# 或者: from xpra.net.protocol.Protocol import set_cipher_out [as 别名]
#.........这里部分代码省略.........
#server never sent hello to us - so disconnect is an error
#(but we don't know which one - the info message may help)
e = EXIT_FAILURE
self.warn_and_quit(e, "server requested disconnect: %s" % info)
def _process_connection_lost(self, packet):
self.warn_and_quit(EXIT_CONNECTION_LOST, "Connection lost")
def _process_challenge(self, packet):
log("processing challenge: %s", packet[1:])
if not self.password_file:
self.warn_and_quit(EXIT_PASSWORD_REQUIRED, "server requires authentication, please provide a password")
return
password = self.load_password()
if not password:
self.warn_and_quit(EXIT_PASSWORD_FILE_ERROR, "failed to load password from file %s" % self.password_file)
return
salt = packet[1]
if self.encryption:
assert len(packet)>=3, "challenge does not contain encryption details to use for the response"
server_cipher = packet[2]
key = self.get_encryption_key()
if key is None:
self.warn_and_quit(EXIT_ENCRYPTION, "encryption key is missing")
return
if not self.set_server_encryption(server_cipher, key):
return
digest = "hmac"
client_can_salt = len(packet)>=4
client_salt = None
if client_can_salt:
#server supports client salt, and tells us which digest to use:
digest = packet[3]
client_salt = get_hex_uuid()+get_hex_uuid()
#TODO: use some key stretching algorigthm? (meh)
salt = xor(salt, client_salt)
if digest=="hmac":
import hmac
challenge_response = hmac.HMAC(password, salt).hexdigest()
elif digest=="xor":
#don't send XORed password unencrypted:
if not self._protocol.cipher_out and not ALLOW_UNENCRYPTED_PASSWORDS:
self.warn_and_quit(EXIT_ENCRYPTION, "server requested digest %s, cowardly refusing to use it without encryption" % digest)
return
challenge_response = xor(password, salt)
else:
self.warn_and_quit(EXIT_PASSWORD_REQUIRED, "server requested an unsupported digest: %s" % digest)
return
if digest:
log("%s(%s, %s)=%s", digest, password, salt, challenge_response)
self.password_sent = True
self.send_hello(challenge_response, client_salt)
def set_server_encryption(self, capabilities, key):
def get(key, default=None):
return capabilities.get(strtobytes(key), default)
cipher = get("cipher")
cipher_iv = get("cipher.iv")
key_salt = get("cipher.key_salt")
iterations = get("cipher.key_stretch_iterations")
if not cipher or not cipher_iv:
self.warn_and_quit(EXIT_ENCRYPTION, "the server does not use or support encryption/password, cannot continue with %s cipher" % self.encryption)
return False
if cipher not in ENCRYPTION_CIPHERS:
self.warn_and_quit(EXIT_ENCRYPTION, "unsupported server cipher: %s, allowed ciphers: %s" % (cipher, ", ".join(ENCRYPTION_CIPHERS)))
return False
self._protocol.set_cipher_out(cipher, cipher_iv, key, key_salt, iterations)
return True
def get_encryption_key(self):
key = load_binary_file(self.encryption_keyfile)
if key is None and self.password_file:
key = load_binary_file(self.password_file)
if key:
log("used password file as encryption key")
if key is None:
raise Exception("failed to load encryption keyfile %s" % self.encryption_keyfile)
return key.strip("\n\r")
def load_password(self):
filename = os.path.expanduser(self.password_file)
password = load_binary_file(filename)
if password is None:
return None
password = password.strip("\n\r")
log("password read from file %s is %s", self.password_file, "".join(["*" for _ in password]))
return password
def _process_hello(self, packet):
if not self.password_sent and self.password_file:
self.warn_and_quit(EXIT_NO_AUTHENTICATION, "the server did not request our password")
return
try:
self.server_capabilities = packet[1]
log("processing hello from server: %s", self.server_capabilities)
c = typedict(self.server_capabilities)
self.parse_server_capabilities(c)
except Exception, e:
self.warn_and_quit(EXIT_FAILURE, "error processing hello packet from server: %s" % e)
示例3: XpraClientBase
# 需要导入模块: from xpra.net.protocol import Protocol [as 别名]
# 或者: from xpra.net.protocol.Protocol import set_cipher_out [as 别名]
#.........这里部分代码省略.........
(bool(self._priority_packets) or bool(self._ordinary_packets) \
or self._mouse_position is not None)
return packet, None, None, has_more
def cleanup(self):
log("XpraClientBase.cleanup() protocol=%s", self._protocol)
if self._protocol:
self._protocol.close()
self._protocol = None
def glib_init(self):
try:
glib = import_glib()
try:
glib.threads_init()
except AttributeError:
#old versions of glib may not have this method
pass
except ImportError:
pass
def run(self):
self._protocol.start()
def quit(self, exit_code=0):
raise Exception("override me!")
def warn_and_quit(self, exit_code, warning):
log.warn(warning)
self.quit(exit_code)
def _process_disconnect(self, packet):
if len(packet)==2:
info = packet[1]
else:
info = packet[1:]
e = EXIT_OK
if self.server_capabilities is None or len(self.server_capabilities)==0:
#server never sent hello to us - so disconnect is an error
#(but we don't know which one - the info message may help)
e = EXIT_FAILURE
self.warn_and_quit(e, "server requested disconnect: %s" % info)
def _process_connection_lost(self, packet):
self.warn_and_quit(EXIT_CONNECTION_LOST, "Connection lost")
def _process_challenge(self, packet):
if not self.password_file and not self.password:
self.warn_and_quit(EXIT_PASSWORD_REQUIRED, "password is required by the server")
return
if not self.password:
if not self.load_password():
return
assert self.password
salt = packet[1]
if self.encryption:
assert len(packet)>=3, "challenge does not contain encryption details to use for the response"
server_cipher = packet[2]
self.set_server_encryption(server_cipher)
import hmac
challenge_response = hmac.HMAC(self.password, salt)
password_hash = challenge_response.hexdigest()
self.password_sent = True
self.send_hello(password_hash)
def set_server_encryption(self, capabilities):
def get(key, default=None):
return capabilities.get(strtobytes(key), default)
cipher = get("cipher")
cipher_iv = get("cipher.iv")
key_salt = get("cipher.key_salt")
iterations = get("cipher.key_stretch_iterations")
if not cipher or not cipher_iv:
self.warn_and_quit(EXIT_ENCRYPTION, "the server does not use or support encryption/password, cannot continue with %s cipher" % self.encryption)
return False
if cipher not in ENCRYPTION_CIPHERS:
self.warn_and_quit(EXIT_ENCRYPTION, "unsupported server cipher: %s, allowed ciphers: %s" % (cipher, ", ".join(ENCRYPTION_CIPHERS)))
return False
self._protocol.set_cipher_out(cipher, cipher_iv, self.get_password(), key_salt, iterations)
def get_password(self):
if self.password is None:
self.load_password()
return self.password
def load_password(self):
try:
filename = os.path.expanduser(self.password_file)
passwordFile = open(filename, "rU")
self.password = passwordFile.read()
passwordFile.close()
while self.password.endswith("\n") or self.password.endswith("\r"):
self.password = self.password[:-1]
except IOError, e:
self.warn_and_quit(EXIT_PASSWORD_FILE_ERROR, "failed to open password file %s: %s" % (self.password_file, e))
return False
log("password read from file %s is %s", self.password_file, self.password)
return True