dnswitch.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /* * **********************
  3. * Config section
  4. * *********************** */
  5. ////MySQL database settings
  6. $db_host = 'localhost';
  7. $db_database = 'stg';
  8. $db_login = 'mylogin';
  9. $db_password = 'newpassword';
  10. // DN switcher files path
  11. $dn_path = "/etc/stargazer/dn/";
  12. //speed size
  13. $speed_size = 'Kbit/s';
  14. /* * **********************
  15. * End of config section
  16. * *********************** */
  17. /**
  18. * Advanced php5 scandir analog with some filters
  19. *
  20. * @param string $directory Directory to scan
  21. * @param string $exp Filter expression - like *.ini or *.dat
  22. * @param string $type Filter type - all or dir
  23. * @param bool $do_not_filter
  24. *
  25. * @return array
  26. */
  27. function rcms_scandir($directory, $exp = '', $type = 'all', $do_not_filter = false) {
  28. $dir = $ndir = array();
  29. if (!empty($exp)) {
  30. $exp = '/^' . str_replace('*', '(.*)', str_replace('.', '\\.', $exp)) . '$/';
  31. }
  32. if (!empty($type) && $type !== 'all') {
  33. $func = 'is_' . $type;
  34. }
  35. if (is_dir($directory)) {
  36. $fh = opendir($directory);
  37. while (false !== ($filename = readdir($fh))) {
  38. if (substr($filename, 0, 1) != '.' || $do_not_filter) {
  39. if ((empty($type) || $type == 'all' || $func($directory . '/' . $filename)) && (empty($exp) || preg_match($exp, $filename))) {
  40. $dir[] = $filename;
  41. }
  42. }
  43. }
  44. closedir($fh);
  45. natsort($dir);
  46. }
  47. return $dir;
  48. }
  49. /**
  50. * Returns result as some query to database
  51. *
  52. * @global string $db_host
  53. * @global string $db_database
  54. * @global string $db_login
  55. * @global string $db_password
  56. * @param string $query
  57. *
  58. * @return array
  59. */
  60. function simple_queryall($query) {
  61. global $db_host, $db_database, $db_login, $db_password;
  62. $result = array();
  63. if (!extension_loaded('mysql')) {
  64. // init mysql link
  65. $dblink = mysqli_connect($db_host, $db_login, $db_password, $db_database);
  66. //executing query
  67. $queried = mysqli_query($dblink, $query);
  68. //getting result as array
  69. while ($row = mysqli_fetch_assoc($queried)) {
  70. $result[] = $row;
  71. }
  72. //closing link
  73. mysqli_close($dblink);
  74. } else {
  75. // init mysql link
  76. $dblink = mysql_connect($db_host, $db_login, $db_password);
  77. //selecting stargazer database
  78. mysql_select_db($db_database, $dblink);
  79. //executing query
  80. $queried = mysql_query($query);
  81. //getting result as array
  82. while ($row = mysql_fetch_assoc($queried)) {
  83. $result[] = $row;
  84. }
  85. //closing link
  86. mysql_close($dblink);
  87. }
  88. //return result of query as array
  89. return($result);
  90. }
  91. /**
  92. * Returns user tariff names array as login=>tariff
  93. *
  94. * @return array
  95. */
  96. function dshape_GetAllUserTariffs() {
  97. $query = "SELECT `login`,`Tariff` from `users`";
  98. $alltariffs = simple_queryall($query);
  99. $result = array();
  100. if (!empty($alltariffs)) {
  101. foreach ($alltariffs as $io => $eachtariff) {
  102. $result[$eachtariff['login']] = $eachtariff['Tariff'];
  103. }
  104. }
  105. return ($result);
  106. }
  107. /**
  108. * Returns list of available shaper time rules as tariff=>rule
  109. *
  110. * @return array
  111. */
  112. function dshape_GetTimeRules() {
  113. $now = date('H:i:s');
  114. $query = "SELECT `tariff`,`speed` from `dshape_time` WHERE '" . $now . "' > `threshold1` AND '" . $now . "' < `threshold2`";
  115. $result = array();
  116. $allrules = simple_queryall($query);
  117. if (!empty($allrules)) {
  118. foreach ($allrules as $io => $eachrule) {
  119. $result[$eachrule['tariff']] = $eachrule['speed'];
  120. }
  121. }
  122. return ($result);
  123. }
  124. /**
  125. * Switches speed directly with dummynet
  126. *
  127. * @param int $speed
  128. * @param int $mark
  129. * @param string $speed_size
  130. *
  131. * @return void
  132. */
  133. function dshape_SwitchSpeed($speed, $mark, $speed_size = 'Kbit/s') {
  134. $shape_command = '/sbin/ipfw -q pipe ' . trim($mark) . ' config bw ' . $speed . '' . $speed_size . ' queue 32Kbytes' . "\n";
  135. shell_exec($shape_command);
  136. }
  137. //parse all online users speed data
  138. $online_users = rcms_scandir($dn_path);
  139. $connect_data = array();
  140. if (!empty($online_users)) {
  141. foreach ($online_users as $ia => $eachdata) {
  142. $connect_data[$eachdata] = file_get_contents($dn_path . $eachdata);
  143. }
  144. }
  145. //getting tariffs and rules data
  146. $AllUserTariffs = dshape_GetAllUserTariffs();
  147. $AllTimeRules = dshape_GetTimeRules();
  148. $debugdata = '#### Shape start' . date("d-M-Y H:i:s") . "####\n";
  149. if (!empty($online_users)) {
  150. if (!empty($AllTimeRules)) {
  151. foreach ($online_users as $eachuser) {
  152. $normal_data = explode(':', $connect_data[$eachuser]);
  153. $normal_speed = $normal_data[0];
  154. $normal_mark = $normal_data[1];
  155. $new_speed = $normal_data[0];
  156. $user_tariff = $AllUserTariffs[$eachuser];
  157. $debugdata.='user login:' . $eachuser . "\n";
  158. $debugdata.='normal mark:' . trim($normal_mark) . "\n";
  159. $debugdata.='user tariff:' . $user_tariff . "\n";
  160. // check is now time to change speed?
  161. if (isset($AllTimeRules[$user_tariff])) {
  162. $new_speed = $AllTimeRules[$user_tariff];
  163. }
  164. $debugdata.='normal speed:' . $normal_speed . "\n";
  165. $debugdata.='new speed:' . $new_speed . "\n";
  166. $debugdata.='===============' . "\n";
  167. dshape_SwitchSpeed($new_speed, $normal_mark, $speed_size);
  168. }
  169. }
  170. }
  171. $debugdata.='####Shape end ' . date("d-M-Y H:i:s") . "####\n";
  172. //debug output
  173. print($debugdata);
  174. ?>