def generateSessions( packets: List[Packet], direction: AnyStr = 'bidirectional', sessionExtractor: Optional[Callable[[Packet], Tuple[AnyStr, AnyStr]]] = None ) -> Sessions: """ generate sessions from packets :param packets: packet list :param direction: 'unidirectional' or 'bidirectional'. If session extractor is not None, this param won't work :param sessionExtractor: Optional[Callable[[Packet], (AnyStr, AnyStr)]] :return: sessions """ if sessionExtractor is None: # use bidirectional session extractor as default # unidirectional session key is the bidirectional session key + direction sessionExtractor = defaultBidirectionalSessionExtractor # to mark the direction, we avoid using sessions provided by scapy sessions = defaultdict(list) for p in probar(packets, color=progressBarColor): sessionKey, pDirection = sessionExtractor(p) if direction == 'unidirectional': sessionKey = f'{sessionKey} {pDirection}' # add additional attribute on packet to mark the direction p.pDirection = pDirection sessions[sessionKey].append(p) return sessions
def generateFeatures(flows: Flows) -> Tuple[FeatureSet, List[AnyStr]]: """Generate Features According to Flows""" featureSet: FeatureSet = list() for flow in probar(flows, color=progressBarColor): flow: Flow features = flow2feature(flow) featureSet.append(features) return featureSet, FeatureExtractor.getAllFeatureNames()
def test_performance(): from tqdm import tqdm N = 5000000 print("probar:") for i in probar(range(N), color='1', time_interval=0.02): pass time.sleep(0.1) print('bar:') for idx, i in enumerate(range(N)): bar(idx, N, text=f'{idx+1}', color='5') print("tqdm:") for i in tqdm(range(N)): pass
def test_performance(): from tqdm import tqdm N = 50000000 print("probar:") for i in probar(range(N), time_interval=0.02, time_zone="Asia/Shanghai"):#color='1', pass time.sleep(0.1) # print('bar:') # for idx, i in enumerate(range(N)): # bar(idx, N, text='', color='5', time_zone="Europe/Brussels") # print("tqdm:") for i in tqdm(range(N)): pass
def packets2features( packets: List[Packet], direction: AnyStr = 'bidirectional', sessionExtractor: Optional[Callable[[Packet], Tuple[AnyStr, AnyStr]]] = None, flowTimeout=Flow.defaultFlowTimeout, activityTimeout=Flow.defaultActivityTimeout ) -> Tuple[FeatureSet, List[AnyStr]]: """ Take packets as input generate features :param packets: A list of packets :param direction: unidirectional or bidirectional :param sessionExtractor: session extractor :param flowTimeout: flow timeout in microseconds :param activityTimeout: activity timeout in microseconds :return: (Feature Set, Feature Names) """ aliveFlows, flows = dict(), list() featureSet: FeatureSet = list() Flow.defaultFlowTimeout, Flow.defaultActivityTimeout = flowTimeout, activityTimeout if sessionExtractor is None: # use bidirectional session extractor as default # unidirectional session key is the bidirectional session key + direction sessionExtractor = defaultBidirectionalSessionExtractor for p in probar(packets, color=progressBarColor): sessionKey, pDirection = sessionExtractor(p) if direction == 'unidirectional': sessionKey = f'{sessionKey} {pDirection}' # add additional attribute on packet to mark the direction p.pDirection = pDirection if sessionKey not in aliveFlows: aliveFlows[sessionKey] = Flow(sessionKey, p) else: flow = aliveFlows[sessionKey] success = flow.add(p) if not success: features = flow2feature(flow) featureSet.append(features) aliveFlows[sessionKey] = Flow(sessionKey, p) # flush alive flows to flows for sessionKey, aliveFlow in aliveFlows.items(): features = flow2feature(aliveFlow) featureSet.append(features) return featureSet, list(featureSet[0].keys())
def sessions2flows(sessions: Sessions, flowTimeout=Flow.defaultFlowTimeout, activityTimeout=Flow.defaultActivityTimeout) -> Flows: aliveFlows, flows = dict(), list() Flow.defaultFlowTimeout, Flow.defaultActivityTimeout = flowTimeout, activityTimeout # generate flows for sessionKey, session in probar(sessions.items(), color=progressBarColor): for p in session: if sessionKey not in aliveFlows: aliveFlows[sessionKey] = Flow(sessionKey, p) else: flow = aliveFlows[sessionKey] success = flow.add(p) if not success: flows.append(flow) aliveFlows[sessionKey] = Flow(sessionKey, p) # flush alive flows to flows for sessionKey, aliveFlow in aliveFlows.items(): flows.append(aliveFlow) # sort the flows flows.sort() return flows
def test_probar3(): for i in probar(range(1234), symbol_2="o"): time.sleep(0.01)
def test_probar2(): res = [i for i in probar(range(10), enum=False) ] print(res)
def test_probar1(): for idx, i in probar(range(1234), enum=True): time.sleep(0.0061)