Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head><title>Router</title></head>
- <body>
- <?php
- const MAX_ENTRIES=20;
- class Proto {
- const IPv4 = 0;
- const IPv6 = 1;
- }
- function start_tun($fproto, $fip, $fport, $tproto, $tip, $tport) {
- if ($fproto == Proto::IPv6 && $fip[0] != '[') $fip = '[' . $fip . ']';
- if ($tproto == Proto::IPv6 && $tip[0] != '[') $tip = '[' . $tip . ']';
- $cmd = '/usr/bin/socat ';
- $arg1 = '';
- $arg2 = '';
- if ($fproto == Proto::IPv4)
- $arg1 .= 'TCP-LISTEN:';
- else
- $arg1 .= 'TCP6-LISTEN:';
- $arg1 .= $fport . ',fork,bind=' . $fip;
- if ($tproto == Proto::IPv4)
- $arg2 .= 'TCP:';
- else
- $arg2 .= 'TCP6:';
- $arg2 .= $tip.':' . $tport;
- $cmd .= escapeshellarg($arg1) . ' ' . escapeshellarg($arg2) . ' > /dev/null 2>&1 & echo $!';
- $pid = exec($cmd);
- return $pid;
- }
- function kill_tuns() {
- $pids = file_get_contents("pids.txt");
- $pids = explode(',', $pids);
- foreach ($pids as $pid) {
- $pid = trim($pid);
- if ($pid != '' && $pid != '0') {
- posix_kill((int)$pid, 15);
- }
- }
- }
- function apply_all_rules() {
- $pids = '';
- $rules = '';
- for ($i = 0; $i < MAX_ENTRIES; $i++) {
- $fproto = ($_POST['fproto'.$i] == 'IPv4' ? Proto::IPv4 : Proto::IPv6);
- $fip = $_POST['fip'.$i];
- $fport = $_POST['fport'.$i];
- $tproto = ($_POST['tproto'.$i] == 'IPv4' ? Proto::IPv4 : Proto::IPv6);
- $tip = $_POST['tip'.$i];
- $tport = $_POST['tport'.$i];
- if ($fip == '' || $fport == '' || $tip == '' || $tport == '') continue;
- $pids .= start_tun($fproto, $fip, $fport, $tproto, $tip, $tport) . ',';
- $rules .= $fproto.",$fip,$fport,".$tproto.",$tip,$tport\n";
- }
- file_put_contents("pids.txt", $pids);
- file_put_contents("rules.txt", $rules);
- }
- if (isset($_POST['submit'])) {
- kill_tuns();
- apply_all_rules();
- }
- $rulestxt = explode("\n", file_get_contents('rules.txt'));
- $rules = array();
- for ($i = 0; $i < MAX_ENTRIES; $i++) {
- if ($i < count($rulestxt)) {
- $curr = explode(',', $rulestxt[$i]);
- if (count($curr) == 6) {
- $curr[0] = ($curr[0] == '0' ? Proto::IPv4 : Proto::IPv6);
- $curr[3] = ($curr[3] == '0' ? Proto::IPv4 : Proto::IPv6);
- $rules[$i] = $curr;
- continue;
- }
- }
- $rules[$i] = array(Proto::IPv4, '', '', Proto::IPv4, '', '');
- }
- ?>
- <center>
- <form id="forwards" method="post">
- <table border="1">
- <tr><td colspan="3">FROM</td><td colspan="3">TO</td></tr>
- <tr><td>Proto</td><td>Address</td><td>Port</td><td>Proto</td><td>Address</td><td>Port</td></tr>
- <?php
- for ($i = 0; $i < MAX_ENTRIES; $i++) {
- echo '<tr>
- <td><select name="fproto'.$i.'"><option>IPv4</option><option'.
- (($rules[$i][0] == Proto::IPv6) ? ' selected="selected"' : '') .'>IPv6</option></select></td>
- <td><input type="text" name="fip'.$i.'" value="'.$rules[$i][1].'" style="width:300px"></td>
- <td><input type="text" name="fport'.$i.'" value="'.$rules[$i][2].'" style="width:40px"></td>
- <td><select name="tproto'.$i.'"><option>IPv4</option><option'.
- (($rules[$i][3] == Proto::IPv6) ? ' selected="selected"' : '') .'>IPv6</option></select></td>
- <td><input type="text" name="tip'.$i.'" value="'.$rules[$i][4].'" style="width:300px"></td>
- <td><input type="text" name="tport'.$i.'" value="'.$rules[$i][5].'" style="width:40px"></td>
- </tr>';
- }
- ?>
- </table>
- <input type="submit" name="submit" value="Save">
- </form>
- </center>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement