/*
  PAC file created via OPNsense
  To use this file you have to enter its URL into your browsers network settings.
*/
function FindProxyForURL(url, host) {
{% if helpers.exists('OPNsense.proxy.pac.rule') %}
{# define only if needed as because of performance issues #}
{% set data = {'dl' : '', 'dstip' : '', 'is_resolvable' : '' } %}
{% set dstip = '' %}
{% set is_resolvable = '' %}
{% for match in helpers.toList('OPNsense.proxy.pac.match') %}
{%   if match.match_type == 'dns_domain_levels' %}
{%     do data.update({ 'dl': 'var dl =  dnsDomainLevels(host);'}) %}
{%   endif %}
{%   if match.match_type == 'dns_domain_levels' or match.match_type == 'destination_in_net' %}
{%     do data.update({ 'dstip': 'var dstip = dnsResolve(host);'}) %}
{%   endif %}
{%   if match.match_type == 'is_resolvable' %}
{%     do data.update({ 'is_resolvable': 'var is_resolvable = isResolvable(host);'}) %}
{%   endif %}
{% endfor %}
{{ data.values()|join("\n") }}

{% if helpers.exists('OPNsense.proxy.pac.rule') %}
{%   for rule in helpers.toList('OPNsense.proxy.pac.rule') %}
{%     if not rule.enabled == '1' %}
{%       continue %}
{%     endif %}
{%     set expression = [] %}
{# Join type is used to join the checks of the if statement #}
{%     set join_type = ' && ' %}
{%     if rule.join_type == 'or' %}
{%       set join_type = ' || ' %}
{%     endif %}
{%     for match_uuid in rule.matches.split(',') %}
{%       set match = helpers.getUUID(match_uuid) %}
{#       be sure it has not been deleted yet #}
{%       if match != None %}
{%         set match_script = '(' %}
{%         if match.negate == '1' %}
{%           set match_script = match_script + '!' %}
{%         endif %}
{%         if match.match_type == 'url_matches' %}
{%           set match_script = match_script + 'shExpMatch(url, "' + match.url + '")' %}
{%         endif %}
{%         if match.match_type == 'hostname_matches' %}
{%           set match_script = match_script + 'shExpMatch(host, "' + match.hostname + '")' %}
{%         endif %}
{%         if match.match_type == 'dns_domain_is' %}
{%           set match_script = match_script + 'dnsDomainIs(host, "' + match.hostname + '")' %}
{%         endif %}
{%         if match.match_type == 'destination_in_net' %}
{%           set tmp_net = helpers.getIPNetwork(match.network) %}
{%           set match_script = match_script + 'isInNet(dstip, "' + tmp_net.network.__str__() + '", "' + tmp_net.netmask.__str__() + '")' %}
{%         endif %}
{%         if match.match_type == 'my_ip_in_net' %}
{%           set tmp_net = helpers.getIPNetwork(match.network) %}
{%           set match_script = match_script + 'isInNet(myIpAddress(), "' + tmp_net.network.__str__() + '", "' + tmp_net.netmask.__str__() + '")' %}
{%         endif %}
{%         if match.match_type == 'plain_hostname' %}
{%           set match_script = match_script + 'isPlainHostName(host)' %}
{%         endif %}
{%         if match.match_type == 'is_resolvable' %}
{%           set match_script = match_script + 'is_resolvable' %}
{%         endif %}
{%         if match.match_type == 'dns_domain_levels' %}
{%           set match_script = match_script + '(' + match.domain_level_from + ' <= dl) && (' + match.domain_level_to + ' >= dl)' %}
{%         endif %}
{%         if match.match_type == 'weekday_range' %}
{%           set match_script = match_script + 'weekdayRange("' + match.weekday_from + '", "' + match.weekday_to + '")' %}
{%         endif %}
{%         if match.match_type == 'date_range' %}
{%           set match_script = match_script + 'dateRange("' + match.date_from + '", "' + match.date_to + '")' %}
{%         endif %}
{%         if match.match_type == 'time_range' %}
{%           set match_script = match_script + 'timeRange(' + match.time_from + ', ' + match.time_to + ')' %}
{%         endif %}
{%         set match_script = match_script + ')' %}
{%         do expression.append(match_script) %}
{%       endif %}
{%     endfor %}
if ({% if rule.match_type == 'unless' %}!{% endif %}({{ expression|join(join_type) }})) {
{%     set proxylist = [] %}
{%     for proxy_uuid in rule.proxies.split(',') %}
{%       set proxy = helpers.getUUID(proxy_uuid) %}
{%       if proxy != None %}
{%         if proxy.proxy_type == 'DIRECT' %}
{%           do proxylist.append("DIRECT") %}
{%         else %}
{%           do proxylist.append(proxy.proxy_type + ' ' + proxy.url) %}
{%         endif %}
{%       endif %}
{%     endfor %}
return "{{ proxylist|join(';') }}";
}
{%   endfor %}
{% else %}
/* no rules active or defined*/
{% endif %}

{% endif %}
   // If no rule exists - use a direct connection
   return "DIRECT";
}
