4月 132012
 

How do ACL match to req.http.X-Forwarded-For? (this is string!!)
I tried to make a vmod.

inet_pton(BOOL ipv6 , STRING str , STRING defaultstr)

example 1

[code=’plain’]
import campur_xcir;

set resp.http.v6 = campur_xcir.inet_pton(true,”2001:0db8:bd05:01d2:288a:1fc0:0001:10ee”,”1982:db8:20:3:1000:100:20:3″);
set resp.http.v4 = campur_xcir.inet_pton(false,”1.1.1.1″,”2.2.2.2″);
set resp.http.v6ng = campur_xcir.inet_pton(true,”2001:0db8:bd05:01d2:288a:1fc0:0001:10eeHOGE”,”1982:db8:20:3:1000:100:20:3″);//NG pattern
set resp.http.v4ng = campur_xcir.inet_pton(false,”1.1.1.1HOGE”,”2.2.2.2″);//NG pattern

//result
v6: 2001:db8:bd05:1d2:288a:1fc0:1:10ee
v4: 1.1.1.1
v6ng: 1982:db8:20:3:1000:100:20:3
v4ng: 2.2.2.2
[/code]

example 2

[code=’plain’]
import campur_xcir;

//acl
acl local {
“192.168.1.0”/24;
!”0.0.0.0″;
}

sub vcl_recv{
if(campur_xcir.inet_pton(false , req.http.X-Forwarded-For , “0.0.0.0”) ~ local){
//acl ok
…
}
}
[/code]

I hope that this code is of help to you.
libvmod-campur_xcir

this module is my motley function.
Others, get varnish generated hash etc…

this vmod’s function is increase at times. 🙂

4月 132012
 

ちょっとdai_yamashitaさんに聞かれたのでつくってみたものですがせっかくなので記事にします。

よくクライアントのIPアドレスをACLと突き合わせて処理をする・しないをやるかと思いますが
上位に他のProxyがいるなど(たとえばNginx)で、X-Forwarded-ForをACLと付き合わせてみたいときはどうすればよいでしょうか?
通常の方法ではできないのでVMOD作ってみました。

campur_xcir.inet_pton(ipv6かどうか , 変換したいIPアドレスな文字列 , 失敗した場合のデフォルトのIPアドレスな文字列)

[code=’plain’]
import campur_xcir;

set resp.http.v6 = campur_xcir.inet_pton(true,”2001:0db8:bd05:01d2:288a:1fc0:0001:10ee”,”1982:db8:20:3:1000:100:20:3″);
set resp.http.v4 = campur_xcir.inet_pton(false,”1.1.1.1″,”2.2.2.2″);
set resp.http.v6ng = campur_xcir.inet_pton(true,”2001:0db8:bd05:01d2:288a:1fc0:0001:10eeHOGE”,”1982:db8:20:3:1000:100:20:3″);//失敗パタン
set resp.http.v4ng = campur_xcir.inet_pton(false,”1.1.1.1HOGE”,”2.2.2.2″);//失敗パタン

//結果
v6: 2001:db8:bd05:1d2:288a:1fc0:1:10ee
v4: 1.1.1.1
v6ng: 1982:db8:20:3:1000:100:20:3
v4ng: 2.2.2.2
[/code]

実際はこんな感じの使い方を想定しています

[code=’plain’]
import campur_xcir;

//acl
acl local {
“192.168.1.0”/24;
!”0.0.0.0″;
}

sub vcl_recv{
if(campur_xcir.inet_pton(false , req.http.X-Forwarded-For , “0.0.0.0”) ~ local){
//acl ok
…
}
}
[/code]

もしよかったら使ってみてください
libvmod-campur_xcir

ちなみにこのモジュールは僕が試しに作ったものを突っ込んでるものです
他にはMLで質問してた人向けに作ったVarnishのhash値を取得するものとかが入ってます
たまに増えたりすると思います