cfillion.tk

MiddleSquid v1.0

Hi there!

Today I’ve released the first version of MiddleSquid, a custom redirector helper for the Squid HTTP proxy configurable in Ruby. The project is hosted on GitHub and is distributed under the terms of the GNU GPL v3 license.

This is an excerpt of the configuration file I am currently using:

require 'nokogiri'

use Adapters::Squid, concurrency: true
database '/home/proxy/blacklist.db'

ads     = blacklist 'ads', aliases: ['adv']
spyware = blacklist 'spyware'
tracker = blacklist 'tracker'

block_group = [tracker, spyware, ads]

# redirect to the direct image link
define_helper :extract_img_like do |req, uri, selector|
  status, headers, body = download_like req, uri

  if status == 200
    parser = Nokogiri::HTML body
    image = parser.at_css(selector)['src']

    [301, {'Location'=>image}, '']
  else
    [status, headers, body]
  end
end

run lambda {|uri, extra|
  url = uri.to_s
  user_ip = extra[0].split('/').first

  # whitelist youtube
  accept if uri.host == 'www.youtube.com'

  # block bad sites
  block_group.each {|bl|
    next unless bl.include? uri

    replace_by "http://webfilter.lan/blocked/?__a=#{URI.encode user_ip}&__u=#{URI.encode uri.to_s}&__t=#{bl.category}"
  }

  # fix nyaa's RSS feed links to open the "view" page
  if url =~ /^http:\/\/(.*nyaa\.se)\/\?page=download&tid=(\d+)$/
    intercept {|req, res|
      unless req.referer
        [301, {'Location'=>"http://#{$1}/?page=view&tid=#{$2}"}, nil]
      else
        download_like req, uri
      end
    }
  end

  # bypass imgur's landing page
  if url =~ /^http:\/\/imgur\.com\/(\w+)$/
    intercept {|req, res|
      extract_img_like req, uri, '.image img'
    }
  end

  # ...
}

You can learn more about this software and my others open source projects on my GitHub profile page.

Screenshot of Another