/* Ċ Copyright Ġ 2 0 2 0 ĠRe think DNS Ġand Ġits Ġauthors Ċ Ċ Licensed Ġunder Ġthe ĠApache ĠLicense , ĠVersion Ġ 2 . 0 Ġ( the Ġ" License "); Ċ you Ġmay Ġnot Ġuse Ġthis Ġfile Ġexcept Ġin Ġcompliance Ġwith Ġthe ĠLicense . Ċ You Ġmay Ġobtain Ġa Ġcopy Ġof Ġthe ĠLicense Ġat Ċ Ċ https :// www . apache . org / licenses / LICENSE - 2 . 0 Ċ Ċ Unless Ġrequired Ġby Ġapplicable Ġlaw Ġor Ġagreed Ġto Ġin Ġwriting , Ġsoftware Ċ distributed Ġunder Ġthe ĠLicense Ġis Ġdistributed Ġon Ġan Ġ" AS ĠIS " ĠBASIS , Ċ WITHOUT ĠWARRANTIES ĠOR ĠCONDITIONS ĠOF ĠANY ĠKIND , Ġeither Ġexpress Ġor Ġimplied . Ċ See Ġthe ĠLicense Ġfor Ġthe Ġspecific Ġlanguage Ġgoverning Ġpermissions Ġand Ċ limitations Ġunder Ġthe ĠLicense . Ċ */ Ċ package Ġcom . cel zero . br aved ns . autom aton Ċ Ċ import Ġandroid . util . Log Ċ import Ġcom . cel zero . br aved ns . data . Connection Rules Ċ import Ġcom . cel zero . br aved ns . database . Blocked Connections Ċ import Ġcom . cel zero . br aved ns . database . Blocked Connections Repository Ċ import Ġcom . cel zero . br aved ns . ui . Conn Tracker Bottom Sheet Fragment Ċ import Ġcom . cel zero . br aved ns . ui . Home Screen Activity . Global Variable . DEBUG Ċ import Ġcom . cel zero . br aved ns . ui . Home Screen Activity . Global Variable . firewall Rules Ċ import Ġcom . cel zero . br aved ns . util . Constants . Companion . LOG _ TAG Ċ import Ġkotlinx . coroutines . Dispatch ers Ċ import Ġkotlinx . coroutines . Global Scope Ċ import Ġkotlinx . coroutines . launch Ċ Ċ class ĠFire wall Rules Ġ{ ĊĊĠĠĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠĠĠĠĠ Ġvar Ġfirewall Rules Obj : ĠFire wall Rules ? Ġ= Ġnull ĊĠĠĠĠĠĠĠ Ġfun ĠgetInstance (): ĠFire wall Rules Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( firewall Rules Obj Ġ== Ġnull ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġfirewall Rules Obj Ġ= ĠFire wall Rules () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn Ġfirewall Rules Obj Ġas ĠFire wall Rules ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġclear Firewall Rules ( uid Ġ: ĠInt , Ġblocked Connections Repository : Blocked Connections Repository ) Ġ{ ĊĠĠĠĠĠĠĠ ĠGlobal Scope . launch ( Dispatch ers . IO ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġblocked Connections Repository . clear Firewall Rules ( uid ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġfirewall Rules . removeAll ( uid ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġremove Firewall Rules ( uid : ĠInt , Ġip Address : ĠString , Ġblocked Connections Repository : Blocked Connections Repository ) Ġ{ ĊĠĠĠĠĠĠĠ Ġif ( DEBUG ) ĠLog . d ( LOG _ TAG ," Remove ĠFire wall : Ġ$ uid , Ġ$ ipAddress ") ĊĠĠĠĠĠĠĠ ĠGlobal Scope . launch ( Dispatch ers . IO ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif ( uid Ġ== ĠConn Tracker Bottom Sheet Fragment . UN IVER SAL _ R ULES _ UID ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġblocked Connections Repository . delete IP Rules Universal ( ipAddress ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġelse ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġblocked Connections Repository . delete IP Rules For UID ( uid , Ġip Address ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġfirewall Rules . remove ( uid , Ġip Address ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġadd Firewall Rules ( uid : ĠInt , Ġip Address : ĠString , Ġrule Type : ĠString , Ġblocked Connections Repository : Blocked Connections Repository ) Ġ{ ĊĠĠĠĠĠĠĠ Ġif ( DEBUG ) ĠLog . d ( LOG _ TAG ," add Firewall Rules : Ġ$ uid , Ġ$ ipAddress ") ĊĠĠĠĠĠĠĠ ĠGlobal Scope . launch ( Dispatch ers . IO ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġblocked Connection Ġ= Ġconstruct Blocked Connections ( uid , Ġip Address , rule Type ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġblocked Connections Repository . insert Async ( blocked Connection ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġfirewall Rules . put ( uid , Ġip Address ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġcheck Rules ( uid : ĠInt , Ġconnection Rules : ĠConnection Rules ): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrule Ġ= Ġfirewall Rules [ uid ] ĊĠĠĠĠĠĠĠ Ġrule ?. forEach Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( it !! Ġ== Ġconnection Rules . ipAddress ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġreturn Ġtrue ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġreturn Ġfalse ĊĠĠĠ Ġ} ĊĊĊĠĠĠ Ġfun Ġload Firewall Rules ( blocked Connections Repository : Blocked Connections Repository ) Ġ{ ĊĠĠĠĠĠĠĠ ĠGlobal Scope . launch ( Dispatch ers . IO ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġdb Val Ġ= Ġblocked Connections Repository . getBlock ed Connections () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġdb Val . forEach Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġkey Ġ= Ġit . uid ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġfirewall Rules . put ( key , Ġit . ipAddress ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġconstruct Blocked Connections ( uid : ĠInt , Ġip Address : ĠString , Ġrule Type Ġ: ĠString ): ĠBlock ed Connections Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġblocked Connections Ġ= ĠBlock ed Connections () ĊĠĠĠĠĠĠĠ Ġblocked Connections . ipAddress Ġ= Ġip Address ĊĠĠĠĠĠĠĠ Ġblocked Connections . port Ġ= Ġ 0 ĊĠĠĠĠĠĠĠ Ġblocked Connections . protocol Ġ= Ġ"" ĊĠĠĠĠĠĠĠ Ġblocked Connections . isActive Ġ= Ġtrue ĊĠĠĠĠĠĠĠ Ġblocked Connections . modified DateTime Ġ= ĠSystem . currentTimeMillis () ĊĠĠĠĠĠĠĠ Ġblocked Connections . rule Type Ġ= Ġrule Type ĊĠĠĠĠĠĠĠ Ġblocked Connections . uid Ġ= Ġuid ĊĠĠĠĠĠĠĠ Ġreturn Ġblocked Connections ĊĠĠĠ Ġ} Ċ Ċ }