Cannot specify a port map to a port of an internal IP
But I have mapped the external IP to the internal Linux Server.
But I also want to use VNC to connect to an internal Windows computer from the outside.
So I wrote this program and the principle is like this
This program will open a port on the Linux Server for listening action. When the external connection is connected to this port, the program will open another connection to the internal Windows VNC and throw the external packets intact to the VNC connection. online, and then throw the data returned by the VNC connection intact back to the external port.
Program code:
#!/usr/bin/php -q
<?php
$IP = '192.168.1.1' ; //IP of Windows computer
$Port = '5900' ; //Port used by VNC
$ServerPort = '9999' ; //Port used by Linux Server externally
$RemoteSocket = false ; //Connect to the Socket of VNC
function SignalFunction & #40;$Signal)
& #123;
//This is the message processing function of the main Process
global $PID ; //Child Process's PID
switch & #40;$Signal)
& #123;
case SIGTRAP & #58;
case SIGTERM & #58;
//Receive the Signal to end the program
if& #40;$PID)
& #123;
//Send a SIGTERM signal to Child to tell him to end it quickly.
posix_kill & #40;$PID,SIGTERM);
//Wait for the Child Process to end to avoid zombies
pcntl_wait & #40;$Status);
& #125;
//Close the Socket opened by the main Process
DestroySocket & #40;);
exit& #40;0); //End the main Process
break;
case SIGCHLD & #58;
/*
When the Child Process ends, Child will send a SIGCHLD signal to Parent
When Parent receives SIGCHLD, it knows that the Child Process has ended and should take some ending actions*/
unset& #40;$PID); //Clear $PID to indicate that the Child Process has ended
pcntl_wait & #40;$Status); //Avoid Zombie
break;
default& #58;
& #125;
& #125;
function ChildSignalFunction & #40;$Signal)
& #123;
//This is the message processing function of Child Process
switch & #40;$Signal)
& #123;
case SIGTRAP & #58;
case SIGTERM & #58;
//Child Process receives the end message
DestroySocket & #40;); //Close Socket
exit& #40;0); //End Child Process
default& #58;
& #125;
& #125;
function ProcessSocket & #40;$ConnectedServerSocket)
& #123;
//Child Process Socket processing function
//$ConnectedServerSocket -> Externally connected Socket
global $ServerSocket , $RemoteSocket , $IP , $Port ;
$ServerSocket = $ConnectedServerSocket ;
declare& #40;ticks = 1); //This line must be added, otherwise the message processing function cannot be set.
//Set message processing function
if& #40;!pcntl_signal(SIGTERM, "ChildSignalFunction")) return;
if& #40;!pcntl_signal(SIGTRAP, "ChildSignalFunction")) return;
//Create a Socket connected to VNC
$RemoteSocket = socket_create & #40;AF_INET, SOCK_STREAM,SOL_TCP);
//Connect to the internal VNC
@ $RemoteConnected = socket_connect & #40;$RemoteSocket,$IP,$Port);
if& #40;!$RemoteConnected) return; //Unable to connect to VNC end
//Set Socket processing to Nonblock to prevent the program from being blocked
if& #40;!socket_set_nonblock($RemoteSocket)) return;
if& #40;!socket_set_nonblock($ServerSocket)) return;
while& #40;true)
& #123;
//Here we use pooling to obtain data
$NoRecvData = false ; //This variable is used to determine whether the external connection has read data.
$NoRemoteRecvData = false ; //This variable is used to determine whether the VNC connection has read data.
@ $RecvData = socket_read & #40;$ServerSocket,4096,PHP_BINARY_READ);
//Read 4096 bytes of data from the external connection
@ $RemoteRecvData = socket_read & #40;$RemoteSocket,4096,PHP_BINARY_READ);
//Read 4096 bytes of data from the vnc connection
if& #40;$RemoteRecvData==='')
& #123;
//VNC connection is interrupted, it’s time to end
echo "Remote Connection Closen" ;
return;
& #125;
if& #40;$RemoteRecvData===false)
& #123;
/*
Since we are using nonblobk mode, the situation here is that the vnc connection has no data to read.
*/
$NoRemoteRecvData = true ;
//Clear Last Error
socket_clear_error & #40;$RemoteSocket);
& #125;
if& #40;$RecvData==='')
& #123;
//The external connection is interrupted, it’s time to end
echo "Client Connection Closen" ;
return;
& #125;
if& #40;$RecvData===false)
& #123;
/*
Since we are using nonblobk mode, the situation here is that there is no data available for reading from the external connection.
*/
$NoRecvData = true ;
//Clear Last Error
socket_clear_error & #40;$ServerSocket);
& #125;
if& #40;$NoRecvData&&$NoRemoteRecvData)
& #123;
//If neither the external connection nor the VNC connection has data to read,
//Let the program sleep for 0.1 seconds to avoid long-term use of CPU resources
usleep & #40;100000);
//After waking up, continue the pooling action to read the socket
continue;
& #125;
//Recv Data
if& #40;!$NoRecvData)
& #123;
//External connection reads data
while& #40;true)
& #123;
//Transfer the data read from the external connection to the VNC connection
@ $WriteLen = socket_write & #40;$RemoteSocket,$RecvData);
if& #40;$WriteLen===false)
& #123;
//Due to network transmission problems, data cannot be written at the moment.
//Sleep for 0.1 seconds before trying again.
usleep & #40;100000);
continue;
& #125;
if& #40;$WriteLen===0)
& #123;
//The remote connection is interrupted, the program should end
echo "Remote Write Connection Closen" ;
return;
& #125;
//When the data read from the external connection has been completely sent to the VNC connection, this loop is interrupted.
if& #40;$WriteLen==strlen($RecvData)) break;
//If the data cannot be sent at one time, it must be split into several transmissions until all the data is sent.
$RecvData = substr & #40;$RecvData,$WriteLen);
& #125;
& #125;
if& #40;!$NoRemoteRecvData)
& #123;
//Here is the data read from the VNC connection and then transferred back to the external connection.
//The principle is almost the same as above and I won’t go into details again.
while& #40;true)
& #123;
@ $WriteLen = socket_write & #40;$ServerSocket,$RemoteRecvData);
if& #40;$WriteLen===false)
& #123;
usleep & #40;100000);
continue;
& #125;
if& #40;$WriteLen===0)
& #123;
echo "Remote Write Connection Closen" ;
return;
& #125;
if& #40;$WriteLen==strlen($RemoteRecvData)) break;
$RemoteRecvData = substr & #40;$RemoteRecvData,$WriteLen);
& #125;
& #125;
& #125;
& #125;
function DestroySocket & #40;)
& #123;
//Used to close the opened Socket
global $ServerSocket , $RemoteSocket ;
if& #40;$RemoteSocket)
& #123;
//If VNC connection is already enabled
//The Socket must be shut down before closing the Socket, otherwise the other party will not know that you have closed the connection.
@ socket_shutdown & #40;$RemoteSocket,2);
socket_clear_error & #40;$RemoteSocket);
//Close Socket
socket_close & #40;$RemoteSocket);
& #125;
//Close external connections
@ socket_shutdown & #40;$ServerSocket,2);
socket_clear_error & #40;$ServerSocket);
socket_close & #40;$ServerSocket);
& #125;
//This is the beginning of the entire program, and the program starts execution from here
//First execute a fork here
$PID = pcntl_fork & #40;);
if& #40;$PID==-1) die("could not fork");
//If $PID is not 0, it means this is a Parent Process
//$PID is Child Process
//This is the Parent Process. End it yourself and let Child become a Daemon.
if& #40;$PID) die("Daemon PID:$PIDn");
//From here on, the Daemon mode is executed.
//Detach the current Process from the terminal and enter daemon mode
if& #40;!posix_setsid()) die("could not detach from terminaln");
//Set the daemon's message processing function
declare& #40;ticks = 1);
if& #40;!pcntl_signal(SIGTERM, "SignalFunction")) die("Error!!!n");
if& #40;!pcntl_signal(SIGTRAP, "SignalFunction")) die("Error!!!n");
if& #40;!pcntl_signal(SIGCHLD, "SignalFunction")) die("Error!!!n");
//Establish a Socket for external connection
$ServerSocket = socket_create & #40;AF_INET, SOCK_STREAM,SOL_TCP);
//Set the IP and Port for external connection monitoring. Set the IP field to 0, which means listening to the IPs of all interfaces.
if& #40;!socket_bind($ServerSocket,0,$ServerPort)) die("Cannot Bind Socket!n");
//Start listening to the Port
if& #40;!socket_listen($ServerSocket)) die("Cannot Listen!n");
//Set Socket to nonblock mode
if& #40;!socket_set_nonblock($ServerSocket)) die("Cannot Set Server Socket to Block!n");
//Clear the $PID variable, indicating that there is currently no Child Process
unset& #40;$PID);
while& #40;true)
& #123;
//Enter pooling mode and check whether there is a connection every 1 second.
sleep & #40;1);
//Check if there is a connection coming in
@ $ConnectedServerSocket = socket_accept & #40;$ServerSocket);
if& #40;$ConnectedServerSocket!==false)
& #123;
//Someone is coming in
//Start a Child Process to handle the connection
$PID = pcntl_fork & #40;);
if& #40;$PID==-1) die("could not fork");
if& #40;$PID) continue;//This is the daemon process, continue to monitor.
//Here is the start of the Child Process
//Execute the function in Socket
ProcessSocket & #40;$ConnectedServerSocket);
//After processing the Socket, end the Socket
DestroySocket & #40;);
//End Child Process
exit& #40;0);
& #125;
& #125;
?>