@@ -48,11 +48,11 @@ function process($user,$msg){
4848 }
4949}
5050
51- function send ($ client ,$ msg ){
51+ function send ($ client ,$ msg ){
5252 say ("> " .$ msg );
5353 $ msg = wrap ($ msg );
5454 socket_write ($ client ,$ msg ,strlen ($ msg ));
55- }
55+ }
5656
5757function WebSocket ($ address ,$ port ){
5858 $ master =socket_create (AF_INET , SOCK_STREAM , SOL_TCP ) or die ("socket_create() failed " );
@@ -92,14 +92,39 @@ function disconnect($socket){
9292function dohandshake ($ user ,$ buffer ){
9393 console ("\nRequesting handshake... " );
9494 console ($ buffer );
95- list ($ resource ,$ host ,$ origin ) = getheaders ($ buffer );
95+ list ($ resource ,$ host ,$ origin, $ strkey1 , $ strkey2 , $ data ) = getheaders ($ buffer );
9696 console ("Handshaking... " );
97- $ upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake \r\n" .
97+
98+ $ pattern = '/[^\d]*/ ' ;
99+ $ replacement = '' ;
100+ $ numkey1 = preg_replace ($ pattern , $ replacement , $ strkey1 );
101+ $ numkey2 = preg_replace ($ pattern , $ replacement , $ strkey2 );
102+
103+ $ pattern = '/[^ ]*/ ' ;
104+ $ replacement = '' ;
105+ $ spaces1 = strlen (preg_replace ($ pattern , $ replacement , $ strkey1 ));
106+ $ spaces2 = strlen (preg_replace ($ pattern , $ replacement , $ strkey2 ));
107+
108+ if ($ spaces1 == 0 || $ spaces2 == 0 || $ numkey1 % $ spaces1 != 0 || $ numkey2 % $ spaces2 != 0 ) {
109+ socket_close ($ user ->socket );
110+ console ('failed ' );
111+ return false ;
112+ }
113+
114+ $ ctx = hash_init ('md5 ' );
115+ hash_update ($ ctx , pack ("N " , $ numkey1 /$ spaces1 ));
116+ hash_update ($ ctx , pack ("N " , $ numkey2 /$ spaces2 ));
117+ hash_update ($ ctx , $ data );
118+ $ hash_data = hash_final ($ ctx ,true );
119+
120+ $ upgrade = "HTTP/1.1 101 WebSocket Protocol Handshake \r\n" .
98121 "Upgrade: WebSocket \r\n" .
99122 "Connection: Upgrade \r\n" .
100- "WebSocket-Origin: " . $ origin . "\r\n" .
101- "WebSocket-Location: ws:// " . $ host . $ resource . "\r\n" .
102- "\r\n" ;
123+ "Sec-WebSocket-Origin: " . $ origin . "\r\n" .
124+ "Sec-WebSocket-Location: ws:// " . $ host . $ resource . "\r\n" .
125+ "\r\n" .
126+ $ hash_data ;
127+
103128 socket_write ($ user ->socket ,$ upgrade .chr (0 ),strlen ($ upgrade .chr (0 )));
104129 $ user ->handshake =true ;
105130 console ($ upgrade );
@@ -112,7 +137,10 @@ function getheaders($req){
112137 if (preg_match ("/GET (.*) HTTP/ " ,$ req ,$ match )){ $ r =$ match [1 ]; }
113138 if (preg_match ("/Host: (.*) \r\n/ " ,$ req ,$ match )){ $ h =$ match [1 ]; }
114139 if (preg_match ("/Origin: (.*) \r\n/ " ,$ req ,$ match )){ $ o =$ match [1 ]; }
115- return array ($ r ,$ h ,$ o );
140+ if (preg_match ("/Sec-WebSocket-Key2: (.*) \r\n/ " ,$ req ,$ match )){ $ key2 =$ match [1 ]; }
141+ if (preg_match ("/Sec-WebSocket-Key1: (.*) \r\n/ " ,$ req ,$ match )){ $ key1 =$ match [1 ]; }
142+ if (preg_match ("/ \r\n(.*?) \$/ " ,$ req ,$ match )){ $ data =$ match [1 ]; }
143+ return array ($ r ,$ h ,$ o ,$ key1 ,$ key2 ,$ data );
116144}
117145
118146function getuserbysocket ($ socket ){
@@ -135,4 +163,4 @@ class User{
135163 var $ handshake ;
136164}
137165
138- ?>
166+ ?>
0 commit comments