Node.js + WebSocket でテザリングしてみた

Node.js + WebSocket でテザリングしてみた

社内勉強会での発表です。
https://github.com/shuhei/tether

Ca0ab6e450f894e06652ee257df9d647?s=128

Shuhei Kagawa

June 07, 2013
Tweet

Transcript

  1. Node.js + WebSocket Ͱ ςβϦϯάͯ͠Έͨ 2013/6/7 M3 Tech Talk #2!

    ! ߳઒ पฏ
  2. ࣗݾ঺հ 5/1 ೖࣾ! લ৬Ͱ͸ .NET Ͱ CAD ΧελϚΠζɺ Web ੍࡞ɺFlash

    ͰαΠωʔδɺ Ruby on Rails Ͱ ΋Ζ΋ΖɺͳͲ! ޷͖ͳݴޠ͸ Ruby ͱ JavaScript ͱ࠷ۙ Scala
  3. https://github.com/shuhei/tether

  4. Server! Client! Browser! HTTP Proxy Mobile! Safari Websit Adhoc Network

    Web! Socket Web! Socket Websit Websit Architecture
  5. ंྠͷ࠶ൃ໊ • t.free http://connectfree.jp/tetherfree/! • https://github.com/spoletto/iOS-HTML5- Tethering! • େֶͷ՝୊
 http://cs.brown.edu/courses/cs168/s12/

    handouts/tether.pdf http://www.flickr.com/photos/60317050@N08/8595670162
  6. ಈػ 12/15 ιϑτόϯΫͷςβϦϯάղېΑΓલ! ͜Ε·Ͱ্ͬ໘͹͔Γɾɾɾ΋͏ͪΐͬͱԼ ͷํΛͪΌΜͱ஌Γ͍ͨ! Node.js Ͱ Express, Socket.IO ࢖Θͳ͍΋ͷ

    ࡞Γ͍ͨ
  7. TUN TAP Software network device! ୺຤ͷύέοτશͯΛԣऔΓͰ͖Δ! Ͱ΋ɺNode.js Ͱ৮Εͳͦ͞͏ɾɾɾ! Native module

    ॻ͚ͳ͍͠ɾɾɾ
  8. HTTP Proxy TCP Ҏ্ͳΒ Node.js Ͱ؆୯ʹ৮ΕΔ! HTTP Tunneling ͢Ε͹ɺ͍Ζ͍Ζͳϓϩτί ϧ΋αϙʔτͰ͖Δ͸ͣʂ

  9. WebSocket Binary data Λྲྀ͍ͨ͠! Base64 encode/decode ஗͘ͳΔ! Socket.IO ͸ x!

    BinaryJS ͱ͔͋Δ͚Ͳɾɾɾ
  10. ࣗ࡞ • ࡞ͬͯΈΑ͏ʂ • ࢓༷ॻ http://tools.ietf.org/html/rfc6455 • WebSocket αʔόͷ࣮૷ͱϓϩτίϧ ղઆ

    http://d.hatena.ne.jp/Jxck/20120725/ 1343174392 http://www.flickr.com/photos/60317050@N08/8735098936/
  11. WebSocket server ͷ࣮૷ UPGRADE request! ͭͳ͗ͬͺͳ͠ͷ TCP ઀ଓ! ͦͷதͰɺϓϩτίϧʹ४ڌͨ͠σʔλͷ΍ ΓऔΓɻ

  12. WebSocket Protocol 0: FIN+OPCODE, 1: Payload Length, 2-: Payload! 0:

    FIN+OPCODE, 1: 0x7e, 2-3: Payload Length, 4-: Payload! ҎԼಉจ! socket ͷσʔλΛ஍ಓʹ 1byte ͣͭຒΊΔ/ಡ ΈऔΔ
  13. Server! Client! Browser! HTTP Proxy Mobile! Safari Websit Adhoc Network

    Web! Socket Web! Socket Websit Websit Architecture
  14. TCP server as HTTP Proxy Browser ͔Βͷ request Λ TCP

    server Ͱड͚ Δ! Binary ͷ·· WebSocket Ͱ server ΁! σʔλͷઌ಄ʹ 2 byte ෼ port ൪߸Λ෇Ճ
  15. TCP client as HTTP client WebSocket ͰૹΒΕ͖ͯͨ binary Λɺ࠷ॳͩ ͚

    toString ͯ͠ host:port Λऔಘ! TCP ઀ଓͨ͠Βɺޙ͸ͻͨ͢Β binary Λྲྀ͢
  16. Keep-alive ͷ᠘ • ϒϥ΢β͸ϓϩΩγͱͷ઀ଓΛ੾Βͣ ʹɺผͷϗετѼͷϦΫΤετΛૹͬ ͯ͘Δ! • ࠞઢʂʂʂ http://www.flickr.com/photos/hatm/5704687902/

  17. HTTP server & client ʹ ΍ͬͺΓϦΫΤετຖʹ header Λ parse ͠

    ͳ͍ͱବ໨ͩͬͨ! HTTP request Λ parse ͢ΔͳΒ http.Server! Node ͷ HTTP parser ͸ native
  18. ࠞઢΛ๷͙ Browser ͔Βͷ request ຖʹ 16 byte ͷ UUID ΛৼΔ!

    Server ଆͰ΋ɺUUID Λ΋ͱʹࣝผ! ෳ਺୺຤ରԠͯ͠΋ࠞઢ͠ͳ͍͸ͣ
  19. HTTPS CONNECT request! Socket ͔Βͷ data Λ browser ͔Β web

    server ΁ૉ௨͢͠Δ! ૉ௨͠ͳͷͰɺ҉߸Խ͞Ε͍ͯͯ΋໰୊ͳ͠
  20. Demo

  21. ײ૝ TCP ͍͢͝ɻॱ൪΋อূ͞ΕͯΔ! όΠφϦʹগ͠׳Εͨ! Keep-alive ͳͲ઀ଓͷ؅ཧΉ͔͍ͣ͠! ंྠͷ࠶։ൃɺͳ͔ͳ͔ͪΌΜͱಈ͔ͳ͍͚ Ͳɺಈ͘ͱָ͍͠! ςβϦϯάΦϓγϣϯղ໿΁ͷಓ͸ԕ͍...

  22. ࠓޙ HTTP tunneling: SSH Λ௨͍ͨ͠! Web αʔό΁ͷಉ࣌઀ଓ਺ͷ੍ޚ