S0lll0s

Open & Free Reverse Proxy Server

Jun 22nd, 2013
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.70 KB | None | 0 0
  1. --redirect.sh
  2. #!/bin/bash
  3. #$1 = "internal" port
  4. #$2 = IP
  5. #$3 = port
  6.  
  7. fifoname = "revprox" + $1 #unique backpipe
  8. nc -l $1 0<$fifoname | nc $2 $3 1>$fifoname #redirect doublepipe
  9.  
  10. --newredir.sh
  11. #!/bin/bash
  12. #$1 "internal" port
  13. #$2 IP
  14. #$3 port
  15. #$4 lifetime (seconds)
  16.  
  17. ./redirect.sh $1 $2 $3 & #run in background
  18. pid=$! #process ID in $!
  19.  
  20. sleep $4
  21.  
  22. kill -9 $pid #kill it (-9 = extra hard)
  23.  
  24. --form.php
  25. <form action="newredirect.php" method="POST">
  26. The IP to forward to: <input type="text" name="IP" /><br/>
  27. The port to forward to: <input type="text" name="port" /><br/>
  28. <input type="submit" name="Forward" />
  29. </form>
  30.  
  31. --newredirect.php
  32. if( ! preg_match( '/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/', $_POST['IP'] ) )
  33.     die( "Invalid IP" );
  34.  
  35. if( ! preg_match( '/[0-9]*/', $_POST['port'] ) )
  36.     die( "Invalid port" );
  37.  
  38. if( $_POST['IP'] == "MY OWN IP" )
  39.     die( "Not forwarding to myself" ); //dont know why, but I guess this could end "bad"
  40.  
  41. mysql_connect( ... );
  42. mysql_select_db( "reverse_proxy" );
  43. $res = mysql_query( "SELECT * FROM ports WHERE expires <= NOW() LIMIT 1" );
  44. $port = mysql_fetch_object( $res );
  45.  
  46. if ( $port == null )
  47.     die( "Sorry, no free ports. Please try again later." );
  48.  
  49. exec( "./newredir.sh " . $port->port . " " . $_POST['IP'] . " " . $_POST['port'] . "1800 &" );
  50. mysql_query( "UPDATE ports SET expires=DATE_ADD(NOW(), INTERVAL 30 MINUTE) WHERE port = " . $port->port );
  51.  
  52. echo( "Hooray, you are now reachable at " . $_SERVER['HTTP_HOST'] . ":" . $port->port . "!" );
  53.  
  54. --ports_table.sql
  55. CREATE TABLE IF NOT EXISTS `ports` (
  56.   `port` int(11) NOT NULL,
  57.   `expires` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  58.   PRIMARY KEY (`port`)
  59. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Add Comment
Please, Sign In to add comment