def __init__(self, code=CoARequest, id=None, secret=six.b(''), authenticator=None, **attributes): CoAPacket.__init__(self, code, id, secret, authenticator, **attributes) self.deferred = Deferred() self.source_user = None self.vendor_id = 0 self.client_macaddr = None self.created = datetime.datetime.now()
def __new__(cls, secret, dict, packet: bytes): response = CoAPacket(code=0, id=0, secret=secret, authenticator=None, dict=dict, packet=packet) # TODO 这里解析报文两次 if response.code in [ PacketCode.CODE_DISCONNECT_ACK, PacketCode.CODE_DISCONNECT_NAK ]: return DmResponse(secret=secret, packet=packet, dict=dict) if response.code in [PacketCode.CODE_COA_ACK, PacketCode.CODE_COA_NAK]: return CoAResponse(secret=secret, packet=packet, dict=dict) raise Exception(f'DAE response not support code: {response.code}')
def CreateCoAPacket(self, **args): """Create a new RADIUS packet. This utility function creates a new RADIUS packet which can be used to communicate with the RADIUS server this client talks to. This is initializing the new packet with the dictionary and secret used for the client. :return: a new empty packet instance :rtype: pyrad.packet.Packet """ if not self.protocol_acct: raise Exception('Transport not initialized') return CoAPacket(id=self.protocol_coa.create_id(), dict=self.dict, secret=self.secret, **args)
def datagram_received(self, data, addr): self.logger.debug('[%s:%d] Received %d bytes from %s', self.ip, self.port, len(data), addr) receive_date = datetime.utcnow() if addr[0] in self.hosts: remote_host = self.hosts[addr[0]] elif '0.0.0.0' in self.hosts: remote_host = self.hosts['0.0.0.0'] else: self.logger.warn('[%s:%d] Drop package from unknown source %s', self.ip, self.port, addr) return try: self.logger.debug('[%s:%d] Received from %s packet: %s', self.ip, self.port, addr, data.hex()) req = Packet(packet=data, dict=self.server.dict) except Exception as exc: self.logger.error('[%s:%d] Error on decode packet: %s', self.ip, self.port, exc) return try: if req.code in (AccountingResponse, AccessAccept, AccessReject, CoANAK, CoAACK, DisconnectNAK, DisconnectACK): raise ServerPacketError('Invalid response packet %d' % req.code) elif self.server_type == ServerType.Auth: if req.code != AccessRequest: raise ServerPacketError( 'Received non-auth packet on auth port') req = AuthPacket(secret=remote_host.secret, dict=self.server.dict, packet=data) if self.server.enable_pkt_verify: if req.VerifyAuthRequest(): raise PacketError('Packet verification failed') elif self.server_type == ServerType.Coa: if req.code != DisconnectRequest and req.code != CoARequest: raise ServerPacketError( 'Received non-coa packet on coa port') req = CoAPacket(secret=remote_host.secret, dict=self.server.dict, packet=data) if self.server.enable_pkt_verify: if req.VerifyCoARequest(): raise PacketError('Packet verification failed') elif self.server_type == ServerType.Acct: if req.code != AccountingRequest: raise ServerPacketError( 'Received non-acct packet on acct port') req = AcctPacket(secret=remote_host.secret, dict=self.server.dict, packet=data) if self.server.enable_pkt_verify: if req.VerifyAcctRequest(): raise PacketError('Packet verification failed') # Call request callback self.request_callback(self, req, addr) except Exception as exc: if self.server.debug: self.logger.exception('[%s:%d] Error for packet from %s', self.ip, self.port, addr) else: self.logger.error('[%s:%d] Error for packet from %s: %s', self.ip, self.port, addr, exc) process_date = datetime.utcnow() self.logger.debug('[%s:%d] Request from %s processed in %d ms', self.ip, self.port, addr, (process_date - receive_date).microseconds / 1000)
def datagram_received(self, data, addr): self.logger.debug('[%s:%d] Received %d bytes from %s', self.ip, self.port, len(data), addr) receive_date = datetime.utcnow() remote_host = self.__get_remote_host__(addr[0]) if remote_host: try: if self.server.debug: self.logger.info('[%s:%d] Received from %s packet: %s.' % (self.ip, self.port, addr, data.hex())) req = Packet(packet=data, dict=self.server.dict) except Exception as exc: self.logger.error( '[%s:%d] Error on decode packet: %s. Ignore it.' % (self.ip, self.port, exc)) req = None if not req: return try: if req.code in (AccountingResponse, AccessAccept, AccessReject, CoANAK, CoAACK, DisconnectNAK, DisconnectACK): raise ServerPacketError('Invalid response packet %d' % req.code) elif self.server_type == ServerType.Auth: if req.code != AccessRequest: raise ServerPacketError( 'Received not-authentication packet ' 'on authentication port') req = AuthPacket(secret=remote_host.secret, dict=self.server.dict, packet=data) if self.server.enable_pkt_verify and \ req.message_authenticator and \ not req.verify_message_authenticator(): raise PacketError( 'Received invalid Message-Authenticator') elif self.server_type == ServerType.Coa: if req.code != DisconnectRequest and \ req.code != CoARequest: raise ServerPacketError( 'Received not-coa packet on coa port') req = CoAPacket(secret=remote_host.secret, dict=self.server.dict, packet=data) if self.server.enable_pkt_verify: if not req.VerifyCoARequest(): raise PacketError('Packet verification failed') if req.message_authenticator and \ not req.verify_message_authenticator(): raise PacketError( 'Received invalid Message-Authenticator') elif self.server_type == ServerType.Acct: if req.code != AccountingRequest: raise ServerPacketError( 'Received not-accounting packet on ' 'accounting port') req = AcctPacket(secret=remote_host.secret, dict=self.server.dict, packet=data) if self.server.enable_pkt_verify: if not req.VerifyAcctRequest(): raise PacketError('Packet verification failed') if req.message_authenticator and not \ req.verify_message_authenticator(): raise PacketError( 'Received invalid Message-Authenticator') # Call request callback self.request_callback(self, req, addr) self.requests += 1 except Exception as e: self.logger.error( '[%s:%d] Unexpected error for packet from %s: %s' % (self.ip, self.port, addr, (e, '\n'.join(traceback.format_exc().splitlines()) )[self.server.debug])) else: self.logger.error('[%s:%d] Drop package from unknown source %s', self.ip, self.port, addr) process_date = datetime.utcnow() self.logger.debug('[%s:%d] Request from %s processed in %d ms', self.ip, self.port, addr, (process_date - receive_date).microseconds / 1000)