ProxyService.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Services;
  3. class ProxyService{
  4. protected $apiKey;
  5. protected $consoleHost;
  6. protected $restFullApi;
  7. const TOKEN_CREATE = 'token';
  8. const TOKEN_PROXMOX = 'proxmox';
  9. const AUTH ='auth';
  10. public function __construct($apiKey, $consoleHost, RestFullApi $restFullApi)
  11. {
  12. if(!$apiKey){
  13. throw new \InvalidArgumentException("Parameter verification failed, API Key is empty");
  14. }
  15. if(!$consoleHost){
  16. throw new \InvalidArgumentException("Parameter verification failed, Console Host is empty");
  17. }
  18. $this->apiKey = $apiKey;
  19. $this->consoleHost = $consoleHost;
  20. $this->restFullApi = $restFullApi;
  21. }
  22. public function token($url,$ip, $cookie=[], $get=[], $post=[]){
  23. $request = http_build_query([
  24. 'securityToken' => $this->apiKey,
  25. 'URL' => $url,
  26. 'GET' => $get,
  27. 'COOKIE' => $cookie,
  28. 'POST' => $post,
  29. 'ip' => $ip
  30. ]);
  31. return $this->restFullApi->post("/".ProxyService::TOKEN_CREATE, $request);
  32. }
  33. public function proxmox($url, $clientIpAddres, $cookie){
  34. if(!$url){
  35. throw new \InvalidArgumentException("noVNC URL is empty");
  36. }
  37. if(!$cookie['PVEAuthCookie']){
  38. throw new \InvalidArgumentException("Auth Cookie is empty");
  39. }
  40. $response = $this->token($url, $clientIpAddres, $cookie);
  41. if(!$response['token']){
  42. throw new \InvalidArgumentException("Parameter verification failed, API Key is invalid.");
  43. }
  44. $host = $this->consoleHost;
  45. if(preg_match("/http/", $this->consoleHost)){
  46. $host = str_replace(["http://", "https://"],["",""],$this->consoleHost);
  47. }
  48. //ws://10.10.10.12/Cvz8PpbSmmqQOmrbQ8Q9GvS3PRXxdWdd/proxmox
  49. return sprintf("wss://%s/%s/%s/%s",$host, ProxyService::AUTH ,$response['token'],ProxyService::TOKEN_PROXMOX);
  50. }
  51. }