4th August 2020, 9:53 PM
I think the programmers among us may find this a bit funny.
I'm working on Volly Bounce for the first time since I moved JV over to the VPS from the old hosting provider. The way the environment was set up before I moved, users would send a login request from the VB client to a login.php file on the old server, which would send the login information to the socket server. (Sound familiar? That's because this process is ripped directly from PR2's code.)
The way the login.php script on PR2 connects to the socket server is it sends a request directly to the socket server via that server's address and port. Just one problem with that: the old hosting provider I was using prohibited any connections that weren't made via a list of approved ports. And, you guessed it, the port I was using happened not to be a port allowed to be used. Of course!
So, my options as I saw them were pretty much to either move JV (ended up doing this anyway) or upgrade the hosting plan (would've cost hundreds of dollars). I knew there had to be a way around this highway robbery. If I could move the process that made the actual connection to the socket server to the VPS itself, I could bypass my hosting provider's restrictions (as long as I made the connection to the VPS using an approved port).
One idea I had was to forward the port from 80 (HTTP) to whatever port on which the socket server would end up running. But... lol. That failed miserably, as could be predicted.
Eventually, I figured it out. I just made a passthrough PHP file that handled the input from the login.php script on JV and sent it as a regular web request to the VPS. Once the request got to the VPS, it was as easy as using the same talk_to_server function that PR2 uses.
Attached, please find the janky code I used to make this possible:
I'm glad not to have needed this, but I figured deleting it would just be a waste. So, here it is in all its janky glory!
I'm working on Volly Bounce for the first time since I moved JV over to the VPS from the old hosting provider. The way the environment was set up before I moved, users would send a login request from the VB client to a login.php file on the old server, which would send the login information to the socket server. (Sound familiar? That's because this process is ripped directly from PR2's code.)
The way the login.php script on PR2 connects to the socket server is it sends a request directly to the socket server via that server's address and port. Just one problem with that: the old hosting provider I was using prohibited any connections that weren't made via a list of approved ports. And, you guessed it, the port I was using happened not to be a port allowed to be used. Of course!
So, my options as I saw them were pretty much to either move JV (ended up doing this anyway) or upgrade the hosting plan (would've cost hundreds of dollars). I knew there had to be a way around this highway robbery. If I could move the process that made the actual connection to the socket server to the VPS itself, I could bypass my hosting provider's restrictions (as long as I made the connection to the VPS using an approved port).
One idea I had was to forward the port from 80 (HTTP) to whatever port on which the socket server would end up running. But... lol. That failed miserably, as could be predicted.
Eventually, I figured it out. I just made a passthrough PHP file that handled the input from the login.php script on JV and sent it as a regular web request to the VPS. Once the request got to the VPS, it was as easy as using the same talk_to_server function that PR2 uses.
Attached, please find the janky code I used to make this possible:
PHP Code:
// send this info to the socket server
$send = new stdClass();
$send->login_id = $login_id;
$send->ip_address = $ip;
$send->user = $user;
$send->guest = $is_guest;
$send->ignored = $ignored;
$str = "process_register_login`" . json_encode($send);
// communicate with the VPS
$data = array('port' => $VB_PORT, 'message' => $str);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents('http://' . $VPS_IP . '/to-socket.php', false, $context);
// get response from VPS
if ($result === false || @json_decode($result)->success === false) {
$msg = !empty(json_decode($result)->error) ? json_decode($result)->error : $result;
throw new Exception($msg);
}
PHP Code:
<?php
header("Content-type: text/plain");
require_once CONFIG;
require_once SOCKET_MGMT_FNS;
$from_ip = $_SERVER['REMOTE_ADDR'];
$message = @$_POST['message'];
$port = (int) @$_POST['port'];
$ret = new stdClass();
$ret->success = false;
try {
if ($from_ip !== $PROCESS_IP) {
throw new Exception("Not privileged. $from_ip");
} elseif ($_SERVER['REQUEST_METHOD'] !== "POST") {
throw new Exception("Invalid request method.");
}
if (empty($_POST['message'])) {
die(var_dump($_POST));
throw new Exception("No message specified.");
}
// send the message to the server
$from_server = talk_to_server($_SERVER['SERVER_ADDR'], $port, $message, true, true);
if ($from_server === false) {
throw new Exception('Could not connect to the server. If this persists, contact bls1999.');
}
$ret->success = true;
} catch (Exception $e) {
error_log('to_socket.php: ' . $e->getMessage() . " $from_ip");
$ret->error = $e->getMessage();
} finally {
die(json_encode($ret));
}