Skip to content

Instantly share code, notes, and snippets.

@rstriquer
Last active November 11, 2022 01:30
Show Gist options
  • Save rstriquer/adb421e7506091eb630365c8ee40eeb1 to your computer and use it in GitHub Desktop.
Save rstriquer/adb421e7506091eb630365c8ee40eeb1 to your computer and use it in GitHub Desktop.
Implements an UDP protocol server that will receive data packets through a port bind from any of the IPs available on the local machine and write the same content (in its entirety) to a third-party server specified in the command line.
<?php
// code inspired from https://www.binarytides.com/udp-socket-programming-in-php/
error_reporting(~E_WARNING);
$argv = RearrangeArguments([
['name' => '--localPort', 'type' => 'optional', 'cast' => 'string', 'default' => '9999'],
['name' => '--targetHost', 'type' => 'required', 'cast' => 'string', 'default' => ''],
['name' => '--targetPort', 'type' => 'required', 'cast' => 'string', 'default' => ''],
]);
if (!($sock = socket_create(AF_INET, SOCK_DGRAM, 0))) {
$errorCode = socket_last_error();
$errorMsg = socket_strerror($errorCode);
die("Couldn't create socket: [$errorCode] $errorMsg \n");
}
echo "Socket created \n";
if (!socket_bind($sock, '0.0.0.0' , $argv['localPort'])) {
$errorCode = socket_last_error();
$errorMsg = socket_strerror($errorCode);
die("Could not bind socket : [$errorCode] $errorMsg \n");
}
echo 'Socket bind to "udp://0.0.0.0:' . $argv['localPort'] . '"';
echo "\n";
echo 'Software will read from any IP available in this machine (by UDP protocol) and forward content to ';
echo $argv['targetHost'] . ':' . $argv['targetPort'];
echo "\nWaiting for data ... \n";
do {
$r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
var_dump($r);
echo "(From:$remote_ip:$remote_port) " . $buf;
socket_sendto($sock, $buf , strlen($buf), 0 , '192.168.0.179', 8125);
} while(true);
socket_close($sock);
/**
* Function checks command line parameters and recreates array $argv
* - type) must be 'required' or 'optional'
* @param array $payload parameters being awaited. Ex: [['name' => '--database', 'type' => 'required', 'cast' => 'string', 'default' => '']],
*/
function RearrangeArguments(array $payload)
{
global $argv;
$result = [];
$index = 0;
if (array_search('--help', $argv) !== false) {
help();
exit();
}
foreach($payload AS $item) {
if (!isset($item['default'])) {
$item['default']='';
}
$index = array_search($item['name'], $argv);
$item['name'] = substr($item['name'], 2);
$result[$item['name']] = $item['default'];
if ($index !== false) {
if (isset($argv[$index+1])) {
$result[$item['name']] = $argv[$index+1];
} else {
$result[$item['name']] = null;
}
} elseif ($item['type'] == 'required') {
help();
exit("Parameter ".$item['name']." required not provided!\n");
}
if ($item['cast'] == 'boolean') {
if ($result[$item['name']] == null) {
$result[$item['name']] = true;
} else if ($result[$item['name']] == 'false') {
$result[$item['name']] = false;
}
}
}
return $result;
}
function help() {
echo "\n";
echo " UDP Server wrapper.\n";
echo "\n";
echo "Sometimes a network have some blocks to receive comunication from another network\n";
echo "this software can receive a package from UDP comunication and forward its contents\n";
echo "to the destination network wrapping its content in another unblocked path\n";
echo "\n";
echo "Must use the following parameters:\n";
echo " --localPort: Local port to bind to and await communications\n";
echo " --targetHost: Host IP to forward content to after receive it\n";
echo " --targetPort: Host communication port to forward content to after receive it\n";
echo "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment