2010年11月9日 星期二

[FreeBSD]DHCP 動態取得 IP

設定要自動取得IP的網路卡
# dhclient fxp0

在rc.conf中加入設定開機自動跟DHCP取IP
#vim /etc/rc.conf
ifconfig_fxp0="DHCP"
#上面這一行表示要在開機時自動將網路卡 fxp0 設定使用 DHCP 連線。

2010年10月26日 星期二

Cisco WCCP V2實做-以FreeBSD + SQUID為例

本文轉載自:http://www.ringline.com.tw/epaper/Product980201.htm


郭璨榮
 前言
Proxy Server(代理伺服器)是在Internet上廣為使用的一種伺服器之一,而其中最廣為人所知的就是Web Proxy Server
Web Proxy Server 依是否需要設定可概分為Explicit Proxy  Transparent Proxy 兩種,比方說需要在瀏覽器中指定代理伺服器的環境,那麼就是Explicit Mode,而使用者若不需指定代理伺服器的話,那就是Transparent Mode

一般實做Transparent Web Proxy的方式有:
 1. Cisco Router Cisco L3 Switch 搭上WCCP
 2. 
L4的網路設備做流量的導向
 3. 
RouterL3 Switch 上以Policy Base Route 的方式來實現
 4. 
Web Proxy Server in-line的方式佈建
 表一 Transparent Proxy 比較表 

WCCP (V2)
L4 Switch
Policy Base Route
In-Line Proxy
不需額外設備
Load Balance
Redundant
?(部分設計可)
Failure Safe
?(有些有Fail -Open)
評論
勝出
要花錢
當機就斷線了
當機就斷線了
在這篇文章中我們要以CiscoRouter /L3 Switch 搭佩上以FreeBSD做為作業系統再搭 配上SQUID來實做 Web Proxy
測試環境
SQUID Server FreeBSD 7.0

L3 Switch: WS-C3560-24 IOS:C3560-IPSERVICESK9-M
Router: Cisco 2821 IOS: c2800nm-advsecurityk9-mz.124-1a
 圖一 L2 WCCP 測試架構
圖二 GRE WCCP 測試架構 
一、WCCP
1.          WCCP (Web Cache Communication Protocol)CISCO所發展的協定,在網路中可以將內容自動的導向網頁代理伺服器(Proxy Server),WCCP有分兩個版本V1V2 。
2.          WCCP實務上大多會設定成V2V2V1多了幾點好處
(1)   相較於V1 Router/Switch Proxy Server 間僅能一對一,V2可以多台 Switch/Router 對映到多台不同台的Proxy Server ,這樣能做到負載平衡也能做到容錯。
(2)   V1Router/Switch 間是沒有任何保護的,只要Router/Switch 打開WCCP,那麼任意的Web Proxy Server都能與Router/Switch 間構成連結,這樣在資安上是個很大的漏洞,V2有新增加了密碼認證保護。
3.          WCCP V2 有支援兩種模式:L2 Forwarding Mode 以及GRE Forwarding Mode。在L2 Forwarding Mode下,Switch 會將封包轉送時的目的端改寫為Proxy Server主機的MAC address,在L2 Forwarding Mode下,Cisco 是以L2Hardware來做這段動作,而在GRE Forwarding modeRouter 需將User HTTP Request 包裝在GRE Tunnel 的封包中來轉送到Proxy Sever,Proxy Server 也要將 GRE Tunnel 解開成一般的HTTP Request 封包這段都要耗費額外的CPU Resorce。但是GRE Tunnel Mode也不是一無是處,GRE Tunnel 並沒有要求Tunnel 的兩端的設備在同一個網段,在佈建時就顯的彈性許多了。 
(1)   L3 Switch 僅支援 L2 Forwarding ModeRouter 可以跑 GRE Mode
(2)   L2 ModeProxy Server必需要跟L3 Switch 在同一網段,而在GRE Mode可以跟Router 不同網段。
4.          網管上使用WCCP的好處
(1)   使用者不需要調整自己的使用習慣,不必在瀏覽器中設定proxy
(2)   Proxy Server 掛掉的時候,使用者依然能夠不受影響。
二、Switch 上的設定
1.          Switch 上的設定
--------------------------------------------------------------------------------------------------------------
ip routing
ip wccp web-cache  
ß啟用WCCP的功能 
interface Vlan6
 ip address 192.168.6.30 255.255.255.0 
interface Vlan100
 ip address 192.168.100.254 255.255.255.0
 ip wccp web-cache redirect in 
ß在本介面上對於所進來的封包做WCCP的轉送 
ip route 0.0.0.0 0.0.0.0 192.168.6.254
--------------------------------------------------------------------------------------------------------------
三、Router上的設定
--------------------------------------------------------------------------------------------------------------
ip wccp web-cache
interface GigabitEthernet0/0
 ip address 192.168.6.30 255.255.255.0
interface GigabitEthernet0/1
 ip address 192.168.100.254 255.255.255.0
 ip wccp web-cache redirect in
ip route 0.0.0.0 0.0.0.0 192.168.6.254
-------------------------------------------------------------------------------------------------------------- 
四、FreeBSD上的設定
1. 
安裝最小需求 User (如圖三) 
 圖三 FreeBSD 安裝時所選擇的套件分類 
2.  Squid的安裝
--------------------------------------------------------------------------------------------------------------
#cd /usr/ports/www/squid/
#make install
#squid -z    
ßsquid server 上存放cache的目錄作初使化
--------------------------------------------------------------------------------------------------------------
當在/usr/ports/www/squid/ 中下了 make install 後會出現安裝的項目選單,而要額外增加的選項是

SQUID_WCCPV2 
及 SQUID_PF如圖四 
 圖四 SQUID 安裝時所選擇的安裝選項 
以您所習慣的編輯器編修 /etc/rc.conf 這個環境設定檔來設定系統的網路環境 (這段要與您的環境相符像是網路卡代號.IP address ,default gateway,lan 網段的靜態路由等等.)
--------------------------------------------------------------------------------------------------------------
#設定網卡le0IP Address
ifconfig_le0="inet 192.168.6.131 netmask 255.255.255.0"
defaultrouter="192.168.6.254"
#
設定允許FreeBSD做封包的導向以接受目的端不是自己的封包
gateway_enable="YES"
#
啟用SQUID
squid_enable="YES"
#
啟用pf
pf_enable="YES"
#
設定通往User時的路由
static_routes="lan"
route_lan="-net 192.168.100.0/24 192.168.6.30"
--------------------------------------------------------------------------------------------------------------

 
以您所習慣的編輯器編修 /etc/pf.conf 這個環境設定檔來設定系統的FIREWALL
環境 要留意的是 le0 這個網路卡代號需要依您的實際環境作修改
/etc/pf.conf (L2 Mode)
--------------------------------------------------------------------------------------------------------------

#L2 Mode
中定義網卡別名以及設定將流經該網卡的80 Port 封包重導向(Redirect)
#
 127.0.0.1 Port 3128 (Transparent Proxy) 
rdr on le0 inet proto tcp from any to any port www -> 127.0.0.1 port 3128
--------------------------------------------------------------------------------------------------------------

 /etc/pf.conf (GRE Mode)

--------------------------------------------------------------------------------------------------------------

#GRE Mode 
中將GRE Tunnrl 的虛擬界面的80 Port 封包重導向(Redirect) #127.0.0.1 Port 3128 (Transparent Proxy) 
rdr on gre0 inet proto tcp from any to any port www -> 127.0.0.1 port 3128
--------------------------------------------------------------------------------------------------------------

 /boot/loader.conf (GRE Mode only)

--------------------------------------------------------------------------------------------------------------

if_gre_load="YES"
--------------------------------------------------------------------------------------------------------------

/etc/rc.local
 (GRE Mode only)

--------------------------------------------------------------------------------------------------------------

#
啟用gre tunnel
ifconfig gre0 create
#
告訴gre Tunnel 我們要跑的是 WCCP V2 如果沒設定的話會跑V1
ifconfig gre0 link2
#
設定 gre tunnel 的由自己的那個IP跟對方的那個IP對連
ifconfig gre0 tunnel 192.168.6.131 192.168.100.254
#
設定gre tunnel  ip address ,請使用沒在用的IP
ifconfig gre0 inet 1.1.1.1 1.1.1.2

-------------------------------------------------------------------------------------------------------------- 
3.          Squid的設定(L2 Mode)節錄 ß/usr/local/etc/squid/squid.conf
--------------------------------------------------------------------------------------------------------------
#
允許任何ip來透過本機 Proxy 出去
http_access allow all
#
設定在127.0.0.1 (Loopback 0) Port 3128 上跑 Transparent Mode Proxy
http_port 127.0.0.1:3128 transparent
#
指定要跑WCCP Router/Switch IP
wccp2_router 192.168.6.30
#
指定由Router/Switch 上所轉送來的封包為L2的格式
wccp2_forwarding_method 2
#
指定由Server 上所送回Router/Switch的封包為L2的格式
wccp2_return_method 2
#WCCP2 
選派的方式1是以Hash 來選派,2Mask來選派,通常Router支援Hash MethodSwitch 支援Mask Method
wccp2_assignment_method 2
-------------------------------------------------------------------------------------------------------------- 
4.          Squid的設定(GRE Mode)節錄
--------------------------------------------------------------------------------------------------------------
#允許任何ip來透過本機 Proxy 出去
http_access allow all
#
設定在127.0.0.1 (Loopback 0) Port 3128 上跑 Transparent Mode Proxy
http_port 127.0.0.1:3128 transparent
#
指定要跑WCCP Router/Switch IP
wccp2_router 192.168.6.30
#
指定由Router上所轉送來的封包為GRE的格式
wccp2_forwarding_method 1
#
指定由Server 上所送回Router的封包為GRE的格式
wccp2_return_method 1
-------------------------------------------------------------------------------------------------------------- 
五、Debug步驟 
1.  Server
(1)   Switch/Router端的網路連線是否正常
l
是否可連線到Switch/Router (ping show ip wccp web-cache Router Identifier欄位的ip位置)
l
Switch/Router連結是否有起來
例如
--------------------------------------------------------------------------------------------------------------
# netstat -na | grep 2048
udp4       0      0  192.168.6.131.2048      192.168.6.30.2048
-------------------------------------------------------------------------------------------------------------- 
(2)   Transparent Proxy是否正常
l  
Server上是否有收到Switch/Router 的封包 (#tcpdump port 80)
l  
Gateway指向Server時,是否Proxy有作用
(3)   GRE Mode 下,gre tunnel  destination 需為 Router 的 Router Identifier: IP Address,跑 tcpdump –I gre0 應可看到User 丟過來的 http Request.例如
--------------------------------------------------------------------------------------------------------------
tcpdump -i gre0
tcpdump: verbose output suppressed
 use -v or -vv for full protocol decode
listening on gre0
 link-type NULL (BSD loopback) capture size 96 bytes
16:35:58.198748 IP 192.168.100.68.34329 > www.google.com.http: S 1742093772:1742093772(0) win 5840 sackOKtimestamp 324599423 0nopwscale 7>
16:35:58.201601 IP 192.168.100.68.34329 > www.google.com.http: . ack 1903031020 win 46 noptimestamp 324599427 3962828907>
16:35:58.204756 IP 192.168.100.68.34329 > www.google.com.http: P 0:225(225) ack 1 win 46 noptimestamp 324599429 3962828907>
-------------------------------------------------------------------------------------------------------------- 
2.  Switch /Router
(1)   
是否有看到Server送來WCCP的封包
l
#debug ip wccp packet
應該會有如下的結果
--------------------------------------------------------------------------------------------------------------
Apr 18 11:56:48: WCCP-PKT:S00: Sending
 I_See_You packet to 192.168.6.131 w/ rcv_id 00000001
Apr 18 11:57:07: %WCCP-5-SERVICEFOUND: 
Service web-cache acquired on WCCP Client 192.168.6.131
Apr 18 11:57:07: WCCP-PKT:S00: Received valid 
Here_I_Am packet from 192.168.6.131 w/rcv_id 00000001

Apr 18 11:57:07: WCCP-PKT:S00: Sending I_See_You packet to 192.168.6.131 w/ rcv_id 00000002
Apr 18 11:57:27: WCCP-PKT:S00: Sending I_See_You packet to 192.168.6.131 w/ rcv_id 00000003

Apr 18 11:57:37: WCCP-PKT:S00: Received valid 
Redirect_Assignment packet from 192.168.6.131 w/rcv_id 00000003

--------------------------------------------------------------------------------------------------------------
(2)   Check IOS版本
l
Cisco 3750. 3560 Switch IOS12.2(37)SE,以後的版本 而最少要支援 IP servicesFeature Set 
l
查看Cisco Web Site上關於現在在執行的版本是否以有支援WCCP 
結語
現在網路環境中都會有L3 Switch 或者是 Router,如果利用WCCP後可以節省頻寬的需求,FreeBSD+SQUID+WCCP可以達到節省頻寬的使用,但是在兩端的設定上有許多要注意的地方,一個地方的不留意,WCCP就常不能建立起來,透過這些Freeware 的使用,花上一些心力一樣也是可以改善網路的效率的 
參考文件:
1. 
Cisco 
官方網站 http://www.cisco.com
2.
Configuring Transparent Interception with FreeBSD and WCCP V2 http://wiki.squid-cache.org/ConfigExamples/FreeBsdAndWccp2?highlight=%28wccp%29
3.FreeBSD-
我的 PF 學習筆記 http://forum.icst.org.tw/phpBB2/viewtopic.php?t=10404
4.
Transparent proxying with squid and pf http://www.benzedrine.cx/transquid.html

2010年8月9日 星期一

好用的usb安裝Linux軟體 - UNetbootin

官網:http://unetbootin.sourceforge.net/

2009年11月8日 星期日

轉貼 http://www.cnblogs.com/hesen/archive/2009/04/06/1430061.html
通過程序自動的讀取其它網站網頁顯示的信息,類似於爬蟲程序。比方說我們有一個系統,要提取BaiDu網站上歌曲搜索排名。分析系統在根據得到的數據進行數據分析。為業務提供參考數據。 
  為了完成以上的需求,我們就需要模擬瀏覽器瀏覽網頁,得到頁面的數據在進行分析,最後把分析的結構,即整理好的數據寫入數據庫。那麼我們的思路就是: 
  1、發送HttpRequest請求。 
  2、接收HttpResponse返回的結果。得到特定頁面的html源文件。 
  3、取出包含數據的那一部分源碼。 
  4、根據html源碼生成HtmlDocument,循環取出數據。 
  5、寫入數據庫。 

  程序如下:  

 

 //根據Url地址得到網頁的html源碼
        private string GetWebContent(string Url)
{
string strResult="";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
    //聲明一個HttpWebRequest請求
                request.Timeout = 30000;
//設置連接超時時間
                request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding("GB2312");
StreamReader streamReader = new StreamReader(streamReceive, encoding);
strResult = streamReader.ReadToEnd();
}
catch
{
MessageBox.Show("出錯");
}
return strResult;
}
為了使用HttpWebRequest和HttpWebResponse,需填名字空間引用
  using System.Net;
以下是程序具體實現過程:
private void button1_Click(object sender, EventArgs e)
{
//要抓取的URL地址
            string Url = "http://list.mp3.baidu.com/topso/mp3topsong.html?id=1#top2";
//得到指定Url的源碼
   string strWebContent = GetWebContent(Url);
richTextBox1.Text = strWebContent;
   //取出和數據有關的那段源碼
            int iBodyStart = strWebContent.IndexOf("", iTableStart);
string strWeb = strWebContent.Substring(iTableStart, iTableEnd - iTableStart + 8);
//生成HtmlDocument
   WebBrowser webb = new WebBrowser();
webb.Navigate("about:blank");
HtmlDocument htmldoc = webb.Document.OpenNew(true);
htmldoc.Write(strWeb);
HtmlElementCollection htmlTR = htmldoc.GetElementsByTagName("TR");
foreach (HtmlElement tr in htmlTR)
{
string strID = tr.GetElementsByTagName("TD")[0].InnerText;
string strName = SplitName(tr.GetElementsByTagName("TD")[1].InnerText, "MusicName");
string strSinger = SplitName(tr.GetElementsByTagName("TD")[1].InnerText, "Singer");
strID = strID.Replace(".", "");
//插入DataTable
                AddLine(strID, strName, strSinger,"0");
string strID1 = tr.GetElementsByTagName("TD")[2].InnerText;
string strName1 = SplitName(tr.GetElementsByTagName("TD")[3].InnerText, "MusicName");
string strSinger1 = SplitName(tr.GetElementsByTagName("TD")[3].InnerText, "Singer");
//插入DataTable
                strID1 = strID1.Replace(".", "");
AddLine(strID1, strName1, strSinger1,"0");
string strID2 = tr.GetElementsByTagName("TD")[4].InnerText;
string strName2 = SplitName(tr.GetElementsByTagName("TD")[5].InnerText, "MusicName");
string strSinger2 = SplitName(tr.GetElementsByTagName("TD")[5].InnerText, "Singer");
//插入DataTable
                strID2 = strID2.Replace(".", "");
AddLine(strID2, strName2, strSinger2,"0");
}
//插入數據庫
            InsertData(dt);
   
dataGridView1.DataSource = dt.DefaultView;
}

2009年11月2日 星期一

C# 開啟瀏覽器語法

System.Diagnostics.Process.Start("http://www.google.com");

2009年10月26日 星期一

重設mysql密碼

1. 停止mysqld
# killall mysqld
2.
# mysqld -u root --skip-grant-tables &
Starting mysqld daemon with databases from /var/lib/mysql
# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> 
3.進入mysql後執行下列命令 new password 自行更改為新密碼
use mysql;
UPDATE user SET password=password('new password') where user='root';
FLUSH PRIVILEGES;

2009年10月18日 星期日

C# 擷取網頁資料

private void button1_Click(object sender, EventArgs e)
{
   //1
   WebRequest ws = HttpWebRequest.Create("http://localhost/table.htm");
   Stream st = ws.GetResponse().GetResponseStream();
   StreamReader sr = new StreamReader(st, Encoding.GetEncoding("big5"));
   textBox1.Text = sr.ReadToEnd();

   //2
   Match m = Regex.Match(textBox1.Text,"<tr><td>(.*)</td><td>(.*)</td></tr>");
   if (m.Success)
   {
      textBox2.Text = m.Groups[1].Value + m.Groups[2].Value;
   }
}