/* Ċ 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 . Home Screen Activity . Global Variable . DEBUG Ċ import Ġcom . cel zero . br aved ns . util . Constants . Companion . UNSPECIFIED _ PORT Ċ import Ġcom . cel zero . br aved ns . util . Logger Constants . Companion . LOG _ TAG _ FIRE WALL Ċ import Ġcom . google . common . collect . Hash Multimap Ċ import Ġcom . google . common . collect . Set Multimap Ċ import Ġkotlinx . coroutines . Coroutine Scope Ċ import Ġkotlinx . coroutines . Dispatch ers Ċ import Ġkotlinx . coroutines . launch Ċ Ċ object ĠFire wall Rules Ġ{ ĊĊĠĠĠ Ġvar Ġapp Ip Rules : ĠSet Multimap < Int , ĠString > Ġ= ĠHash Multimap . create () ĊĊĠĠĠ Ġ// ĠThe ĠUID Ġused Ġto Ġbe Ġgeneric Ġuid Ġused Ġto Ġblock ĠIP Ġaddresses Ġwhich Ġare Ġintended Ġto ĊĠĠĠ Ġ// Ġblock Ġfor Ġall Ġthe Ġapplications . ĊĠĠĠ Ġconst Ġval ĠUID _ E VERY BODY Ġ= Ġ- 1 0 0 0 ĊĊĠĠĠ Ġfun Ġclear Firewall Rules ( uid : ĠInt , Ġblocked Connections Repository : ĠBlock ed Connections Repository ) Ġ{ ĊĠĠĠĠĠĠĠ Ġio Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġblocked Connections Repository . clear Firewall Rules ( uid ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġapp Ip Rules . removeAll ( uid ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġremove Firewall Rules ( uid : ĠInt , Ġip Address : ĠString ?, ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġblocked Connections Repository : ĠBlock ed Connections Repository ) Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( DEBUG ) ĠLog . d ( LOG _ TAG _ FIRE WALL , Ġ" Remove ĠFire wall : Ġ$ uid , Ġ$ ipAddress ") ĊĠĠĠĠĠĠĠ Ġif Ġ( ipAddress . isNull OrEmpty ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġio Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( uid Ġ== ĠUID _ E VERY BODY ) Ġblocked Connections Repository . delete IP Rules Universal ( ipAddress ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġelse Ġblocked Connections Repository . delete IP Rules For UID ( uid , Ġip Address ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġapp Ip Rules . remove ( uid , Ġip Address ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġadd Firewall Rules ( uid : ĠInt , Ġip Address : ĠString , Ġrule Type : ĠString , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġblocked Connections Repository : ĠBlock ed Connections Repository ) Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( DEBUG ) ĠLog . d ( LOG _ TAG _ FIRE WALL , Ġ" add Firewall Rules : Ġ$ uid , Ġ$ ipAddress ") ĊĠĠĠĠĠĠĠ Ġio Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġblocked Connection Ġ= Ġconstruct Blocked Connections ( uid , Ġip Address , Ġrule Type ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġblocked Connections Repository . insert ( blocked Connection ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġapp Ip Rules . put ( uid , Ġip Address ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġhas Rule ( uid : ĠInt , Ġrules : ĠConnection Rules ): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġapp Ip Rules . get ( uid ). contains ( rules . ipAddress ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġclear All Ip Rules ( blocked Connections Repository : ĠBlock ed Connections Repository ) Ġ{ ĊĠĠĠĠĠĠĠ Ġio Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġblocked Connections Repository . delete All IP Rules Universal () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġapp Ip Rules . clear () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġload Firewall Rules ( blocked Connections Repository : ĠBlock ed Connections Repository ) Ġ{ ĊĠĠĠĠĠĠĠ Ġio Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġrules Ġ= Ġblocked Connections Repository . getBlock ed Connections () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġrules . forEach Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġkey Ġ= Ġit . uid ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġapp Ip 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 Ġ= ĠUNS PE CI FIED _ PORT ĊĠĠĠĠĠĠĠ Ġ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 ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġio ( f : Ġ() Ġ-> ĠUnit ) Ġ{ ĊĠĠĠĠĠĠĠ ĠCor outine Scope ( Dispatch ers . IO ). launch Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġf () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ Ċ } Ċ