function process($socket, $msg) { $action = unmask($msg); console("< " . $action); }
array_push($arr1, trim($data[4])); // echo $today.'='.substr($data[1],0,10).'<br>'; if (substr($data[1], 0, 10) == $today) { array_push($arr2, trim($data[4])); } } } echo 'he go from: today ' . $today . '<hr>'; array_multisort($arr2, SORT_DESC); $arr2 = array_count_values($arr2); array_multisort($arr2, SORT_DESC); while (list($key, $val) = each($arr2)) { echo '<a href="' . $key . '">' . "{$val} " . unmask($key) . "</a><br>"; } echo '<br>he go from: all<hr> '; array_multisort($arr1, SORT_DESC); $arr1 = array_count_values($arr1); array_multisort($arr1, SORT_DESC); while (list($key, $val) = each($arr1)) { echo '<a href="' . $key . '">' . "{$val} " . unmask($key) . "</a><br>"; } //**************************************** function unmask($str = "") { return $str; } ?>
//read data sent by the socket perform_handshaking($header, $socket_new, $host, $port); //perform websocket handshake socket_getpeername($socket_new, $address, $port); //get ip address of connected socket $address_clients[$found_socket] = $address; $found_socket = array_search($sock, $changed); echo "Client[{$address}] connected to us on port {$port}\n"; //make room for new socket unset($changed[$found_socket]); } //loop through all connected sockets foreach ($changed as $changed_socket) { //check for any incomming data while (socket_recv($changed_socket, $buf, 1024, 0) >= 1) { $received_text = unmask($buf); //unmask data $received = explode(',', $received_text); if (!empty($received[1])) { echo $received[0] . "\t" . $received[1] . "\n"; if (array_search($received[0], $address_clients)) { $found_socket = array_search($received[0], $address_clients); socket_write($clients[$found_socket], mask($received[1]), strlen(mask($received[1]))); } } break 2; //exit this loop } $buf = socket_read($changed_socket, 1024, PHP_NORMAL_READ); if ($buf === false) { // remove client for $clients array
/* 바이트 값을 하나씩 출력 for ($i = 0; $i < strlen($buffer); $i++) { echo ord($buffer[$i]).','; } echo "\n"; */ if ($bytes === false || (ord($buffer[0]) & 15) == 8) { ## 연결종료 ########################################### unset($socket_list[$addr_port]); unset($handshake_check[$addr_port]); socket_close($read_socket); echo "<{$addr_port}>[Close]\n"; ##################################################### } else { ## 메시지전달 ########################################## $msg = unmask($buffer); $echo_msg = mask("ECHO: {$msg}"); socket_write($read_socket, $echo_msg); echo "<{$addr_port}>: {$msg}\n"; ##################################################### } } // end if } // end if } // end foreach } // end while function parse_header($str) {
echo "wait...\n\n"; $msgsock = socket_accept($m_sock); array_push($cls, $msgsock); echo "Connected...\n\n"; socket_recv($msgsock, $hds, 4096, 0); if (preg_match("/Sec-WebSocket-Key: (.*)\r\n/", $hds, $matchs)) { echo "do handshake...\n\n"; $key = $matchs[1] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; $key = base64_encode(sha1($key, true)); $headers = "HTTP/1.1 101 Switching Protocols\r\n" . "Upgrade: websocket\r\n" . "Connection: Upgrade\r\n" . "Sec-WebSocket-Accept: {$key}" . "\r\n\r\n"; socket_write($msgsock, $headers); echo "handshak done...\n"; } } else { $bytes = socket_recv($sock, $data, 2048, null); $d = unmask($data); foreach ($cls as $socket) { if ($socket != $m_sock && $val > 0) { try { socket_write($socket, encode($d)); } catch (Exception $e) { unset($cls[$socket]); socket_close($cls[$socket]); } } } } } } while (1); socket_close($m_sock); function unmask($payload)
$opt = getopt('hd:', ['help', 'mask', 'unmask']); if (isset($opt['h']) || isset($opt['help'])) { showHelp(); exit(0); } $dir = realpath(__DIR__ . DIRECTORY_SEPARATOR . '..'); if (isset($opt['d'])) { $dir = $opt['d']; } if (isset($opt['mask'])) { echo "Mask *.php files in {$dir}\n"; $re = scanDirectory($dir, function ($f) { mask($f); }); if ($re !== 0) { echo "Fail to mask!\n"; } } elseif (isset($opt['unmask'])) { echo "Unmask *.php files in {$dir}\n"; $re = scanDirectory($dir, function ($f) { unmask($f); }); if ($re !== 0) { echo "Fail to unmask!\n"; } } else { echo "Invalid options\n"; showHelp(); $re = 0; } exit($re);