Another big epp update

This commit is contained in:
Pinga 2023-08-09 11:40:36 +03:00
parent 581eedb3a6
commit 8719aad491
2 changed files with 140 additions and 81 deletions

View file

@ -394,14 +394,18 @@ class EppWriter {
$writer->writeAttribute('xmlns:contact', 'urn:ietf:params:xml:ns:contact-1.0'); $writer->writeAttribute('xmlns:contact', 'urn:ietf:params:xml:ns:contact-1.0');
$writer->writeAttribute('xsi:schemaLocation', 'urn:ietf:params:xml:ns:contact-1.0 contact-1.0.xsd'); $writer->writeAttribute('xsi:schemaLocation', 'urn:ietf:params:xml:ns:contact-1.0 contact-1.0.xsd');
foreach ($resp['ids'] as $ids) { foreach ($resp['ids'] as $ids) {
$writer->startElement('contact:cd'); $writer->startElement('contact:cd');
$writer->writeElement('contact:id', $ids[0], ['avail' => $ids[1]]); $writer->startElement('contact:id');
if (isset($ids[2])) { $writer->writeAttribute('avail', $ids[1]);
$writer->writeElement('contact:reason', $ids[2]); $writer->text($ids[0]);
} $writer->endElement(); // End of 'contact:id'
$writer->endElement(); // End of 'contact:cd'
} if (isset($ids[2])) {
$writer->writeElement('contact:reason', $ids[2]);
}
$writer->endElement(); // End of 'contact:cd'
}
$writer->endElement(); // End of 'contact:chkData' $writer->endElement(); // End of 'contact:chkData'
$writer->endElement(); // End of 'resData' $writer->endElement(); // End of 'resData'

View file

@ -3,6 +3,7 @@
//require 'vendor/autoload.php'; //require 'vendor/autoload.php';
global $c; global $c;
$c = require 'config.php'; $c = require 'config.php';
require_once 'EppWriter.php';
use Swoole\Coroutine\Server; use Swoole\Coroutine\Server;
use Swoole\Coroutine\Server\Connection; use Swoole\Coroutine\Server\Connection;
@ -68,26 +69,22 @@ $server->handle(function (Connection $conn) use ($table, $db) {
{ {
$clID = (string) $xml->command->login->clID; $clID = (string) $xml->command->login->clID;
$pw = (string) $xml->command->login->pw; $pw = (string) $xml->command->login->pw;
$clTRID = (string) $xml->command->clTRID;
if (checkLogin($db, $clID, $pw)) { if (checkLogin($db, $clID, $pw)) {
$table->set($connId, ['clid' => $clID, 'logged_in' => 1]); $table->set($connId, ['clid' => $clID, 'logged_in' => 1]);
$eppLoginResponse = '<?xml version="1.0" encoding="UTF-8" standalone="no"?> $response = [
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0"> 'command' => 'login',
<response> 'resultCode' => 1000,
<result code="1000"> 'lang' => 'en-US',
<msg>Login successful</msg> 'message' => 'Login successful',
</result> 'clTRID' => $clTRID,
<trID> 'svTRID' => generateSvTRID(),
<clTRID>ABC-12345</clTRID> ];
<svTRID>SRV-54321</svTRID>
</trID>
</response>
</epp>';
$length = strlen($eppLoginResponse) + 4; // Total length including the 4-byte header $epp = new EPP\EppWriter();
$lengthData = pack('N', $length); // Pack the length into 4 bytes $xml = $epp->epp_writer($response);
sendEppResponse($conn, $xml);
$conn->send($lengthData . $eppLoginResponse);
} else { } else {
sendEppError($conn, 2200, 'Authentication error'); sendEppError($conn, 2200, 'Authentication error');
} }
@ -97,23 +94,19 @@ $server->handle(function (Connection $conn) use ($table, $db) {
case isset($xml->command->logout): case isset($xml->command->logout):
{ {
$table->del($connId); $table->del($connId);
$eppLogoutResponse = '<?xml version="1.0" encoding="UTF-8" standalone="no"?> $clTRID = (string) $xml->command->clTRID;
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
<response>
<result code="1500">
<msg>Logout successful</msg>
</result>
<trID>
<clTRID>ABC-12345</clTRID>
<svTRID>SRV-54321</svTRID>
</trID>
</response>
</epp>';
$length = strlen($eppLogoutResponse) + 4; // Total length including the 4-byte header $response = [
$lengthData = pack('N', $length); // Pack the length into 4 bytes 'command' => 'logout',
'resultCode' => 1500,
'lang' => 'en-US',
'clTRID' => $clTRID,
'svTRID' => generateSvTRID(),
];
$conn->send($lengthData . $eppLogoutResponse); $epp = new EPP\EppWriter();
$xml = $epp->epp_writer($response);
sendEppResponse($conn, $xml);
$conn->close(); $conn->close();
break; break;
} }
@ -190,8 +183,29 @@ Swoole\Coroutine::create(function () use ($server) {
$server->start(); $server->start();
}); });
function sendEppResponse($conn, $response) {
$length = strlen($response) + 4; // Total length including the 4-byte header
$lengthData = pack('N', $length); // Pack the length into 4 bytes
$conn->send($lengthData . $response);
}
function generateSvTRID($prefix = "Namingo") {
// Get current timestamp
$timestamp = time();
// Generate a random 5-character alphanumeric string
$randomString = bin2hex(random_bytes(5));
// Combine the prefix, timestamp, and random string to form the svTRID
$svTRID = "{$prefix}-{$timestamp}-{$randomString}";
return $svTRID;
}
function processContactCheck($conn, $db, $xml) { function processContactCheck($conn, $db, $xml) {
$contactIDs = $xml->command->check->children('urn:ietf:params:xml:ns:contact-1.0')->check->{'id'}; $contactIDs = $xml->command->check->children('urn:ietf:params:xml:ns:contact-1.0')->check->{'id'};
$clTRID = (string) $xml->command->clTRID;
$results = []; $results = [];
foreach ($contactIDs as $contactID) { foreach ($contactIDs as $contactID) {
@ -203,41 +217,40 @@ function processContactCheck($conn, $db, $xml) {
return; return;
} }
$stmt = $db->prepare("SELECT 1 FROM contacts WHERE id = :id"); $stmt = $db->prepare("SELECT 1 FROM contact WHERE id = :id");
$stmt->execute(['id' => $contactID]); $stmt->execute(['id' => $contactID]);
$results[$contactID] = $stmt->fetch() ? '0' : '1'; // 0 if exists, 1 if not $results[$contactID] = $stmt->fetch() ? '0' : '1'; // 0 if exists, 1 if not
} }
$checkResults = ''; $ids = [];
foreach ($results as $id => $available) { foreach ($results as $id => $available) {
$checkResults .= "<contact:cd><contact:id avail=\"$available\">$id</contact:id></contact:cd>"; $entry = [$id, $available];
// Check if the contact is unavailable
if (!$available) {
$entry[] = "Contact ID already registered";
}
$ids[] = $entry;
} }
$response = <<<XML $response = [
<?xml version="1.0" encoding="UTF-8"?> 'command' => 'check_contact',
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0"> 'resultCode' => 1000,
<response> 'lang' => 'en-US',
<result code="1000"> 'message' => 'Command completed successfully',
<msg>Contact check completed</msg> 'ids' => $ids,
</result> 'clTRID' => $clTRID,
<resData> 'svTRID' => generateSvTRID(),
<contact:chkData xmlns:contact="urn:ietf:params:xml:ns:contact-1.0"> ];
$checkResults
</contact:chkData>
</resData>
</response>
</epp>
XML;
$length = strlen($response) + 4; // Total length including the 4-byte header $epp = new EPP\EppWriter();
$lengthData = pack('N', $length); // Pack the length into 4 bytes $xml = $epp->epp_writer($response);
sendEppResponse($conn, $xml);
$conn->send($lengthData . $response);
} }
function processContactInfo($conn, $db, $xml) { function processContactInfo($conn, $db, $xml) {
$contactID = (string) $xml->command->{'info'}->{'contact:info'}->{'id'}; $contactID = (string) $xml->command->info->children('urn:ietf:params:xml:ns:contact-1.0')->info->{'id'};
$clTRID = (string) $xml->command->clTRID;
// Validation for contact ID // Validation for contact ID
if (!ctype_alnum($contactID) || strlen($contactID) > 255) { if (!ctype_alnum($contactID) || strlen($contactID) > 255) {
@ -246,7 +259,7 @@ function processContactInfo($conn, $db, $xml) {
} }
try { try {
$stmt = $db->prepare("SELECT * FROM contacts WHERE id = :id"); $stmt = $db->prepare("SELECT * FROM contact WHERE id = :id");
$stmt->execute(['id' => $contactID]); $stmt->execute(['id' => $contactID]);
$contact = $stmt->fetch(PDO::FETCH_ASSOC); $contact = $stmt->fetch(PDO::FETCH_ASSOC);
@ -256,22 +269,66 @@ function processContactInfo($conn, $db, $xml) {
return; return;
} }
$response = <<<XML // Fetch authInfo
<?xml version="1.0" encoding="UTF-8"?> $stmt = $db->prepare("SELECT * FROM contact_authInfo WHERE contact_id = :id");
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0"> $stmt->execute(['id' => $contactID]);
<response> $authInfo = $stmt->fetch(PDO::FETCH_ASSOC);
<result code="1000">
<msg>Contact information retrieved successfully</msg>
</result>
<!-- Add contact details here -->
</response>
</epp>
XML;
$length = strlen($response) + 4; // Total length including the 4-byte header // Fetch status
$lengthData = pack('N', $length); // Pack the length into 4 bytes $stmt = $db->prepare("SELECT * FROM contact_status WHERE contact_id = :id");
$stmt->execute(['id' => $contactID]);
$statuses = $stmt->fetchAll(PDO::FETCH_ASSOC);
$conn->send($lengthData . $response); $statusArray = [];
foreach($statuses as $status) {
$statusArray[] = [$status['status']];
}
// Fetch postal_info
$stmt = $db->prepare("SELECT * FROM contact_postalInfo WHERE contact_id = :id");
$stmt->execute(['id' => $contactID]);
$postals = $stmt->fetchAll(PDO::FETCH_ASSOC);
$postalArray = [];
foreach ($postals as $postal) {
$postalType = $postal['type']; // 'int' or 'loc'
$postalArray[$postalType] = [
'name' => $postal['name'],
'org' => $postal['org'],
'street' => [$postal['street1'], $postal['street2'], $postal['street3']],
'city' => $postal['city'],
'sp' => $postal['sp'],
'pc' => $postal['pc'],
'cc' => $postal['cc']
];
}
$response = [
'command' => 'info_contact',
'clTRID' => $clTRID,
'svTRID' => generateSvTRID(),
'resultCode' => 1000,
'msg' => 'Command completed successfully',
'id' => $contact['id'],
'roid' => $contact['identifier'],
'status' => $statusArray,
'postal' => $postalArray,
'voice' => $contact['voice'],
'fax' => $contact['fax'],
'email' => $contact['email'],
'clID' => $contact['clid'],
'crID' => $contact['crid'],
'crDate' => $contact['crdate'],
'upID' => $contact['upid'],
'upDate' => $contact['update'],
'authInfo' => 'valid',
'authInfo_type' => $authInfo['authtype'],
'authInfo_val' => $authInfo['authinfo']
];
$epp = new EPP\EppWriter();
$xml = $epp->epp_writer($response);
sendEppResponse($conn, $xml);
} catch (PDOException $e) { } catch (PDOException $e) {
sendEppError($conn, 2400, 'Database error'); sendEppError($conn, 2400, 'Database error');
@ -448,7 +505,5 @@ function sendEppError($conn, $code, $msg) {
</response> </response>
</epp> </epp>
XML; XML;
$length = strlen($errorResponse) + 4; // Total length including the 4-byte header sendEppResponse($conn, $errorResponse);
$lengthData = pack('N', $length); // Pack the length into 4 bytes
$conn->send($lengthData . $errorResponse);
} }