Skip to content

Instantly share code, notes, and snippets.

@kamenarov
Last active February 28, 2022 10:34
Show Gist options
  • Save kamenarov/15c20ccedb58b53d4cd338fc262ac5c2 to your computer and use it in GitHub Desktop.
Save kamenarov/15c20ccedb58b53d4cd338fc262ac5c2 to your computer and use it in GitHub Desktop.
Mikrotik VPN / Specific websites and devices / AdBlocker / RouterOS v7

Info: My network IP addresses are in range: 10.0.0.0 - 10.0.0.256

Add VPN

No description here

For example I'm using as a name NameVPN for the VPN connection

#Add Route table Routing>Tables>Add New Enabled - Checked Name: TableVPN FIB - Checked

#Add IP routes and rules IP>Routes>Add New Dst. Address: 0.0.0.0/0 Gateway: %NameVPN Distance: 1 Routing Table: TableVPN

#Add AddressLists of Target Devices and Target Websites IP>Firewall>Address Lists>Add New

Name: Devices Address: 10.0.0.0/24 (all network IP's)

Same opperation for the Target Websites

The Difference is that you are changing the name and on address instead of IP, there you fill the website URL. It's importat to be added the exact website URL, sub-domains have to be added separately in the list.

Name: Target Address: www.youtube.com

#Add NAT IP>Firewall>NAT>Add New Chain: srcnat Out. Interface: NameVPN Action: masquerade Comment: VPN //Optional

#Add Mangle IP>Firewall>Mangle>Add New Chain: prerouting Src. Address List: Devices Dst. Address List: Target Action: mark routing New Routing Mark: TableVPN Passthrough: Checked Comment: VPN //Optional

#Clean DNS Cache // Optional IP>DNS>Cache>Flush Cache

Reconnect to VPN on startup

System>Scripts>Add New Name: Startup Policy: IDK, all pre-selected. Source: /interface enable NameVPN

System>Scheduler>Add New Name: Startup Start Time: startup Interval: 00:00:00 (This will run once 3 seconds after the router boot) Policy: IDK, all pre-selected. On Event: /system script run Startup


AdBlocker

System>Scripts>Add New

Name: AdBlocker Policy: Read, Write, Policy, Test Source:

## StopAD - Script for blocking advertisements, based on your defined hosts files
## For changing any parameters, please, use this link: https://stopad.hook.sh/
##
## @github    <https://github.com/tarampampam/mikrotik-hosts-parser>
## @version   4.4.0
##
## Setup this Policy for script: [X] Read [X] Write [X] Policy [X] Test

:local hostScriptUrl "https://stopad.hook.sh/script/source?format=routeros&version=4.4.0&redirect_to=127.0.0.1&limit=5000&sources_urls=https%3A%2F%2Fcdn.jsdelivr.net%2Fgh%2Ftarampampam%2Fmikrotik-hosts-parser%40master%2F.hosts%2Fbasic.txt,https%3A%2F%2Fadaway.org%2Fhosts.txt,https%3A%2F%2Fwww.malwaredomainlist.com%2Fhostslist%2Fhosts.txt,https%3A%2F%2Fpgl.yoyo.org%2Fadservers%2Fserverlist.php%3Fhostformat%3Dhosts%26showintro%3D0%26mimetype%3Dplaintext&excluded_hosts=localhost,localhost.localdomain,broadcasthost,local,ip6-localhost,ip6-loopback,ip6-localnet,ip6-mcastprefix,ip6-allnodes,ip6-allrouters,ip6-allhosts";
:local scriptName "stop_ad.script";
:local backupFileName "before_stopad";
:local logPrefix "[StopAD]";

do {
  /tool fetch check-certificate=no mode=https url=$hostScriptUrl dst-path=("./".$scriptName);
  :delay 3s;
  :if ([:len [/file find name=$scriptName]] > 0) do={
    /system backup save name=$backupFileName;
    :delay 1s;
    :if ([:len [/file find name=($backupFileName.".backup")]] > 0) do={
      /ip dns static remove [/ip dns static find comment=ADBlock];
      /import file-name=$scriptName;
      /file remove $scriptName;
      :log info "$logPrefix AD block script imported, backup file (\"$backupFileName.backup\") created";
    } else={
      :log warning "$logPrefix Backup file not created, importing AD block script stopped";
    }
  } else={
    :log warning "$logPrefix AD block script not downloaded, script stopped";
  }
} on-error={
  :log warning "$logPrefix AD block script download FAILED";
};

System>Scheduler>Add New Name: AdBlocker Update Start Time: Startup Interval: 30d 00:00:00 ( every 30 days will update ) Policy: Read, Write, Policy, Test On Event: /system script run AdBlocker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment