Run Moodle APIs on a separate port than the moodle user interface.

Run Moodle APIs on a separate port than the moodle user interface.

av Roshan Gujrathi -
Antal svar: 10

Hello,

I would like guidance on how to configure Moodle so that the Moodle APIs are accessible through a specific port (e.g., 12345), while the Moodle Web User Interface remains on the default port 443. I would appreciate it if you could provide me with step-by-step instructions on how to achieve this. Thank you for your assistance.

Genomsnitt av betyg: -
Som svar till Roshan Gujrathi

Re: Run Moodle APIs on a separate port than the moodle user interface.

av Michael Hughes -
Bild på Core developers Bild på Particularly helpful Moodlers Bild på Plugin developers
Pretty sure that Moodle won't let you do this, because the URL for the *whole* service is coded into the $CFG->wwwroot; parameter in config.php and Moodle expects *everything* to go via that host.

Also...why???
Som svar till Michael Hughes

Re: Run Moodle APIs on a separate port than the moodle user interface.

av Roshan Gujrathi -
@Michael Thanks for your reply.

We need this to enhance security.

Can we do this by making $CFG->wwwroot dynamic in Moodle's config file? Or by setting the reverse proxy rule in IIS based on the web service URLs?
Som svar till Michael Hughes

Re: Run Moodle APIs on a separate port than the moodle user interface.

av Roshan Gujrathi -
@Michael We want to limit network access to the API with firewalls, while the web interface is available externally.
Som svar till Roshan Gujrathi

Re: Run Moodle APIs on a separate port than the moodle user interface.

av Michael Hughes -
Bild på Core developers Bild på Particularly helpful Moodlers Bild på Plugin developers
Could you not put an access control rule in for anything that is going to <host>/webservice/* instead.
Som svar till Michael Hughes

Re: Run Moodle APIs on a separate port than the moodle user interface.

av Roshan Gujrathi -
Thanks Michael. I'm trying to create an inbound rule in the IIS server but it is not redirecting to the correct URL. However, it is redirecting me to port 12345 when I add /webservice/* in the pattern.

Could you please help me with the pattern and redirect URL?

My URL is something like this- 

https://domainname.com/webservice/rest/server.php?wstoken=xxxxxxxxxxxxxxxxxxxxxxxxx&wsfunction=nameofthefunction&moodlewsrestformat=json&cname=companyname

Please help me with the Pattern, Action Type and Redirect URL.

Pattern: -
Action Type:- 
Redirect URL: - 

Som svar till Roshan Gujrathi

Re: Run Moodle APIs on a separate port than the moodle user interface.

av Mark Johnson -
Bild på Core developers Bild på Particularly helpful Moodlers Bild på Peer reviewers Bild på Plugin developers

When you generate a web service token, you can add an IP restriction so it can only be used from a certain IP range.

Som svar till Roshan Gujrathi

Re: Run Moodle APIs on a separate port than the moodle user interface.

av Andreas Grabs -
Bild på Core developers Bild på Peer reviewers Bild på Plugin developers Bild på Translators
Hi Roshan,

doe to the config.php is a PHP file you could make the $CFG-wwwroot some how dynamic.
For example you could check, whether or not the call is a webservice call and change this setting on demand.

Best regards
Andreas
Som svar till Andreas Grabs

Re: Run Moodle APIs on a separate port than the moodle user interface.

av Roshan Gujrathi -
Hi Anderas,

Based on the solution you provided to make changes in the config file, I have implemented a solution to redirect Moodle web services to port 12345 and the Moodle web UI to port 443 by making the $CFG->wwwroot dynamic.

In Moodle, web service URLs typically include the string "/webservice/rest/server.php". By using this information, I have added conditions to the code that check if the current PHP file being executed is the web service endpoint. If it matches, the code redirects to the desired port 12345. Otherwise, if it's not a web service URL, the code redirects to port 443.

Please review and verify the following implementation for redirecting API traffic. Kindly let me know if any corrections are needed or if this solution adequately satisfies the requirement.


$CFG->wwwroot   = 'https://'.(array_key_exists('SERVER_NAME', $_SERVER) ? $_SERVER["SERVER_NAME"] : 'domainname.com'); - initialize

// Set the value of $CFG->wwwroot based on the current request

if ($_SERVER['PHP_SELF'] == '/webservice/rest/server.php') {

    if ($_SERVER['SERVER_PORT'] != '12345') {

        $redirect = 'https://' . $_SERVER["SERVER_NAME"] . ':12345' . $_SERVER['REQUEST_URI'];

        $CFG->wwwroot = $redirect;

    }

} else {

    if ($_SERVER['SERVER_PORT'] != '443') {

        $redirect = 'https://' . $_SERVER["SERVER_NAME"] . ':443' . $_SERVER['REQUEST_URI'];

        $CFG->wwwroot = $redirect;

    }

}

Som svar till Roshan Gujrathi

Re: Run Moodle APIs on a separate port than the moodle user interface.

av Andreas Grabs -
Bild på Core developers Bild på Peer reviewers Bild på Plugin developers Bild på Translators
Hi,
I had more in mind something like this:

if (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 12345) {
     $port = ':12345'; } else {
     $port = ''; } $CFG->wwwroot = 'your-domain' . $port;
Best regards
Andreas

Som svar till Andreas Grabs

Re: Run Moodle APIs on a separate port than the moodle user interface.

av Roshan Gujrathi -
Thanks! The code provided by you won't work as there is no condition to check the string in the URL.

$CFG->wwwroot = 'https://'.(array_key_exists('SERVER_NAME', $_SERVER) ? $_SERVER["SERVER_NAME"] : 'domainname.com'); - initialize

// Set the value of $CFG->wwwroot based on the current request

if ($_SERVER['PHP_SELF'] == '/webservice/rest/server.php') {

if ($_SERVER['SERVER_PORT'] != '12345') {

$redirect = 'https://' . $_SERVER["SERVER_NAME"] . ':12345' . $_SERVER['REQUEST_URI'];

$CFG->wwwroot = $redirect;

}

} else {

if ($_SERVER['SERVER_PORT'] != '443') {

$redirect = 'https://' . $_SERVER["SERVER_NAME"] . ':443' . $_SERVER['REQUEST_URI'];

$CFG->wwwroot = $redirect;

}

}

The above-mentioned code appears to be functioning correctly, as I have verified that it redirects to port 443 when accessing the Moodle UI and redirects to port 11443 when accessing the Moodle web service URL. Thus, it seems to be a suitable solution for the requirements, or else do I need to create additional rules or implement IIS forwarding?