Skip to content

Commit

Permalink
[S] Add IPV6 support to game server
Browse files Browse the repository at this point in the history
  • Loading branch information
Benau committed Aug 21, 2019
1 parent e6e913c commit 8eb6d71
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 2 additions & 0 deletions api/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
$userid = isset($_POST['userid']) ? (int)$_POST['userid'] : 0;
$token = isset($_POST['token']) ? $_POST['token'] : "";
$address = isset($_POST['address']) ? (int)utf8_encode($_POST['address']) : null;
$address_ipv6 = isset($_POST['address_ipv6']) ? $_POST['address_ipv6'] : "";
$port = isset($_POST['port']) ? (int)utf8_encode($_POST['port']) : null;
$private_port = isset($_POST['private_port']) ? (int)utf8_encode($_POST['private_port']) : null;
$server_name = isset($_POST['name']) ? $_POST['name'] : "";
Expand All @@ -46,6 +47,7 @@
$version = isset($_POST['version']) ? (int)$_POST['version'] : 1;
$server = ClientSession::get($token, $userid)->createServer(
$address,
$address_ipv6,
$port,
$private_port,
$server_name,
Expand Down
3 changes: 3 additions & 0 deletions include/ClientSession.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function getFriendsOf(int $visiting_id)
* Create a server instance
*
* @param int $ip
* @param string $ipv6
* @param int $port
* @param int $private_port
* @param string $server_name
Expand All @@ -91,6 +92,7 @@ public function getFriendsOf(int $visiting_id)
*/
public function createServer(
$ip,
$ipv6,
int $port,
int $private_port,
$server_name,
Expand All @@ -102,6 +104,7 @@ public function createServer(
) {
return Server::create(
$ip,
$ipv6,
$port,
$private_port,
$this->user->getId(),
Expand Down
18 changes: 15 additions & 3 deletions include/Server.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class Server implements IAsXML
*/
private $ip;

/**
* IPV6 address of server if supported
* @var string
*/
private $ipv6;

/**
* The server's public port
* @var int
Expand Down Expand Up @@ -139,6 +145,7 @@ private function __construct(array $data, array $player_info = [])
$this->name = $data["name"];
$this->max_players = (int)$data["max_players"];
$this->ip = $data["ip"];
$this->ipv6 = $data["ipv6"];
$this->port = (int)$data["port"];
$this->private_port = (int)$data["private_port"];
$this->difficulty = (int)$data["difficulty"];
Expand Down Expand Up @@ -215,6 +222,7 @@ public function asXMLFromClientLocation($client_latitude, $client_longitude)
$server_xml->writeAttribute("name", $this->name);
$server_xml->writeAttribute("max_players", $this->max_players);
$server_xml->writeAttribute("ip", $this->ip);
$server_xml->writeAttribute("ipv6", $this->ipv6);
$server_xml->writeAttribute("port", $this->port);
$server_xml->writeAttribute("private_port", $this->private_port);
$server_xml->writeAttribute("difficulty", $this->difficulty);
Expand Down Expand Up @@ -279,6 +287,7 @@ public static function cleanOldServers(int $since_seconds = 15)
* Create server
*
* @param int $ip
* @param string $ipv6
* @param int $port
* @param int $private_port
* @param int $user_id
Expand All @@ -294,6 +303,7 @@ public static function cleanOldServers(int $since_seconds = 15)
*/
public static function create(
$ip,
$ipv6,
int $port,
int $private_port,
int $user_id,
Expand All @@ -320,9 +330,9 @@ public static function create(
$server_geolocation = Util::getIPGeolocation($ip);
$result = DBConnection::get()->query(
"INSERT INTO `{DB_VERSION}_servers` (host_id, name,
last_poll_time, ip, port, private_port, max_players,
last_poll_time, ip, ipv6, port, private_port, max_players,
difficulty, game_mode, password, version, latitude, longitude, country_code)
VALUES (:host_id, :name, :last_poll_time, :ip, :port,
VALUES (:host_id, :name, :last_poll_time, :ip, :ipv6, :port,
:private_port, :max_players, :difficulty, :game_mode,
:password, :version, :latitude, :longitude, :country_code)",
DBConnection::ROW_COUNT,
Expand All @@ -332,6 +342,7 @@ public static function create(
':last_poll_time' => time(),
// Do not use (int) or it truncates to 127.255.255.255
':ip' => $ip,
':ipv6' => $ipv6,
':port' => $port,
':private_port' => $private_port,
':max_players' => $max_players,
Expand All @@ -348,6 +359,7 @@ public static function create(
':name' => DBConnection::PARAM_STR,
':last_poll_time' => DBConnection::PARAM_INT,
':ip' => DBConnection::PARAM_INT,
':ipv6' => DBConnection::PARAM_STR,
':port' => DBConnection::PARAM_INT,
':private_port' => DBConnection::PARAM_INT,
':max_players' => DBConnection::PARAM_INT,
Expand Down Expand Up @@ -467,7 +479,7 @@ public static function getAllServerConnectionsWithUsers(): array
static::cleanOldServers();
return DBConnection::get()->query(
"SELECT `{DB_VERSION}_servers`.id, `{DB_VERSION}_servers`.host_id, `{DB_VERSION}_servers`.name,
`{DB_VERSION}_servers`.ip, `{DB_VERSION}_servers`.port, `{DB_VERSION}_servers`.private_port,
`{DB_VERSION}_servers`.ip, `{DB_VERSION}_servers`.ipv6, `{DB_VERSION}_servers`.port, `{DB_VERSION}_servers`.private_port,
`{DB_VERSION}_servers`.max_players, `{DB_VERSION}_servers`.difficulty, `{DB_VERSION}_servers`.game_mode,
`{DB_VERSION}_servers`.current_players, `{DB_VERSION}_servers`.password, `{DB_VERSION}_servers`.version,
`{DB_VERSION}_servers`.game_started, `{DB_VERSION}_servers`.latitude, `{DB_VERSION}_servers`.longitude,
Expand Down
1 change: 1 addition & 0 deletions install/install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ CREATE TABLE IF NOT EXISTS `v3_servers` (
`name` VARCHAR(256) NOT NULL,
`last_poll_time` INT NOT NULL,
`ip` INT UNSIGNED NOT NULL DEFAULT '0',
`ipv6` VARCHAR(45) NOT NULL DEFAULT '',
`port` SMALLINT UNSIGNED NOT NULL DEFAULT '0',
`private_port` SMALLINT UNSIGNED NOT NULL DEFAULT '0',
`max_players` TINYINT UNSIGNED NOT NULL DEFAULT '0',
Expand Down

0 comments on commit 8eb6d71

Please sign in to comment.