<?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>LinuxByte &#187; 限速</title>
	<atom:link href="http://www.linuxbyte.org/tag/%e9%99%90%e9%80%9f/feed" rel="self" type="application/rss+xml" />
	<link>http://www.linuxbyte.org</link>
	<description>一个Linux Blog</description>
	<lastBuildDate>Thu, 26 Jan 2012 08:13:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>网吧每IP 限速补充（squid 限速）</title>
		<link>http://www.linuxbyte.org/wang-ba-mei-ip-xian-su-bu-chong-squid-xian-su.html</link>
		<comments>http://www.linuxbyte.org/wang-ba-mei-ip-xian-su-bu-chong-squid-xian-su.html#comments</comments>
		<pubDate>Sat, 30 Jan 2010 17:00:32 +0000</pubDate>
		<dc:creator>xiao H</dc:creator>
				<category><![CDATA[服务器应用]]></category>
		<category><![CDATA[squid]]></category>
		<category><![CDATA[限速]]></category>

		<guid isPermaLink="false">http://www.linuxbyte.org/?p=503</guid>
		<description><![CDATA[之前的Iptables＋tc 网吧每IP 限速脚本一文中有一个问题需要补充，如果同时使用squid 做透明代理会使该脚本失效。 做透明代理时有一条iptables规则 iptables -A PREROUTING -s 192.168.0.0/24 -i eth0 -p tcp -m tcp... ]]></description>
			<content:encoded><![CDATA[<p>之前的<a href="http://www.linuxbyte.org/iptables%EF%BC%8Dtc-wang-ba-mei-ip-xian-su-jiao-ben.html">Iptables＋tc 网吧每IP 限速脚本</a>一文中有一个问题需要补充，如果同时使用squid 做透明代理会使该脚本失效。<br />
做透明代理时有一条iptables规则</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">iptables <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-s</span> 192.168.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">24</span> <span style="color: #660033;">-i</span> eth0 <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">-m</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> REDIRECT <span style="color: #660033;">--to-ports</span> <span style="color: #000000;">3128</span></pre></div></div>

<p>这一规则把所有内网80端口的请求都转发到了网关，如此一来所有向外网的普通http 请求者都成了网关（192.168.0.254），而网关是不做限速的，所以所有http下载都不被限速了。<br />
<span id="more-503"></span><br />
所以为了能现在http下载，我们要使用squid 的限速功能，配置命令如下：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">acl LIMIT_IP src 192.168.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">25</span> 
delay_pools <span style="color: #000000;">1</span>
delay_class <span style="color: #000000;">1</span> <span style="color: #000000;">2</span>
delay_access <span style="color: #000000;">1</span> allow LIMIT_IP
delay_parameters <span style="color: #000000;">1</span> -<span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">/</span>-<span style="color: #000000;">1</span> <span style="color: #000000;">500000</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">500000</span></pre></div></div>

<p>squid 的限速控制不是很精确限速500000 bytes/sec 的浮动在4xx/KB 到8XX/KB 之间 -__-!</p>
<h2  class="related_post_title">与本文关系暧昧的文字</h2><ul class="related_post"><li><a href="http://www.linuxbyte.org/wang-ba-linux-wang-guan-she-zhi-ji-lu-bu-chong.html" title="网吧Linux 网关设置记录补充">网吧Linux 网关设置记录补充</a></li><li><a href="http://www.linuxbyte.org/refreshpattern-xue-xi-fan-li.html" title="refresh_pattern 学习范例">refresh_pattern 学习范例</a></li><li><a href="http://www.linuxbyte.org/squid-tou-ming-dai-li-you-hua.html" title="Squid 透明代理优化">Squid 透明代理优化</a></li><li><a href="http://www.linuxbyte.org/guan-yu-squid-de-yi-xie-ji-lu.html" title="关于squid的一些记录">关于squid的一些记录</a></li><li><a href="http://www.linuxbyte.org/squid-xian-zhi-yong-hu-bing-fa-lian-jie-shu-2.html" title="Squid 限制用户并发连接数">Squid 限制用户并发连接数</a></li><li><a href="http://www.linuxbyte.org/%e5%8d%87%e7%ba%a7squid-26-%e5%88%b027-%e7%9a%84%e5%86%a4%e6%9e%89%e8%b7%af.html" title="升级squid 2.6 到2.7 的冤枉路">升级squid 2.6 到2.7 的冤枉路</a></li><li><a href="http://www.linuxbyte.org/squid-unofficial-support-for-rhelcentos.html" title="Squid unofficial support for RHEL/CentOS">Squid unofficial support for RHEL/CentOS</a></li><li><a href="http://www.linuxbyte.org/iptables%ef%bc%8dtc-wang-ba-mei-ip-xian-su-jiao-ben.html" title="Iptables＋tc 网吧每IP 限速脚本">Iptables＋tc 网吧每IP 限速脚本</a></li><li><a href="http://www.linuxbyte.org/qzone-squid-iptables.html" title="新版qzone 无法被squid 缓存">新版qzone 无法被squid 缓存</a></li><li><a href="http://www.linuxbyte.org/squid-havp-clamav.html" title="squid+havp+clamav搭建防毒代理">squid+havp+clamav搭建防毒代理</a></li></ul><hr />
<p><small>© xiao H for <a href="http://www.linuxbyte.org">LinuxByte</a>, 2010<br />
Source: <a href="http://www.linuxbyte.org/wang-ba-mei-ip-xian-su-bu-chong-squid-xian-su.html">网吧每IP 限速补充（squid 限速）</a><br />
Post tags: <a href="http://www.linuxbyte.org/tag/squid" rel="tag">squid</a>, <a href="http://www.linuxbyte.org/tag/%e9%99%90%e9%80%9f" rel="tag">限速</a><br/>
</small></p>
	<p style="margin:3px 0 0 0;">标签：<a href="http://www.linuxbyte.org/tag/squid" title="squid" rel="tag">squid</a>, <a href="http://www.linuxbyte.org/tag/%e9%99%90%e9%80%9f" title="限速" rel="tag">限速</a></p>

<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.linuxbyte.org/wang-ba-mei-ip-xian-su-bu-chong-squid-xian-su.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Iptables＋tc 网吧每IP 限速脚本</title>
		<link>http://www.linuxbyte.org/iptables%ef%bc%8dtc-wang-ba-mei-ip-xian-su-jiao-ben.html</link>
		<comments>http://www.linuxbyte.org/iptables%ef%bc%8dtc-wang-ba-mei-ip-xian-su-jiao-ben.html#comments</comments>
		<pubDate>Sat, 09 May 2009 12:32:43 +0000</pubDate>
		<dc:creator>xiao H</dc:creator>
				<category><![CDATA[shell]]></category>
		<category><![CDATA[htb]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[tc]]></category>
		<category><![CDATA[限速]]></category>

		<guid isPermaLink="false">http://www.linuxbyte.org/?p=421</guid>
		<description><![CDATA[网吧以前是没做限速的，但最近遇到几个狂爱看A片的哥们，用不知什么东西下载的，好几次把带宽占个精光，所以不得不做了限速。 总体思想很简单，为每个IP 打标，然后归入各自的tc 限速规... ]]></description>
			<content:encoded><![CDATA[<p>网吧以前是没做限速的，但最近遇到几个狂爱看A片的哥们，用不知什么东西下载的，好几次把带宽占个精光，所以不得不做了限速。<br />
总体思想很简单，为每个IP 打标，然后归入各自的tc 限速规则中去。<br />
<span id="more-421"></span></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #666666; font-style: italic;"># xiaoh www.linuxbyte.org</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#  定义进出设备(eth0 内网，eth1外网)</span>
<span style="color: #007800;">IDEV</span>=<span style="color: #ff0000;">&quot;eth0&quot;</span>
<span style="color: #007800;">ODEV</span>=<span style="color: #ff0000;">&quot;eth1&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#  定义总的上下带宽</span>
<span style="color: #007800;">UP</span>=<span style="color: #ff0000;">&quot;50mbit&quot;</span>
<span style="color: #007800;">DOWN</span>=<span style="color: #ff0000;">&quot;50mbit&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#  定义每个受限制的IP上下带宽</span>
＃rate 起始带宽
<span style="color: #007800;">UPLOAD</span>=<span style="color: #ff0000;">&quot;4mbit&quot;</span>
<span style="color: #007800;">DOWNLOAD</span>=<span style="color: #ff0000;">&quot;5mbit&quot;</span>
＃ceil 最大带宽
<span style="color: #007800;">MUPLOAD</span>=<span style="color: #ff0000;">&quot;5mbit&quot;</span>
<span style="color: #007800;">MDOWNLOAD</span>=<span style="color: #ff0000;">&quot;10mbit&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#内网IP段</span>
<span style="color: #007800;">INET</span>=<span style="color: #ff0000;">&quot;192.168.0.&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># 受限IP范围，IPS 起始IP，IPE 结束IP。</span>
<span style="color: #007800;">IPS</span>=<span style="color: #ff0000;">&quot;1&quot;</span> 
<span style="color: #007800;">IPE</span>=<span style="color: #ff0000;">&quot;114&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># 清除网卡原有队列规则</span>
tc qdisc del dev <span style="color: #007800;">$ODEV</span> root <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null
tc qdisc del dev <span style="color: #007800;">$IDEV</span> root <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null
&nbsp;
<span style="color: #666666; font-style: italic;"># 定义最顶层(根)队列规则，并指定 default 类别编号</span>
tc qdisc add dev <span style="color: #007800;">$ODEV</span> root handle <span style="color: #000000;">10</span>: htb default <span style="color: #000000;">256</span>
tc qdisc add dev <span style="color: #007800;">$IDEV</span> root handle <span style="color: #000000;">10</span>: htb default <span style="color: #000000;">256</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># 定义第一层的 10:1 类别 (上行/下行 总带宽)</span>
tc class add dev <span style="color: #007800;">$ODEV</span> parent <span style="color: #000000;">10</span>: classid <span style="color: #000000;">10</span>:<span style="color: #000000;">1</span> htb rate <span style="color: #007800;">$UP</span> ceil <span style="color: #007800;">$UP</span>
tc class add dev <span style="color: #007800;">$IDEV</span> parent <span style="color: #000000;">10</span>: classid <span style="color: #000000;">10</span>:<span style="color: #000000;">1</span> htb rate <span style="color: #007800;">$DOWN</span> ceil <span style="color: #007800;">$DOWN</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#开始iptables 打标和设置具体规则</span>
<span style="color: #007800;">i</span>=<span style="color: #007800;">$IPS</span>;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$i</span> <span style="color: #660033;">-le</span> <span style="color: #007800;">$IPE</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">do</span>
tc class add dev <span style="color: #007800;">$ODEV</span> parent <span style="color: #000000;">10</span>:<span style="color: #000000;">1</span> classid <span style="color: #000000;">10</span>:<span style="color: #000000;">2</span><span style="color: #007800;">$i</span> htb rate <span style="color: #007800;">$UPLOAD</span> ceil <span style="color: #007800;">$MUPLOAD</span> prio <span style="color: #000000;">1</span>
tc qdisc add dev <span style="color: #007800;">$ODEV</span> parent <span style="color: #000000;">10</span>:<span style="color: #000000;">2</span><span style="color: #007800;">$i</span> handle <span style="color: #000000;">100</span><span style="color: #007800;">$i</span>: pfifo
tc filter add dev <span style="color: #007800;">$ODEV</span> parent <span style="color: #000000;">10</span>: protocol ip prio <span style="color: #000000;">100</span> handle <span style="color: #000000;">2</span><span style="color: #007800;">$i</span> fw classid <span style="color: #000000;">10</span>:<span style="color: #000000;">2</span><span style="color: #007800;">$i</span>
tc class add dev <span style="color: #007800;">$IDEV</span> parent <span style="color: #000000;">10</span>:<span style="color: #000000;">1</span> classid <span style="color: #000000;">10</span>:<span style="color: #000000;">2</span><span style="color: #007800;">$i</span> htb rate <span style="color: #007800;">$DOWNLOAD</span> ceil <span style="color: #007800;">$MDOWNLOAD</span> prio <span style="color: #000000;">1</span>
tc qdisc add dev <span style="color: #007800;">$IDEV</span> parent <span style="color: #000000;">10</span>:<span style="color: #000000;">2</span><span style="color: #007800;">$i</span> handle <span style="color: #000000;">100</span><span style="color: #007800;">$i</span>: pfifo
tc filter add dev <span style="color: #007800;">$IDEV</span> parent <span style="color: #000000;">10</span>: protocol ip prio <span style="color: #000000;">100</span> handle <span style="color: #000000;">2</span><span style="color: #007800;">$i</span> fw classid <span style="color: #000000;">10</span>:<span style="color: #000000;">2</span><span style="color: #007800;">$i</span>
iptables <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-s</span> <span style="color: #007800;">$INET</span><span style="color: #007800;">$i</span> <span style="color: #660033;">-j</span> MARK <span style="color: #660033;">--set-mark</span> <span style="color: #000000;">2</span><span style="color: #007800;">$i</span>
iptables <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-s</span> <span style="color: #007800;">$INET</span><span style="color: #007800;">$i</span> <span style="color: #660033;">-j</span> RETURN
iptables <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> POSTROUTING <span style="color: #660033;">-d</span> <span style="color: #007800;">$INET</span><span style="color: #007800;">$i</span> <span style="color: #660033;">-j</span> MARK <span style="color: #660033;">--set-mark</span> <span style="color: #000000;">2</span><span style="color: #007800;">$i</span>
iptables <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> POSTROUTING <span style="color: #660033;">-d</span> <span style="color: #007800;">$INET</span><span style="color: #007800;">$i</span> <span style="color: #660033;">-j</span> RETURN
<span style="color: #007800;">i</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$i</span> + <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<h2  class="related_post_title">与本文关系暧昧的文字</h2><ul class="related_post"><li><a href="http://www.linuxbyte.org/wang-ba-linux-wang-guan-she-zhi-ji-lu-bu-chong.html" title="网吧Linux 网关设置记录补充">网吧Linux 网关设置记录补充</a></li><li><a href="http://www.linuxbyte.org/wang-ba-mei-ip-xian-su-bu-chong-squid-xian-su.html" title="网吧每IP 限速补充（squid 限速）">网吧每IP 限速补充（squid 限速）</a></li><li><a href="http://www.linuxbyte.org/yong-iptables-er-fei-tc-xian-zhi-liu-liang.html" title="用Iptables 而非tc 限制流量">用Iptables 而非tc 限制流量</a></li><li><a href="http://www.linuxbyte.org/linux-iptables-nat-squid-pdnsd.html" title="网吧Linux 网关设置记录">网吧Linux 网关设置记录</a></li><li><a href="http://www.linuxbyte.org/iptables-command-example.html" title="iptables 基本命令使用举例">iptables 基本命令使用举例</a></li><li><a href="http://www.linuxbyte.org/linux-syn-attack-defense.html" title="LINUX下的SYN攻击防御">LINUX下的SYN攻击防御</a></li></ul><hr />
<p><small>© xiao H for <a href="http://www.linuxbyte.org">LinuxByte</a>, 2009<br />
Source: <a href="http://www.linuxbyte.org/iptables%ef%bc%8dtc-wang-ba-mei-ip-xian-su-jiao-ben.html">Iptables＋tc 网吧每IP 限速脚本</a><br />
Post tags: <a href="http://www.linuxbyte.org/tag/htb" rel="tag">htb</a>, <a href="http://www.linuxbyte.org/tag/iptables" rel="tag">iptables</a>, <a href="http://www.linuxbyte.org/tag/tc" rel="tag">tc</a>, <a href="http://www.linuxbyte.org/tag/%e9%99%90%e9%80%9f" rel="tag">限速</a><br/>
</small></p>
	<p style="margin:3px 0 0 0;">标签：<a href="http://www.linuxbyte.org/tag/htb" title="htb" rel="tag">htb</a>, <a href="http://www.linuxbyte.org/tag/iptables" title="iptables" rel="tag">iptables</a>, <a href="http://www.linuxbyte.org/tag/tc" title="tc" rel="tag">tc</a>, <a href="http://www.linuxbyte.org/tag/%e9%99%90%e9%80%9f" title="限速" rel="tag">限速</a></p>

<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.linuxbyte.org/iptables%ef%bc%8dtc-wang-ba-mei-ip-xian-su-jiao-ben.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

