|
楼主 |
发表于 2011-9-19 21:03
|
显示全部楼层
SetupSquidTransparentProxy
, R; w2 o: p) {5 S3 s% |; q7 d0 X( W; H! b) c6 c
Squid Transparent Proxy on Palm Pre
+ D% d m* d1 d9 S% m* {/ O2 q
- f9 O0 e# f, x$ ~$ NOk, someone asked for it, so I figured it out, how to do it for fun and profit:+ Z4 i d$ \; C r, D( [
) l% e2 [+ w- j% M7 }0 mHide Preparation, ~/ |5 Q3 r" N. ^& ~, b
; D& z* a. @8 D
First, you need to have root access to your device. How to get root, is described here. It would be a good idea to know a little about linux and networking too. Be warned: this can make your networking unfunctional!0 `. t( w! p9 T
6 y: `! K& B) G; ^! @$ d& i
Next thing you need to do is to install the package Optware Advanced Linux Command Line Installer from Preware, which installs ipkg-opt!
) g0 n/ @" T2 q- s
; Y! t$ ?7 _8 z9 p' V3 }↑ Jump Back A Section3 K/ r9 O9 f; b s9 y; i
Hide Installing Squid
9 `: ]. l0 `4 J8 Y; I5 K
6 k( n N$ h5 t' SLogin to the pre console and make your root disk writable:+ x8 R0 f# j$ |0 j% R3 H. v
3 P* v$ d' t! c( S2 mroot@palm-webos-device: # cd / && mount -v -o remount,rw /+ _* n4 ]! |. Q6 A
Type this command to install the squid optware package; }+ o, i$ W9 x) M8 L
- K& L# e4 | }; x1 v* ?
root@palm-webos-device: # ipkg-opt update* P* C5 N7 }* }+ S) I' R# H
...% t% E3 |3 F" @! p6 |: F
root@palm-webos-device: # ipkg-opt install squid
5 \; [3 T$ z0 F: a2 j3 M Installing squid (2.6.21-2) to root...% S5 t4 z1 h t; H
Downloading http://ipkg.nslu2-linux.org/feed ... id_2.6.21-2_arm.ipk! }# V/ _& C6 B, V& s, T
Configuring squid
" |% T6 u' o4 a. Z) l create default cache and logs dir
" m/ }5 Q8 F0 v1 L/ v: T7 X chown: unknown user/group nobody:nobody
- Y" L& G0 c* D5 D chown: unknown user/group nobody:nobody" c$ t$ z& q% u$ F" y9 n' i! Y
FATAL: Could not determine fully qualified hostname.Please set 'visible_hostname'
" j G7 l' A: U, b# R5 b" X
- v% b% d7 q0 p) R, Q Squid Cache (Version 2.6.STABLE21): Terminated abnormally.
' X' p' j4 H L( M3 z CPU Usage: 0.020 seconds = 0.010 user + 0.010 sys9 Z3 E/ @ w1 k( V1 C$ m- n- ^+ j
Maximum Resident Size: 0 KB5 }, H( B; h. P1 y* c, ~$ k
Page faults with physical i/o: 07 l# V$ ~ a- ~1 m2 |
Segmentation fault2 q" ~% W1 Q M I1 Q( X! x; N/ o
; {% q2 O( w6 G/ \ You should review the configuration file /opt/etc/squid/squid.conf,
! p4 g6 J& O8 K$ Q, _0 z3 B make any necessary change, and complete the install by running -
$ }( J: W7 p P# e m* a$ c7 @- f9 G /opt/etc/init.d/S80squid start
7 V9 b- ]2 ~; b# I9 M
& |8 h$ ^' P, ^7 E Successfully terminated.! B0 Q& ]/ U# g5 j) v/ k
Ignore the errors for the moment, we'll get to it soon.
! y/ ~7 H1 n8 M$ n0 I/ J4 y. H( v- H( V
↑ Jump Back A Section
0 c7 T' `7 N, G% y' kHide Create a basic proxy config; N* Z" G1 g( u
0 u4 ^- P3 p8 e- v: W0 |Change to the directory /opt/etc/squid and move the file squid.conf which is already there to somewhere else. Now create a new squid.conf, which should have the following contents:
/ M1 N. b% c, n& p; o4 g. [* K2 V7 Y- y4 k# U9 F: {. q2 }* `
visible_hostname localhost5 E# g! F: R3 F8 W& I
http_port 3128 transparent
$ D H# d A3 w% U& T8 q: p L hierarchy_stoplist cgi-bin ?
( c! Z0 J& C# A5 |7 k' Z access_log /var/cache/squid/squid.log squid
# S! e( a _1 R- \ acl QUERY urlpath_regex cgi-bin \? 9 B: L* D+ {+ e( n+ N0 C! E
cache deny QUERY . {6 w3 j: q4 _ P# U1 a; L
/ P2 E8 f7 b1 N8 R" i. y! A # replace the ip address with the address of your proxy
7 i- z& f+ M0 Q. _! N; b # replace the port 3128 with the port of your proxy0 \ w- _; U% i3 I* K# l( v) U- `
cache_peer 192.168.2.205 parent 3128 3130 default
% [. \( q2 W. }2 I- y* J 8 R- t2 a+ d& \2 z. a
# if your proxy requires authentication, use this:
8 R/ G% D9 `8 Y9 l# x& l9 I # cache_peer 192.168.2.205 parent 3128 3130 default login USERNAME PASSWORD1 }; g8 r* @- x3 H, q0 v
7 @+ Q' y0 o! O refresh_pattern ^ftp: 1440 20% 10080
x z( b' e7 Z! u2 { refresh_pattern ^gopher: 1440 0% 1440
/ W; _; z- a# c1 j9 k) ^ refresh_pattern . 0 20% 4320
* O9 r( N: X' i1 x acl apache rep_header Server ^Apache
; N ~2 z+ ^ S% H broken_vary_encoding allow apache 8 p/ t' r. U1 P: N0 Z% o% u& Z. {, H
coredump_dir /var/cache/squid 7 j6 K% P( e p/ _7 q
cache_dir ufs /var/cache/squid 20 5 5
) j) Y7 a3 X- X3 F# O2 f3 Z access_log /var/cache/squid/squid.log
- q6 j( z. O: W! I cache_log /var/cache/squid/cache.log8 [5 V+ O+ g, V7 f3 [5 ~6 i
cache_store_log /dev/null( W/ p# c8 d1 T' u+ h" |
# `9 @% E3 n, [& ]+ O) H! [3 T# x1 q; J acl all src 0.0.0.0/0.0.0.0
, x* \( d" O" c M7 N1 l, L/ [ acl manager proto cache_object1 ]( U& h1 M3 }" C9 Q
acl localhost src 127.0.0.1/255.255.255.255
5 Z. K/ P; j4 n+ }- P& O q acl to_localhost dst 127.0.0.0/8
& ~2 ]# ~7 a: X/ u acl SSL_ports port 443 ^, |8 J3 Y4 p# P' V2 D
acl Safe_ports port 80 # http* g5 z. v. W2 I) E" i4 O
acl Safe_ports port 21 # ftp9 I# D9 c4 B! F, \! D# n
acl Safe_ports port 443 # https0 w2 P2 n y* \, ~# S4 L6 O
acl Safe_ports port 70 # gopher
! \ P2 l" H( F2 t acl Safe_ports port 210 # wais
1 a9 K) v6 @+ d7 w( X acl Safe_ports port 1025-65535 # unregistered ports* v+ b! G. D' `0 k$ P! [6 e
acl Safe_ports port 280 # http-mgmt1 G' N4 b: c3 b% D' q3 I
acl Safe_ports port 488 # gss-http
. G: p- H+ m/ C4 L, s( s: w2 y3 N acl Safe_ports port 591 # filemaker0 p# ^( l: r2 }. T# J
acl Safe_ports port 777 # multiling http, U+ @& H; K( k
acl CONNECT method CONNECT) O1 I/ m% ]% b |0 y
http_access allow manager localhost
/ D, Y7 {* A2 k) N% x- { http_access deny manager9 [6 I+ a. V. w/ @) E* P( I
http_access deny !Safe_ports
2 l2 @8 i4 F% ~ http_access deny CONNECT !SSL_ports
C; w+ S `& s8 y2 j+ } # example for an ACL, uncomment and modify' C! l" Q) t. V1 o0 H: x
#acl enemy dstdomain www.apple.com
( X# r2 `, b8 o/ p) A4 Y( n- M #http_access deny enemy
9 r% R7 G, x& R" B b" N$ t
$ l/ [; u; @* e0 H& K9 A X http_access allow all
$ n% e* J9 p7 O) E' Z icp_access allow all
+ Z" v K2 D$ N4 l6 WBasically this is a config for a transparent proxy, thus a browser will be redirected to it without knowing that it is actually connected to a proxy. We must configure it this way because the WebOS browser cannot be configured to use a proxy.
6 Y3 a5 @" k5 x; t. q2 U4 y/ S' r! c* R1 ?: C2 ?
Note that we have added a single ACL here, which denies access to some random site *g*. Any other requests will be allowed. SSL (HTTPS) will not be passed through the proxy because this requires the browser to connect to it using a proxy request.( {; p S( ~. G2 h$ I4 F
3 @9 Z$ [* L8 b9 N9 d( d, s$ xYou WILL need to modify the squid.conf variable cache_peer, it points to your upstream proxy, e.g. your companys proxy. Refer to the comments for the syntax., a7 y; g) j: `( S4 G% ~' W. K+ O
^4 J4 B/ I) `! o↑ Jump Back A Section/ N: s8 I' K4 b) A
Hide Prepare the directories for squid
/ R3 C: ]' v; ]' |; |& R/ q: O8 c J* P6 x
Squid needs a directory where to store it's files, as content it caches and logfiles. You may consider to disable caching and logging. But caching may be a good idea if your traffic is limited, so you'll save some. Logging is always usefull for debugging. If you want to turn it off, use /dev/null as target for the logfiles.
3 M7 t6 \; X8 O% M' u _( _% M
4 v: C; g0 v- K" pmkdir /var/cache/squid' ^3 v- Y# p8 [
chown nobody /var/cache/squid
/ F# n7 A% m# w7 d0 K3 P* r& m) Q2 XNext you need let squid to prepare the directory structure using the -z option:$ N s, h: H: J5 N
7 ^2 O$ w3 I2 o
root@palm-webos-device: # squid -z -f squid.conf( Y3 j0 R$ e8 s6 \( V
2009/11/26 23:19:29| Creating Swap Directories/ v+ `& ` a) E. c* `8 M4 E( W
You should not see any errors here!- m. a% p+ ^. `* K4 l
9 D) }; [) p3 i9 F( f/ N
↑ Jump Back A Section) X9 D5 t8 I5 |4 x/ V3 O
Hide Starting squid
) o9 e! J+ F' D6 _3 M: v# i- m9 Y6 V" z8 q; V7 i6 `1 n
The installer already added it to the startup daemons, now we will start it manually:& D+ g t5 c. m; b& \, s
5 P4 k* L! `' n4 ]; d- Y* i/opt/etc/init.d/S80squid start
, ^& w( G: n; BLook in /var/cache/squid/cache.log for any errors. Use ps to see if it really runs. You may consider to install lynx and test the proxy:
+ a/ {/ S; }& X9 ?& g
0 P2 y" a& b3 v9 H9 Ripkg-opt install lynx. s1 v7 `2 K3 I" _+ c
export http_proxy=http://localhost:31284 I1 b7 z5 g8 l0 T2 n3 G' e
lynx www.google.com; O0 u M( T0 W8 `2 {" C
If you see a google textmode site, it works.# p4 [$ C' f8 }' q- a
! u- k7 ]1 |) V$ B, gTurn it off if everything is ok:$ f- y* D6 t2 E! r
: K' ^ m, V/ G: O
/opt/etc/init.d/S80squid stop, c, Q7 d/ x" j( c ~1 l
↑ Jump Back A Section
5 c: u' ?/ L' }Hide Add the rules to the squid startup file* b: D% a4 G3 Q* o7 S! |
6 J: R; Q; Z! g% @5 \Now comes the ugly part. I'll not explain it in detail: we install some iptable Rules. We tell iptables to forward any traffic destined to tcp port 80 to localhost port 3128 (this is where squid listens for incoming connections). To avoid that the requests of the proxy itself are forwarded to localhost (which would create a loop), we add a rule telling iptables to not forward web traffic if it were generated by user nobody (the user squid runs as).1 n& h' y; B# D( j" a* ^' ? F* k2 O
0 s1 j/ v5 G4 @8 X7 a
To do that, edit /opt/etc/init.d/S80squid so that it looks like this:
. }$ y* K o1 y: b$ e( {' [! J: T! {- S5 d3 J
#! /bin/sh! A, w. B" l2 M/ G
- r0 P/ ?" R" p' W! A$ } case "$1" in
* U( c' Z! _; y$ }# @" ~, m7 } start)/ L0 u2 X B$ p% Y6 M
echo -n "Starting proxy server: "
5 Y3 [; k. o, ^4 ` if [ -n "`pidof quotacheck`" ]; then v/ w# x- N& H4 y
#you don't need it if you don't have quota check enable.
1 w4 {& Z$ ~' v$ c4 x, F) e echo "Starting squid-cache server after delay for few mins:"/ B2 w! {4 u% s* ?$ S! F
/opt/etc/squid/squid.delay-start.sh&
T7 E4 m9 W2 L- \% y) O) ~' I2 ` else
+ a. ^' v7 }1 }& W, ~" o* I /opt/sbin/squid -f /opt/etc/squid/squid.conf" q- z6 Y5 F7 t7 H
iptables -t nat -A OUTPUT -p tcp --dport 80 -m owner --gid-owner 65534 -j ACCEPT
D; t8 R' T D( Q: j- r$ N# _ iptables -t nat -A OUTPUT -p tcp --dport 80 -j DNAT --to-destination 127.0.0.1:3128
" I: J& ]- e, [. ^; P& K echo "done."+ P1 |0 N" A! `; `# W M& ]4 c1 P
fi
% m# ^( H! g' @9 X$ g/ i echo "done."
+ {( \& _ I! W1 Z% Y ;;
& u) C8 a% y: @ [5 X stop)
, c% f" R. T* I. `; H echo -n "Stopping proxy server: "1 o K! e7 G6 @! b% `- w4 l
/opt/sbin/squid -f /opt/etc/squid/squid.conf -k shutdown$ g6 T) [* |& Y! K* ?
iptables -t nat -D OUTPUT -p tcp --dport 80 -m owner --gid-owner 65534 -j ACCEPT
" l( Y0 x- w5 \% K& M2 v% v$ H iptables -t nat -D OUTPUT -p tcp --dport 80 -j DNAT --to-destination 127.0.0.1:3128
' {- c4 Y/ N2 d" g' {! g! n" T1 j; o echo "done."4 \4 M+ [) X8 N- s8 @& u: W
;;
6 E' r# P- n- f" y. | reload|force-reload)
2 R; H7 m) R+ Q; f3 g! ? echo -n "Reloading proxy server configuration files: "
6 p: R3 D3 ?) \2 p- z4 K /opt/sbin/squid -f /opt/etc/squid/squid.conf -k reconfigure+ k. Z' V1 \+ Q* K) O
echo "done."
`- C% f; ^- X2 b e ;;' j: J+ T/ p/ J) V$ V; ?
restart)
' P& u8 @# d9 Q X) H) o echo -n "Restarting proxy server: "
2 ?# R0 }* R5 D7 \$ L, m% T6 m /opt/sbin/squid -f /opt/etc/squid/squid.conf -k shutdown
. G' i2 ], T3 H" u* |' m sleep 2
0 E' O+ k" i! W2 V /opt/sbin/squid -f /opt/etc/squid/squid.conf9 c0 a' X! m' e7 [6 r! F
echo "done."2 V) P. J9 q) R) |
;;6 E2 h, r& ]1 o; v; b
*)
4 w6 ~ k/ \' `. A/ T/ J echo "Usage: /opt/etc/init.d/S80squid {start|stop|reload|force-reload|restart}"
) m- M" j% S2 f/ ]8 c: z exit 11 K' L2 G$ w8 B. m8 W3 n
;;
7 }5 Q' c: L' |7 m% X esac* b% N- ^9 N& J; O7 v N1 m
↑ Jump Back A Section1 t( \( A2 l6 l
Hide Configure network to startup squid if WIFI comes up& w, _. X- s/ t
/ ]3 C% G$ v/ C4 A+ l, kNow we need to alter the behavior of the palm network manager so that it starts squid if WIFI comes up and stops it when WIFI goes down./ {. w; ~ A/ B# V& i
3 w- R9 u3 _5 {! R# {Change to directory /etc/pmnetconfig/.
5 n+ |- H' H& Z
* x$ I" o! A( v, f; `In the file if-up modify this part:
3 |: `, B+ p$ s, E$ M, j$ M6 t, N. d, l7 J; |/ }0 L
if [ "$ISPPP" -ne 4 ] && [ "$ISRMNET" -ne 6 ] && [ "$ISTUN" -ne 4 ] && [ "$ISCSCOTUNVPN" -ne 8 ]; then
# W* y' t8 _9 Z) c NetCfgSetAddr# S S/ g$ @! Y4 q6 P; i
CMSERVICES=$((${CMSERVICES} | ${CMSVCINTERNET}))
7 f# Y9 u; g0 Z# N1 J& c fi8 c, P2 Y$ k" s9 }, d0 k4 {1 S
to
5 l* \! U$ F) F1 @* e+ X* Y# {# B- |
if [ "$ISPPP" -ne 4 ] && [ "$ISRMNET" -ne 6 ] && [ "$ISTUN" -ne 4 ] && [ "$ISCSCOTUNVPN" -ne 8 ]; then
4 {8 p* x$ r$ [6 m3 r& q NetCfgSetAddr- w/ q1 G$ b/ x3 T
CMSERVICES=$((${CMSERVICES} | ${CMSVCINTERNET}))
) M' y+ X- t7 Z. J. f /usr/bin/logger "wifi up: execute /opt/etc/init.d/S80squid start" 3 n# c/ M8 C/ k( I* g+ D. a
/opt/etc/init.d/S80squid start/ {0 p; p* ?8 s( F7 F! [0 @
fi* L# P3 p8 B, \ ] l
In the file if-down modify this part:
$ D: {1 t& _5 _5 a$ S! ~
1 M2 V% e' s9 u7 C, R, ` if [ ${CMINTERFACE} = ${CMBTINTERFACENAME} ] || [ ${CMINTERFACE} = ${CMBRINTERFACENAME} ]; then1 g" F" w' m0 n7 }
${LOG} "${IPTABLES} -t nat -D POSTROUTING -s $CMNETADDR/$CMPREFIXLEN -j MASQUERADE"
+ D: t; U, f5 j% v- \ ${IPTABLES} -t nat -D POSTROUTING -s $CMNETADDR/$CMPREFIXLEN -j MASQUERADE
# k/ A0 A- \( O: w3 j. S ${LOG} "${ECHO} 0 >/proc/sys/net/ipv4/ip_forward"
. @) A; X* o( k7 g% L ${ECHO} 0 >/proc/sys/net/ipv4/ip_forward
; ]. c0 b1 ]6 _, i! R9 e fi- R8 O- W( d5 c; k: x
to:2 P% W j, m, A6 H, z
9 [& l% ?- o9 \
if [ ${CMINTERFACE} = ${CMBTINTERFACENAME} ] || [ ${CMINTERFACE} = ${CMBRINTERFACENAME} ]; then
1 q1 x* |9 p3 M' K5 p9 d ${LOG} "${IPTABLES} -t nat -D POSTROUTING -s $CMNETADDR/$CMPREFIXLEN -j MASQUERADE"4 ?/ A/ [; }4 f B
${IPTABLES} -t nat -D POSTROUTING -s $CMNETADDR/$CMPREFIXLEN -j MASQUERADE1 ~6 T: x4 C |
${LOG} "${ECHO} 0 >/proc/sys/net/ipv4/ip_forward". V# u5 ?1 @" f/ o0 y8 w
${ECHO} 0 >/proc/sys/net/ipv4/ip_forward9 ?8 Z, j5 @) {: R7 q
/usr/bin/logger "wifi up: execute /opt/etc/init.d/S80squid stop"
$ t" F; }) R* q$ `1 q9 m$ J+ V /opt/etc/init.d/S80squid stop $ ~3 j e2 ]1 l. p4 m! x
fi
- G3 P+ E+ r% D$ S↑ Jump Back A Section |5 G& i/ [7 ^) D! v2 }6 _) b
Hide Make your root disk read-only again, D& r' A8 V! a) s0 t, X/ P
- y8 W! b, X7 Y* ]
root@palm-webos-device: # cd / && mount -v -o remount,ro /
# p. a, c6 v; p/ A↑ Jump Back A Section
! G8 {, j0 |7 ?Hide See it working
" W% V) K& h7 l" q& W6 ]! b& v6 `+ N$ c7 h6 u8 |" V
So, let's see how it works. Enable WIFI and fire up the browser and see if it works. You should see your requests on the remote proxy.
$ J. S& }/ X6 ?0 d) q( t3 f8 `7 ] u3 E8 q8 }& B
If you added an ACL as mentioned earlier, a message like the following should appear if you enter the url of your favourite enemy vendor (the one we configured above):
' M' \6 G, Y. `" r( j9 R# I4 {! L+ P( v( Q3 R6 n) F
9 U3 D- _/ v4 D8 P! J* m% }$ B
& k4 b7 l' E* ` L+ i& n↑ Jump Back A Section
& x, h$ `- K( L8 }8 ?+ O$ F: P+ ?Hide Blocking Adverts
4 M4 o& E$ K- S) [
" D* [ ~4 }; j6 T$ Z: j. A; ]& oNow squid is set up, we can use it for ad blocking. There are two steps to this:
0 @' s: h* h4 p3 S# z) x
+ M: l( ?* m9 V! G# n) |4 bObtain a block list# [. p% \; C/ v
3 p; l9 A' N5 Y5 Y0 r: r0 d7 r, v
An excellent ad block list is maintained at http://pgl.yoyo.org/as/. We need to download it in a suitable format for squid, which can be done by running the following command:
% q. x- g6 r3 q' `. H) Z/ j; h# T: q. c/ I6 M
wget -q -O/opt/etc/squid/blocklist 'http://pgl.yoyo.org/as/serverlist.php?hostformat=squid-dstdom-regex;showintro=0&nohtml=true&mimetype=plaintext > /opt/etc/squid/blocklist
. t! L: x/ G+ F' n5 o(Technical details: the wget command downloads the URL given in single quotes, then we strip the HTML mark-up and blank lines using grep. The filename at the end is where the black list will be stored.)+ p/ j5 z) K' y
. p/ n. @2 m! |$ u7 q
Configure squid to use the block list
e2 e% s$ u3 A. L) |9 u( w, G- D0 `& f3 P4 d0 }- _. R6 F0 o+ |! |
Now we need to tell squid to block the list of URLs in the file we've just created. Open the /opt/etc/squid/squid.conf file we created above, and scroll down to where it reads:8 B/ K( F2 e- \6 K3 S, `* F
$ A: n, ]$ @! u! t* Z/ b
acl enemy dstdomain www.apple.com7 k* r1 X6 _3 H5 b
Replace this line with:6 j- B( z; T" z/ S" B0 x( q( `
/ ?7 G) E& x/ |$ y8 b! i1 c6 S! [acl enemy dstdom_regex "/opt/etc/squid/blocklist"" \& n f: _5 f5 H8 h7 I; a1 P9 F
Finally, restart squid:9 C4 B, r% |% ?
" B. H0 E* ?+ s4 O; \0 J! u1 y) `/opt/etc/init.d/S80squid restart" A( I2 u/ b! |6 ~& @2 R+ y
To test it's working, open a browser and try to visit ads.msn.com (one of the blocked URLs). The error given should be similar to the screenshot given above./ a+ |7 V8 r: G
) K: f- S) L7 G0 y1 X0 \If in the future you need to update the ad blocking list, run the commands:
5 P0 x; |$ E4 T% q1 U, m6 B/ E1 T: b7 Y9 M
cd / && mount -v -o remount,rw /
" I9 N5 p' z! ?- u8 e0 D2 r4 W6 p wget -q -O - 'http://pgl.yoyo.org/as/serverlist.php?hostformat=squid-dstdom-regex;showintro=0' | grep -v ">" | grep "." > /opt/etc/squid/blocklist+ F# `$ F6 Z u2 g
/opt/etc/init.d/S80squid restart
" R8 O: X& N8 c: N6 w- UFor further details of filtering URLs using squid, please refer to the excelent documentation of the squid cache project. |
|