Skip to content

Instantly share code, notes, and snippets.

@yseto
Last active October 5, 2015 01:36
Show Gist options
  • Save yseto/522d56ec91a0dca98af3 to your computer and use it in GitHub Desktop.
Save yseto/522d56ec91a0dca98af3 to your computer and use it in GitHub Desktop.
h2o+mruby-geoip
diff --git CMakeLists.txt CMakeLists.txt
index 2f76264..7e6d920 100644
--- CMakeLists.txt
+++ CMakeLists.txt
@@ -346,6 +346,7 @@ IF (WITH_MRUBY)
TARGET_LINK_LIBRARIES(h2o
"${CMAKE_BINARY_DIR}/mruby/host/lib/libmruby.a"
"${CMAKE_BINARY_DIR}/mruby/host/mrbgems/mruby-onig-regexp/Onigmo-Onigmo-5.15.0/.libs/libonig.a"
+ "GeoIP"
"m")
ADD_DEPENDENCIES(h2o mruby)
ENDIF (WITH_MRUBY)
diff --git src/mrb_geoip.c src/mrb_geoip.c
index dd60c29..c9ccd0a 100644
--- src/mrb_geoip.c
+++ src/mrb_geoip.c
@@ -203,6 +203,14 @@ static mrb_value mrb_geoip_time_zone(mrb_state *mrb, mrb_value self)
data->gir->country_code, data->gir->region)));
}
+static mrb_value mrb_geoip_clear(mrb_state *mrb, mrb_value self)
+{
+ mrb_geoip_data *data = DATA_PTR(self);
+ GeoIPRecord_delete(data->gir);
+ GeoIP_delete(data->gi);
+ return mrb_fixnum_value(0);
+}
+
void mrb_mruby_geoip_gem_init(mrb_state *mrb)
{
struct RClass *geoip;
@@ -220,6 +228,7 @@ void mrb_mruby_geoip_gem_init(mrb_state *mrb)
mrb_define_method(mrb, geoip, "metro_code", mrb_geoip_metro_code, MRB_ARGS_NONE());
mrb_define_method(mrb, geoip, "area_code", mrb_geoip_area_code, MRB_ARGS_NONE());
mrb_define_method(mrb, geoip, "time_zone", mrb_geoip_time_zone, MRB_ARGS_NONE());
+ mrb_define_method(mrb, geoip, "clear", mrb_geoip_clear, MRB_ARGS_NONE());
DONE;
}
class CountryDeniedViaGeoIP
def call(env)
geoip = GeoIP.new("/usr/share/GeoIP/GeoIPCity.dat")
geoip.record_by_addr env["REMOTE_ADDR"]
ret = [399, {}, []]
if geoip.country_code != "JP"
ret = [403, {'content-type' => 'text/plain'}, [ "forbidden" ]]
end
# geoip.clear
return ret
end
end
hosts:
"example.net:80":
header.set: "X-Robots-Tag: noindex, nofollow"
paths:
"/":
mruby.handler-file: /app/country_denied_via_geoip.rb
proxy.reverse.url: "http://192.0.2.1:5000/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment