<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>雨天阳光 &#187; 互联二亩田</title>
	<atom:link href="http://shit.name/category/sunnyinternet/feed/" rel="self" type="application/rss+xml" />
	<link>http://shit.name</link>
	<description>此人之水镜，见之莹然，若披云雾而睹青天也。</description>
	<lastBuildDate>Wed, 31 Dec 2014 06:21:04 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.33</generator>
	<item>
		<title>纯代码完美实现WordPress内容回复可见</title>
		<link>http://shit.name/pure-code-of-reply-to-read/</link>
		<comments>http://shit.name/pure-code-of-reply-to-read/#comments</comments>
		<pubDate>Thu, 24 Oct 2013 09:56:40 +0000</pubDate>
		<dc:creator><![CDATA[Oo雨天阳光oO]]></dc:creator>
				<category><![CDATA[互联二亩田]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[回复可见]]></category>
		<category><![CDATA[纯代码]]></category>

		<guid isPermaLink="false">http://shit.name/?p=289</guid>
		<description><![CDATA[前两天找到了旧博的备份数据，想着把旧文整理上来也好丰富下久不更新的博客。 整理的第一篇是智商、财商的测试类文章 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>前两天找到了旧博的备份数据，想着把旧文整理上来也好丰富下久不更新的博客。</p>
<p>整理的第一篇是智商、财商的测试类文章——<a title="【转】智商财商大测试" href="http://shit.name/iq-fq-big-test/" target="_blank">《【转】智商财商大测试》</a>，前面是题目，后面是答案。一般网页上这类的都会做分页处理，答案会放在题目的下一页，但是在一篇博文中去做分页或是另开一篇博文公布答案感觉都不合适。所以要是能像论坛那样可设置回复可见，不仅能增强与博友的互动，还能提高博客的访问量，多棒！<br />
<span id="more-289"></span><br />
网上找了一圈，有用各种各样插件达成的，其中提到最多的是“<a title="Easy2Hide" href="http://wordpress.org/plugins/easy2hide/" target="_blank">Easy2Hide</a>”，虽然它代码也蛮短的，但是“插件”二字总让我嫌弃，还是找纯代码的才能抚慰我轻微的强迫症。</p>
<p>搜索不负强迫人，总算找到一段完美的代码可以实现这个功能，具体样式请见已用该效果的博文——<a title="【转】智商财商大测试" href="http://shit.name/iq-fq-big-test/" target="_blank">《【转】智商财商大测试》</a>，我在这里介绍该代码分别对于一般主题和Mossight主题的添加方法。</p>
<p>对于一般主题，只要把这段代码添加到function.php中即可，详细代码如下：</p>
<pre class="brush: php; title: ; notranslate">
function reply_to_read($atts, $content=null) {
extract(shortcode_atts(array(&quot;notice&quot; =&gt; '

温馨提示: 此处内容需要评论本文后才能查看

'), $atts));
$email = null;
$user_ID = (int) wp_get_current_user()-&gt;ID;
if ($user_ID &gt; 0) {
$email = get_userdata($user_ID)-&gt;user_email; //对博主直接显示内容
$admin_email = &quot;on@sunny.lu&quot;; //博主Email
if ($email == $admin_email) {
return $content;
}
} else if (isset($_COOKIE['comment_author_email_' . COOKIEHASH])) {
$email = str_replace('%40', '@', $_COOKIE['comment_author_email_' . COOKIEHASH]);
} else {
return $notice;
}
if (empty($email)) {
return $notice;
}
global $wpdb;
$post_id = get_the_ID();
$query = &quot;SELECT `comment_ID` FROM {$wpdb-&gt;comments} WHERE `comment_post_ID`={$post_id} and `comment_approved`='1' and `comment_author_email`='{$email}' LIMIT 1&quot;;
if ($wpdb-&gt;get_results($query)) {
return do_shortcode($content);
} else {
return $notice;
}
}
add_shortcode('reply', 'reply_to_read');
</pre>
<div class='newcode'><h2>Mossight主题</h2><div class='newcontent'><br />
Mossight主题本身就自带丰富的短代码，我们无需修改function.php，只要把这个“回复可见”添加进短代码库，在使用后台编辑器写文章时就可以很方便地调用了。（话说Mossight主题里有个下拉菜单的短代码可以暂时把想隐藏的内容藏起来，但是既点既现让懒博友又方便了，所以还是回复可见比较好）。</p>
<p>短代码库：wp-content\themes\Mossight\functions\shortcode.php<br />
找到代码</p>
<pre class="brush: php; title: ; notranslate">add_shortcode('toggle','post_toggle');</pre>
<p>另起一行插入代码：</p>
<pre class="brush: php; title: ; notranslate">
//shortcode - self
//reply_to_read
function reply_to_read($atts, $content=null) { 
extract(shortcode_atts(array(&quot;notice&quot; =&gt; '
&lt;blockquote&gt;
&lt;p style=&quot;color: #993300;&quot;&gt;&lt;strong&gt;温馨提示:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;此处内容需要评论本文后才能查看&lt;/p&gt; 
&lt;/blockquote&gt;
'), $atts)); 
$email = null; 
$user_ID = (int) wp_get_current_user()-&gt;ID; 
if ($user_ID &gt; 0) { 
$email = get_userdata($user_ID)-&gt;user_email; //对博主直接显示内容 
$admin_email = &quot;on@sunny.lu&quot;; //博主Email 
if ($email == $admin_email) { 
return $content; 
} 
} else if (isset($_COOKIE['comment_author_email_' . COOKIEHASH])) { 
$email = str_replace('%40', '@', $_COOKIE['comment_author_email_' . COOKIEHASH]); 
} else { 
return $notice; 
} 
if (empty($email)) { 
return $notice; 
} 
global $wpdb; 
$post_id = get_the_ID(); 
$query = &quot;SELECT `comment_ID` FROM {$wpdb-&gt;comments} WHERE `comment_post_ID`={$post_id} and `comment_approved`='1' and `comment_author_email`='{$email}' LIMIT 1&quot;; 
if ($wpdb-&gt;get_results($query)) { 
return do_shortcode($content); 
} else { 
return $notice; 
} 
} 
add_shortcode('reply', 'reply_to_read');
</pre>
<p>然后再找到最后一个QTags.addButton项</p>
<pre class="brush: php; title: ; notranslate">QTags.addButton( 'm31', '下载按钮', '【butdown href=&quot;链接&quot;】标题【/butdown】');//这用【 】防止代码运行，使用时请用[ ]替换</pre>
<p>在其后面新增一项：</p>
<pre class="brush: php; title: ; notranslate">QTags.addButton( 'm32', '回复可见', '【reply】这里输入内容【/reply】');//这用【 】防止代码运行，使用时请用[ ]替换</pre>
<p>就可以了。<br />
</div></div>
<blockquote><p>这段代码的显示样式是可以设定的，只要用Html标签和样式把“温馨提示: 此处内容需要评论本文后才能查看”美化即可,比如在Mossight主题里的，我使用了blockquote标签、p标签和css，博友们可以根据自己的需求更改。</p>
<p><span style="color: #993300;"><strong>使用方法：</strong></span><br />
一般主题：在后台编辑文章时使用“【reply】【/reply】”把要隐藏的内容包起来即可（这用【 】防止代码运行，使用时请用[ ]替换）<br />
Mossight主题：修改好shortcode.php后，进后台编辑器直接点“回复可见”按钮插入</p>
<p><strong>“回复可见”这个功能虽有优点，但也是有缺点的</strong><br />
<span style="color: #993300;"><strong>优点：</strong></span>增强与博友的互动，提高博客的访问量<br />
<span style="color: #993300;"><strong>缺点：</strong></span>回复可见不是实时的，内容要等评论审核通过后才可见</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://shit.name/pure-code-of-reply-to-read/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>在CentOS下搭建OpenVPN</title>
		<link>http://shit.name/openvpn-on-centos/</link>
		<comments>http://shit.name/openvpn-on-centos/#comments</comments>
		<pubDate>Mon, 17 Sep 2012 16:51:50 +0000</pubDate>
		<dc:creator><![CDATA[Oo雨天阳光oO]]></dc:creator>
				<category><![CDATA[互联二亩田]]></category>
		<category><![CDATA[OpenVPN]]></category>

		<guid isPermaLink="false">http://shit.name/?p=208</guid>
		<description><![CDATA[国内网络环境不怎么好，为了更好地在互联网中学习和娱乐，弄部梯子也是有必要的，当然市售的有很多，但不折腾下怎么能 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-207" title="ladder" src="http://shit.name/wp-content/uploads/2012/09/ladder.jpg" alt="" width="614" height="140" /><br />
国内网络环境不怎么好，为了更好地在互联网中学习和娱乐，弄部梯子也是有必要的，当然市售的有很多，但不折腾下怎么能睡得着觉呢 ^_^</p>
<p>那梯子各式各样，为什么用OpenVPN而不用PPTP或是更简单的SSH呢？SSH是简单但是打开网页多了，它也就力不从心了；OpenVPN比PPTP好伺候，能装在基于OpenVZ虚拟化技术的VPS上，当然装Xen上更好（PPTP只能装在基于Xen的VPS，但大多数VPS因为成本原因是OpenVZ的），而且它穿透性更强，功能更强大，比如能穿透中国移动的cmwap代理实现cmnet的功能。</p>
<p>我这篇讲解在CentOS 5.6上的搭建过程，如有童鞋需要在Debian上的搭建方法，请参考我的另外一篇博文<a title="在Debian下搭建OpenVPN" href="http://shit.name/openvpn-on-debian/" target="_blank">《在Debian下搭建OpenVPN》</a>。</p>
<p>为什么会写两篇基于不同系统的教程呢？这里有个小插曲，同事L小姐在MAC下更换授权文件时没有全部更换完，加上本人对<a href="http://code.google.com/p/tunnelblick/">tunnelblick</a>不熟，以为是VPN服务端的原因，所以就在两种系统下都进行了搭建，然后调整各种参数……最后的最后L小姐以初次安装时的配置方法去配置授权文件才结束了这次各种折腾。虽然过程有些曲折，不过收获还是蛮多，优化后的速度比之前快了许多。</p>
<p>好了，下面就准备动手折腾吧：</p>
<p>首先得准备个VPS，起步配置很低，128M内存的就OK，要有哪位看客非得用32G独服来搭，那爷您里边请，VIP雅座——美女水果、茶水点心伺候。<span id="more-208"></span></p>
<p><span style="color: #000000;"><strong>一、OpenVPN需要tun和iptables_nat模块支持，所以先检查你的VPS支持不支持。</strong></span><br />
SSH登陆VPS后输入</p>
<pre class="brush: plain; title: ; notranslate">cat /dev/net/tun</pre>
<p>若返回信息为：cat: /dev/net/tun: File descriptor in bad state 说明tun/tap已经可以使用；<br />
如果返回：cat: /dev/net/tun: No such device 或其他则说明tun/tap没有被正确配置，发TK联系客服申请开通tun/tap。</p>
<p>然后检查iptables_nat模块（如果还没有安装iptables，先安装）</p>
<pre class="brush: plain; title: ; notranslate">yum install iptables    #如已安装跳过</pre>
<p>如果你的VPS是Xen或KVM的请输入：（eth0要根据具体的网卡标识来，可以通过ifconfig查看）</p>
<pre class="brush: plain; title: ; notranslate">iptables -t nat -A POSTROUTING -s 10.168.0.0/16 -o eth0 -j MASQUERADE</pre>
<p>如果是OpenVZ的请输入：（11.22.33.44是你VPS的IP）</p>
<pre class="brush: plain; title: ; notranslate">iptables -t nat -A POSTROUTING -s 10.168.0.0/16 -j SNAT --to-source 11.22.33.44</pre>
<p>然后用</p>
<pre class="brush: plain; title: ; notranslate">iptables -t nat -L</pre>
<p>查看iptables转发状态，如果显示以下类似的结果则说明转发成功：<br />
target             prot opt source                      destination<br />
SNAT              all -- 10.168.0.0/16                anywhere                    to:11.22.33.44</p>
<p><span style="color: #000000;"><strong>二、tun和iptables_nat模块检测通过后，正式进入安装阶段：</strong></span><br />
默认情况下CentOS的yum源是没有OpenVPN的（不像Debian很多东西都可以一键apt-get install，这也是我比较喜欢Debian的原因），不过安装<a href="https://fedoraproject.org/wiki/EPEL">EPEL</a>这个东西成功后,yum源里面就有OpenVPN了。<br />
5.x版系统安装EPEL：</p>
<pre class="brush: plain; title: ; notranslate">rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm</pre>
<p>6.x版系统安装EPEL：</p>
<pre class="brush: plain; title: ; notranslate">rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-5.noarch.rpm</pre>
<p>有的童鞋可能会说直接去openvpn.net下载安装包手动编译好了，个人认为手动编译听起来是比较cool,不过没有yum方便，像这里OpenVPN需要lzo支持，安装的时候会自动检测系统，把lzo也安装进去,手动的话还需要编译其他没有的组件。</p>
<pre class="brush: plain; title: ; notranslate">yum -y install openvpn</pre>
<p>对了，在所有步骤之前（就是刚登陆进来时），最好先更新升级下系统，这样可以自动分析当前系统的安装环境并根据具体命令网络升级安装所需组件，更便于之后组件的安装。</p>
<pre class="brush: plain; title: ; notranslate">yum -y update</pre>
<p><span style="color: #000000;"><strong>三、安装好后，先使用easy-rsa生成服务端证书：</strong></span><br />
默认OpenVPN的easy-rsa文档会在/usr/share/doc/openvpn/examples/easy-rsa/，如果不在的话请先检查是否安装成功然后用locate或find命令查找该文档。然后将该文档下所需的配置文件复制到/etc/openvpn/下面：</p>
<pre class="brush: plain; title: ; notranslate">cp -r /usr/share/doc/openvpn/examples/easy-rsa/ /etc/openvpn/</pre>
<blockquote><p>根据网友<a title="kuge" href="#comment-198">@kuge</a>反映的情况——CentOS 6 系统升级后，如果安装的是OpenVPN2.3版本，执行这一步时OpenVPN2.3版本报错，显示：</p>
<pre class="brush: plain; title: ; notranslate">cp : cannot stat ' /usr/share/doc/openvpn/examples/easy-rsa/ ' no such file or dictionary</pre>
<p>解决办法：（感谢<a title="kuge" href="#comment-198">@kuge</a>）</p>
<pre class="brush: plain; title: ; notranslate">wget https://cloud.github.com/downloads/OpenVPN/easy-rsa/easy-rsa-2.2.0_master.tar.gz
tar -zxvf easy-rsa-2.2.0_master.tar.gz
cp -R easy-rsa-2.2.0_master/easy-rsa/ /etc/openvpn/</pre>
</blockquote>
<p><span style="color: #000000;">1、生成CA证书：</span></p>
<pre class="brush: plain; title: ; notranslate">cd /etc/openvpn/easy-rsa/2.0
source vars
./clean-all
./build-ca</pre>
<p>期间会提示输入一些信息，直接回车默认即可。</p>
<p><span style="color: #000000;">2、生成服务器端证书和密钥：（server为服务端名字可以自定义）</span></p>
<pre class="brush: plain; title: ; notranslate">./build-key-server server</pre>
<p>期间也会提示输入一些信息，直接回车默认，选择[Y/n]的都选Y。</p>
<p><span style="color: #000000;">3、生成客户端证书和密钥：（client为客户端名字可以自定义，注意这里的客户端名字不能与上步的服务端名字相同）</span></p>
<pre class="brush: plain; title: ; notranslate">./build-key client</pre>
<p>期间也会提示输入一些信息，直接回车默认，选择[Y/n]的都选Y。 若要生成多个客户端的证书和密钥，将client改成另外的名字重复操作即可。所有生成的证书和密钥存都放在/etc/openvpn/easy-rsa/2.0/keys/下面。</p>
<p><span style="color: #000000;">4、生成Diffie Hellman参数：</span></p>
<pre class="brush: plain; title: ; notranslate">./build-dh</pre>
<p><span style="color: #000000;"><strong>四、配置OpenVPN服务器端文件</strong></span></p>
<p><span style="color: #000000;">1、编辑/etc/openvpn/server.conf 文件，如果没有可以创建一个，加入下面的内容：（复制到VPS上时，最好把注释去掉）</span></p>
<pre class="brush: plain; title: ; notranslate">
local 11.22.33.44    #11.22.33.44为VPS的IP
port 8080    #端口，需要与客户端配置保持一致，并保证与其他软件无共用
proto udp    #使用协议，需要与客户端配置保持一致
dev tun      #也可以选择tap模式
ca /etc/openvpn/easy-rsa/2.0/keys/ca.crt
cert /etc/openvpn/easy-rsa/2.0/keys/server.crt
key /etc/openvpn/easy-rsa/2.0/keys/server.key
dh /etc/openvpn/easy-rsa/2.0/keys/dh1024.pem

ifconfig-pool-persist ipp.txt

server 10.168.1.0 255.255.255.0    #给客户的分配的局域网IP段，注意不要与客户端网段冲突！

push &amp;quot;redirect-gateway&amp;quot;
push &amp;quot;dhcp-option DNS 8.8.8.8&amp;quot;
push &amp;quot;dhcp-option DNS 8.8.4.4&amp;quot;
client-to-client

;duplicate-cn        #若不止一人同时使用该证书，请去掉前面的;

keepalive 20 60
comp-lzo
max-clients 50

persist-key
persist-tun

status openvpn-status.log
log-append openvpn.log

verb 3
mute 20</pre>
<p><span style="color: #000000;">2、设置IP转发，其实刚检查iptables_nat模块的步骤就是在设置转发IP，再重复一遍：</span><br />
若Xen或KVM的请输入：（eth0要根据具体的网卡标示来，可以通过ifconfig查看）</p>
<pre class="brush: plain; title: ; notranslate">iptables -t nat -A POSTROUTING -s 10.168.0.0/16 -o eth0 -j MASQUERADE</pre>
<p>若OpenVZ的请输入：（11.22.33.44是你VPS的IP）</p>
<pre class="brush: plain; title: ; notranslate">iptables -t nat -A POSTROUTING -s 10.168.0.0/16 -j SNAT --to-source 11.22.33.44</pre>
<p><span style="color: #000000;">3、修改/etc/sysctl.conf的内容为：</span></p>
<pre class="brush: plain; title: ; notranslate">
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0</pre>
<p>重新载入/etc/sysctl.conf使其生效，执行如下命令：</p>
<pre class="brush: plain; title: ; notranslate">sysctl -p</pre>
<blockquote><p><span style="color: #993300;"><strong>至此VPS上的服务器端配置就全部完成了。</strong></span><br />
若想使其配置生效，则需重启OpenVPN服务</p>
<pre class="brush: plain; title: ; notranslate">service openvpn restart --config /etc/openvpn/server.conf</pre>
<p>由于VPS可能会因为各种原因被重启，为了方便，可以使用cron写个小脚本让VPS在启动时自动运行OpenVPN服务并设置iptables_nat转发。<br />
创建/root/vpn.sh 文件，加入下面的内容：（复制到VPS上时，最好把注释去掉）</p>
<pre class="brush: plain; title: ; notranslate">
#!/bin/bash
/sbin/service openvpn start --config /etc/openvpn/server.conf   #CentOS下好像OpenVPN不能开机就自动运行，所以这里设置下，如可以自动运行就去掉这句
/sbin/iptables -t nat -A POSTROUTING -s 10.168.0.0/16 -j SNAT --to-source 11.22.33.44    #实际根据自己VPS来做调整，这只做示例</pre>
<p>然后将这一脚本写入cron执行计划</p>
<pre class="brush: plain; title: ; notranslate">crontab -e

@reboot /bin/bash /root/vpn.sh &amp;gt;/dev/null 2&amp;gt;&amp;amp;1</pre>
</blockquote>
<p><span style="color: #000000;"><strong>五、安装配置OpenVPN客户端文件</strong></span></p>
<p>1、下载安装客户端<br />
Windwos用户推荐去<a href="http://openvpn.net/index.php/download.html">openvpn.net</a>选择合适的版本进行安装，安装中的选项全部按默认即可；Mac用户推荐使用<a href="http://code.google.com/p/tunnelblick/">tunnelblick</a>。</p>
<p>2、下载之前在VPS上生成的客户端证书及密钥<br />
证书和密钥存都在/etc/openvpn/easy-rsa/2.0/keys/里，可以使用winscp链接到VPS上下载，将<span style="color: #993300;">ca.crt、client.crt、client.key</span>这三个文件下载到OpenVPN客户端程序的config文件夹里,默认为：C:\Program Files\OpenVPN\config</p>
<p>3、创建客户端配置文件<br />
在C:\Program Files\OpenVPN\config 下面创建一个sunny.ovpn的文件，添加如下内容：</p>
<pre class="brush: plain; title: ; notranslate">
client        #这个client不是自定义名称 不能更改
dev tun       #要与前面server.conf中的配置一致。
proto udp              #要与前面server.conf中的配置一致。
remote 11.22.33.44 8080    #将11.22.33.44替换为你VPS的IP，端口与前面的server.conf中配置一致。
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt              #具体名称以刚下载的为准
cert client.crt              #具体名称以刚下载的为准
key client.key              #具体名称以刚下载的为准
ns-cert-type server
redirect-gateway
keepalive 20 60
#tls-auth ta.key 1
comp-lzo
verb 3
mute 20
route-method exe
route-delay 2</pre>
<p><span style="color: #000000;"><strong>六、运行OpenVPN客户端</strong></span><br />
运行OpenVPN GUI后，屏幕右下角的系统托盘区，会显示一个由两个红屏电脑组成的图标，右击它，选中菜单中添加的名为sunny的服务器，点击Connect，过一会儿，OpenVPN图标变成绿色时就表示链接成功了。</p>
<blockquote><p><span style="color: #993300;"><strong>文章最后给大家推荐个性价比超高的搭梯用VPS</strong></span><br />
<a title="123systems搭梯VPS专用" href="http://shit.name/go/123systems/" target="_blank">123systems</a>的VPS用来搭梯是个非常不错的选择——稳定、速度也不错 128M的每月有250G的流量可以用 而且更关键的是年付才10刀（192M的每月500G流量 年付15刀）比起市售的各种梯子都划算而且有保障 自用或者与朋友分享都比较不错<br />
<a title="123systems搭梯VPS专用" href="http://shit.name/go/123systems/" target="_blank">去看看吧 ：）</a></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://shit.name/openvpn-on-centos/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>在Debian下搭建OpenVPN</title>
		<link>http://shit.name/openvpn-on-debian/</link>
		<comments>http://shit.name/openvpn-on-debian/#comments</comments>
		<pubDate>Sun, 16 Sep 2012 12:30:43 +0000</pubDate>
		<dc:creator><![CDATA[Oo雨天阳光oO]]></dc:creator>
				<category><![CDATA[互联二亩田]]></category>
		<category><![CDATA[OpenVPN]]></category>

		<guid isPermaLink="false">http://shit.name/?p=193</guid>
		<description><![CDATA[国内网络环境不怎么好，为了更好地在互联网中学习和娱乐，弄部梯子也是有必要的，当然市售的有很多，但不折腾下怎么能 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-194" title="stepladder" src="http://shit.name/wp-content/uploads/2012/09/stepladder.jpg" alt="" width="614" height="140" /><br />
国内网络环境不怎么好，为了更好地在互联网中学习和娱乐，弄部梯子也是有必要的，当然市售的有很多，但不折腾下怎么能睡得着觉呢 ^_^</p>
<p>那梯子各式各样，为什么用OpenVPN而不用PPTP或是更简单的SSH呢？SSH是简单但是打开网页多了，它也就力不从心了；OpenVPN比PPTP好伺候，能装在基于OpenVZ虚拟化技术的VPS上，当然装Xen上更好（PPTP只能装在基于Xen的VPS，但大多数VPS因为成本原因是OpenVZ的），而且它穿透性更强，功能更强大，比如能穿透中国移动的cmwap代理实现cmnet的功能。</p>
<p>我先讲解在Debian 6.0上的搭建过程（Ubuntu下也同样适用），如有童鞋需要在CentOS上的搭建方法，请参考我的另外一篇博文<a title="在CentOS下搭建OpenVPN" href="http://shit.name/openvpn-on-centos/" target="_blank">《在CentOS下搭建OpenVPN》</a>。<br />
好了，下面就准备动手折腾吧：</p>
<p>首先得准备个VPS，起步配置很低，128M内存的就OK，要有哪位看客非得用32G独服来搭，那爷您里边请，VIP雅座——美女水果、茶水点心伺候。<span id="more-193"></span></p>
<p><span style="color: #000000;"><strong>一、OpenVPN需要tun和iptables_nat模块支持，所以先检查你的VPS支持不支持。</strong></span><br />
SSH登陆VPS后输入</p>
<pre class="brush: plain; title: ; notranslate">cat /dev/net/tun</pre>
<p>若返回信息为：cat: /dev/net/tun: File descriptor in bad state  说明tun/tap已经可以使用；<br />
如果返回：cat: /dev/net/tun: No such device  或其他则说明tun/tap没有被正确配置，发TK联系客服申请开通tun/tap。</p>
<p>然后检查iptables_nat模块（如果还没有安装iptables，先安装）</p>
<pre class="brush: plain; title: ; notranslate">apt-get install iptables    #如已安装跳过</pre>
<p>如果你的VPS是Xen或KVM的请输入：（eth0要根据具体的网卡标识来，可以通过ifconfig查看）</p>
<pre class="brush: plain; title: ; notranslate">iptables -t nat -A POSTROUTING -s 10.168.0.0/16 -o eth0 -j MASQUERADE</pre>
<p>如果是OpenVZ的请输入：（11.22.33.44是你VPS的IP）</p>
<pre class="brush: plain; title: ; notranslate">iptables -t nat -A POSTROUTING -s 10.168.0.0/16 -j SNAT --to-source 11.22.33.44</pre>
<p>然后用</p>
<pre class="brush: plain; title: ; notranslate">iptables -t nat -L</pre>
<p>查看iptables转发状态，如果显示以下类似的结果则说明转发成功：<br />
target           prot opt source                  destination<br />
SNAT            all -- 10.168.0.0/16             anywhere                 to:11.22.33.44</p>
<p><span style="color: #000000;"><strong>二、tun和iptables_nat模块检测通过后，正式进入安装阶段：</strong></span><br />
OpenVPN需要lzo支持，可以OpenVPN与lzo一起安装：</p>
<pre class="brush: plain; title: ; notranslate">apt-get install openvpn lzop</pre>
<p>如果执行安装提示没有可用的安装包，那先更新升级下系统</p>
<pre class="brush: plain; title: ; notranslate">apt-get update &amp;&amp; apt-get dist-upgrade</pre>
<p><span style="color: #000000;"><strong>三、安装好后，先使用easy-rsa生成服务端证书：</strong></span><br />
默认OpenVPN的easy-rsa文档会在/usr/share/doc/openvpn/examples/easy-rsa/，如果不在的话请先检查是否安装成功然后用locate或find命令查找该文档。然后将该文档下所需的配置文件复制到/etc/openvpn/下面：</p>
<pre class="brush: plain; title: ; notranslate">cp -r /usr/share/doc/openvpn/examples/easy-rsa/ /etc/openvpn/</pre>
<p><span style="color: #000000;">1、生成CA证书：</span></p>
<pre class="brush: plain; title: ; notranslate">cd /etc/openvpn/easy-rsa/2.0
source vars
./clean-all
./build-ca</pre>
<p>期间会提示输入一些信息，直接回车默认即可。</p>
<p><span style="color: #000000;">2、生成服务器端证书和密钥：（server为服务端名字可以自定义）</span></p>
<pre class="brush: plain; title: ; notranslate">./build-key-server server</pre>
<p>期间也会提示输入一些信息，直接回车默认，选择[Y/n]的都选Y。</p>
<p><span style="color: #000000;">3、生成客户端证书和密钥：（client为客户端名字可以自定义，注意这里的客户端名字不能与上步的服务端名字相同）</span></p>
<pre class="brush: plain; title: ; notranslate">./build-key client</pre>
<p>期间也会提示输入一些信息，直接回车默认，选择[Y/n]的都选Y。 若要生成多个客户端的证书和密钥，将client改成另外的名字重复操作即可。所有生成的证书和密钥存都放在/etc/openvpn/easy-rsa/2.0/keys/下面。</p>
<p><span style="color: #000000;">4、生成Diffie Hellman参数：</span></p>
<pre class="brush: plain; title: ; notranslate">./build-dh</pre>
<p><span style="color: #000000;"><strong>四、配置OpenVPN服务器端文件</strong></span><br />
<span style="color: #000000;">1、编辑/etc/openvpn/server.conf 文件，如果没有可以创建一个，加入下面的内容：（复制到VPS上时，最好把注释去掉）</span></p>
<pre class="brush: plain; title: ; notranslate">
local 11.22.33.44    #11.22.33.44为VPS的IP
port 8080    #端口，需要与客户端配置保持一致，并保证与其他软件无共用
proto udp    #使用协议，需要与客户端配置保持一致
dev tun      #也可以选择tap模式
ca /etc/openvpn/easy-rsa/2.0/keys/ca.crt
cert /etc/openvpn/easy-rsa/2.0/keys/server.crt
key /etc/openvpn/easy-rsa/2.0/keys/server.key
dh /etc/openvpn/easy-rsa/2.0/keys/dh1024.pem

ifconfig-pool-persist ipp.txt

server 10.168.1.0 255.255.255.0    #给客户的分配的局域网IP段，注意不要与客户端网段冲突！

push &quot;redirect-gateway&quot;
push &quot;dhcp-option DNS 8.8.8.8&quot;
push &quot;dhcp-option DNS 8.8.4.4&quot;
client-to-client

;duplicate-cn        #若不止一人同时使用该证书，请去掉前面的;

keepalive 20 60
comp-lzo
max-clients 50

persist-key
persist-tun

status openvpn-status.log
log-append openvpn.log

verb 3
mute 20</pre>
<p><span style="color: #000000;">2、设置IP转发，其实刚检查iptables_nat模块的步骤就是在设置转发IP，再重复一遍：</span><br />
若Xen或KVM的请输入：（eth0要根据具体的网卡标示来，可以通过ifconfig查看）</p>
<pre class="brush: plain; title: ; notranslate">iptables -t nat -A POSTROUTING -s 10.168.0.0/16 -o eth0 -j MASQUERADE</pre>
<p>若OpenVZ的请输入：（11.22.33.44是你VPS的IP）</p>
<pre class="brush: plain; title: ; notranslate">iptables -t nat -A POSTROUTING -s 10.168.0.0/16 -j SNAT --to-source 11.22.33.44</pre>
<p><span style="color: #000000;">3、修改/etc/sysctl.conf的内容为：</span></p>
<pre class="brush: plain; title: ; notranslate">
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0</pre>
<p>重新载入/etc/sysctl.conf使其生效，执行如下命令：</p>
<pre class="brush: plain; title: ; notranslate">sysctl -p</pre>
<blockquote><p><span style="color: #993300;"><strong>至此VPS上的服务器端配置就全部完成了。</strong></span><br />
若想使其配置生效，则需重启OpenVPN服务</p>
<pre class="brush: plain; title: ; notranslate">/etc/init.d/openvpn restart</pre>
<p>由于VPS可能会因为各种原因被重启，为了方便可以使用cron写个小脚本让VPS在启动时自动运行OpenVPN服务并设置iptables_nat转发。<br />
创建/root/vpn.sh 文件，加入下面的内容：（复制到VPS上时，最好把注释去掉）</p>
<pre class="brush: plain; title: ; notranslate">
#!/bin/bash
/etc/init.d/openvpn start  #Debian下好像OpenVPN不用设置就可以开机自动运行，如可以自动运行就去掉这句
/sbin/iptables -t nat -A POSTROUTING -s 10.168.0.0/16 -j SNAT --to-source 11.22.33.44    #实际根据自己VPS来做调整，这只做示例</pre>
<p>然后将这一脚本写入cron执行计划</p>
<pre class="brush: plain; title: ; notranslate">crontab -e

@reboot /bin/bash /root/vpn.sh &gt;/dev/null 2&gt;&amp;1</pre>
</blockquote>
<p><span style="color: #000000;"><strong>五、安装配置OpenVPN客户端文件</strong></span></p>
<p>1、下载安装客户端<br />
Windwos用户推荐去<a href="http://openvpn.net/index.php/download.html">openvpn.net</a>选择合适的版本进行安装，安装中的选项全部按默认即可；Mac用户推荐使用<a href="http://code.google.com/p/tunnelblick/">tunnelblick</a>。</p>
<p>2、下载之前在VPS上生成的客户端证书及密钥<br />
证书和密钥存都在/etc/openvpn/easy-rsa/2.0/keys/里，可以使用winscp链接到VPS上下载，将<span style="color: #993300;">ca.crt、client.crt、client.key</span>这三个文件下载到OpenVPN客户端程序的config文件夹里,默认为：C:\Program Files\OpenVPN\config</p>
<p>3、创建客户端配置文件<br />
在C:\Program Files\OpenVPN\config 下面创建一个sunny.ovpn的文件，添加如下内容：</p>
<pre class="brush: plain; title: ; notranslate">
client        #这个client不是自定义名称 不能更改
dev tun       #要与前面server.conf中的配置一致。
proto udp              #要与前面server.conf中的配置一致。
remote 11.22.33.44 8080    #将11.22.33.44替换为你VPS的IP，端口与前面的server.conf中配置一致。
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt              #具体名称以刚下载的为准
cert client.crt              #具体名称以刚下载的为准
key client.key              #具体名称以刚下载的为准
ns-cert-type server
redirect-gateway
keepalive 20 60
#tls-auth ta.key 1
comp-lzo
verb 3
mute 20
route-method exe
route-delay 2</pre>
<p><span style="color: #000000;"><strong>六、运行OpenVPN客户端</strong></span><br />
运行OpenVPN GUI后，屏幕右下角的系统托盘区，会显示一个由两个红屏电脑组成的图标，右击它，选中菜单中添加的名为sunny的服务器，点击Connect，过一会儿，OpenVPN图标变成绿色时就表示链接成功了。</p>
<blockquote><p><span style="color: #993300;"><strong>文章最后给大家推荐个性价比超高的搭梯用VPS</strong></span><br />
<a title="123systems搭梯VPS专用" href="http://shit.name/go/123systems/" target="_blank">123systems</a>的VPS用来搭梯是个非常不错的选择——稳定、速度也不错 128M的每月有250G的流量可以用 而且更关键的是年付才10刀（192M的每月500G流量 年付15刀）比起市售的各种梯子都划算而且有保障 自用或者与朋友分享都比较不错<br />
<a title="123systems搭梯VPS专用" href="http://shit.name/go/123systems/" target="_blank">去看看吧 ：）</a></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://shit.name/openvpn-on-debian/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>利用WP原生函数纯代码实现WordPress归档页面模板</title>
		<link>http://shit.name/pure-code-of-archives/</link>
		<comments>http://shit.name/pure-code-of-archives/#comments</comments>
		<pubDate>Wed, 15 Aug 2012 10:24:26 +0000</pubDate>
		<dc:creator><![CDATA[Oo雨天阳光oO]]></dc:creator>
				<category><![CDATA[互联二亩田]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[原生函数]]></category>
		<category><![CDATA[归档]]></category>
		<category><![CDATA[纯代码]]></category>

		<guid isPermaLink="false">http://shit.name/?p=174</guid>
		<description><![CDATA[比起其他的收费主题，Mossight唯一的不足就是没有现成的归档页面，个人觉得归档页面对于一个博客来说还是比较 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><img src="http://shit.name/wp-content/uploads/2012/08/archives.jpg" alt="" title="archives" width="614" height="140" class="alignnone size-full wp-image-175" /><br />
比起其他的收费主题，<a title="Mossight收费主题" href="http://mossight.jezzzz.me/" target="_blank">Mossight</a>唯一的不足就是没有现成的归档页面，个人觉得归档页面对于一个博客来说还是比较重要，所以自己动手，丰衣足食。</p>
<p>对于这个问题最简单的办法就是弄个归档页面的插件。不过插件让WordPress越发臃肿，所以秉着简洁高效的原则，我还是想通过代码来实现这个功能，通过"谷哥"和"度娘"，半个码农的我也是能解决这个问题的。</p>
<p>网上看下来，关于用代码写归档页大致有两种，一是直接使用 SQL 语句实现调用，另外一种是使用WordPress 推荐的 WP_Query() 函数来调用。WordPress 官方不提倡直接使用 SQL 语句实现，所以我会参照后面一种方法进行。</p>
<p>下面方法每个步骤都分两种，一种是对于一般主题，另外一种是针对<a title="Mossight收费主题" href="http://mossight.jezzzz.me/">Mossight</a>主题的。如各位看客需了解其原理，请移步<strong>原创作者：</strong><a title="WordPress归档页面" href="http://zww.me/archives/25589" target="_blank">ZWWoOoOo</a>（毕竟借鉴他人的没有那么足底气跟人讲解 呵呵）查看具体效果：请看<a title="归档" href="http://shit.name/archives" target="_blank">我博客的存档页</a><span id="more-174"></span></p>
<p>1、原教程说把下面的函数扔到所用主题的 functions.php 文件（ 为了避免中文乱码，将其转化为UTF8 无 BOM 格式）里面：</p>
<pre class="brush: php; title: ; notranslate">/* Archives list by zwwooooo | http://zww.me */
 function zww_archives_list() {
     if( !$output = get_option('zww_archives_list') ){
         $output = '&lt;div id=&quot;archives&quot;&gt;&lt;p&gt;[&lt;a id=&quot;al_expand_collapse&quot; href=&quot;#&quot;&gt;全部展开/收缩&lt;/a&gt;] &lt;em&gt;(注: 点击月份可以展开)&lt;/em&gt;&lt;/p&gt;';
         $the_query = new WP_Query( 'posts_per_page=-1&amp;ignore_sticky_posts=1' ); //update: 加上忽略置顶文章
         $year=0; $mon=0; $i=0; $j=0;
         while ( $the_query-&gt;have_posts() ) : $the_query-&gt;the_post();
             $year_tmp = get_the_time('Y');
             $mon_tmp = get_the_time('m');
             $y=$year; $m=$mon;
             if ($mon != $mon_tmp &amp;&amp; $mon &gt; 0) $output .= '&lt;/ul&gt;&lt;/li&gt;';
             if ($year != $year_tmp &amp;&amp; $year &gt; 0) $output .= '&lt;/ul&gt;';
             if ($year != $year_tmp) {
                 $year = $year_tmp;
                 $output .= '&lt;h3 class=&quot;al_year&quot;&gt;'. $year .' 年&lt;/h3&gt;&lt;ul class=&quot;al_mon_list&quot;&gt;'; //输出年份
             }
             if ($mon != $mon_tmp) {
                 $mon = $mon_tmp;
                 $output .= '&lt;li&gt;&lt;span class=&quot;al_mon&quot;&gt;'. $mon .' 月&lt;/span&gt;&lt;ul class=&quot;al_post_list&quot;&gt;'; //输出月份
             }
             $output .= '&lt;li&gt;'. get_the_time('d日: ') .'&lt;a href=&quot;'. get_permalink() .'&quot;&gt;'. get_the_title() .'&lt;/a&gt; &lt;em&gt;('. get_comments_number('0', '1', '%') .')&lt;/em&gt;&lt;/li&gt;'; //输出文章日期和标题
         endwhile;
         wp_reset_postdata();
         $output .= '&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;';
         update_option('zww_archives_list', $output);
     }
     echo $output;
 }
 function clear_zal_cache() {
     update_option('zww_archives_list', ''); // 清空 zww_archives_list
 }
 add_action('save_post', 'clear_zal_cache'); // 新发表文章/修改文章时</pre>
<div class='newcode'><h2>Mossight主题</h2><div class='newcontent'>本人启用了Mossight-child子主题，而且这个子主题里的functions.php 文件内容为空，所以直接将上面代码复制到functions.php 文件内即可</div></div>
<p>2. 复制一份主题的 page.php 更名为 archives.php，然后在最顶端加入：</p>
<pre class="brush: php; title: ; notranslate">&lt;?php
/* Template Name: archives */
?&gt;</pre>
<p>然后找到其中类似</p>
<pre class="brush: php; title: ; notranslate">&lt;?php content(); ?&gt;</pre>
<p>代码，在其下面加入如下代码：</p>
<pre class="brush: php; title: ; notranslate">&lt;!--archives--&gt;
&lt;?php zww_archives_list(); ?&gt;
&lt;!--archives end--&gt;</pre>
<p>然后进wp后台添加一新页面，在右侧栏模板选择 archives。<br />
<div class='newcode'><h2>Mossight主题</h2><div class='newcontent'>将Mossight文件夹里的page.php按照上面的方法更改即可。</div></div></p>
<p>3. 给主题加载 jQuery 库，没有加载的，把下面这句扔到 functions.php 里面就行了。</p>
<pre class="brush: php; title: ; notranslate">wp_enqueue_script('jquery');</pre>
<div class='newcode'><h2>Mossight主题</h2><div class='newcontent'>由于我已启用主题调用的新浪jquery库，所以这步省略。</div></div>
<p>4. jQuery 效果代码</p>
<pre class="brush: jscript; title: ; notranslate">jQuery(document).ready(function($){
 //===================================存档页面 jQ伸缩
     (function(){
         $('#al_expand_collapse,#archives span.al_mon').css({cursor:&quot;s-resize&quot;});
         $('#archives span.al_mon').each(function(){
             var num=$(this).next().children('li').size();
             var text=$(this).text();
             $(this).html(text+'&lt;em&gt; ( '+num+' 篇文章 )&lt;/em&gt;');
         });
         var $al_post_list=$('#archives ul.al_post_list'),
             $al_post_list_f=$('#archives ul.al_post_list:first');
         $al_post_list.hide(1,function(){
             $al_post_list_f.show();
         });
         $('#archives span.al_mon').click(function(){
             $(this).next().slideToggle(400);
             return false;
         });
         $('#al_expand_collapse').toggle(function(){
             $al_post_list.show();
         },function(){
             $al_post_list.hide();
         });
     })();
 });</pre>
<p>PS：不知道怎么写js文件调用的就直接打开 header.php 并找到
<pre class="brush: php; title: ; notranslate">&lt;?php wp_head(); ?&gt;</pre>
<p>在其下面加上</p>
<pre class="brush: jscript; title: ; notranslate">&lt;script type=&quot;text/javascript&quot;&gt;上面那段jQuery代码&lt;/script&gt;</pre>
<div class='newcode'><h2>Mossight主题</h2><div class='newcontent'>个人认为上面那段jQuery效果代码应该只作用于归档页面，如果贸然添加到header.php这全局文件可能会影响到别页面的其他jQuery效果，所以我将上面那段代码添加到之前创建的archives.php文件里，找到
<pre class="brush: php; title: ; notranslate">&lt;?php get_header()?&gt;</pre>
<p>并在下面添加</p>
<pre class="brush: jscript; title: ; notranslate">&lt;script type=&quot;text/javascript&quot;&gt;上面那段jQuery代码&lt;/script&gt;</pre>
<p>即可。</div></div>
<blockquote><p><span style="color: #993300;">到这步，归档页面的功能已经都实现了，如果对其原始的样式不满意，还可以根据下面给出的HTML结构进行美化。</span></p></blockquote>
<p>5.css根据需要写，不写也可以。HTML结构如下：
<pre class="brush: php; title: ; notranslate">&lt;div id=&quot;archives&quot;&gt;
     &lt;p&gt;[&lt;a id=&quot;al_expand_collapse&quot; href=&quot;#&quot;&gt;全部展开/收缩&lt;/a&gt;] &lt;em&gt;(注: 点击月份可以展开)&lt;/em&gt;&lt;/p&gt;
     &lt;h3 class=&quot;al_year&quot;&gt;'年份&lt;/h3&gt;
     &lt;ul class=&quot;al_mon_list&quot;&gt;
         &lt;li&gt;&lt;span class=&quot;al_mon&quot;&gt;月份&lt;em&gt; (本月文章数量)&lt;/em&gt;&lt;/span&gt;
             &lt;ul class=&quot;al_post_list&quot;&gt;
                 &lt;li&gt;号数: &lt;a href=&quot;文章链接&quot;&gt;文章标题&lt;/a&gt; &lt;em&gt;(评论数量)&lt;/em&gt;&lt;/li&gt;
             &lt;/ul&gt;
         &lt;/li&gt;
     &lt;/ul&gt;
 &lt;/div</pre>
<div class='newcode'><h2>Mossight主题</h2><div class='newcontent'>我这里只做了相关段落的缩进。<br />
同样在之前创建的archives.php文件里，找到
<pre class="brush: php; title: ; notranslate">&lt;?php get_header()?&gt;</pre>
<p>并在下面添加</p>
<pre class="brush: css; title: ; notranslate">&lt;style type=&quot;text/css&quot;&gt;
#archives .al_mon {
	margin-left:3em;
}
#archives .al_post_list {
	margin-left:5.6em;
}
&lt;/style&gt;</pre>
<p></div></div>
<p>OK 大功告成了！现在你的博客也有jQuery伸缩效果的归档页面了，而且还是纯代码、原生态的。</p>
<p>也许有对代码头晕，但又想用归档页面的Mossight童鞋，好吧，那我好人做到底，将上面两个文件打包，提供给大家下载。<br />
<span style="color: #993300;"><strong>archives.php文件</strong></span><br />
使用方法：将该文件上传至Mossight文件夹即可（需要修改归档页面样式的童鞋，请在里面进行修改，具体位置已标注）<br />
<span class="but-down"><a href="http://shit.name/wp-content/uploads/2012/archives/archives.rar" target="_blank"><span>archives.php打包文件</span></a></span><br />
<span style="color: #993300;"><strong>functions.php文件</strong></span><br />
使用方法：将该文件覆盖Mossight-child文件夹里的同名文件即可<br />
<span class="but-down"><a href="http://shit.name/wp-content/uploads/2012/archives/functions.rar" target="_blank"><span>functions.php打包文件</span></a></span></p>
<blockquote><p><span style="color: #993300;"><strong>对于再次升级的Mossight1.8.0版主题也同样适用</strong></span><br />
再次感谢一直为该主题更新的三位童鞋   <A title="虾子酱" href="http://jezzzz.me/" target="_blank">Jezzzz</A>   <A title="沐歌" href="http://mugee.info/" target="_blank">Mugee</A>   <A title="牧风" href="http://mufeng.me" target="_blank">牧风</A></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://shit.name/pure-code-of-archives/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>WordPress无法正确记录访客评论IP解决方法</title>
		<link>http://shit.name/solution-of-recording-ip/</link>
		<comments>http://shit.name/solution-of-recording-ip/#comments</comments>
		<pubDate>Sat, 11 Aug 2012 13:32:15 +0000</pubDate>
		<dc:creator><![CDATA[Oo雨天阳光oO]]></dc:creator>
				<category><![CDATA[互联二亩田]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[访客]]></category>
		<category><![CDATA[评论]]></category>

		<guid isPermaLink="false">http://shit.name/?p=156</guid>
		<description><![CDATA[这个博客架设在Xehost的达拉斯VPS上，不过细心的童鞋应该发觉访问速度比美国的VPS快很多，实际Ping值 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-157" title="message" src="http://shit.name/wp-content/uploads/2012/08/message.jpg" alt="" width="614" height="120" /><br />
这个博客架设在<a title="Xehost" href="http://shit.name/go/xehost/" target="_blank">Xehost</a>的达拉斯VPS上，不过细心的童鞋应该发觉访问速度比美国的VPS快很多，实际Ping值也在100以下。其实这都归功于CDN，目前在用<a title="incapsula" href="http://shit.name/go/incapsula/" target="_blank">incapsula</a>的免费款（每月50G流量对于一般BLOG都够用），而且幸运地被分配到了日本节点，所以访问速度比较不错。<br />
速度是上去了，不过随之而来的问题就是无法获得访客评论时的真正IP——显示的都是那个日本节点的IP地址。经过一番搜索后，找到很多方法，实际测试下来有两种方法是比较靠谱的。<span id="more-156"></span></p>
<div class='newcode'><h2>第一种方法：</h2><div class='newcontent'>在wp-config.php开头加入以下代码：（把wp-config.php中的代码全部看了遍，并无关于获取IP地址的其他代码，所以我加在了最后，实际效果和在开头一样）</p>
<pre class="brush: php; title: ; notranslate">/* 获取评论的真实IP */
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
$list = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = $list[0];}</pre>
<p></div></div>
<div class='newcode'><h2>第二种方法：</h2><div class='newcontent'>打开wp-includes/comment.php，找到wp_new_comment函数，这个函数是将一个新评论入库。<br />
把其中
<pre class="brush: php; title: ; notranslate">$commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', &quot;,$_SERVER['REMOTE_ADDR'] );</pre>
<p>修改为
<pre class="brush: php; title: ; notranslate">$commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', &quot;,$_SERVER['HTTP_X_FORWARDED_FOR'] );</pre>
<p>另外将
<pre class="brush: php; title: ; notranslate">$_SERVER['REMOTE_ADDR']</pre>
<p>替换为
<pre class="brush: php; title: ; notranslate">$_SERVER[&quot;HTTP_X_FORWARDED_FOR&quot;]</pre>
<p>这样就OK了</div></div>
<blockquote><p>做下注解：<br />
$_SERVER['REMOTE_ADDR'] #正在浏览当前页面用户的 IP 地址。<br />
$_SERVER["HTTP_X_FORWARDED_FOR"] #透过代理服务器取得客户端的真实 IP 地址</p></blockquote>
<p>两种方法我测试下来，实际上是有区别的，主要在于系统因为评论给博主自动发的通知邮件，如图中红框所示：<br />
<img class="alignnone size-full wp-image-158" title="recording_ip_screenshot" src="http://shit.name/wp-content/uploads/2012/08/recording_ip_screenshot.jpg" alt="" width="560" height="616" /><br />
上面是用方法一被评论后收到的邮件——评论者IP和其主机名一致；下面是用方法二后得到的——评论者IP有两个而且其主机名没有获取到。</p>
<blockquote><p>从上得出的结论就是：<br />
<span style="color: #993300;"><strong>强烈建议使用第一种方法——简洁、方便、完美！</strong></span></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://shit.name/solution-of-recording-ip/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
