/app/Http/Controllers/Server/ProxmoxController.php

https://github.com/MercyCloudTeam/TomatoIDC · PHP · 212 lines · 138 code · 20 blank · 54 comment · 7 complexity · cea9c5b08339427e919b93f8beac63df MD5 · raw file

  1. <?php
  2. namespace App\Http\Controllers\Server;
  3. use App\HostModel;
  4. use App\Http\Controllers\Controller;
  5. use App\OrderModel;
  6. use App\ServerModel;
  7. use GuzzleHttp\Cookie\CookieJar;
  8. use GuzzleHttp\Exception\RequestException;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Log;
  11. use GuzzleHttp\Client;
  12. class ProxmoxController extends Controller
  13. {
  14. public $diyConfigureFrom = false;//使用自定义表单
  15. public $type = true; //服务器插件类型
  16. /**
  17. * 获取服务器状态 成功返回 接口值 失败返回false
  18. * @param $server
  19. * @return bool|mixed
  20. * @throws \GuzzleHttp\Exception\GuzzleException
  21. */
  22. public function serverStatus($server)
  23. {
  24. $result = $this->clientApi($server, 'GET', '/api2/json/nodes');
  25. // dd(json_decode($result));
  26. return false;
  27. }
  28. /**
  29. * 向服务器发送请求
  30. * @param $server ServerModel
  31. * @param $method string GET|POST
  32. * @param $action string url
  33. * @param null $configure
  34. * @param null $param
  35. * @return bool|string
  36. * @throws \GuzzleHttp\Exception\GuzzleException
  37. */
  38. protected function clientApi($server, $method, $action, $configure = null, $param = null)
  39. {
  40. $url = $this->getUrl($server);
  41. $ticket = $this->getTicket($server, $configure);
  42. if (empty($ticket)) {
  43. return false;
  44. }
  45. $cookies = [
  46. 'PVEAuthCookie' => $ticket->data->ticket,
  47. 'CSRFPreventionToken' => $ticket->data->CSRFPreventionToken,
  48. ];
  49. // try {
  50. $cookieJar = CookieJar::fromArray($cookies, $server->ip);
  51. $public_params = [
  52. 'verify' => false, //不验证证书
  53. 'cookies' => $cookieJar,
  54. 'form_params' => [
  55. // 'CSRFPreventionToken' => $ticket->data->CSRFPreventionToken,
  56. ],
  57. 'headers' => [
  58. 'CSRFPreventionToken' => $ticket->data->CSRFPreventionToken,
  59. ]
  60. ];
  61. if (!empty($param)) {
  62. $params = array_merge_recursive($public_params, $param);
  63. } else {
  64. $params = $public_params;
  65. }
  66. // dd($params);
  67. $client = new Client();
  68. $response = $client->request(
  69. $method, $url . $action, $params
  70. );
  71. // }
  72. // catch (RequestException $e) {
  73. // Log::error('Proxmox RequestException error', [$e, $server, $url, $cookies, $param]);
  74. // return false;
  75. // }
  76. if (!empty($response->getBody()->getContents())) {
  77. return (string)$response->getBody();
  78. }
  79. Log::error('Proxmox result error', [$response, $server, $url, $cookies, $param]);
  80. return false;
  81. }
  82. protected function createUserPve($server)
  83. {
  84. $param['form_params']['userid'] = strtolower('test');
  85. $result = $this->clientApi($server, 'POST', '/api2/json/access/users', null, $param);
  86. dd($result);
  87. // /api2/json/access/users
  88. }
  89. //https://your.server:8006/api2/json/
  90. protected function getTicket($server, $configure)
  91. {
  92. $url = $this->getUrl($server) . "/api2/json/access/ticket";
  93. $realm = $server->key2 ?? "pam";
  94. $arr = [
  95. 'form_params' => [
  96. 'username' => $server->username,
  97. 'password' => $server->password,
  98. 'realm' => $realm,
  99. ],
  100. 'verify' => false //不验证证书
  101. ];
  102. try {
  103. $client = new Client();
  104. $response = $client->request('POST', $url, $arr);
  105. }
  106. catch (\Exception $e) {
  107. Log::error('Proxmox get ticket error', [$server, $url, $arr]);
  108. return false;
  109. }
  110. $result = $response->getBody()->getContents();
  111. if (!empty($result)) {
  112. return $result = json_decode($result);
  113. }
  114. return false;
  115. }
  116. protected function getUrl($server)
  117. {
  118. $port = $server->port ?? "8006";
  119. //判断是否填写前缀
  120. preg_match("/(http|https):\/\//", $server->ip, $arr);
  121. if (empty($arr)) {
  122. $protocol = "https://";
  123. } else {
  124. $protocol = '';
  125. }
  126. return $url = $protocol . $server->ip . ':' . $port;
  127. }
  128. /**
  129. * 创建主机
  130. * @param $server
  131. * @param $configure
  132. * @param $order
  133. * @return bool
  134. * @throws \GuzzleHttp\Exception\GuzzleException
  135. */
  136. public function createHost($server, $configure, $order)
  137. {
  138. // $this->createUserPve($server);
  139. $node = $configure->customip ?? "pve";
  140. $osTemplate = $configure->template ?? null;
  141. $vmid = mt_rand(10,99).substr(time(),7).mt_rand(100,999);
  142. $param['form_params']=[
  143. 'ostemplate'=>$osTemplate,
  144. 'vmid'=>$vmid
  145. ];
  146. $this->clientApi($server,'POST',' /api2/json/nodes/'.$node.'/lxc',null,$param);
  147. dd(json_decode($this->clientApi($server, 'GET', '/api2/json/nodes', $configure)));
  148. $host = HostModel::create(
  149. [
  150. 'order_id' => $order->id,
  151. 'user_id' => $order->user_id,
  152. 'host_name' => $order->good->title,
  153. 'host_pass' => 'null',
  154. 'host_panel' => 'null',
  155. 'host_url' => 'null'
  156. ]
  157. );
  158. return $host;
  159. }
  160. //TODO 获取主机信息
  161. public function getVh($server, $host)
  162. {
  163. }
  164. /**
  165. * 续费主机
  166. * @return bool
  167. */
  168. public function renewHost()
  169. {
  170. return true;
  171. }
  172. /**
  173. * 开通主机
  174. * @param $server
  175. * @param $host
  176. * @return bool
  177. */
  178. public function openHost($server, $host)
  179. {
  180. return $host;
  181. }
  182. /**
  183. * 停用主机
  184. * @param $server
  185. * @param $host
  186. * @return false|object
  187. */
  188. public function closeHost($server, $host)
  189. {
  190. return $host;
  191. }
  192. }