1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
#!/usr/bin/php
<?php
/*
* Configuration
*/
define('IRC_ADDR', 'ip serveur a joindre');
define('IRC_PORT', '6667');
define('FLASH_ADDR', 'ip site');
define('FLASH_PORT', '6679');
define('MAXLINE', '2048');
define('MAXCONCURRENT', '30');
/*
* Fin de la configuration
*/
// Modification de configuration php
error_reporting (E_ALL);
set_time_limit(0);
ob_implicit_flush ();
// Fonction de decodage utf8
// hack for utf8
function smart_utf8_decode($in_str) {
$new_str = str_replace('?', 'q0u0e0s0t0i0o0n', $in_str);
$new_str=utf8_decode($new_str);
if (strpos($new_str,'?') !== false) {
$new_str=$in_str;
} else {
$new_str = str_replace('q0u0e0s0t0i0o0n', '?', $new_str);
}
return $new_str;
}
// Création du socket d'écoute (vers flash)
$socketHandler = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ( !$socketHandler ) {
fwrite(STDOUT, 'Creation du socket vers le flash [FAILED]'."\n");
fwrite(STDOUT, 'Erreur lors du socket_create : '.socket_strerror($socketHandler)."\n");
} else {
fwrite(STDOUT, 'Creation du serveur [OK]'."\n");
}
// Adressage du socket
socket_setopt($socketHandler, SOL_SOCKET, SO_REUSEADDR, 1);
$bindRes = socket_bind($socketHandler, FLASH_ADDR, FLASH_PORT);
if ( !$bindRes ) {
fwrite(STDOUT, 'Erreur lors du socket_bind du flash : '.socket_strerror($bindRes)."\n");
} else {
fwrite(STDOUT, 'Socket du flash adressé : '."\n");
}
// Mise en ecoute
$listenRes = socket_listen($socketHandler, MAXCONCURRENT);
if ( !$listenRes ) {
fwrite(STDOUT, 'Erreur lors du socket_listen du flash '."\n");
} else {
fwrite(STDOUT, 'Socket du flash en écoute '."\n");
}
// Initialisation des clients flash
$maxi = -1;
for ($i = 0; $i < MAXCONCURRENT; $i++) {
$client[$i] = null;
$clientBuff[$i] = '';
}
// Initialisation des liaison irc
$maxi = -1;
for ($i = 0; $i < MAXCONCURRENT; $i++) {
$irc[$i] = null;
$ircBuff[$i] = '';
}
$h = 0;
// Boucle Principale
while(true) {
++$h;
// On recompose le tableau tabSocket
// Socket Principal
$tabSocket[0] = $socketHandler;
// Les clients flash
for ($i = 0; $i < MAXCONCURRENT; $i++) {
if ($client[$i] != null) {
$tabSocket[$i + 1] = $client[$i];
}
}
// Les liaisons irc
for ($i = 0; $i < MAXCONCURRENT; $i++) {
if ($irc[$i] != null) {
$tabSocket[MAXCONCURRENT + $i] = $irc[$i];
}
}
// On nettoie les connexion fermees
foreach ($tabSocket as $key=>$value) {
if (get_resource_type($value)=='Unknown') {
unset($tabSocket[$key]);
}
}
// On bloque en attendant une connexion
$pret = socket_select($tabSocket, $null = null, $null = null, null);
// Si on detecte une nouvelle connexion
if (in_array($socketHandler, $tabSocket)) {
for ($i=0; $i < MAXCONCURRENT; $i++) {
if ($client[$i]==null) {
// On se met à l'écoute
$client[$i] = socket_accept($socketHandler);
socket_setopt($client[$i], SOL_SOCKET, SO_REUSEADDR, 1);
socket_getpeername($client[$i], $remote_host[$i], $remote_port[$i]);
// On fait une liaison à l'ircd
$todisconnect = false;
$irc[$i] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if(!$irc[$i]) {
$todisconnect = true;
} else {
$test = socket_connect($irc[$i], IRC_ADDR, IRC_PORT);
if(!$test) {
$todisconnect = true;
}
}
if ($todisconnect) {
// Y'a eut un probleme de connexion irc, on deconnecte le client
socket_shutdown($client[$i]);
socket_close($client[$i]);
$client[$i] = null;
unset($remote_host[$i]);
unset($remote_port[$i]);
}
break;
}
if ($i == MAXCONCURRENT - 1) {
echo 'Trop de clients'."\n";
}
}
if ($i>$maxi) {
$maxi = $i;
}
if (--$pret<=0) {
continue;
}
}
// On balaye les clients pour voir si un client a parlé
for ($i = 0; $i <= $maxi; $i++) {
if ($client[$i]==null) {
continue;
}
if (in_array($client[$i], $tabSocket)) {
$buff = trim(socket_read($client[$i], MAXLINE));
if (!$buff) {
// On ferme la connexion flash
socket_shutdown($client[$i]);
socket_close($client[$i]);
$client[$i] = null;
unset($remote_host[$i]);
unset($remote_port[$i]);
// On ferme la connexion ircd
socket_shutdown($irc[$i]);
socket_close($irc[$i]);
$irc[$i] = null;
} else {
// On modifie les < pour que ca passe bien
$buff = str_replace('<','<', $buff);
// On envoie du flash vers le irc
socket_write($irc[$i], $buff."\n");
}
}
}
// On balaye connexionx irc pour voir si un irc a parlé
for ($i = 0; $i <= $maxi; $i++) {
if ($irc[$i]==null) {
continue;
}
if (in_array($irc[$i], $tabSocket)) {
$buff = trim(socket_read($irc[$i], MAXLINE));
if (!$buff) {
// On ferme la connexion flash
socket_shutdown($client[$i]);
socket_close($client[$i]);
$client[$i] = null;
unset($remote_host[$i]);
unset($remote_port[$i]);
// On ferme la connexion ircd
socket_shutdown($irc[$i]);
socket_close($irc[$i]);
$irc[$i] = null;
} else {
// On decode si c'est en utf8
$buff = smart_utf8_decode($buff);
// On modifie les < en <
$buff = str_replace('<', '<', $buff);
// On envoie de irc vers flash
$tabout = explode("\r\n", $buff);
foreach($tabout as $value) {
socket_write($client[$i], $value."\0");
}
}
}
}
}
// Fermeture du serveur
socket_close($socketHandler);
?> |
Partager