ServiceController.php 5.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
<?php
/**
 *    Copyright (C) 2015 Deciso B.V.
 *
 *    All rights reserved.
 *
 *    Redistribution and use in source and binary forms, with or without
 *    modification, are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 *    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 *    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 *    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 *    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *    POSSIBILITY OF SUCH DAMAGE.
 *
 */
namespace OPNsense\Proxy\Api;

use \OPNsense\Base\ApiControllerBase;
use \OPNsense\Core\Backend;
Jos Schellevis's avatar
Jos Schellevis committed
33
use \OPNsense\Proxy\Proxy;
34 35 36 37 38 39 40 41

/**
 * Class ServiceController
 * @package OPNsense\Proxy
 */
class ServiceController extends ApiControllerBase
{
    /**
42
     * start proxy service (in background)
43 44 45 46
     * @return array
     */
    public function startAction()
    {
47 48 49 50 51 52 53
        if ($this->request->isPost()) {
            $backend = new Backend();
            $response = $backend->configdRun("proxy start", true);
            return array("response" => $response);
        } else {
            return array("response" => array());
        }
54 55 56 57 58 59 60 61
    }

    /**
     * stop proxy service
     * @return array
     */
    public function stopAction()
    {
62 63 64 65 66 67 68
        if ($this->request->isPost()) {
            $backend = new Backend();
            $response = $backend->configdRun("proxy stop");
            return array("response" => $response);
        } else {
            return array("response" => array());
        }
69 70 71 72 73 74 75 76
    }

    /**
     * restart proxy service
     * @return array
     */
    public function restartAction()
    {
77 78 79 80 81 82 83
        if ($this->request->isPost()) {
            $backend = new Backend();
            $response = $backend->configdRun("proxy restart");
            return array("response" => $response);
        } else {
            return array("response" => array());
        }
84 85 86 87 88 89 90 91 92 93
    }

    /**
     * retrieve status of squid proxy
     * @return array
     * @throws \Exception
     */
    public function statusAction()
    {
        $backend = new Backend();
94
        $mdlProxy = new Proxy();
95
        $response = $backend->configdRun("proxy status");
96 97

        if (strpos($response, "not running") > 0) {
98 99 100 101 102
            if ($mdlProxy->general->enabled->__toString() == 1) {
                $status = "stopped";
            } else {
                $status = "disabled";
            }
103
        } elseif (strpos($response, "is running") > 0) {
104
            $status = "running";
105 106
        } elseif ($mdlProxy->general->enabled->__toString() == 0) {
            $status = "disabled";
107 108 109 110 111 112 113
        } else {
            $status = "unkown";
        }


        return array("status" => $status);
    }
114 115 116 117 118 119

    /**
     * reconfigure squid, generate config and reload
     */
    public function reconfigureAction()
    {
120 121 122
        if ($this->request->isPost()) {
            // close session for long running action
            $this->sessionClose();
123

124 125
            $mdlProxy = new Proxy();
            $backend = new Backend();
126

127
            $runStatus = $this->statusAction();
128

129 130 131 132
            // stop squid when disabled
            if ($runStatus['status'] == "running" && $mdlProxy->general->enabled->__toString() == 0) {
                $this->stopAction();
            }
133

134 135
            // generate template
            $backend->configdRun("template reload OPNsense.Proxy");
136

137 138 139 140 141 142 143
            // (res)start daemon
            if ($mdlProxy->general->enabled->__toString() == 1) {
                if ($runStatus['status'] == "running") {
                    $backend->configdRun("proxy reconfigure");
                } else {
                    $this->startAction();
                }
144
            }
145

146 147 148 149
            return array("status" => "ok");
        } else {
            return array("status" => "failed");
        }
150
    }
151 152 153 154 155 156 157 158 159 160


    /**
     * fetch acls
     * @return array
     */
    public function fetchaclsAction()
    {
        if ($this->request->isPost()) {
            $backend = new Backend();
161 162 163 164 165 166
            // generate template
            $backend->configdRun("template reload OPNsense.Proxy");

            // fetch files
            $response = $backend->configdRun("proxy fetchacls");
            return array("response" => $response,"status" => "ok");
167 168 169 170
        } else {
            return array("response" => array());
        }
    }
171
}