l#g9NddlZddlZddlZddlZddlZddlZddlZddlZddlZej d Z ej d Z ej d Z ej d ZejeZGddeZGd d eZGd d ZeZGd dZeZdZGddZGddeZddZdS)N)minutes)hours)days)weeksceZdZdZdZdZdZdZdZdZ Gdd e Z Gd d e Z Gd d e Z Gdde ZdS) ServiceBasezBase service class.cd||_d|_d|_|||_dSNF)_loop _should_stop _main_task StoppedState_state)selfloops Q/opt/imunify360/venv/lib/python3.11/site-packages/defence360agent/utils/common.py__init__zServiceBase.__init__s1 !''-- c4|jSN)rstartrs rrzServiceBase.starts{  """rc4|jSr)r should_stoprs rrzServiceBase.should_stops{&&(((rcDK|jd{VSr)rwaitrs rrzServiceBase.wait"s,[%%'''''''''rc4|jSr)r is_runningrs rrzServiceBase.is_running%s{%%'''rcKtr)NotImplementedErrorrs r_runzServiceBase._run(s !!rc,eZdZdZdZdZdZdZdS)ServiceBase.Statec||_dS)z:type obj: ServiceBaseN)_objrobjs rrzServiceBase.State.__init__,s DIIIrcdSrrs rrzServiceBase.State.start0 DrcdSrr*rs rrzServiceBase.State.should_stop3r+rc:K|jj}|r |d{VdSdSr)r&r)rtasks rrzServiceBase.State.wait6s79'D    rcdSr r*rs rrzServiceBase.State.is_running;s5rN)__name__ __module__ __qualname__rrrrrr*rrStater$+s_                  rr3ceZdZdZdZdS)ServiceBase.StoppedStatecpt|j|j_d|j_dSr )r rr&rr )rfutures r_on_stopz!ServiceBase.StoppedState._on_stop?s,*77 BBDI %*DI " " "rc|j}|j||_|j|jt||_ dSr) r&r create_taskr"radd_done_callbackr8r RunningStaterr's rrzServiceBase.StoppedState.startCsY)C Y22388::>>CN N , ,T] ; ; ;$11#66CJJJrN)r0r1r2r8rr*rrrr5>s2 + + + 7 7 7 7 7rrceZdZdZdZdS)ServiceBase.RunningStatec|j}d|_|jt||_dSNT)r&r rcancelr StoppingStaterr's rrz$ServiceBase.RunningState.should_stopJs>)C#C  N ! ! # # #$22377CJJJrcdSr@r*rs rrz#ServiceBase.RunningState.is_runningPs4rN)r0r1r2rrr*rrr<r>Is2 8 8 8      rr<ceZdZdZdS)ServiceBase.StoppingStatec td)Nz9Cannot start stopping service. Please wait while it stop.)ProgrammingErrorrs rrzServiceBase.StoppingState.startTs"K rN)r0r1r2rr*rrrBrESs#     rrBN)r0r1r2__doc__rrrrrr"objectr3rr<rBr*rrr r s... ###)))(((((("""& 7 7 7 7 7u 7 7 7urr ceZdZdS)rGN)r0r1r2r*rrrGrGZsDrrGcHeZdZdZejfdddZedZdZ dS) RateLimita3Decorator to limit function calls to one per *period* seconds. If less than *period* seconds have passed since the last call, then the request to call the function is replace with an *on_drop* call with the same arguments. If *on_drop* is None [default] then the call is just dropped N)on_dropc>d|_||_||_||_dSr)_next_call_time_period_timer_on_drop)rperiodtimerrMs rrzRateLimit.__init__is##   rcN|jdup|j|kSr)rOrQrs rshould_be_calledzRateLimit.should_be_calledos,  D ( 5#t{{}}4 rctjfd}tjfd}tjr|n|S)Ncjr)jz_|i|Sj j|i|SdSrrVrQrPrOrRargskwargsfuncrs rwrapperz#RateLimit.__call__..wrapperwsa$ 6'+{{}}t|'C$tT,V,,,*$t}d5f555+*rcKjr/jz_|i|d{VSj j|i|SdSrrYrZs r async_wrapperz)RateLimit.__call__..async_wrappersw$ 6'+{{}}t|'C$!T426222222222*$t}d5f555+*r) functoolswrapsasyncioiscoroutinefunction)rr]r^r`s`` r__call__zRateLimit.__call__vs    6 6 6 6 6   6    6 6 6 6 6   6!( ;D A AN}}wNr) r0r1r2rHtime monotonicrpropertyrVrer*rrrLrL^st&*^        X OOOOOrrLc eZdZdZdddZdS) CoalesceCallsc<td|_d|_dS)Nz-inf)float call_time delayed_callrs rrzCoalesceCalls.__init__sv rN) done_callbackcfd}|S)a Decorator to coalesce coroutine calls to one per *period* seconds. Requests for a coroutine call in a given time period are coalesced: If t is the time of the last call, then N call requests in the [t, t+period) time interval results in a single call at the t+period time iff N>0 i.e., if less than *period* seconds have passed since the last call, then the calls are coalesced: (N-1) requests are dropped, Nth requests is performed in *period* seconds. It is unspecified which exact call is made if arguments differ. If the call is not dropped then *done_callback* is attached to the task when the coroutine is scheduled with the event loop. Given `c` is the time of the last [actual] call (`loop.create_task()`) And `T` is the coalesce time period When a call request arrives at `t` time Then | call pending? | t>c+T | c<=t<=c+T | t.decorator..wrapper..log_exceptions}  >>++0@0@0L33+H"+,,-1^^-=-=(, 0L0Lrctd_d_||i|}|ndS)z*Call & schedule the delayed coroutine now.zSchedule call %sN)loggerinforfrmrnr:r;) coror[r\r.r|ror}rrs r call_delayedzVCoalesceCalls.coalesce_calls..decorator..wrapper..call_delayedsKK 2I>>>%)YY[[DN(,D%++DD$,A&,A,ABBD**(0& *rziThere was a scheduled call (%s) but more than period (%r) seconds passed since the last call (%r, now=%r)zSSatisfy the call request soon: %s. No calls in more than %r seconds since the startz`Drop call request for %s, enforcing one call per %r seconds limit. Next call is in ~%.2f secondszQDelay call request: %s for ~%.2f seconds. Enforcing one call per %r seconds limitzNDrop call request for %s, reason: last call time (%r, now=%r) is in the future)getrcget_event_loopr0rfrmrnstrrArwarningr call_sooncall_at)r[r\ args_reprrnowold_delayed_call_reprdelayr|r}rrrorSrs @@@rr^z@CoalesceCalls.coalesce_calls..decorator..wrapperszz&))<"133D#6#/3ttVV &"!  $0888 H%!" -1LL NV3(  " --)))NN9! r)rarb)rr^rorSrs` r decoratorz/CoalesceCalls.coalesce_calls..decoratorsQ _T " "c c c c c c c # "c JNrr*)rrSrors``` rcoalesce_callszCoalesceCalls.coalesce_callss88g g g g g g g Rr)r0r1r2rrr*rrrjrjsH!!!7;EEEEEEErrjctj}|'|drtjS|S)ziReturns readable name of the server. It is sent to CLN and allows user to sort out his servers. N localhost)socketgetfqdnlower startswith gethostname)hostnames r get_hostnamersH ~H8>>++66{CC!### Orc>eZdZdZd dZdZdZdZdZdZ d Z dS) VersionzAbstract base class for version numbering classes. Just provides constructor (__init__) and reproducer (__repr__), because those seem to be the same for all version numbering classes; and route rich comparisons to _cmp. Nc8|r||dSdSr)parse)rvstrings rrzVersion.__init__3s,  JJw       rc\d|jjt|S)Nz {} ('{}'))format __class__r0rrs r__repr__zVersion.__repr__7s#!!$."93t99EEErcN||}|tur|S|dkSNr_cmpNotImplementedrothercs r__eq__zVersion.__eq__:, IIe     HAv rcN||}|tur|S|dkSrrrs r__lt__zVersion.__lt__@, IIe     H1u rcN||}|tur|S|dkSrrrs r__le__zVersion.__le__FrrcN||}|tur|S|dkSrrrs r__gt__zVersion.__gt__LrrcN||}|tur|S|dkSrrrs r__ge__zVersion.__ge__Rrrr) r0r1r2rHrrrrrrrr*rrrr,s     FFF    rrcVeZdZdZejdejZdZdZ dZ dZ dS) LooseVersionaVersion numbering for anarchists and software realists. Implements the standard interface for version number classes as described above. A version number consists of a series of numbers, separated by either periods or strings of letters. When comparing version numbers, the numeric components will be compared numerically, and the alphabetic components lexically. The following are all valid version numbers, in no particular order: 1.5.1 1.5.2b2 161 3.10a 8.02 3.4j 1996.07.12 3.2.pl0 3.1.1.6 2g6 11g 0.960923 2.2beta29 1.13++ 5.5.kw 2.0b1pl0 In fact, there is no such thing as an invalid version number under this scheme; the rules for comparison are simple and predictable, but may not always give the results you want (for some definition of "want"). z(\d+ | [a-z]+ | \.)c||_d|j|D}t|D](\}} t |||<#t $rY%wxYw||_dS)Nc"g|] }|r|dk | S).r*).0xs r z&LooseVersion.parse..s,   1 ABcAr)r component_resplit enumerateint ValueErrorversion)rr componentsir(s rrzLooseVersion.parse}s   (..w77    ++  FAs  #C 1     " sA A! A!c|jSr)rrs r__str__zLooseVersion.__str__s |rc&dt|zS)NzLooseVersion ('%s'))rrs rrzLooseVersion.__repr__s$s4yy00rct|trt|}nt|tstS|j|jkrdS|j|jkrdS|j|jkrdSdS)Nrr) isinstancerrrr)rrs rrzLooseVersion._cmps eS ! ! " ''EEE<00 "! ! <5= ( (1 <%- ' '2 <%- ' '1 ( 'rN) r0r1r2rHrecompileVERBOSErrrrrr*rrrrZsr>2:4bjAAL""" 111     rrcRtj|\}}tjdkr |dkr|dz}tj|r|S|[tjdd}|9 tjd}n##ttf$rtj }YnwxYw|sdS| tj }|D]E}tj||}tj|r|cSFdS)zTries to find 'executable' in the directories listed in 'path'. A string listing directories separated by 'os.pathsep'; defaults to os.environ['PATH']. Returns the complete filename or None if not found. win32z.exeNPATHCS_PATH)ospathsplitextsysplatformisfileenvironrconfstrAttributeErrorrdefpathrpathsepjoin) executabler_extpathspfs rfind_executablers) W  j ) )FAs cVmm&(  w~~j!! |z~~fd++ < "z),,"J/ " " "z " t JJrz " "E  GLLJ ' ' 7>>!   HHH  4sBB98B9r)rcdatetimeraloggingrrfrrr timedelta total_secondsMINUTEHOURDAYWEEK getLoggerr0rrIr ExceptionrGrL rate_limitrjwebserver_gracefull_restartrrrrr*rrrsL  A & & & 4 4 6 6x"""0022ha   ..00x"""0022  8 $ $DDDDD&DDDN     y   )O)O)O)O)O)O)O)OX JJJJJJJJZ,moo********\DDDDD7DDDP""""""r