Advertisement
Guest User

Untitled

a guest
Jun 5th, 2014
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.44 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head><title>Router</title></head>
  4. <body>
  5.  
  6. <?php
  7. const MAX_ENTRIES=20;
  8. class Proto {
  9.   const IPv4 = 0;
  10.   const IPv6 = 1;
  11. }
  12. function start_tun($fproto, $fip, $fport, $tproto, $tip, $tport) {
  13.   if ($fproto == Proto::IPv6 && $fip[0] != '[') $fip = '[' . $fip . ']';
  14.   if ($tproto == Proto::IPv6 && $tip[0] != '[') $tip = '[' . $tip . ']';
  15.  
  16.   $cmd = '/usr/bin/socat ';
  17.   $arg1 = '';
  18.   $arg2 = '';
  19.   if ($fproto == Proto::IPv4)
  20.     $arg1 .= 'TCP-LISTEN:';
  21.   else
  22.     $arg1 .= 'TCP6-LISTEN:';
  23.   $arg1 .= $fport . ',fork,bind=' . $fip;
  24.  
  25.   if ($tproto == Proto::IPv4)
  26.     $arg2 .= 'TCP:';
  27.   else
  28.     $arg2 .= 'TCP6:';
  29.   $arg2 .= $tip.':' . $tport;
  30.  
  31.   $cmd .= escapeshellarg($arg1) . ' ' . escapeshellarg($arg2) . ' > /dev/null 2>&1 & echo $!';
  32.  
  33.   $pid = exec($cmd);
  34.   return $pid;
  35. }
  36.  
  37. function kill_tuns() {
  38.   $pids = file_get_contents("pids.txt");
  39.   $pids = explode(',', $pids);
  40.   foreach ($pids as $pid) {
  41.     $pid = trim($pid);
  42.     if ($pid != '' && $pid != '0') {
  43.       posix_kill((int)$pid, 15);
  44.     }
  45.   }
  46. }
  47.  
  48. function apply_all_rules() {
  49.   $pids = '';
  50.   $rules = '';
  51.   for ($i = 0; $i < MAX_ENTRIES; $i++) {
  52.     $fproto = ($_POST['fproto'.$i] == 'IPv4' ? Proto::IPv4 : Proto::IPv6);
  53.     $fip = $_POST['fip'.$i];
  54.     $fport = $_POST['fport'.$i];
  55.     $tproto = ($_POST['tproto'.$i] == 'IPv4' ? Proto::IPv4 : Proto::IPv6);
  56.     $tip = $_POST['tip'.$i];
  57.     $tport = $_POST['tport'.$i];
  58.     if ($fip == '' || $fport == '' || $tip == '' || $tport == '') continue;
  59.     $pids .= start_tun($fproto, $fip, $fport, $tproto, $tip, $tport) . ',';
  60.     $rules .= $fproto.",$fip,$fport,".$tproto.",$tip,$tport\n";
  61.   }
  62.   file_put_contents("pids.txt", $pids);
  63.   file_put_contents("rules.txt", $rules);
  64. }
  65.  
  66. if (isset($_POST['submit'])) {
  67.   kill_tuns();
  68.   apply_all_rules();
  69. }
  70.  
  71. $rulestxt = explode("\n", file_get_contents('rules.txt'));
  72. $rules = array();
  73. for ($i = 0; $i < MAX_ENTRIES; $i++) {
  74.   if ($i < count($rulestxt)) {
  75.     $curr = explode(',', $rulestxt[$i]);
  76.     if (count($curr) == 6) {
  77.       $curr[0] = ($curr[0] == '0' ? Proto::IPv4 : Proto::IPv6);
  78.       $curr[3] = ($curr[3] == '0' ? Proto::IPv4 : Proto::IPv6);
  79.       $rules[$i] = $curr;
  80.       continue;
  81.     }
  82.   }
  83.   $rules[$i] = array(Proto::IPv4, '', '', Proto::IPv4, '', '');
  84. }
  85. ?>
  86.  
  87.  
  88. <center>
  89. <form id="forwards" method="post">
  90. <table border="1">
  91. <tr><td colspan="3">FROM</td><td colspan="3">TO</td></tr>
  92. <tr><td>Proto</td><td>Address</td><td>Port</td><td>Proto</td><td>Address</td><td>Port</td></tr>
  93. <?php
  94. for ($i = 0; $i < MAX_ENTRIES; $i++) {
  95.   echo '<tr>
  96.    <td><select name="fproto'.$i.'"><option>IPv4</option><option'.
  97.                                     (($rules[$i][0] == Proto::IPv6) ? ' selected="selected"' : '') .'>IPv6</option></select></td>
  98.    <td><input type="text" name="fip'.$i.'" value="'.$rules[$i][1].'" style="width:300px"></td>
  99.    <td><input type="text" name="fport'.$i.'" value="'.$rules[$i][2].'" style="width:40px"></td>
  100.    <td><select name="tproto'.$i.'"><option>IPv4</option><option'.
  101.                                     (($rules[$i][3] == Proto::IPv6) ? ' selected="selected"' : '') .'>IPv6</option></select></td>
  102.    <td><input type="text" name="tip'.$i.'" value="'.$rules[$i][4].'" style="width:300px"></td>
  103.    <td><input type="text" name="tport'.$i.'" value="'.$rules[$i][5].'" style="width:40px"></td>
  104.  </tr>';
  105. }
  106. ?>
  107. </table>
  108. <input type="submit" name="submit" value="Save">
  109. </form>
  110. </center>
  111. </body>
  112. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement