Use console command send API



  • I'm playing around with the Remote Requests feature on PufferPanel, trying to make a page with all my servers displayed at once.

    I have the log view and power controls working, but only because PHP code was provided for them.

    There's no documentation on /server/console, and I have no experience in PHP, so I don't know how to POST data through it. How can I do this?



  • I havent looked much at the remote part of the server but i may have an idea. Go to your puffer panel files and try to find the file with submits the commands to the console. Try to find what method it uses and once you do mimick the method but with your style. If i have time ill try doing it myself.



  • Well what I mostly needed was a POST example for it, like the docs for getting logs and such provide. I think I've found something.



  • OK, I managed to put something together using the provided GET files, some Google, some chatrooms and the generic /server POST example, but it doesn't fully work.

    Currently I have:

    <?php
    
    $ch = curl_init();
    $data = $_POST['data'];
    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    
    // Set url
    curl_setopt($ch, CURLOPT_URL, 'https://xpw1.no-ip.org:8443/server/console');
    
    // Set method
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    
    // Set options
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 'TRUE');
    
    // Set headers
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
      'Content-Type:application/json',
      "X-Access-Server: <OMITTED>",
      "X-Access-Token: <OMITTED>",
     ]
    );
    
    $response = curl_exec($ch);
    $err = curl_error($ch);
    
    $body = json_encode($data);
    
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
    curl_close($ch);
    
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
        echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
      
    }
    

    It doesn't return any errors, but the console log tells me "command not found," which must mean I'm sending the data incorrectly. I'm sending a form value through an AJAX request to PHP. I know it gets to the PHP variable, because sending it back to JS and printing it out returns the correct value. I can only think I'm not sending this correctly.

    I have no idea how to read the PHP files in PP, or even where to look. It'd be amazing if maybe @puffrfish or someone else could help out.



  • Here's my complete code, which sends a command key with the value of whatever I try to send through the form box. It sends the key and value in the same way Wireshark says other POST operations in PP do, but I don't know what the key needs to be or if there even needs to be a key.

    The HTML/JS:

                                <div id="inline2">
                                    <form id="bungeesend" class="cmdform" style="padding: 0; margin: 0; display: inline; text-align: center;">
                                        <fieldset>
                                            <input name="cmdb" id="cmdb" class="cmd" type="text" />
                                            <input id="send" type="submit" value="Send" />
                                        </fieldset>
                                    </form>
                                </div>
                                <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
                                <script>
                                    $('#bungeesend').submit(function () {
                                        event.preventDefault();
                                        bungeesend();
                                        return false;
                                    });
                                    function bungeesend() {
                                        var cmdb = document.querySelector("#cmdb").value;
                                        $.ajax({
                                            type: "POST",
                                            url: "php/bungeesend.php",
                                            data: {command:cmdb},
                                            dataType: "json",
                                            success: function(data){
                                                console.log(data);
                                            }
                                        });
                                    }
                               </script>
    

    The PHP (with the keys taken out):

    
    <?php
    
    $ch = curl_init();
    $data = $_POST['data'];
    $array = array('command' => 'glist');
    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    
    // Set url
    curl_setopt($ch, CURLOPT_URL, 'https://xpw1.no-ip.org:8443/server/console');
    
    // Set method
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    
    // Set options
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 'TRUE');
    
    // Set headers
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
      'Content-Type:application/json',
      "X-Access-Server: <OMITTED>",
      "X-Access-Token: <OMITTED>",
     ]
    );
    
    $response = curl_exec($ch);
    $err = curl_error($ch);
    
    $body = json_decode(stripslashes($data));
    
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
    
    curl_close($ch);
    
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
    //    echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
        echo $data;
      
    }
    

    I really need help with this. I have no idea where I might find the files that deal with this request, or what to look for if I find them.



  • I got it to work!

    I looked at the source for PP and found the code for the bulk command sender. I used the below snippet and modified it with a static URL and tokens to work:

    Unirest\Request::post(
    		"https://" . $node['fqdn'] . ":" . $node['daemon_listen'] . "/server/console",
    		array(
    			"X-Access-Token" => $node['daemon_secret'],
    			"X-Access-Server" => $server
    		),
    		array(
    			"command" => $command
    		)
    	);
    

    My complete (fixed) code, if anyone wants it:

    The HTML/JS:

    <div id="inline2">
                                    <form id="bungeesend" class="cmdform" style="padding: 0; margin: 0; display: inline; text-align: center;">
                                        <fieldset>
                                            <input name="cmdb" id="cmdb" class="cmd" type="text" />
                                            <input id="send" type="submit" value="Send" />
                                        </fieldset>
                                    </form>
                                </div>
                                <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
                                <script>
                                    $('#bungeesend').submit(function () {
                                        event.preventDefault();
                                        bungeesend();
                                        return false;
                                    });
                                    function bungeesend() {
                                        var cmdb = $("#cmdb").val();
                                        console.log("CMDB:" + cmdb);
                                        $.ajax({
                                            type: "POST",
                                            url: "php/bungeesend.php",
                                            data: "command=" + cmdb,
                                            success: function(data){
                                                console.log(data);
                                            }
                                        });
                                    }
                             </script>
    

    /php/bungeesend.php

    
    <?php
    
    $data = $_POST["command"];
    $array = array('command' => $data);
    $send = 'command=' . $data;
    require_once '/srv/homepage/unirest-php/src/Unirest.php';
    
    $body = json_decode(stripslashes($data));
    
    Unirest\Request::verifyPeer(false);
    
    Unirest\Request::post(
        "https://xpw1.no-ip.org:8443/server/console",
        array(
            "X-Access-Token" => "<NOPE>",
            "X-Access-Server" => "<UH-UH>"
        ),
        array(
            "command" => $data
        )
    );
    
    
    curl_close($ch);
    
    if ($err) {
    } else {
        echo $data;
    }
    

    Unirest needs to be installed to the webserver somewhere.



  • So 1.0.0 will most likely break this code, which I don't mind, as it is most likely going to be much easier to use the API. Since there's also most likely going to be fuller documentation, I won't have to go through this trouble again.

    Can't wait to upgrade, break my code, and learn some more PHP :)



  • @zacharee said:

    So 1.0.0 will most likely break this code, which I don't mind, as it is most likely going to be much easier to use the API. Since there's also most likely going to be fuller documentation, I won't have to go through this trouble again.

    Can't wait to upgrade, break my code, and learn some more PHP :)

    Assuming it goes well, you won't need to read PHP code ;)


Log in to reply
 

Looks like your connection to PufferPanel Community was lost, please wait while we try to reconnect.