123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527 |
- <?php
- namespace App\Http\Controllers;
- use Carbon\Carbon;
- use App\Models\Site;
- use App\Jobs\CronSSH;
- use App\Models\Server;
- use App\Jobs\PhpCliSSH;
- use phpseclib3\Net\SSH2;
- use App\Jobs\RootResetSSH;
- use Illuminate\Support\Str;
- use Illuminate\Http\Request;
- use App\Jobs\PanelDomainAddSSH;
- use App\Jobs\PanelDomainSslSSH;
- use App\Jobs\PanelDomainRemoveSSH;
- use Illuminate\Support\Facades\URL;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Support\Facades\Validator;
- class ServerController extends Controller
- {
- /**
- * List all servers
- *
- * @OA\Get(
- * path="/api/servers",
- * summary="List all servers",
- * tags={"Servers"},
- * description="List all servers managed by panel.",
- * @OA\Parameter(
- * name="Authorization",
- * description="Use Apikey prefix (e.g. Authorization: Apikey XYZ)",
- * required=true,
- * in="header",
- * @OA\Schema(type="string")
- * ),
- * @OA\Response(
- * response=200,
- * description="Successful request",
- * @OA\JsonContent(
- * type="array",
- * @OA\Items(
- * @OA\Property(
- * property="server_id",
- * description="Server unique ID",
- * type="string",
- * example="abc-123-def-456"
- * ),
- * @OA\Property(
- * property="name",
- * description="Server name",
- * type="string",
- * example="Staging Server",
- * ),
- * @OA\Property(
- * property="ip",
- * description="Server IP",
- * type="string",
- * example="123.123.123.123",
- * ),
- * @OA\Property(
- * property="provider",
- * description="Server provider",
- * type="string",
- * example="AWS",
- * ),
- * @OA\Property(
- * property="location",
- * description="Server location",
- * type="string",
- * example="Frankfurt"
- * ),
- * @OA\Property(
- * property="php",
- * description="Server PHP CLI version",
- * type="string",
- * example="7.4"
- * ),
- * @OA\Property(
- * property="default",
- * description="Server default status (panel server)",
- * type="boolean",
- * example="false"
- * ),
- * @OA\Property(
- * property="status",
- * description="Server installation status (0 not installed, 1 installed)",
- * type="integer",
- * example="1"
- * ),
- * @OA\Property(
- * property="sites",
- * description="The number of sites on this server",
- * type="integer",
- * example="12"
- * ),
- * )
- * )
- * ),
- * @OA\Response(
- * response=401,
- * description="Unauthorized access error"
- * )
- * )
- */
- public function index()
- {
- $servers = Server::all();
- $response = [];
- foreach ($servers as $server) {
- $data = [
- 'server_id' => $server->server_id,
- 'name' => $server->name,
- 'ip' => $server->ip,
- 'provider' => $server->provider,
- 'location' => $server->location,
- 'default' => $server->default,
- 'status' => $server->status,
- 'sites' => count($server->sites)
- ];
- array_push($response, $data);
- }
- return response()->json($response);
- }
- /**
- * Add a new server
- *
- * @OA\Post(
- * path="/api/servers",
- * summary="Add a new Server",
- * tags={"Servers"},
- * description="Add a new server to manage with panel.",
- * @OA\Parameter(
- * name="Authorization",
- * description="Use Apikey prefix (e.g. Authorization: Apikey XYZ)",
- * required=true,
- * in="header",
- * @OA\Schema(type="string")
- * ),
- * @OA\RequestBody(
- * required = true,
- * description = "Server creation payload",
- * @OA\JsonContent(
- * type="object",
- * @OA\Property(
- * property="ip",
- * description="Server IP",
- * type="string",
- * example="123.123.123.123",
- * ),
- * @OA\Property(
- * property="name",
- * description="Server name",
- * type="string",
- * example="Production Server",
- * minLength=3
- * ),
- * @OA\Property(
- * property="provider",
- * description="Server provider",
- * type="string",
- * example="Digital Ocean",
- * ),
- * @OA\Property(
- * property="location",
- * description="Server location",
- * type="string",
- * example="Amsterdam",
- * ),
- * required={"ip","name"}
- * )
- * ),
- * @OA\Response(
- * response=200,
- * description="Successful server creation",
- * @OA\JsonContent(
- * @OA\Property(
- * property="server_id",
- * description="Server unique ID",
- * type="string",
- * example="abc-123-def-456"
- * ),
- * @OA\Property(
- * property="name",
- * description="Server name",
- * type="string",
- * example="Staging Server",
- * ),
- * @OA\Property(
- * property="ip",
- * description="Server IP",
- * type="string",
- * example="123.123.123.123",
- * ),
- * @OA\Property(
- * property="provider",
- * description="Server provider",
- * type="string",
- * example="AWS",
- * ),
- * @OA\Property(
- * property="location",
- * description="Server location",
- * type="string",
- * example="Frankfurt"
- * ),
- * @OA\Property(
- * property="setup",
- * description="Server setup script",
- * type="string",
- * example="https://panel.domain.ltd/sh/setup/123456"
- * ),
- * )
- * ),
- * @OA\Response(
- * response=409,
- * description="Server conflict"
- * ),
- * @OA\Response(
- * response=400,
- * description="Bad Request"
- * ),
- * @OA\Response(
- * response=401,
- * description="Unauthorized access error"
- * )
- * )
- */
- public function create(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'ip' => 'required|ip',
- 'name' => 'required|min:3',
- ]);
- if ($validator->fails()) {
- return response()->json([
- 'message' => __('cipi.bad_request'),
- 'errors' => $validator->errors()->getMessages()
- ], 400);
- }
- if ($request->ip == $request->server('SERVER_ADDR')) {
- return response()->json([
- 'message' => __('cipi.server_conflict_ip_current_message'),
- 'errors' => __('cipi.server_conflict')
- ], 409);
- }
- if (Server::where('ip', $request->ip)->first()) {
- return response()->json([
- 'message' => __('cipi.server_conflict_ip_duplicate_message'),
- 'errors' => __('cipi.server_conflict')
- ], 409);
- }
- $server = new Server();
- $server->ip = $request->ip;
- $server->name = $request->name;
- $server->provider = $request->provider;
- $server->location = $request->location;
- $server->password = Str::random(24);
- $server->database = Str::random(24);
- $server->server_id = Str::uuid();
- $server->cron = ' ';
- $server->save();
- return response()->json([
- 'server_id' => $server->server_id,
- 'name' => $request->name,
- 'provider' => $request->provider,
- 'location' => $request->location,
- 'ip' => $request->ip,
- 'setup' => URL::to('/sh/setup/'.$server->server_id)
- ]);
- }
- /**
- * Delete a server
- *
- * @OA\Delete(
- * path="/api/servers/{server_id}",
- * summary="Delete a Server",
- * tags={"Servers"},
- * description="Delete a server from panel.",
- * @OA\Parameter(
- * name="Authorization",
- * description="Use Apikey prefix (e.g. Authorization: Apikey XYZ)",
- * required=true,
- * in="header",
- * @OA\Schema(type="string")
- * ),
- * @OA\Parameter(
- * name="server_id",
- * description="The id of the server to delete.",
- * required=true,
- * in="path",
- * @OA\Schema(type="string")
- * ),
- * @OA\Response(
- * response=200,
- * description="Successful server deleted",
- * ),
- * @OA\Response(
- * response=404,
- * description="Server not found"
- * ),
- * @OA\Response(
- * response=400,
- * description="Bad Request"
- * ),
- * @OA\Response(
- * response=401,
- * description="Unauthorized access error"
- * )
- * )
- */
- public function destroy(string $server_id)
- {
- $server = Server::where('server_id', $server_id)->first();
- if (!$server) {
- return response()->json([
- 'message' => __('cipi.server_not_found_message_default'),
- 'errors' => __('cipi.server_not_found')
- ], 404);
- }
- if ($server->default) {
- return response()->json([
- 'message' => __('cipi.delete_default_server_message'),
- 'errors' => __('cipi.bad_request')
- ], 400);
- }
- $server->delete();
- return response()->json([]);
- }
- /**
- * Server information
- *
- * @OA\Get(
- * path="/api/servers/{server_id}",
- * summary="Server information",
- * tags={"Servers"},
- * description="Get server information.",
- * @OA\Parameter(
- * name="Authorization",
- * description="Use Apikey prefix (e.g. Authorization: Apikey XYZ)",
- * required=true,
- * in="header",
- * @OA\Schema(type="string")
- * ),
- * @OA\Parameter(
- * name="server_id",
- * description="The id of the server.",
- * required=true,
- * in="path",
- * @OA\Schema(type="string")
- * ),
- * @OA\Response(
- * response=200,
- * description="Successful server information",
- * @OA\JsonContent(
- * @OA\Property(
- * property="server_id",
- * description="Server unique ID",
- * type="string",
- * example="abc-123-def-456"
- * ),
- * @OA\Property(
- * property="name",
- * description="Server name",
- * type="string",
- * example="Staging Server",
- * ),
- * @OA\Property(
- * property="ip",
- * description="Server IP",
- * type="string",
- * example="123.123.123.123",
- * ),
- * @OA\Property(
- * property="provider",
- * description="Server provider",
- * type="string",
- * example="AWS",
- * ),
- * @OA\Property(
- * property="default",
- * description="Server default status (panel server)",
- * type="boolean",
- * example="false"
- * ),
- * @OA\Property(
- * property="php",
- * description="Server PHP CLI version",
- * type="string",
- * example="7.4"
- * ),
- * @OA\Property(
- * property="github_key",
- * description="Server Github deploy key",
- * type="string"
- * ),
- * @OA\Property(
- * property="build",
- * description="Server build version",
- * type="integer",
- * example="20210317001"
- * ),
- * @OA\Property(
- * property="cron",
- * description="Server cron",
- * type="text",
- * ),
- * @OA\Property(
- * property="sites",
- * description="The number of sites on this server",
- * type="integer",
- * example="12"
- * ),
- * )
- * ),
- * @OA\Response(
- * response=404,
- * description="Server not found or not installed"
- * ),
- * @OA\Response(
- * response=400,
- * description="Bad Request"
- * ),
- * @OA\Response(
- * response=401,
- * description="Unauthorized access error"
- * )
- * )
- */
- public function show(string $server_id)
- {
- $server = Server::where('server_id', $server_id)->where('status', 1)->first();
- if (!$server) {
- return response()->json([
- 'message' => __('cipi.server_not_found_message'),
- 'errors' => __('cipi.server_not_found')
- ], 404);
- }
- return response()->json([
- 'sever_id' => $server->server_id,
- 'name' => $server->name,
- 'ip' => $server->ip,
- 'location' => $server->location,
- 'provider' => $server->provider,
- 'default' => $server->default,
- 'php' => $server->php,
- 'github_key'=> $server->github_key,
- 'build' => $server->build,
- 'cron' => $server->cron,
- 'sites' => count($server->sites)
- ]);
- }
- /**
- * Panel server information
- *
- * @OA\Get(
- * path="/api/servers/panel",
- * summary="Panel server information",
- * tags={"Servers"},
- * description="Get panel server information.",
- * @OA\Parameter(
- * name="Authorization",
- * description="Use Apikey prefix (e.g. Authorization: Apikey XYZ)",
- * required=true,
- * in="header",
- * @OA\Schema(type="string")
- * ),
- * @OA\Response(
- * response=200,
- * description="Successful server information",
- * @OA\JsonContent(
- * @OA\Property(
- * property="server_id",
- * description="Server unique ID",
- * type="string",
- * example="abc-123-def-456"
- * ),
- * @OA\Property(
- * property="name",
- * description="Server name",
- * type="string",
- * example="Staging Server",
- * ),
- * @OA\Property(
- * property="ip",
- * description="Server IP",
- * type="string",
- * example="123.123.123.123",
- * ),
- * @OA\Property(
- * property="provider",
- * description="Server provider",
- * type="string",
- * example="AWS",
- * ),
- * @OA\Property(
- * property="domain",
- * description="Server default domain for panel",
- * type="string",
- * example="panel.domain.ltd"
- * ),
- * @OA\Property(
- * property="php",
- * description="Server PHP CLI version",
- * type="string",
- * example="7.4"
- * ),
- * @OA\Property(
- * property="github_key",
- * description="Server Github deploy key",
- * type="string"
- * ),
- * @OA\Property(
- * property="build",
- * description="Server build version",
- * type="integer",
- * example="20210317001"
- * ),
- * @OA\Property(
- * property="cron",
- * description="Server cron",
- * type="text",
- * ),
- * @OA\Property(
- * property="sites",
- * description="The number of sites on this server",
- * type="integer",
- * example="12"
- * ),
- * )
- * ),
- * @OA\Response(
- * response=401,
- * description="Unauthorized access error"
- * ),
- * @OA\Response(
- * response=404,
- * description="Server not found"
- * ),
- * )
- */
- public function panel()
- {
- $server = Server::where('default', 1)->first();
- if (!$server) {
- return response()->json([
- 'message' => __('cipi.server_not_found_native_message'),
- 'errors' => __('cipi.server_not_found')
- ], 404);
- }
- $site = Site::where('server_id', $server->id)->where('panel', 1)->first();
- if (!$site) {
- $domain = '';
- } else {
- $domain = $site->domain;
- }
- return response()->json([
- 'sever_id' => $server->server_id,
- 'name' => $server->name,
- 'ip' => $server->ip,
- 'location' => $server->location,
- 'provider' => $server->provider,
- 'domain' => $domain,
- 'php' => $server->php,
- 'github_key'=> $server->github_key,
- 'build' => $server->build,
- 'cron' => $server->cron,
- 'sites' => count($server->sites)
- ]);
- }
- /**
- * Add a domain / subdomain to panel
- *
- * @OA\Patch(
- * path="/api/servers/panel/domain",
- * summary="Add a domain / subdomain to panel",
- * tags={"Servers"},
- * description="Add a domain / subdomain to panel.",
- * @OA\Parameter(
- * name="Authorization",
- * description="Use Apikey prefix (e.g. Authorization: Apikey XYZ)",
- * required=true,
- * in="header",
- * @OA\Schema(type="string")
- * ),
- * @OA\RequestBody(
- * required = true,
- * description = "Panel domain payload",
- * @OA\JsonContent(
- * type="object",
- * @OA\Property(
- * property="domain",
- * description="Panel domain",
- * type="string",
- * example="panel.domain.ltd",
- * ),
- * )
- * ),
- * @OA\Response(
- * response=200,
- * description="Successful panel domain update",
- * ),
- * @OA\Response(
- * response=401,
- * description="Unauthorized access error"
- * ),
- * @OA\Response(
- * response=400,
- * description="Bad Request"
- * ),
- * @OA\Response(
- * response=404,
- * description="Server not found"
- * ),
- * )
- */
- public function paneldomain(Request $request)
- {
- $server = Server::where('default', 1)->first();
- if (!$server) {
- return response()->json([
- 'message' => __('cipi.server_not_found_native_message'),
- 'errors' => __('cipi.server_not_found')
- ], 404);
- }
- $site = Site::where('server_id', $server->id)->where('panel', true)->first();
- if ($site) {
- $site->delete();
- PanelDomainRemoveSSH::dispatch($server)->delay(Carbon::now()->addSeconds(3));
- }
- if ($request->domain && $request->domain != '') {
- $validator = Validator::make($request->all(), [
- 'domain' => 'required'
- ]);
- if ($validator->fails()) {
- return response()->json([
- 'message' => __('cipi.bad_request'),
- 'errors' => $validator->errors()->getMessages()
- ], 400);
- }
- $newsite = new Site;
- $newsite->server_id = $server->id;
- $newsite->domain = $request->domain;
- $newsite->site_id = sha1(microtime());
- $newsite->username = md5(microtime());
- $newsite->password = 'Secret_123';
- $newsite->database = 'Secret_123';
- $newsite->panel = true;
- $newsite->save();
- PanelDomainAddSSH::dispatch($server)->delay(Carbon::now()->addSeconds(3));
- }
- return response()->json([]);
- }
- /**
- * Require SSL for panel
- *
- * @OA\Post(
- * path="/api/servers/panel/ssl",
- * summary="Require SSL for panel",
- * tags={"Servers"},
- * description="Require SSL for panel domain / subdomain.",
- * @OA\Parameter(
- * name="Authorization",
- * description="Use Apikey prefix (e.g. Authorization: Apikey XYZ)",
- * required=true,
- * in="header",
- * @OA\Schema(type="string")
- * ),
- * @OA\Response(
- * response=200,
- * description="Successful SSL generation"
- * ),
- * @OA\Response(
- * response=401,
- * description="Unauthorized access error"
- * ),
- * @OA\Response(
- * response=400,
- * description="Bad request"
- * ),
- * @OA\Response(
- * response=404,
- * description="Server not found"
- * ),
- * )
- */
- public function panelssl()
- {
- $server = Server::where('default', 1)->first();
- if (!$server) {
- return response()->json([
- 'message' => __('cipi.server_not_found_native_message'),
- 'errors' => __('cipi.server_not_found')
- ], 404);
- }
- $site = Site::where('server_id', $server->id)->where('panel', true)->first();
- if ($site) {
- PanelDomainSslSSH::dispatch($server, $site)->delay(Carbon::now()->addSeconds(3));
- } else {
- return response()->json([
- 'message' => __('cipi.ssl_request_error_message'),
- 'errors' => __('cipi.bad_request')
- ], 400);
- }
- return response()->json([]);
- }
- /**
- * Server edit
- *
- * @OA\Patch(
- * path="/api/servers/{server_id}",
- * summary="Server edit",
- * tags={"Servers"},
- * description="Edit server information.",
- * @OA\Parameter(
- * name="Authorization",
- * description="Use Apikey prefix (e.g. Authorization: Apikey XYZ)",
- * required=true,
- * in="header",
- * @OA\Schema(type="string")
- * ),
- * @OA\Parameter(
- * name="server_id",
- * description="The id of the server to edit.",
- * required=true,
- * in="path",
- * @OA\Schema(type="string")
- * ),
- * @OA\RequestBody(
- * required = true,
- * description = "Server creation payload",
- * @OA\JsonContent(
- * type="object",
- * @OA\Property(
- * property="ip",
- * description="Server IP",
- * type="string",
- * example="123.123.123.123",
- * ),
- * @OA\Property(
- * property="name",
- * description="Server name",
- * type="string",
- * example="Production Server",
- * minLength=3
- * ),
- * @OA\Property(
- * property="provider",
- * description="Server provider",
- * type="string",
- * example="Digital Ocean",
- * ),
- * @OA\Property(
- * property="location",
- * description="Server location",
- * type="string",
- * example="Amsterdam",
- * ),
- * @OA\Property(
- * property="php",
- * description="Server PHP CLI version",
- * type="string",
- * example="7.4",
- * ),
- * @OA\Property(
- * property="cron",
- * description="Server crontab",
- * type="text",
- * ),
- * )
- * ),
- * @OA\Response(
- * response=200,
- * description="Successful server editing",
- * @OA\JsonContent(
- * @OA\Property(
- * property="server_id",
- * description="Server unique ID",
- * type="string",
- * example="abc-123-def-456"
- * ),
- * @OA\Property(
- * property="name",
- * description="Server name",
- * type="string",
- * example="Staging Server",
- * ),
- * @OA\Property(
- * property="ip",
- * description="Server IP",
- * type="string",
- * example="123.123.123.123",
- * ),
- * @OA\Property(
- * property="provider",
- * description="Server provider",
- * type="string",
- * example="AWS",
- * ),
- * @OA\Property(
- * property="default",
- * description="Server default status (panel server)",
- * type="boolean",
- * example="false"
- * ),
- * @OA\Property(
- * property="status",
- * description="Server status",
- * type="integer",
- * example="1"
- * ),
- * @OA\Property(
- * property="php",
- * description="Server PHP CLI version",
- * type="string",
- * example="7.4"
- * ),
- * @OA\Property(
- * property="github_key",
- * description="Server Github deploy key",
- * type="string"
- * ),
- * @OA\Property(
- * property="build",
- * description="Server build version",
- * type="integer",
- * example="20210317001"
- * ),
- * @OA\Property(
- * property="cron",
- * description="Server cron",
- * type="text",
- * ),
- * )
- * ),
- * @OA\Response(
- * response=404,
- * description="Server not found or not installed"
- * ),
- * @OA\Response(
- * response=400,
- * description="Bad Request"
- * ),
- * @OA\Response(
- * response=401,
- * description="Unauthorized access error"
- * ),
- * @OA\Response(
- * response=409,
- * description="Server conflict"
- * ),
- * )
- */
- public function edit(Request $request, string $server_id)
- {
- $server = Server::where('server_id', $server_id)->where('status', 1)->first();
- if (!$server) {
- return response()->json([
- 'message' => __('cipi.server_not_found_message'),
- 'errors' => __('cipi.server_not_found')
- ], 404);
- }
- if ($request->ip) {
- $validator = Validator::make($request->all(), [
- 'ip' => 'required|ip'
- ]);
- if ($validator->fails()) {
- return response()->json([
- 'message' => __('cipi.bad_request'),
- 'errors' => $validator->errors()->getMessages()
- ], 400);
- }
- if (!$server->default && $request->ip == str_replace("\n", '', file_get_contents('https://checkip.amazonaws.com'))) {
- return response()->json([
- 'message' => __('cipi.edit_server_current_ip_error_message'),
- 'errors' => __('cipi.server_conflict')
- ], 409);
- }
- if (Server::where('ip', $request->ip)->where('server_id', '<>', $server_id)->first()) {
- return response()->json([
- 'message' => __('cipi.server_conflict_ip_duplicate_message'),
- 'errors' => __('cipi.server_conflict')
- ], 409);
- }
- if ($server->default) {
- $server->ip = str_replace("\n", '', file_get_contents('https://checkip.amazonaws.com'));
- } else {
- $server->ip = $request->ip;
- }
- }
- if ($request->name) {
- $validator = Validator::make($request->all(), [
- 'name' => 'required|min:3'
- ]);
- if ($validator->fails()) {
- return response()->json([
- 'message' => __('cipi.bad_request'),
- 'errors' => $validator->errors()->getMessages()
- ], 400);
- }
- $server->name = $request->name;
- }
- if ($request->provider) {
- $server->provider = $request->provider;
- }
- if ($request->location) {
- $server->location = $request->location;
- }
- if ($request->cron) {
- $server->cron = $request->cron;
- $server->save();
- CronSSH::dispatch($server)->delay(Carbon::now()->addSeconds(3));
- }
- if ($request->php) {
- if (!in_array($request->php, config('cipi.phpvers'))) {
- return response()->json([
- 'message' => __('cipi.bad_request'),
- 'errors' => 'Invalid PHP version.'
- ], 400);
- }
- PhpCliSSH::dispatch($server, $request->php)->delay(Carbon::now()->addSeconds(3));
- $server->php = $request->php;
- }
- $server->save();
- return response()->json([
- 'sever_id' => $server->server_id,
- 'name' => $server->name,
- 'ip' => $server->ip,
- 'location' => $server->location,
- 'provider' => $server->provider,
- 'default' => $server->default,
- 'status' => $server->status,
- 'php' => $server->php,
- 'github_key'=> $server->github_key,
- 'build' => $server->build,
- 'cron' => $server->cron
- ]);
- }
- /**
- * Server ping
- *
- * @OA\Get(
- * path="/api/servers/{server_id}/ping",
- * summary="Server ping",
- * tags={"Servers"},
- * description="Check real time server ping.",
- * @OA\Parameter(
- * name="Authorization",
- * description="Use Apikey prefix (e.g. Authorization: Apikey XYZ)",
- * required=true,
- * in="header",
- * @OA\Schema(type="string")
- * ),
- * @OA\Parameter(
- * name="server_id",
- * description="The id of the server to check.",
- * required=true,
- * in="path",
- * @OA\Schema(type="string")
- * ),
- * @OA\Response(
- * response=200,
- * description="Successful server ping check",
- * ),
- * @OA\Response(
- * response=404,
- * description="Server not found or not installed"
- * ),
- * @OA\Response(
- * response=401,
- * description="Unauthorized access error"
- * ),
- * @OA\Response(
- * response=503,
- * description="Server unavailable"
- * ),
- * )
- */
- public function ping(string $server_id)
- {
- $server = Server::where('server_id', $server_id)->where('status', 1)->first();
- if (!$server) {
- return response()->json([
- 'message' => __('cipi.server_not_found_message'),
- 'errors' => __('cipi.server_not_found')
- ], 404);
- }
- try {
- $remote = Http::get('http://'.$server->ip.'/ping_'.$server->server_id.'.php');
- if ($remote->status() == 200) {
- //
- } else {
- return response()->json([
- 'message' => __('cipi.server_unavailable_message'),
- 'errors' => __('cipi.server_unavailable')
- ], 503);
- }
- } catch (\Throwable $th) {
- return response()->json([
- 'message' => __('cipi.server_unavailable_message'),
- 'errors' => __('cipi.server_unavailable')
- ], 503);
- }
- }
- /**
- * Server healthy
- *
- * @OA\Get(
- * path="/api/servers/{server_id}/healthy",
- * summary="Server healthy",
- * tags={"Servers"},
- * description="Check real time server healthy.",
- * @OA\Parameter(
- * name="Authorization",
- * description="Use Apikey prefix (e.g. Authorization: Apikey XYZ)",
- * required=true,
- * in="header",
- * @OA\Schema(type="string")
- * ),
- * @OA\Parameter(
- * name="server_id",
- * description="The id of the server to check.",
- * required=true,
- * in="path",
- * @OA\Schema(type="string")
- * ),
- * @OA\Response(
- * response=200,
- * description="Successful server healthy check",
- * @OA\JsonContent(
- * @OA\Property(
- * property="cpu",
- * description="Current usage of CPU in %",
- * type="float",
- * example="72.50"
- * ),
- * @OA\Property(
- * property="ram",
- * description="Current usage of RAM in %",
- * type="float",
- * example="56.34",
- * ),
- * @OA\Property(
- * property="hdd",
- * description="Current usage of HDD in %",
- * type="float",
- * example="32",
- * ),
- * )
- * ),
- * @OA\Response(
- * response=404,
- * description="Server not found or not installed"
- * ),
- * @OA\Response(
- * response=400,
- * description="Bad Request"
- * ),
- * @OA\Response(
- * response=401,
- * description="Unauthorized access error"
- * ),
- * @OA\Response(
- * response=500,
- * description="SSH server connection issue"
- * ),
- * )
- */
- public function healthy(string $server_id)
- {
- $server = Server::where('server_id', $server_id)->where('status', 1)->first();
- if (!$server) {
- return response()->json([
- 'message' => __('cipi.server_not_found_message'),
- 'errors' => __('cipi.server_not_found')
- ], 404);
- }
- try {
- $remote = Http::get('http://'.$server->ip.'/ping_'.$server->server_id.'.php');
- if ($remote->status() != 200) {
- return response()->json([
- 'cpu' => '0',
- 'ram' => '0',
- 'hdd' => '0'
- ]);
- }
- } catch (\Throwable $th) {
- return response()->json([
- 'cpu' => '0',
- 'ram' => '0',
- 'hdd' => '0'
- ]);
- }
- try {
- $ssh = new SSH2($server->ip, 22);
- if (!$ssh->login('cipi', $server->password)) {
- return response()->json([
- 'message' => __('cipi.server_error_ssh_error_message').$server->server_id,
- 'errors' => __('cipi.server_error')
- ], 500);
- }
- $ssh->setTimeout(360);
- $status = $ssh->exec('echo "`LC_ALL=C top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk \'{print 100 - $1}\'`%;`free -m | awk \'/Mem:/ { printf("%3.1f%%", $3/$2*100) }\'`;`df -h / | awk \'/\// {print $(NF-1)}\'`"');
- $ssh->exec('exit');
- } catch (\Throwable $th) {
- return response()->json([
- 'message' => __('cipi.something_error_message'),
- 'errors' => __('cipi.error')
- ], 500);
- }
- $status = str_replace('%', '', $status);
- $status = str_replace("\n", '', $status);
- $api = explode(';', $status);
- return response()->json([
- 'cpu' => $api[0],
- 'ram' => $api[1],
- 'hdd' => $api[2]
- ]);
- }
- /**
- * Server root password reset
- *
- * @OA\Post(
- * path="/api/servers/{server_id}/rootreset",
- * summary="Server root password reset",
- * tags={"Servers"},
- * description="Reset server root password (for cipi user).",
- * @OA\Parameter(
- * name="Authorization",
- * description="Use Apikey prefix (e.g. Authorization: Apikey XYZ)",
- * required=true,
- * in="header",
- * @OA\Schema(type="string")
- * ),
- * @OA\Parameter(
- * name="server_id",
- * description="The id of the server.",
- * required=true,
- * in="path",
- * @OA\Schema(type="string")
- * ),
- * @OA\Response(
- * response=200,
- * description="Successful password reset",
- * @OA\JsonContent(
- * @OA\Property(
- * property="password",
- * description="New assigned password for cipi root user",
- * type="string",
- * example="Secret_123"
- * ),
- * )
- * ),
- * @OA\Response(
- * response=404,
- * description="Server not found or not installed"
- * ),
- * @OA\Response(
- * response=401,
- * description="Unauthorized access error"
- * ),
- * )
- */
- public function rootreset(string $server_id)
- {
- $server = Server::where('server_id', $server_id)->where('status', 1)->first();
- if (!$server) {
- return response()->json([
- 'message' => __('cipi.server_not_found_message'),
- 'errors' => __('cipi.server_not_found')
- ], 404);
- }
- $last_password = $server->password;
- $new_password = Str::random(24);
- $server->password = $new_password;
- $server->save();
- RootResetSSH::dispatch($server, $new_password, $last_password)->delay(Carbon::now()->addSeconds(1));
- return response()->json([
- 'password' => $server->password
- ]);
- }
- /**
- * Server service restart
- *
- * @OA\Post(
- * path="/api/servers/{server_id}/servicerestart/{service}",
- * summary="Server service restart",
- * tags={"Servers"},
- * description="Restart a server server (nginx, php, mysql, redis or supervisor).",
- * @OA\Parameter(
- * name="Authorization",
- * description="Use Apikey prefix (e.g. Authorization: Apikey XYZ)",
- * required=true,
- * in="header",
- * @OA\Schema(type="string")
- * ),
- * @OA\Parameter(
- * name="server_id",
- * description="The id of the server.",
- * required=true,
- * in="path",
- * @OA\Schema(type="string")
- * ),
- * @OA\Parameter(
- * name="service",
- * description="The service to restart.",
- * required=true,
- * in="path",
- * @OA\Schema(type="string")
- * ),
- * @OA\Response(
- * response=200,
- * description="Successful service restart"
- * ),
- * @OA\Response(
- * response=404,
- * description="Server not found or not installed"
- * ),
- * @OA\Response(
- * response=400,
- * description="Bad request"
- * ),
- * @OA\Response(
- * response=401,
- * description="Unauthorized access error"
- * ),
- * @OA\Response(
- * response=500,
- * description="SSH server connection issue"
- * ),
- * )
- */
- public function servicerestart(string $server_id, string $service)
- {
- if (!in_array($service, config('cipi.services'))) {
- return response()->json([
- 'message' => __('cipi.invalid_service_error_message'),
- 'errors' => __('cipi.bad_request')
- ], 400);
- }
- $server = Server::where('server_id', $server_id)->where('status', 1)->first();
- if (!$server) {
- return response()->json([
- 'message' => __('cipi.server_not_found_message'),
- 'errors' => __('cipi.server_not_found')
- ], 404);
- }
- try {
- $ssh = new SSH2($server->ip, 22);
- if (!$ssh->login('cipi', $server->password)) {
- return response()->json([
- 'message' => __('cipi.server_error_ssh_error_message').$server->server_id,
- 'errors' => __('cipi.server_error')
- ], 500);
- }
- $ssh->setTimeout(360);
- switch ($service) {
- case 'nginx':
- $ssh->exec('sudo systemctl restart nginx.service');
- break;
- case 'php':
- $ssh->exec('sudo service php8.0-fpm restart');
- $ssh->exec('sudo service php7.4-fpm restart');
- $ssh->exec('sudo service php7.3-fpm restart');
- break;
- case 'mysql':
- $ssh->exec('sudo service mysql restart');
- break;
- case 'redis':
- $ssh->exec('sudo systemctl restart redis.service');
- break;
- case 'supervisor':
- $ssh->exec('service supervisor restart');
- break;
- default:
- //
- break;
- }
- $ssh->exec('exit');
- return response()->json([]);
- } catch (\Throwable $th) {
- return response()->json([
- 'message' => __('cipi.something_error_message'),
- 'errors' => __('cipi.error')
- ], 500);
- }
- }
- /**
- * List all server sites
- *
- * @OA\Get(
- * path="/api/servers/{server_id}/sites",
- * summary="List all server sites",
- * tags={"Servers"},
- * description="List all sites in required server.",
- * @OA\Parameter(
- * name="Authorization",
- * description="Use Apikey prefix (e.g. Authorization: Apikey XYZ)",
- * required=true,
- * in="header",
- * @OA\Schema(type="string")
- * ),
- * @OA\Parameter(
- * name="server_id",
- * description="The id of the server.",
- * required=true,
- * in="path",
- * @OA\Schema(type="string")
- * ),
- * @OA\Response(
- * response=200,
- * description="Successful request",
- * @OA\JsonContent(
- * type="array",
- * @OA\Items(
- * @OA\Property(
- * property="site_id",
- * description="Site unique ID",
- * type="string",
- * example="abc-123-def-456"
- * ),
- * @OA\Property(
- * property="domain",
- * description="Main site domain",
- * type="string",
- * example="domain.ltd"
- * ),
- * @OA\Property(
- * property="username",
- * description="Site username",
- * type="string",
- * example="cp123456"
- * ),
- * @OA\Property(
- * property="php",
- * description="Site PHP version",
- * type="string",
- * example="7.4"
- * ),
- * @OA\Property(
- * property="basepath",
- * description="Site basepath",
- * type="string",
- * example="/public"
- * ),
- * @OA\Property(
- * property="aliases",
- * description="The number of aliases of this site",
- * type="integer",
- * example="8"
- * ),
- * )
- * )
- * ),
- * @OA\Response(
- * response=404,
- * description="Server not found or not installed"
- * ),
- * @OA\Response(
- * response=401,
- * description="Unauthorized access error"
- * )
- * )
- */
- public function sites(string $server_id)
- {
- $server = Server::where('server_id', $server_id)->where('status', 1)->first();
- if (!$server) {
- return response()->json([
- 'message' => __('cipi.server_not_found_message'),
- 'errors' => __('cipi.server_not_found')
- ], 404);
- }
- $sites = Site::where('panel', false)->where('server_id', $server->id)->get();
- $response = [];
- foreach ($sites as $site) {
- $data = [
- 'site_id' => $site->site_id,
- 'domain' => $site->domain,
- 'username' => $site->username,
- 'php' => $site->php,
- 'basepath' => $site->basepath,
- 'aliases' => count($site->aliases)
- ];
- array_push($response, $data);
- }
- return response()->json($response);
- }
- /**
- * List all server domains
- *
- * @OA\Get(
- * path="/api/servers/{server_id}/domains",
- * summary="List all server domains",
- * tags={"Servers"},
- * description="List all domains hosted in required server.",
- * @OA\Parameter(
- * name="Authorization",
- * description="Use Apikey prefix (e.g. Authorization: Apikey XYZ)",
- * required=true,
- * in="header",
- * @OA\Schema(type="string")
- * ),
- * @OA\Parameter(
- * name="server_id",
- * description="The id of the server.",
- * required=true,
- * in="path",
- * @OA\Schema(type="string")
- * ),
- * @OA\Response(
- * response=200,
- * description="Successfull response (Domain list array)"
- * ),
- * @OA\Response(
- * response=404,
- * description="Server not found or not installed"
- * ),
- * @OA\Response(
- * response=401,
- * description="Unauthorized access error"
- * )
- * )
- */
- public function domains(string $server_id)
- {
- $server = Server::where('server_id', $server_id)->where('status', 1)->first();
- if (!$server) {
- return response()->json([
- 'message' => __('cipi.server_not_found_message'),
- 'errors' => __('cipi.server_not_found')
- ], 404);
- }
- $response = [];
- foreach ($server->allsites as $site) {
- array_push($response, $site->domain);
- foreach ($site->aliases as $alias) {
- array_push($response, $alias->domain);
- }
- }
- return response()->json($response);
- }
- }
|