SSH Tunneling via Apache

อุปกรณ์ที่มี

  1. Apache web server ที่ติด mod_proxy และ mod_proxy_connect หรือ Web proxy server อะไรก็ได้ที่รองรับคำสั่ง CONNECT
  2. Proxytunnel
  3. SSH client

เริ่มจากตั้งค่าให้ Apache ก่อน โดยกำหนดให้ Apache เป็น Forward Proxy

<VirtualHost *:80>
  ServerName blah.com
 
  ProxyRequests On
  ProxyVia On
  AllowCONNECT 22 80 443 563
  <Proxy *>
    Order allow,deny
    Allow from all
  </Proxy>
</VirtualHost>
  • ProxyRequests เพื่อให้ Apache ทำตัวเป็น ForwardProxy เอา request เรียกไปที่เครื่องอื่นต่อได้
  • ProxyVia เพื่อให้ Apache แปะไปที่ request ที่เรียกต่อไปด้วยว่า เรียกผ่าน Proxy ที่ไหนมา
  • AllowCONNECT ไว้บอกว่าจะใช้คำสั้ง CONNECT ยิงไปที่ปลายทางได้ที่ port ไรบ้าง
  • <Proxy wildcard-url> เพื่อกำหนดขอบเขตของ Host ที่จะให้ใช้ Proxy นี้เรียกต่อได้

จากนั้นไปเอา Proxytunnel มาคอมไพล์และติดตั้ง (ถ้าเป็น Linux คาดว่าจะสามารถทำผ่าน repository ได้เลยมั้ง Windows ก็มี .exe ให้ เพราะงั้นสามารถใช้ได้หมด) ความสนุกที่เหลือคือ เรียกใช้คำสั่ง

ssh user@targethost -o ProxyCommand='proxytunnel -p proxy1:port -r proxy2:port -d %h:%p -H "User-Agent: Mozilla/4.0 ( compatible\; MSIE 6.0\; Win32 ) \n"'

สำหรับ -r จะใส่หรือไม่ใส่ก็ได้ แต่ใช้สำหรับกรณีที่ต้องใช้ Proxy มากกว่า 1 ที่ ส่วน targethost คือเครื่องภายในเครือข่ายที่อยู่ที่เดียวกับ proxy ปลายทางที่ใส่ไว้ใน -r (หรือ -p ถ้าไม่ได้ใส่ -r ไว้)

รู้สึกซับซ้อนมาก จริงๆ มี Option สำหรับ bind port หรือจะทำให้เป็น Sock Server อีกแต่จะใช้แล้วค่อยมาเขียนจดอีกที

Tags: , , ,

Post Author

This post was written by llun who has written 512 posts on แนท (/næt/).

I'm programmer at Throughwave

One Response to “SSH Tunneling via Apache”

  1. Namwan September 30, 2009 at 1:49 pm #

    Hi, I saw your blog on google, I was wondering if I could have your e-mail please?

    Thanks

Leave a Reply