Permalink
Browse files

github公開

  • Loading branch information...
0 parents commit 55f166bae58f003f4bdf0cedd8027e1596b42cfc @aileron committed Jul 10, 2011
Showing with 345 additions and 0 deletions.
  1. +23 −0 README
  2. +84 −0 cidr.txt
  3. +238 −0 mod_cidr_lookup.c
23 README
@@ -0,0 +1,23 @@
+
+モバイルサイト作成の際に、IPアドレスによる振り分けを行う目的の為に作成しました
+
+* summary
+mod_cidr_lookupは、アクセスしてきたクライアントのIPアドレスが
+起動時に読み込んでおいたCIDRブロック群のいずれかにマッチするかどうかを
+判別するためのモジュールです。
+Apache 2.2系 (FreeBSD) にて 開発および動作実績が有ります。
+マッチした結果は、環境変数 (X_CLIENT_TYPE) とHTTPリクエストヘッダ (X-Client-Type) にセットするので
+Apache自身とバックエンドのWebアプリの両方で
+同じ情報を参照することができます。
+
+* install
+sudo、wget、apxsがコマンドが使用可能な環境での豪快なインストール方法
+
+wget http://aileron.cc/mod_cidr_lookup.c
+sudo apxs -c -i -a mod_cidr_lookup.c
+
+httpd.confに帯域ファイルへのパスを追加(絶対パス)
+CIDRFile /usr/local/etc/apache22/cidr.txt
+
+再起動
+sudo apacectl restart
@@ -0,0 +1,84 @@
+# KDDI
+KDDI 210.230.128.224/28
+KDDI 121.111.227.160/27
+KDDI 61.117.1.0/28
+KDDI 219.108.158.0/27
+KDDI 219.125.146.0/28
+KDDI 61.117.2.32/29
+KDDI 61.117.2.40/29
+KDDI 219.108.158.40/29
+KDDI 219.125.148.0/25
+KDDI 222.5.63.0/25
+KDDI 222.5.63.128/25
+KDDI 222.5.62.128/25
+KDDI 59.135.38.128/25
+KDDI 219.108.157.0/25
+KDDI 219.125.145.0/25
+KDDI 121.111.231.0/25
+KDDI 121.111.227.0/25
+KDDI 118.152.214.192/26
+KDDI 118.159.131.0/25
+KDDI 118.159.133.0/25
+KDDI 118.159.132.160/27
+KDDI 111.86.142.0/26
+KDDI 111.86.141.64/26
+KDDI 111.86.141.128/26
+KDDI 111.86.141.192/26
+KDDI 118.159.133.192/26
+KDDI 111.86.143.192/27
+KDDI 111.86.143.224/27
+KDDI 111.86.147.0/27
+KDDI 111.86.142.128/26
+KDDI 111.86.142.192/26
+KDDI 111.86.143.0/26
+# SOFTBANK
+SOFTBANK 123.108.237.0/27
+SOFTBANK 202.253.96.224/27
+SOFTBANK 210.146.7.192/26
+SOFTBANK 210.175.1.128/25
+# DOCOMO
+DOCOMO 210.153.84.0/24
+DOCOMO 210.136.161.0/24
+DOCOMO 210.153.86.0/24
+DOCOMO 124.146.174.0/24
+DOCOMO 124.146.175.0/24
+DOCOMO 202.229.176.0/24
+DOCOMO 202.229.177.0/24
+DOCOMO 202.229.178.0/24
+DOCOMO 202.229.179.0/24
+DOCOMO 111.89.188.0/24
+#CRAWLER-MB
+CRAWLER-MB-DOCOMO 210.153.84.0/24
+CRAWLER-MB-DOCOMO 210.136.161.0/24
+CRAWLER-MB-DOCOMO 210.153.86.0/24
+CRAWLER-MB-DOCOMO 124.146.174.0/24
+CRAWLER-MB-DOCOMO 124.146.175.0/24
+CRAWLER-MB-DOCOMO 202.229.176.0/22
+CRAWLER-MB-DOCOMO 202.229.179.0/24
+CRAWLER-MB-DOCOMO 111.89.188.0/24
+CRAWLER-MB-DOCOMO 111.89.188.0/22
+CRAWLER-MB-AU 66.249.64.0/20
+CRAWLER-MB-GOOGLE 66.102.0.0/16
+CRAWLER-MB-GOOGLE 64.233.0.0/16
+CRAWLER-MB-GOOGLE 216.239.0.0/16
+CRAWLER-MB-GOOGLE 74.125.0.0/16
+CRAWLER-MB-GOOGLE 66.249.64.0/20
+CRAWLER-MB-GOOGLE 72.14.192.0/18
+CRAWLER-MB-GOOGLE 209.85.128.0/17
+CRAWLER-MB-YAHOO 124.83.159.128/26
+CRAWLER-MB-YAHOO 124.83.159.224/27
+CRAWLER-MB-YAHOO 124.83.191.0/27
+CRAWLER-MB-YAHOO 203.216.255.112/29
+CRAWLER-MB-MOBA 202.238.103.126
+CRAWLER-MB-MOBA 202.213.221.97
+CRAWLER-MB-GOO 218.213.130.0/24
+CRAWLER-MB-GOO 203.131.250.0/24
+CRAWLER-MB-GOO 203.131.251.0/24
+CRAWLER-MB-GOO 203.131.252.0/24
+CRAWLER-MB-GOO 203.131.253.0/24
+CRAWLER-MB-GOO 203.131.254.0/24
+CRAWLER-MB-GOO 203.131.255.0/24
+CRAWLER-MB-GOO 203.131.248.0/21
+CRAWLER-MB-GOO 218.213.128.0/20
+CRAWLER-MB-FROUTE 60.43.36.253
+CRAWLER-MB-LIVEDOOR 203.104.254.0/24
@@ -0,0 +1,238 @@
+/*
+** mod_cidr_lookup.c -- Apache sample cidr_lookup module
+** [Autogenerated via ``apxs -n cidr_lookup -g'']
+**
+** To play with this sample module first compile it into a
+** DSO file and install it into Apache's modules directory
+** by running:
+**
+** $ apxs -c -i mod_cidr_lookup.c
+**
+** Then activate it in Apache's httpd.conf file for instance
+** for the URL /cidr_lookup in as follows:
+**
+** # httpd.conf
+** LoadModule cidr_lookup_module modules/mod_cidr_lookup.so
+** <Location /cidr_lookup>
+** SetHandler cidr_lookup
+** </Location>
+**
+** Then after restarting Apache via
+**
+** $ apachectl restart
+**
+** you immediately can request the URL /cidr_lookup and watch for the
+** output of this module. This can be achieved for instance via:
+**
+** $ lynx -mime_header http://localhost/cidr_lookup
+**
+** The output should be similar to the following one:
+**
+** HTTP/1.1 200 OK
+** Date: Tue, 31 Mar 1998 14:42:22 GMT
+** Server: Apache/1.3.4 (Unix)
+** Connection: close
+** Content-Type: text/html
+**
+** The sample page from mod_cidr_lookup.c
+*/
+
+#include "httpd.h"
+#include "http_config.h"
+#include "http_protocol.h"
+#include "http_log.h"
+#include "ap_config.h"
+
+#include "apr.h"
+#include "apr_file_io.h"
+#include "apr_network_io.h"
+#include "apr_strings.h"
+
+#include <string.h>
+
+#define MAX_CIDR_ITEM 1024
+#define get_module_context(c) (cidr_item *)(ap_get_module_config(c, &cidr_lookup_module))
+#define MODULE_NAME cidr_lookup_module
+
+#ifdef DEBUG
+ #define DEBUGLOG(...) ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, NULL, __VA_ARGS__)
+#else
+ #define DEBUGLOG(...)
+#endif
+
+#define ERRORLOG(...) ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, __VA_ARGS__)
+
+module AP_MODULE_DECLARE_DATA cidr_lookup_module;
+
+typedef struct CIDRItem
+{
+ apr_ipsubnet_t *subnet;
+ char *type ;
+
+} cidr_item;
+
+typedef struct CIDRLookupContext
+{
+ apr_pool_t *pool;
+ cidr_item **items;
+ int items_len ;
+
+} cidr_context;
+
+static void *new_cidr_context(const apr_pool_t *p, const server_rec *s)
+{
+ DEBUGLOG("new_cidr_context");
+ cidr_context *c = apr_palloc(p, sizeof(cidr_context));
+ c->items = apr_palloc(p, MAX_CIDR_ITEM * sizeof(cidr_item *));
+ c->items_len = 0 ;
+ DEBUGLOG("new_cidr_context ok");
+ return c;
+}
+
+static char *set_cidr_type(cmd_parms *cmd, void *mconfig, char *filename)
+{
+ DEBUGLOG("set_cidr_type");
+
+ /*
+ * pool
+ */
+ apr_pool_t *p = cmd->pool ;
+
+ /*
+ * cidr_context
+ */
+ cidr_context *c = get_module_context(cmd->server->module_config);
+
+ /*
+ * itmes
+ */
+ cidr_item **items = c->items;
+
+ /*
+ * open file
+ */
+ apr_file_t *file ;
+ if(apr_file_open(&file, filename, APR_READ, APR_OS_DEFAULT, cmd->pool))
+ {
+ DEBUGLOG("file not open [%s]", filename);
+ return NULL;
+ }
+
+
+ /*
+ * read line
+ */
+ char line[255];
+ while( apr_file_gets(line, 255, file) == APR_SUCCESS )
+ {
+ if(c->items_len == MAX_CIDR_ITEM)
+ {
+ ERRORLOG("cidr item max size ...");
+ break;
+ }
+
+ const int line_len = strlen(line);
+ line[ line_len-1 ] = '\0';
+ DEBUGLOG("line:%s", line);
+ if(line[0]=='#' || line_len==0)
+ {
+ continue;
+ }
+
+ char *last=0;
+ const char *type = apr_strtok(line, " ", &last);
+ const char *ipnet = last;
+ last = 0;
+
+ DEBUGLOG("type:%s,ip:%s", type,ipnet);
+
+ const char *ip = apr_strtok(ipnet, "/", &last);
+ const char *numbits = last;
+
+ apr_ipsubnet_t *subnet;
+ if(apr_ipsubnet_create(&subnet, ip, numbits, p)!=APR_SUCCESS)
+ {
+ DEBUGLOG("type:%s,ip:%s,numbits:%s error", type,ip,numbits);
+ continue ;
+ }
+
+ cidr_item *item = (cidr_item *) apr_palloc(p, sizeof(cidr_item));
+ item->type = apr_pstrdup(p, type);
+ item->subnet = subnet;
+ items[c->items_len++] = item;
+ }
+ apr_file_close(file);
+
+ DEBUGLOG("set_cidr_type ok [len:%d]", c->items_len);
+ return NULL;
+}
+
+static char *get_cidr_type(const cidr_context *c, const apr_sockaddr_t *remote_addr)
+{
+ DEBUGLOG("get_cidr_type");
+ DEBUGLOG("get_cidr_type size:%d", c->items_len);
+
+ int i=0; int size= c->items_len;
+ for(i=0; i< size; i++)
+ {
+ DEBUGLOG("get_item %d", i);
+ const cidr_item *item = c->items[i];
+ DEBUGLOG("get_item %d ok", i);
+
+ DEBUGLOG("item->get");
+ const char *type = item->type;
+ DEBUGLOG("item->get type");
+ const apr_ipsubnet_t *subnet = item->subnet ;
+ DEBUGLOG("item->get type subnet ok");
+
+ DEBUGLOG("apr_ipsubnet_test");
+ if(apr_ipsubnet_test(subnet, remote_addr))
+ {
+ DEBUGLOG("apr_ipsubnet_test ok : %s", type);
+ return type;
+ }
+ }
+ return "unknown";
+}
+
+static int cidr_lookup_handler(const request_rec *r)
+{
+ DEBUGLOG("cidr_lookup_handler");
+ const cidr_context *c = get_module_context(r->server->module_config);
+ const char *cidr_type = get_cidr_type(c,r->connection->remote_addr);
+ DEBUGLOG("cidr_lookup ok");
+
+ DEBUGLOG("set header...");
+ apr_table_set(r->subprocess_env, "X_CLIENT_TYPE", cidr_type);
+ apr_table_set(r->headers_in, "X-Client-Type", cidr_type);
+ DEBUGLOG("set header ok");
+ DEBUGLOG("cidr_lookup_handler ok [remote_addr: %s] [X-Client-Type or X_CLIENT_TYPE : %s]"
+ ,r->connection->remote_ip, cidr_type);
+ return OK;
+}
+
+static void cidr_lookup_register_hooks(apr_pool_t *p)
+{
+ DEBUGLOG("cidr_lookup_register_hooks");
+ ap_hook_post_read_request(cidr_lookup_handler, NULL, NULL, APR_HOOK_MIDDLE);
+ ap_hook_header_parser(cidr_lookup_handler, NULL, NULL, APR_HOOK_MIDDLE);
+ DEBUGLOG("cidr_lookup_register_hooks ok");
+}
+
+static const command_rec cidr_module_cmds[] =
+{
+ AP_INIT_TAKE1("CIDRFile", set_cidr_type, NULL, OR_OPTIONS, "cidr file name"),
+ { NULL },
+};
+
+/* Dispatch list for API hooks */
+module AP_MODULE_DECLARE_DATA cidr_lookup_module = {
+ STANDARD20_MODULE_STUFF,
+ NULL, /* create per-dir config structures */
+ NULL, /* merge per-dir config structures */
+ new_cidr_context, /* create per-server config structures */
+ NULL, /* merge per-server config structures */
+ cidr_module_cmds, /* table of config file commands */
+ cidr_lookup_register_hooks /* register hooks */
+};
+

0 comments on commit 55f166b

Please sign in to comment.