def try_auth_ntlm(self, challenge=b''): append_tmp = b'\r\nproxy-Authorization: NTLM %s\r\n\r\n' rep = self.ntlm.get_response(challenge.decode()).encode() append = append_tmp % (rep) tmp = self.server_protocol.cache.split(b'\r\n\r\n', 1) auth_data = tmp[0] + append if len(auth_data) == 2: auth_data += tmp[1] log.debug(self.fmt('auth send : ', auth_data)) self.transport.write(auth_data)
def data_received(self, data): try: log.debug(self.fmt('server receive : ', data)) if not self.method: self.first_request_hand(data) if not self.client_protocol or not self.client_protocol.is_ready: self.cache += data else: log.debug(self.fmt('request write : ', data)) self.client_protocol.transport.write(data) except Exception as e: log.exception(self.fmt(e, data)) self.transport.close()
def hand_connect(self, data): http_code = data.split(maxsplit=2)[1] if http_code == b'407': challenge = get_challenge(data) if challenge: self.try_auth_ntlm(challenge) return else: log.error(self.fmt('auth failed ! data: ', data)) self.transport.close() self.server_protocol.transport.close() self.is_ready = True self.server_protocol.transport.write(data) log.debug(self.fmt('response write : ', data))
def data_received(self, data): try: log.debug(self.fmt('client receive : ', data)) if not self.is_ready: return self.hand_connect(data) if self.server_protocol.transport.is_closing(): log.info(self.fmt('server transport is closing !')) self.transport.close() return log.debug(self.fmt('response write : ', data)) self.server_protocol.transport.write(data) except Exception as e: log.exception(self.fmt(e, data)) self.server_protocol.transport.close() self.transport.close()
def client_connetc_cb(self, future): log.debug(self.fmt('server client_connetc_cb')) _, self.client_protocol = future.result()