Squid url redirector

Hex Artifact Content
anonymous

Hex Artifact Content

Artifact 44562e88d1362b45c03e0fbaae9cf2091902a6287a549b81256b58f0edd1a6fb:


0000: 2d 2d 20 67 65 6e 65 72 61 6c 20 61 72 72 61 79  -- general array
0010: 20 73 6f 72 74 69 6e 67 20 61 6e 64 20 64 6f 6d   sorting and dom
0020: 61 69 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 66  ain processing f
0030: 75 6e 63 74 69 6f 6e 73 0a 2d 2d 20 73 6f 72 74  unctions.-- sort
0040: 73 20 61 72 72 61 79 0a 43 52 45 41 54 45 20 6f  s array.CREATE o
0050: 72 20 72 65 70 6c 61 63 65 20 46 55 4e 43 54 49  r replace FUNCTI
0060: 4f 4e 20 73 6f 72 74 28 6f 72 69 67 69 6e 61 6c  ON sort(original
0070: 20 61 6e 79 61 72 72 61 79 29 20 52 45 54 55 52   anyarray) RETUR
0080: 4e 53 20 61 6e 79 61 72 72 61 79 0a 09 4c 41 4e  NS anyarray..LAN
0090: 47 55 41 47 45 20 73 71 6c 20 49 4d 4d 55 54 41  GUAGE sql IMMUTA
00a0: 42 4c 45 20 53 54 52 49 43 54 0a 09 41 53 20 24  BLE STRICT..AS $
00b0: 5f 24 0a 73 65 6c 65 63 74 20 61 72 72 61 79 5f  _$.select array_
00c0: 61 67 67 28 69 74 65 6d 29 20 61 73 20 72 65 73  agg(item) as res
00d0: 75 6c 74 20 66 72 6f 6d 20 28 73 65 6c 65 63 74  ult from (select
00e0: 20 75 6e 6e 65 73 74 28 24 31 29 20 61 73 20 69   unnest($1) as i
00f0: 74 65 6d 20 6f 72 64 65 72 20 62 79 20 69 74 65  tem order by ite
0100: 6d 29 20 61 3b 0a 24 5f 24 3b 0a 0a 2d 2d 20 73  m) a;.$_$;..-- s
0110: 6f 72 74 73 20 61 72 72 61 79 20 61 6e 64 20 72  orts array and r
0120: 65 6d 6f 76 65 73 20 64 75 70 6c 69 63 61 74 65  emoves duplicate
0130: 73 0a 43 52 45 41 54 45 20 6f 72 20 72 65 70 6c  s.CREATE or repl
0140: 61 63 65 20 46 55 4e 43 54 49 4f 4e 20 75 73 6f  ace FUNCTION uso
0150: 72 74 28 6f 72 69 67 69 6e 61 6c 20 61 6e 79 61  rt(original anya
0160: 72 72 61 79 29 20 52 45 54 55 52 4e 53 20 61 6e  rray) RETURNS an
0170: 79 61 72 72 61 79 0a 09 4c 41 4e 47 55 41 47 45  yarray..LANGUAGE
0180: 20 73 71 6c 20 49 4d 4d 55 54 41 42 4c 45 20 53   sql IMMUTABLE S
0190: 54 52 49 43 54 0a 09 41 53 20 24 5f 24 0a 73 65  TRICT..AS $_$.se
01a0: 6c 65 63 74 20 61 72 72 61 79 5f 61 67 67 28 69  lect array_agg(i
01b0: 74 65 6d 29 20 61 73 20 72 65 73 75 6c 74 20 66  tem) as result f
01c0: 72 6f 6d 20 28 73 65 6c 65 63 74 20 64 69 73 74  rom (select dist
01d0: 69 6e 63 74 20 75 6e 6e 65 73 74 28 24 31 29 20  inct unnest($1) 
01e0: 61 73 20 69 74 65 6d 20 6f 72 64 65 72 20 62 79  as item order by
01f0: 20 69 74 65 6d 29 20 61 3b 0a 24 5f 24 3b 0a 0a   item) a;.$_$;..
0200: 2d 2d 20 67 65 6e 65 72 61 6c 20 64 61 74 61 62  -- general datab
0210: 61 73 65 20 73 74 72 75 63 74 75 72 65 0a 2d 2d  ase structure.--
0220: 20 74 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20 61   table to hold a
0230: 6c 6c 20 72 75 6c 65 73 0a 43 52 45 41 54 45 20  ll rules.CREATE 
0240: 54 41 42 4c 45 20 72 75 6c 65 73 20 28 0a 09 6e  TABLE rules (..n
0250: 65 74 6d 61 73 6b 20 63 69 64 72 20 4e 4f 54 20  etmask cidr NOT 
0260: 4e 55 4c 4c 2c 0a 09 72 65 64 69 72 65 63 74 5f  NULL,..redirect_
0270: 75 72 6c 20 74 65 78 74 20 44 45 46 41 55 4c 54  url text DEFAULT
0280: 20 27 61 62 6f 75 74 3a 3a 62 6c 61 6e 6b 27 3a   'about::blank':
0290: 3a 74 65 78 74 20 4e 4f 54 20 4e 55 4c 4c 2c 0a  :text NOT NULL,.
02a0: 09 66 72 6f 6d 5f 77 65 65 6b 64 61 79 20 73 6d  .from_weekday sm
02b0: 61 6c 6c 69 6e 74 20 44 45 46 41 55 4c 54 20 30  allint DEFAULT 0
02c0: 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 74 6f 5f 77   NOT NULL,..to_w
02d0: 65 65 6b 64 61 79 20 73 6d 61 6c 6c 69 6e 74 20  eekday smallint 
02e0: 44 45 46 41 55 4c 54 20 36 20 4e 4f 54 20 4e 55  DEFAULT 6 NOT NU
02f0: 4c 4c 2c 0a 09 66 72 6f 6d 5f 74 69 6d 65 20 74  LL,..from_time t
0300: 69 6d 65 20 77 69 74 68 6f 75 74 20 74 69 6d 65  ime without time
0310: 20 7a 6f 6e 65 20 44 45 46 41 55 4c 54 20 27 30   zone DEFAULT '0
0320: 30 3a 30 30 3a 30 30 27 3a 3a 74 69 6d 65 20 77  0:00:00'::time w
0330: 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65  ithout time zone
0340: 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 74 6f 5f 74   NOT NULL,..to_t
0350: 69 6d 65 20 74 69 6d 65 20 77 69 74 68 6f 75 74  ime time without
0360: 20 74 69 6d 65 20 7a 6f 6e 65 20 44 45 46 41 55   time zone DEFAU
0370: 4c 54 20 27 32 33 3a 35 39 3a 35 39 27 3a 3a 74  LT '23:59:59'::t
0380: 69 6d 65 20 77 69 74 68 6f 75 74 20 74 69 6d 65  ime without time
0390: 20 7a 6f 6e 65 20 4e 4f 54 20 4e 55 4c 4c 2c 0a   zone NOT NULL,.
03a0: 09 74 61 67 20 74 65 78 74 5b 5d 20 4e 4f 54 20  .tag text[] NOT 
03b0: 4e 55 4c 4c 0a 29 3b 0a 0a 63 72 65 61 74 65 20  NULL.);..create 
03c0: 69 6e 64 65 78 20 72 75 6c 65 73 5f 74 61 67 5f  index rules_tag_
03d0: 67 20 6f 6e 20 72 75 6c 65 73 20 75 73 69 6e 67  g on rules using
03e0: 20 67 69 6e 20 28 74 61 67 29 3b 0a 0a 2d 2d 20   gin (tag);..-- 
03f0: 74 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20 74 61  table to hold ta
0400: 67 20 2d 20 73 69 74 65 20 6c 69 6e 6b 73 0a 43  g - site links.C
0410: 52 45 41 54 45 20 54 41 42 4c 45 20 75 72 6c 73  REATE TABLE urls
0420: 20 28 0a 09 64 61 74 65 5f 61 64 64 65 64 20 74   (..date_added t
0430: 69 6d 65 73 74 61 6d 70 20 77 69 74 68 6f 75 74  imestamp without
0440: 20 74 69 6d 65 20 7a 6f 6e 65 20 44 45 46 41 55   time zone DEFAU
0450: 4c 54 20 28 27 6e 6f 77 27 3a 3a 74 65 78 74 29  LT ('now'::text)
0460: 3a 3a 74 69 6d 65 73 74 61 6d 70 28 30 29 20 77  ::timestamp(0) w
0470: 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65  ithout time zone
0480: 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 73 69 74 65   NOT NULL,..site
0490: 20 74 65 78 74 5b 5d 20 4e 4f 54 20 4e 55 4c 4c   text[] NOT NULL
04a0: 2c 0a 09 74 61 67 20 74 65 78 74 5b 5d 20 4e 4f  ,..tag text[] NO
04b0: 54 20 4e 55 4c 4c 2c 0a 09 72 65 67 65 78 70 20  T NULL,..regexp 
04c0: 74 65 78 74 0a 29 3b 0a 0a 63 72 65 61 74 65 20  text.);..create 
04d0: 69 6e 64 65 78 20 75 72 6c 73 5f 74 61 67 5f 67  index urls_tag_g
04e0: 20 6f 6e 20 75 72 6c 73 20 75 73 69 6e 67 20 67   on urls using g
04f0: 69 6e 20 28 74 61 67 29 3b 0a 63 72 65 61 74 65  in (tag);.create
0500: 20 69 6e 64 65 78 20 75 72 6c 73 5f 73 69 74 65   index urls_site
0510: 5f 67 20 6f 6e 20 75 72 6c 73 20 75 73 69 6e 67  _g on urls using
0520: 20 67 69 6e 20 28 73 69 74 65 29 3b 0a 63 72 65   gin (site);.cre
0530: 61 74 65 20 75 6e 69 71 75 65 20 69 6e 64 65 78  ate unique index
0540: 20 75 72 6c 73 5f 72 73 74 20 6f 6e 20 75 72 6c   urls_rst on url
0550: 73 20 28 72 65 67 65 78 70 2c 20 75 73 6f 72 74  s (regexp, usort
0560: 28 73 69 74 65 29 2c 20 75 73 6f 72 74 28 74 61  (site), usort(ta
0570: 67 29 29 3b 0a 0a 2d 2d 20 72 75 6c 65 20 74 6f  g));..-- rule to
0580: 20 6a 6f 69 6e 20 61 6c 6c 20 74 61 62 6c 65 73   join all tables
0590: 20 69 6e 74 6f 20 6f 6e 65 20 74 6f 20 73 69 6d   into one to sim
05a0: 70 6c 69 66 79 20 61 63 63 65 73 73 0a 2d 2d 20  plify access.-- 
05b0: 61 75 74 6f 6d 61 74 69 63 61 6c 6c 20 75 73 65  automaticall use
05c0: 73 20 63 75 72 72 65 6e 74 20 64 61 79 20 61 6e  s current day an
05d0: 64 20 74 69 6d 65 20 64 61 74 61 0a 63 72 65 61  d time data.crea
05e0: 74 65 20 76 69 65 77 20 73 69 74 65 5f 72 75 6c  te view site_rul
05f0: 65 20 61 73 0a 73 65 6c 65 63 74 20 72 65 64 69  e as.select redi
0600: 72 65 63 74 5f 75 72 6c 2c 20 6e 65 74 6d 61 73  rect_url, netmas
0610: 6b 2c 20 73 69 74 65 2c 20 72 65 67 65 78 70 0a  k, site, regexp.
0620: 66 72 6f 6d 20 72 75 6c 65 73 20 6a 6f 69 6e 20  from rules join 
0630: 75 72 6c 73 0a 6f 6e 20 28 75 72 6c 73 2e 74 61  urls.on (urls.ta
0640: 67 20 26 26 20 72 75 6c 65 73 2e 74 61 67 29 0a  g && rules.tag).
0650: 77 68 65 72 65 20 28 27 6e 6f 77 27 3a 3a 74 65  where ('now'::te
0660: 78 74 29 3a 3a 74 69 6d 65 20 77 69 74 68 6f 75  xt)::time withou
0670: 74 20 74 69 6d 65 20 7a 6f 6e 65 20 3e 3d 20 66  t time zone >= f
0680: 72 6f 6d 5f 74 69 6d 65 0a 09 61 6e 64 20 28 27  rom_time..and ('
0690: 6e 6f 77 27 3a 3a 74 65 78 74 29 3a 3a 74 69 6d  now'::text)::tim
06a0: 65 20 77 69 74 68 6f 75 74 20 74 69 6d 65 20 7a  e without time z
06b0: 6f 6e 65 20 3c 3d 20 74 6f 5f 74 69 6d 65 0a 09  one <= to_time..
06c0: 61 6e 64 20 64 61 74 65 5f 70 61 72 74 28 27 64  and date_part('d
06d0: 6f 77 27 3a 3a 74 65 78 74 2c 20 6e 6f 77 28 29  ow'::text, now()
06e0: 29 20 3e 3d 20 28 66 72 6f 6d 5f 77 65 65 6b 64  ) >= (from_weekd
06f0: 61 79 29 3a 3a 64 6f 75 62 6c 65 20 70 72 65 63  ay)::double prec
0700: 69 73 69 6f 6e 0a 09 61 6e 64 20 64 61 74 65 5f  ision..and date_
0710: 70 61 72 74 28 27 64 6f 77 27 3a 3a 74 65 78 74  part('dow'::text
0720: 2c 20 6e 6f 77 28 29 29 20 3c 3d 20 28 74 6f 5f  , now()) <= (to_
0730: 77 65 65 6b 64 61 79 29 3a 3a 64 6f 75 62 6c 65  weekday)::double
0740: 20 70 72 65 63 69 73 69 6f 6e 3b 0a 0a 43 52 45   precision;..CRE
0750: 41 54 45 20 50 52 4f 43 45 44 55 52 41 4c 20 4c  ATE PROCEDURAL L
0760: 41 4e 47 55 41 47 45 20 70 6c 70 67 73 71 6c 3b  ANGUAGE plpgsql;
0770: 0a 0a 2d 2d 20 74 72 61 6e 73 66 6f 72 6d 73 20  ..-- transforms 
0780: 64 6f 6d 61 69 6e 20 69 6e 74 6f 20 6f 72 64 65  domain into orde
0790: 72 65 64 20 61 72 72 61 79 20 66 6f 72 20 69 6e  red array for in
07a0: 64 65 78 69 6e 67 0a 43 52 45 41 54 45 20 6f 72  dexing.CREATE or
07b0: 20 72 65 70 6c 61 63 65 20 46 55 4e 43 54 49 4f   replace FUNCTIO
07c0: 4e 20 74 72 69 70 64 6f 6d 61 69 6e 28 75 72 6c  N tripdomain(url
07d0: 20 74 65 78 74 29 20 52 45 54 55 52 4e 53 20 74   text) RETURNS t
07e0: 65 78 74 5b 5d 0a 09 4c 41 4e 47 55 41 47 45 20  ext[]..LANGUAGE 
07f0: 70 6c 70 67 73 71 6c 20 49 4d 4d 55 54 41 42 4c  plpgsql IMMUTABL
0800: 45 20 53 54 52 49 43 54 0a 09 41 53 20 24 5f 24  E STRICT..AS $_$
0810: 0a 64 65 63 6c 61 72 65 0a 09 72 65 73 75 6c 74  .declare..result
0820: 20 74 65 78 74 5b 5d 3b 0a 09 73 70 6c 69 74 74   text[];..splitt
0830: 65 64 20 74 65 78 74 5b 5d 3b 0a 09 78 20 69 6e  ed text[];..x in
0840: 74 65 67 65 72 3b 0a 09 6c 65 6e 67 74 68 20 69  teger;..length i
0850: 6e 74 65 67 65 72 3b 0a 62 65 67 69 6e 0a 09 73  nteger;.begin..s
0860: 70 6c 69 74 74 65 64 20 3a 3d 20 73 74 72 69 6e  plitted := strin
0870: 67 5f 74 6f 5f 61 72 72 61 79 28 24 31 2c 20 27  g_to_array($1, '
0880: 2e 27 29 3b 0a 09 6c 65 6e 67 74 68 20 3a 3d 20  .');..length := 
0890: 61 72 72 61 79 5f 6c 65 6e 67 74 68 28 73 70 6c  array_length(spl
08a0: 69 74 74 65 64 2c 20 31 29 3b 0a 09 78 20 3a 3d  itted, 1);..x :=
08b0: 20 31 3b 0a 09 6c 6f 6f 70 0a 09 09 65 78 69 74   1;..loop...exit
08c0: 20 77 68 65 6e 20 73 70 6c 69 74 74 65 64 5b 78   when splitted[x
08d0: 5d 20 69 73 20 6e 75 6c 6c 3b 0a 09 09 72 65 73  ] is null;...res
08e0: 75 6c 74 5b 78 5d 20 3a 3d 20 73 70 6c 69 74 74  ult[x] := splitt
08f0: 65 64 5b 78 5d 20 7c 7c 20 27 3a 27 20 7c 7c 20  ed[x] || ':' || 
0900: 6c 65 6e 67 74 68 20 2d 20 78 3b 0a 09 09 78 20  length - x;...x 
0910: 3a 3d 20 78 20 2b 20 31 3b 0a 09 65 6e 64 20 6c  := x + 1;..end l
0920: 6f 6f 70 3b 0a 09 72 65 74 75 72 6e 20 72 65 73  oop;..return res
0930: 75 6c 74 3b 0a 65 6e 64 3b 24 5f 24 3b 0a 0a 2d  ult;.end;$_$;..-
0940: 2d 20 74 72 61 6e 73 66 6f 72 6d 73 20 6f 72 64  - transforms ord
0950: 65 72 65 64 20 61 72 72 61 79 20 69 6e 74 6f 20  ered array into 
0960: 64 6f 6d 61 69 6e 0a 63 72 65 61 74 65 20 6f 72  domain.create or
0970: 20 72 65 70 6c 61 63 65 20 66 75 6e 63 74 69 6f   replace functio
0980: 6e 20 75 6e 74 72 69 70 28 73 69 74 65 20 74 65  n untrip(site te
0990: 78 74 5b 5d 29 20 72 65 74 75 72 6e 73 20 74 65  xt[]) returns te
09a0: 78 74 0a 09 6c 61 6e 67 75 61 67 65 20 70 6c 70  xt..language plp
09b0: 67 73 71 6c 20 69 6d 6d 75 74 61 62 6c 65 20 73  gsql immutable s
09c0: 74 72 69 63 74 0a 09 61 73 20 24 5f 24 0a 64 65  trict..as $_$.de
09d0: 63 6c 61 72 65 0a 09 78 20 69 6e 74 65 67 65 72  clare..x integer
09e0: 3b 0a 09 73 70 6c 69 74 74 65 64 20 74 65 78 74  ;..splitted text
09f0: 5b 5d 3b 0a 09 70 61 69 72 20 74 65 78 74 5b 5d  [];..pair text[]
0a00: 3b 0a 62 65 67 69 6e 0a 09 78 20 3a 3d 20 61 72  ;.begin..x := ar
0a10: 72 61 79 5f 6c 65 6e 67 74 68 28 73 69 74 65 2c  ray_length(site,
0a20: 20 31 29 3b 0a 09 6c 6f 6f 70 20 0a 09 09 65 78   1);..loop ...ex
0a30: 69 74 20 77 68 65 6e 20 73 69 74 65 5b 78 5d 20  it when site[x] 
0a40: 69 73 20 6e 75 6c 6c 3b 0a 09 09 70 61 69 72 20  is null;...pair 
0a50: 3a 3d 20 73 74 72 69 6e 67 5f 74 6f 5f 61 72 72  := string_to_arr
0a60: 61 79 28 73 69 74 65 5b 78 5d 2c 20 27 3a 27 29  ay(site[x], ':')
0a70: 3b 0a 09 09 73 70 6c 69 74 74 65 64 5b 30 20 2d  ;...splitted[0 -
0a80: 20 70 61 69 72 5b 32 5d 3a 3a 69 6e 74 65 67 65   pair[2]::intege
0a90: 72 5d 20 3a 3d 20 70 61 69 72 5b 31 5d 3b 0a 09  r] := pair[1];..
0aa0: 09 78 20 3a 3d 20 78 20 2d 20 31 3b 0a 09 65 6e  .x := x - 1;..en
0ab0: 64 20 6c 6f 6f 70 3b 0a 09 72 65 74 75 72 6e 20  d loop;..return 
0ac0: 61 72 72 61 79 5f 74 6f 5f 73 74 72 69 6e 67 28  array_to_string(
0ad0: 73 70 6c 69 74 74 65 64 2c 20 27 2e 27 29 3b 0a  splitted, '.');.
0ae0: 65 6e 64 3b 0a 24 5f 24 3b 0a                    end;.$_$;.