Community discussions

MikroTik App
 
electravis
Member Candidate
Member Candidate
Topic Author
Posts: 274
Joined: Tue Jan 26, 2010 12:06 am

Adding SSL to API

Tue Jun 19, 2018 12:35 am

So I am trying to add SSL to my API calls. I dont have a lot of experience in this part but I can tell from other posts its more then just change the port in the code I am using.

I am using the base API from
* RouterOS PHP API class v1.4
* Author: Denis Basta

I think its in the socket variable that i need to add something? I have tried both adding ssl and tls with no luck.

Sorry so vague but not sure where to start. Thanks

class Routeros_api
{
var $debug = false; // Show debug information
var $error_no; // Variable for storing connection error number, if any
var $error_str; // Variable for storing connection error text, if any
var $attempts = 5; // Connection attempt count
var $connected = false; // Connection state
var $delay = 3; // Delay between connection attempts in seconds
var $port = 8728; // Port to connect to
var $timeout = 3; // Connection attempt timeout and data read timeout
var $socket; // Variable for storing socket resource
var $maxReadLoopCount=4000; // max read loop count...this is a fail safe to prevent the read loop from going to infinity...can be modified if needed





function connect($ip, $login, $password)
{
for ($ATTEMPT = 1; $ATTEMPT <= $this->attempts; $ATTEMPT++) {
$this->connected = false;
$this->debug('Connection attempt #' . $ATTEMPT . ' to ' . $ip . ':' . $this->port . '...');
if ($this->socket = @fsockopen($ip, $this->port, $this->error_no, $this->error_str, $this->timeout)) {
socket_set_timeout($this->socket, $this->timeout);
$this->write('/login');
$RESPONSE = $this->read(false);
if ($RESPONSE[0] == '!done') {
if (preg_match_all('/[^=]+/i', $RESPONSE[1], $MATCHES)) {
if ($MATCHES[0][0] == 'ret' && strlen($MATCHES[0][1]) == 32) {
$this->write('/login', false);
$this->write('=name=' . $login, false);
$this->write('=response=00' . md5(chr(0) . $password . pack('H*', $MATCHES[0][1])));
$RESPONSE = $this->read(false);
if ($RESPONSE[0] == '!done') {
$this->connected = true;
break;
 
R1CH
Forum Guru
Forum Guru
Posts: 1101
Joined: Sun Oct 01, 2006 11:44 pm

Re: Adding SSL to API

Tue Jun 19, 2018 6:54 pm

You should use fsockopen ("tls://$ip"). Be aware that without a valid certificate this will fail.
 
electravis
Member Candidate
Member Candidate
Topic Author
Posts: 274
Joined: Tue Jan 26, 2010 12:06 am

Re: Adding SSL to API

Wed Jun 20, 2018 9:20 pm

Thanks for the info yesterday I found the latest API code which included all the ssl api options i needed. Got it working after that.

Who is online

Users browsing this forum: No registered users and 14 guests