Category Archives

11 Articles

cidr.me地理位置查询

2   14519 转为繁体

基于OpenResty ,MaxMind GeoIP数据库和从bgp.he.net生成的ASN数据库,因为没有经纬度的需求所以没有显示。下文有源代码的链接,如果需要可以自行修改加上经纬度或者将输出变为JSON等。

这里有chrome插件

使用方法

查询当前IPv4地址

$ curl http://cidr.me/ip
x.x.x.x

查询当前IPv6地址

$ curl http://ipv6.cidr.me/ip
x.x.x.x

查询当前IP的PTR记录(反向DNS),地理位置和ASN

$ curl http://cidr.me/
x.x.x.x
Country, City
ASN number

查询当前IP的PTR记录

$ curl http://cidr.me/rdns
x-x-x-x.com

查询指定IP的地理位置

$ curl http://cidr.me/74.125.203.199
74.125.203.199 th-in-f199.1e100.net
United States, California, Mountain View
AS15169 Google Inc.

查询域名的地理位置

$ curl http://cidr.me/www.google.com.hk
172.217.3.195 sea15s12-in-f3.1e100.net
United States, California, Mountain View
AS15169 Google LLC

2607:f8b0:400a:809::2003 sea15s12-in-x03.1e100.net
United States
AS15169 Google LLC

查询域名的IP

$ curl http://cidr.me/www.google.com.hk/ip
74.125.203.199
2607:f8b0:400a:809::2003

查询域名的IP和CNAME(如果存在)

$ curl http://cidr.me/www.youtube.com/dns
youtube-ui.l.google.com 172.217.3.174
172.217.3.206
216.58.193.78
216.58.217.46
2607:f8b0:400a:808::200e

源代码

在这里https://gist.github.com/fffonion/44e5fb59e2a8f0efba5c1965c6043584

需要ngx_http_geoip_module, echo-nginx-module, lua-nginx-module,安装libgeoip,并将maxmind的旧版geoip数据库放在/usr/share/GeoIP

通过bgp.he.net生成ASN数据库

Read More

用 OpenResty 写了一个 SNI 代理

0   12264 转为繁体

功能类似于dlundquist/sniproxy

推荐 OpenResty 加上 stream 模块和 ngx_stream_lua_module 模块。在 1.9.15.1 上测试通过。

示例配置:

A Lua table sni_rules should be defined in the init_worker_by_lua_block directive.

The key can be either whole host name or regular expression. Use . for a default host name. If no entry is matched, connection will be closed.

The value is a table containing host name and port. If host is set to nil, the server_name in SNI will be used. If the port is not defined or set to nil, 443 will be used.

Rules are applied with the priority as its occurrence sequence in the table. In the example above, twitter.com will match the third rule rather than the fourth.

If the protocol version is less than TLSv1 (eg. SSLv3, SSLv2), connection will be closed, since SNI extension is not supported in these versions.

nginx 批量配置同步

0   70427 转为繁体

在编译了lua-nginx-module的nginx上,可以方便地使用shared dict特性,在不reload配置文件的情况下实现配置同步。

由于shared dict使用一块共享内存,因此所有worker均可读写,也就不存在一致性的问题。

使用shared dict

Read More

nginx/openresty的一些记录

28   39464 转为繁体

日志

屏蔽user-agent并屏蔽日志

不屏蔽user-agent(允许其访问),但屏蔽日志

按uri屏蔽日志(可以和上面的按user-agent用同一个变量来同时过滤uri和user-agent)

 

Header

按mime type设置缓存时间

防攻击

简单的无状态cookie challenge(需要lua-nginx-module)

crawlers块中可以手动填写要屏蔽的IP

将其中的s改成随机字符串+时间戳可以变成有状态版本(需使用redis/memcached/shared memory存储生成的随机字符串)

将set-cookie改成通过js生成cookie可以变成javascript challenge,注意要在js里加上浏览器上下文判断,如var cookie=location.protocol?cookie:””; 或者DOM操作

这里有个更高级的输验证码的示例

其他

植入cookie

需要注意的是使用ngx.time()产生秒级的时间,用来做随机数种子可能会冲突,因此建议加上另外的随机变量(如下面的例子用的是客户端的ip) 可以使用ngx.now()产生毫秒精度时间

Lua用setmetatable返回默认值为function时的暗坑

0   118257 转为繁体

想在api服务器里实现一个acl的功能,对某些请求(需要登录,需要检查appkey,需要限制频次等)做限制,对某些起始状态(比如登陆)或者终结状态(比如报错)的请求放行。

因为lua里木有switch case,因此通过一个acl_list的table去查找规则,因为需要限制的请求种类比较多,就把rule_check_token当成默认值了,一看是是这么写的:

然而却会在local token= xxxx那一行报stack overflow,想了半天也发现哪里有无限递归,因为query传进来的是http请求的query string解析出的键值对表。

把query打印出来一看,发现竟然是这个模块本身……

仔细看了文档才知道,原来__index后面的值是一个function时,lua会调用这个function去获得不存在的键,并且第一个参数是模块本身(即_M,一个table)。在这个例子里:

  1. 调用一个非默认规则的api
  2. lua调用了rule_check_token
  3. 参数query被传入了_M本身
  4. 运行到local token= xxxx这一行
  5. 这个table里(_M)又没有token这个键
  6. 回到2

所以就死循环了

 

所以要好好看文档

 

解决方法是可以套一个function

 

贴一个打印table的工具,方便调试:

可以打印出如下形式:

使用openresty请自行改写成local function和ngx.say的形式