Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # OUTPUT LOADBALANCE / SERVICE PUBLICATION
- # DYNAMIC IP ADDRESSES
- # [INTERNAL]
- NET: 192.168.99.0/24
- GW-MT: 192.168.99.1/24
- IFACE: lan-bridge
- HOST: 192.168.99.100
- PROTOCOL/PORT: TCP/51234
- # [FIREWALL ADDRESS-LIST]
- # LOCAL-NETWORKS
- /ip firewall address-list add address=192.168.99.0/24 list=alst-LOCAL-NETS
- # CHECK-GATEWAY HOSTS (one per ISP)
- /ip firewall address-list add address=8.8.4.4 list=alst-wan1-CHKGW
- /ip firewall address-list add address=8.8.8.8 list=alst-wan2-CHKGW
- # [IFACE-LIST]
- /interface list add comment="userconf: contains pppoe connected interfaces" name=pppoe-conn
- /interface list add comment="userconf: contains wan interfaces" include=pppoe-conn name=wan
- # [ISP1]
- # 1. WAN1 (PPPoE-client)
- # 1.1 Interface configuration
- /interface ethernet set [ find default-name=ether1 ] comment="=EXT1-LNK="
- /ppp profile add address-list=alst-PPPOE-CONN change-tcp-mss=yes interface-list=pppoe-conn name=pppoe-wan1 remote-address=10.64.64.64 use-compression=no use-encryption=no use-mpls=no use-upnp=no
- # 1.2 Scripts
- # 1.2.1 --- PPP-UP-SCRIPT ---
- # Copy to on-up="" section of /ppp profile configuration
- # Main
- :local DEBUG 0;
- :local pppAddr $"local-address" ;
- :local pppGateWay $"remote-address" ;
- :local pppIfaceNum $"interface" ;
- :local pppIface [ interface get $pppIfaceNum name ];
- :local rtName ( "rt-" . $pppIface );
- :local rtRuleComment ( "pppoe-" . $pppIface );
- :local alstRoutesName ("alst-" . $pppIface . "-CHKGW" );
- # Debug info
- :log info ( "ppp-up: running custom script on interface up" );
- :if ( $DEBUG=1 ) do={
- :log info ( "ppp-up: address - $pppAddr / gateway - $pppGateWay / iface - $pppIface" );
- :log info ( "ppp-up: rt - $rtName / comment - $rtRuleComment / alst - $alstRoutesName" );
- }
- # Load functions
- :do { /system script run ros-functions } on-error={
- :log error ( "ppp-up: can't load necessery functions" );
- :error "[ERR]: can't load necessery functions - inconsistent list of scrips" ;
- }
- # Add route rules for connected routes
- :global funcAddRouteRuleConn;
- $funcAddRouteRuleConn $pppAddr $rtName $rtRuleComment
- # Add static routes if address-list present
- :global funcAddStaticRoute;
- :if ( [ len [ /ip firewall address-list find list="$alstRoutesName" ] ] !=0 ) do={
- foreach number in=[ /ip firewall address-list find list="$alstRoutesName" ] do={
- :local dst [ ip firewall address-list get $number address ];
- $funcAddStaticRoute $dst $pppGateWay 1 10 $rtRuleComment
- }
- }
- # Environment cleanup
- :do { /system script environment remove [ find name~"func" ] } on-error={
- :log warning ( "ppp-up: script environment cleanup is failed" );
- }
- # End
- # 1.2.2 --- PPP-DOWN-SCRIPT ---
- # Copy to on-down="" section of /ppp profile configuration
- # Main
- :local DEBUG 0;
- :local pppAddr $"local-address" ;
- :local pppGateWay $"remote-address" ;
- :local pppIfaceNum $"interface" ;
- :local pppIface [ interface get $pppIfaceNum name ];
- :local rtName ( "rt-" . $pppIface );
- :local rtRuleComment ( "pppoe-" . $pppIface );
- # Debug info
- :log info ( "ppp-down: running custom script on interface down" );
- :if ( $DEBUG=1 ) do={
- :log info ( "ppp-down: address - $pppAddr / gateway - $pppGateWay / iface - $pppIface" );
- :log info ( "ppp-down: rt - $rtName / comment - $rtRuleComment" );
- }
- # Clean-up route rules
- /ip route rule remove [ find comment~"$rtRuleComment" ] ;
- # Clean-up static routes
- /ip route remove [ find comment~"$rtRuleComment" ];
- # End
- # 1.3 PPPoE-client configuration
- /interface pppoe-client add allow=mschap2 comment="pppoe-client::wan1=" disabled=no interface=ether1 max-mru=1492 max-mtu=1492 mrru=1614 name=wan1 password=qwe345 profile=pppoe-wan1 user=vpn-acc02
- # [ISP2]
- # 2. WAN2 (DHCP-client)
- # 2.1 Interface configuration
- /interface ethernet set [ find default-name=ether2 ] comment="=EXT2=" name=wan2
- /interface list member add interface=wan2 list=wan
- /ip dhcp-client add add-default-route=no comment="dhcp-client::wan2" dhcp-options=hostname,clientid disabled=no interface=wan2
- # 2.2 Scripts
- # 2.2.1 --- DHCP-LEASE SCRIPT ---
- # Copy to script="" section of /ip dhcp-client configuration
- # Main
- :local DEBUG 0;
- :local dhcpAddr $"lease-address" ;
- :local dhcpGateWay $"gateway-address" ;
- :local dhcpIface $"interface" ;
- :local rtName ( "rt-" . $dhcpIface );
- :local rtRuleComment ( "dhcp-" . $dhcpIface );
- :local alstRoutesName ("alst-" . $dhcpIface . "-CHKGW" );
- :if ($bound=1) do={
- :log info ( "dhcp-lease: running custom script on address bound" );
- # Debug info
- :if ( $DEBUG=1 ) do={
- :log info ( "dhcp-lease: address - $dhcpAddr / gateway - $dhcpGateWay / iface - $dhcpIface" );
- :log info ( "dhcp-lease: rt - $rtName / comment - $rtRuleComment" );
- }
- # Load functions
- :do { /system script run ros-functions } on-error={
- :log error ( "dhcp-lease: can't load necessery functions" );
- :error "[ERR]: can't load necessery functions - inconsistent list of scrips" ;
- }
- # Add route rules for connected routes
- :global funcAddRouteRuleConn;
- $funcAddRouteRuleConn $dhcpAddr $rtName $rtRuleComment
- # Add static routes if address-list present
- :global funcAddStaticRoute;
- :if ( [ len [ /ip firewall address-list find list="$alstRoutesName" ] ] !=0 ) do={
- foreach number in=[ /ip firewall address-list find list="$alstRoutesName" ] do={
- :local dst [ ip firewall address-list get $number address ];
- $funcAddStaticRoute $dst $dhcpGateWay 1 10 $rtRuleComment
- }
- }
- # Environment cleanup
- :do { /system script environment remove [ find name~"func" ] } on-error={
- :log warning ( "dhcp-lease: script environment cleanup is failed" );
- }
- } else {
- :log info ( "dhcp-lease: running custom script on address release" );
- # Debug info
- :if ( $DEBUG=1 ) do={
- :log info ( "dhcp-lease: address - $dhcpAddr / gateway - $dhcpGateWay / iface - $dhcpIface" );
- :log info ( "dhcp-lease: rt - $rtName / comment - $rtRuleComment" );
- }
- # Clean-up route rules
- /ip route rule remove [ find comment~"$rtRuleComment" ];
- # Clean-up static routes
- /ip route remove [ find comment~"$rtRuleComment" ];
- };
- # End
- # [SCRIPTS-MAGIC]
- # 3. Scripts are mentioned above loads /system script which contains necesserry functions
- # 3.1 Script settings
- /system script add comment="=ROS-FUNCTIONS=" dont-require-permissions=no name=ros-functions owner=admin policy=ftp,reboot,read,write,policy,test,sniff
- # 3.2 --- ROS-FUNCTIONS ---
- # 3.2.1 Add route rule for wan iface's ip addresses in custom routing table
- :global funcAddRouteRuleConn do={
- :local DEBUG 0;
- :local Ip $1;
- :local Self;
- :if ( [ :len $Ip ] != 0 ) do={ :set Self ( $Ip . "/32" ); }
- :local Table $2;
- :local CommentPrfx $3;
- :local Comment;
- :if ( [ :len $CommentPrfx ] != 0 ) do={ :set Comment ( $CommentPrfx . "::connected" ); }
- :local Action "lookup-only-in-table";
- :if ( $DEBUG=1 ) do={
- :log info ( "functions: self - $Self / table - $Table / comment - $Comment" );
- :log info ("functions: /ip route rule add comment=$Comment dst-address=0.0.0.0/0 src-address=$Self action=$Action table=$Table");
- }
- :local count [ /ip route rule print count-only where !disabled and comment=$Comment and table=$Table ];
- :if ($count = 0) do={
- # No route rules - add new one to the top of the list
- do { /ip route rule add comment=$Comment dst-address=0.0.0.0/0 src-address=$Self action=$Action table=$Table } on-error={
- :log error ("functions: cannot add route rule - params error? (funcAddRouteRuleConn)");
- :error "functions: cannot add route rule - params error? (funcAddRouteRuleConn)"
- };
- } else={
- :if ($count = 1) do={
- # Update existing route rule if it's required
- :local ruleNum [ /ip route rule find where !disabled and comment=$Comment and table=$Table ];
- :if ( [ /ip route rule get $ruleNum src-address ] != $Self ) do={
- do { /ip route rule set $ruleNum src-address=$Self } on-error={
- :log error ("functions: cannot update route rule - params error? (funcAddRouteRuleConn)");
- :error "functions: cannot update route rule - params error? (funcAddRouteRuleConn)" };
- }
- } else={
- # More then two route rules with same comment and rt - abnormal condition (???)
- :log warn ("functions: multiple route rules for connected addresses in rt $Table (funcAddRouteRuleConn)");
- # Clean up & recreate single one
- do { /ip route rule remove [ find where !disabled and comment=$Comment and table=$Table ] } on-error={
- :log error ("functions: cannot remove duplicate route rules - params error? (funcAddRouteRuleConn)");
- :error "functions: cannot remove duplicate route rules - params error? (funcAddRouteRuleConn)"
- }
- do { /ip route rule add comment=$Comment dst-address=0.0.0.0/0 src-address=$Self action=$Action table=$Table } on-error={
- :log error ("functions: cannot add route rule - params error? (funcAddRouteRuleConn)");
- :error "functions: cannot add route rule - params error? (funcAddRouteRuleConn)"
- }
- }
- }
- }
- # 3.2.2 Add static route in given routing table
- :global funcAddStaticRoute do={
- :local DEBUG 0;
- :local Dst $1;
- if ( $Dst ~ "^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\$" ) do={ :set $Dst ( $Dst . "/32" )};
- :local Gateway $2;
- :local Distance $3;
- :if ( [ :len $Distance ] = 0 ) do={ :set Distance 255 };
- :local Scope $4;
- :if ( [ :len $Scope ] = 0 ) do={ :set Scope 30 };
- :local CommentPrfx $5;
- :local Comment;
- :if ( [ :len $CommentPrfx ] != 0 ) do={ :set Comment ( $CommentPrfx . "::userconf-scripts" ); } else {
- :set Comment ( $Dst . "::userconf-scripts" );
- }
- :local Table $6;
- :if ( [ :len $Table ] = 0 ) do={ :set $Table "main" };
- :if ( $DEBUG=1 ) do={
- :log info ( "functions: dst - $Dst / gw - $Gateway / dist - $Distance / scope - $Scope / comment - $Comment / table - $Table" );
- }
- :local count;
- :if ( $Table = "main" ) do={
- :set count [ /ip route print count-only where dst-address=$Dst and comment=$Comment ];
- } else={
- :set count [ /ip route print count-only where dst-address=$Dst and comment=$Comment and routing-mark=$Table ];
- }
- :if ($count = 0) do={
- # No routes - add new one
- do { /ip route add comment=$Comment dst-address=$Dst gateway=$Gateway distance=$Distance routing-mark=$Table scope=$Scope } on-error={
- :log error ("functions: cannot add route - params error? (funcAddRoute)");
- :error "functions: cannot add route - params error? (funcAddRoute)"
- };
- } else={
- :if ($count = 1) do={
- # Update existing route rule if it's required
- :local routeNum;
- :if ( $Table = "main" ) do={
- :set routeNum [ /ip route find where dst-address=$Dst and comment=$Comment ];
- } else={
- :set routeNum [ /ip route find where dst-address=$Dst and comment=$Comment and routing-mark=$Table ];
- }
- # Gateway
- :if ( [ /ip route get $routeNum gateway ] != $Gateway ) do={
- do { /ip route set $routeNum gateway=$Gateway } on-error={
- :log error ("functions: cannot update route gateway - params error? (funcAddRoute)");
- :error "functions: cannot update route gateway - params error? (funcAddRoute)" };
- }
- # Distance
- :if ( [ /ip route get $routeNum distance ] != $Distance ) do={
- do { /ip route set $routeNum distance=$Distance } on-error={
- :log error ("functions: cannot update route distance - params error? (funcAddRoute)");
- :error "functions: cannot update route distance - params error? (funcAddRoute)" };
- }
- # Scope
- :if ( [ /ip route get $routeNum scope ] != $Scope ) do={
- do { /ip route set $routeNum distance=$Distance } on-error={
- :log error ("functions: cannot update route distance - params error? (funcAddRoute)");
- :error "functions: cannot update route distance - params error? (funcAddRoute)" };
- }
- } else={
- # More then two route rules with same comment and rt - abnormal condition (???)
- :log warn ("functions: multiple route to given destination in rt $Table (funcAddRoute)");
- # Clean up & recreate single one
- :if ( $Table = "main" ) do={
- do { /ip route remove [ find where dst-address=$Dst comment=$Comment ] } on-error={
- :log error ("functions: cannot remove duplicate routes - params error? (funcAddRoute)");
- :error "functions: cannot remove duplicate route rules - params error? (funcAddRoute)"
- }
- } else={
- do { /ip route remove [ find where dst-address=$Dst comment=$Comment and routing-mark=$Table ] } on-error={
- :log error ("functions: cannot remo`e duplicate routes - params error? (funcAddRoute)");
- :error "functions: cannot remove duplicate route rules - params error? (funcAddRoute)"
- }
- }
- do { /ip route add comment=$Comment dst-address=$Dst gateway=$Gateway distance=$Distance routing-mark=$Table scope=$Scope } on-error={
- :log error ("functions: cannot add route - params error? (funcAddRoute)");
- :error "functions: cannot add route - params error? (funcAddRoute)"
- }
- }
- }
- }
- # [ROUTES]
- # All specific recursive routes will be added using scripts
- # MAIN
- /ip route add check-gateway=ping comment="main::default" distance=11 gateway=8.8.4.4
- /ip route add check-gateway=ping comment="main::default" distance=15 gateway=8.8.8.8
- # rt-WAN1
- /ip route add comment="wan1::default" distance=11 gateway=8.8.4.4 routing-mark=rt-wan1
- /ip route add comment="wan1::default" distance=15 gateway=8.8.8.8 routing-mark=rt-wan1
- # rt-WAN2
- /ip route add comment="wan2::default" distance=11 gateway=8.8.8.8 routing-mark=rt-wan2
- /ip route add comment="wan2::default" distance=15 gateway=8.8.4.4 routing-mark=rt-wan2
- # [ROUTE RULES]
- # All look-up-only rules will be added using scripts
- /ip route rule add comment="ext1::checkgateway" dst-address=8.8.8.4/32 table=main
- /ip route rule add comment="ext2::checkgateway" dst-address=8.8.8.8/32 table=main
- /ip route rule add dst-address=192.168.99.0/24 table=main
- # [MANGLE]
- # PORT-FORWARDING
- /ip firewall mangle add action=mark-connection chain=prerouting comment="cmark::wan1-in" connection-state=new dst-port=51234 in-interface=wan1 new-connection-mark=cin-wan1 passthrough=yes protocol=tcp
- /ip firewall mangle add action=mark-routing chain=prerouting comment="lan::wan1-in" connection-mark=cin-wan1 new-routing-mark=rt-wan1 passthrough=no
- /ip firewall mangle add action=mark-connection chain=prerouting comment="cmark::wan2-in" connection-state=new dst-port=51234 in-interface=wan2 new-connection-mark=cin-wan2 passthrough=yes protocol=tcp
- /ip firewall mangle add action=mark-routing chain=prerouting comment="lan::wan2-in" connection-mark=cin-wan2 new-routing-mark=rt-wan2 passthrough=no
- # LOAD-BALANCE
- /ip firewall mangle add action=mark-connection chain=prerouting comment="cmark::wan1-out" connection-state=related,new dst-address-list=!alst-LOCAL-NETS in-interface=lan-bridge new-connection-mark=cout-wan1 passthrough=yes per-connection-classifier=both-addresses-and-ports:2/0
- /ip firewall mangle add action=mark-routing chain=prerouting comment="lan::wan1-out" connection-mark=cout-wan1 new-routing-mark=rt-wan1 passthrough=no
- /ip firewall mangle add action=mark-connection chain=prerouting comment="cmark::wan2-out" connection-state=related,new dst-address-list=!alst-LOCAL-NETS in-interface=lan-bridge new-connection-mark=cout-wan2 passthrough=yes per-connection-classifier=both-addresses-and-ports:2/1
- /ip firewall mangle add action=mark-routing chain=prerouting comment="lan::wan2-out" connection-mark=cout-wan2 new-routing-mark=rt-wan2 passthrough=no
- # [NAT]
- # PORT-FORWARDING
- /ip firewall nat add action=dst-nat chain=dstnat comment="pfwd::wan-all" dst-port=51234 in-interface-list=wan protocol=tcp to-addresses=192.168.99.100
- # SRC-NAT
- /ip firewall nat add action=masquerade chain=srcnat comment="masquerade::wan-all" out-interface-list=wan src-address-list=alst-LOCAL-NETS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement