LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-09-2006, 03:25 AM   #1
win32sux
Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
Question Making a Squid Redirector using BASH


Hi there. I'm trying to make a simple redirector for my Squid, but I have chosen to make it using BASH - instead of the typical Perl - because I have no Perl knowledge at all. Needless to say, I've run into trouble. My first problem is that I can't seem to figure-out how to capture a field from the incoming array.

This is a simple redirector I put together (based on the examples in the Squid FAQ) - its only purpose is to make sure whenever I access Gmail I do it using the secure connection (HTTPS):
Code:
#!/usr/bin/perl
$|=1;
        while (<>) {
                @X = split;
                $url = $X[0];

                if ($url =~ /^http:\/\/gmail\.google\.com/) {
                        print "302:https:\/\/gmail\.google\.com\n";
                }

                elsif ($url =~ /^http:\/\/www\.gmail\.com/) {
                        print "302:https:\/\/gmail\.google\.com\n";
                }           

                elsif ($url =~ /^http:\/\/gmail\.com/) {
                        print "302:https:\/\/gmail\.google\.com\n";
                }

                else {
                        print "$url\n";
                }
            }
It seems to work well, I've been using it without any apparent issues for a very long time. I think I might be able to make some progress with my BASH redirector if you could show me how to write the above in BASH.

Here's the section on redirectors in the Squid FAQ in case it helps: http://www.squid-cache.org/Doc/FAQ/FAQ-15.html

As always, your help is greatly appreciated.

Last edited by win32sux; 06-09-2006 at 03:46 AM.
 
Old 06-09-2006, 04:02 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,492
Blog Entries: 54

Rep: Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909
I can't seem to figure-out how to capture the first field of the incoming array
Args are caught with "$@" (all, IFS separated). Assign separately one tru nine "$1", "$2", etc.
Whatever args Squid supplies the Perl redirector with it only works on the first one, so:
Code:
#!/bin/sh
expr match $1 https://gmail.google.com/ 2>&1>/dev/null && echo $1 || echo "302:${1:0:4}s${1:4}"
exit 0
or in full:
Code:
#!/bin/sh
if [ "$1" = "https://gmail.google.com/" ]; then echo $1; else echo "302:${1:0:4}s${1:4}"; fi
exit 0

Last edited by unSpawn; 06-09-2006 at 04:06 AM. Reason: //have kbd, can't type
 
Old 06-09-2006, 04:39 AM   #3
win32sux
Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
thanks for the reply unspawn... i've used your example to drum this up:
Code:
#!/bin/bash

           while true ; do
                url="$1"

                if [ "$url" = "http://gmail.google.com/" ] ; then
                        echo -e "302:https://gmail.google.com\n"

                elsif [ "$url" = "http://www.gmail.com/" ] ; then
                        echo -e "302:https://gmail.google.com\n"

                elsif [ "$url" = "http://gmail.com/" ] ; then
                        echo -e "302:https://gmail.google.com\n"

                else
                        echo -e "$url\n"
                fi
           done
but it won't work... squid exits after a few seconds... i'm still working on it, though...

BTW, if i provide it with an argument from the command-line it fails like this:
Code:
bash-3.00$ ./squid-gmail-redirect.sh http://www.gmail.com/
./squid-gmail-redirect.sh: line 9: syntax error near unexpected token `then'
./squid-gmail-redirect.sh: line 9: `                elsif [ "$url" = "http://www.gmail.com/" ] ; then'
a couple questions:

what does the "302:${1:0:4}s${1:4}" in your examples stand for??

also, does it look like i am doing the loop properly in the shell script i just posted?? cuz i read it needs to be an infinite loop so that it can run in "daemon mode"...

Last edited by win32sux; 06-09-2006 at 04:46 AM.
 
Old 06-09-2006, 05:34 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,492
Blog Entries: 54

Rep: Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909
also, does it look like i am doing the loop properly in the shell script i just posted?? cuz i read it needs to be an infinite loop so that it can run in "daemon mode"...
Your condition tests true but there's nothing to work with, while using read it will just wait for and act on input:
Code:
#!/bin/bash
while read s; do
        if [ "$s" = "http://gmail.google.com/" ] ; then
                echo -e "302:https://gmail.google.com\n"
        elif [ "$s" = "http://www.gmail.com/" ] ; then
                echo -e "302:https://gmail.google.com\n"
        elif [ "$s" = "http://gmail.com/" ] ; then
                echo -e "302:https://gmail.google.com\n"
        else
                echo -e "$s\n"
        fi
done

what does the "302:${1:0:4}s${1:4}" in your examples stand for?
Just experiment I'd say: i="1 2 3"; echo ${i:0:1} ${i:4:1} ${i/1/LQ Rules}
 
Old 06-09-2006, 05:52 AM   #5
win32sux
Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
thanks...

i'm gonna do some experiments/research and i'll post back later...
 
Old 06-09-2006, 07:14 AM   #6
win32sux
Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
Quote:
Originally Posted by unSpawn
Code:
#!/bin/bash
while read s; do
        if [ "$s" = "http://gmail.google.com/" ] ; then
                echo -e "302:https://gmail.google.com\n"
        elif [ "$s" = "http://www.gmail.com/" ] ; then
                echo -e "302:https://gmail.google.com\n"
        elif [ "$s" = "http://gmail.com/" ] ; then
                echo -e "302:https://gmail.google.com\n"
        else
                echo -e "$s\n"
        fi
done
i tried with this, but no luck... in other words, i don't get redirected to the HTTPS site like i do with the perl script... although in "top" i can see that squid is at least using the script because there is a cpu usage spike whenever a URL is entered in the browser... shouldn't $s be given the value of $1, or the first item in the array?? i'm currenlty still trying to figure-out what's wrong... anyone know of a logfile i could look at?? my cache.log has nothing useful...

BTW, does anyone know how this part translates to BASH:
Code:
$|=1;
from what i understand, that command clears the buffers or something like that, right??
 
Old 06-14-2006, 06:28 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,492
Blog Entries: 54

Rep: Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909
i tried with this, but no luck
That's bad.


shouldn't $s be given the value of $1
In this case "read" assigns everything ($@) given to variable "s". So if there's only one argument given it will only use one argument. You can test / see this easily by running the script on the CLI and enter args.
If you explicitly want to use the first arg there's two ways:
("bad")
Code:
- while read s; do
+ while read s crap; do
where "crap" just will be a wasted variable, or
(less "bad")
Code:
while read s; do
+      s=(${s}); s=${s[0]}
        if [ "$s" = "http://gmail.google.com/" ] ; then
where you space-separate (IFS!) into an array and on;y use the first arg.


i'm currenlty still trying to figure-out what's wrong... anyone know of a logfile i could look at?? my cache.log has nothing useful.
Make it go into debug mode or log verbose?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Squid+Clam+Redirector depam Linux - Software 1 02-16-2006 04:57 AM
Making squid transparent zefram Linux - Networking 1 01-31-2006 07:54 PM
squid complaining about redirector processes lewus Linux - Software 0 02-08-2005 01:31 AM
making squidguard start whenever squid starts Niceman2005 Linux - Software 2 11-24-2004 06:31 PM
Help making simpel Squid.confd Gadget Linux - Networking 3 10-23-2003 10:14 AM


All times are GMT -5. The time now is 01:10 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration