Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diyism/a32decce4fd3cd831a44493c1096b047 to your computer and use it in GitHub Desktop.
Save diyism/a32decce4fd3cd831a44493c1096b047 to your computer and use it in GitHub Desktop.
udp traffic forwording with swoole udp server or swoole udp client
<?php
$ch='';
$udp_server=new swoole_server('0.0.0.0', 21414, SWOOLE_BASE, SWOOLE_UDP);
$udp_server->on('packet', function ($ipsock, $data, $ipsock_con) /* use(&$ch) */
{
//echo $data."=========sent====\n\n";
if ($ipsock_con['port']!=21406)
{
$ipsock->sendto('127.0.0.1', 21406, $data); //to wg
}
else
{
$ipsock->sendto('127.0.0.2', 21409, $data); //to tws
}
}
);
/*$udp_server->on('workerstart', function($ipsock, $worker_id)
{
if ($worker_id==0)
{
$ch=new swoole_client(SWOOLE_UDP | SWOOLE_ASYNC);
$ch->set(array('bind_address'=>'127.0.0.1', 'bind_port'=>21414));
$ch->on('receive', function ($ch, $data)
{
$src_ip_port=$ch->getPeerName();
//echo $data.$src_ip_port['port']."==========received===\n\n";
if ($src_ip_port['port']!=21406)
{
$ch->sendto('127.0.0.1', 21406, $data); //to wg
}
else
{
$ch->sendto('127.0.0.2', 21409, $data); //to tws
}
}
);
$ch->on('connect', function($ch){}); //don't remove this line
$ch->connect('127.0.0.2', 21409, 0.3);
}
}
);*/
$udp_server->start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment