Created
January 16, 2021 05:39
-
-
Save jakebathman/5c48af8380d1f9930c47993b6d6f5be9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$host = 'palindromer-bd7e0fc867d57915.elb.us-east-1.amazonaws.com'; | |
$port = 7777; | |
$socket = fsockopen($host, $port); | |
if (! $socket) { | |
die('Error: connection not successful'); | |
} | |
echo 'Processing lines '; | |
while ($line = fgets($socket)) { | |
echo '.'; | |
// Divide the string into word chunks along spaces | |
$words = explode(' ', $line); | |
// Check for special line, denoting flag | |
if (stripos($line, '!!!') === 0) { | |
if (preg_match('/!!! flag\[(.*?)\]/i', $line, $matches)) { | |
$response = $matches[1] . "\n"; | |
echo "\n\nFlag received: {$response}"; | |
fputs($socket, $response); | |
return fclose($socket); | |
} | |
} | |
// Get the palindromes | |
$return = []; | |
foreach ($words as $word) { | |
if (isPalindromic($word)) { | |
$return[] = $word; | |
} | |
} | |
// Must respond with newline at the end | |
$response = trim(implode(' ', $return)) . "\n"; | |
// Send response string | |
fputs($socket, $response); | |
} | |
function isPalindromic($word) | |
{ | |
$word = trim($word); | |
return $word == strrev($word); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment