/**
  * Function for handle and build request to send to their servers
  *
  * @param ServerRequest $request
  */
 public function sendServerRequests(ServerRequest $request)
 {
     $requestEnvelope = new RequestEnvelope();
     $this->resetBuilder($requestEnvelope);
     $requestEnvelope->addAllRequests([$request->getRequest()]);
     $httpRequest = new Client(["headers" => ["User-Agent" => Settings::USER_AGENT]]);
     $requestResponse = $httpRequest->post($this->apiEndpoint, ["body" => $requestEnvelope->toProtobuf()]);
     if ($requestResponse->getStatusCode() != 200) {
         $this->PokemonGoAPI->getOutput()->write("Got a unexpected http code", false, $this->PokemonGoAPI->getOutput()->ERROR);
     }
     $responseEnvelope = new ResponseEnvelope((string) $requestResponse->getBody());
     if ($responseEnvelope->getAuthTicket() != null) {
         $this->lastAuthTicket = $responseEnvelope->getAuthTicket();
         $this->PokemonGoAPI->getOutput()->write("Got Auth Ticket! ", false, $this->PokemonGoAPI->getOutput()->INFO);
     }
     if ($responseEnvelope->getApiUrl() != null && count($responseEnvelope->getApiUrl()) > 0) {
         $this->apiEndpoint = "https://" . $responseEnvelope->getApiUrl() . "/rpc";
         $this->PokemonGoAPI->getOutput()->write("Got apiEndpoint: " . $this->apiEndpoint, false, $this->PokemonGoAPI->getOutput()->INFO);
     }
     if ($responseEnvelope->getStatusCode() == 53) {
         $this->PokemonGoAPI->getOutput()->write("Resend Request", false, $this->PokemonGoAPI->getOutput()->INFO);
         $this->sendServerRequests($request);
         return;
     }
     $count = 0;
     foreach ($responseEnvelope->getReturnsArray() as $payload) {
         if ($payload != null) {
             $request->handleData($payload);
         }
         $count++;
     }
     $this->nextRequestId();
 }
Esempio n. 2
0
 /**
  * @param Request[] $reqs
  * @param bool|false $createEndpoint
  * @param bool|false $redo
  * @throws Exception
  */
 public function execute($reqs, $createEndpoint = false, $redo = true)
 {
     $reqTypes = [];
     foreach ($reqs as $req) {
         $reqTypes[] = get_class($req);
     }
     $this->logger->info("Execute " . implode(", ", $reqTypes));
     $env = new RequestEnvelope();
     $env->setStatusCode(2);
     $env->setRequestId($this->getRPCId());
     $location = $this->session->getLocation();
     $env->setLatitude($location->getLatitude());
     $env->setLongitude($location->getLongitude());
     $env->setAltitude($location->getAltitude());
     if ($this->session->hasAuthTicket() && $this->session->getAuthTicket()->isValid()) {
         $this->logger->debug("Existing auth ticket");
         $env->setAuthTicket($this->session->getAuthTicket()->toProto());
     } else {
         if (!$this->session->hasToken()) {
             $this->session->authenticate();
         }
         $this->session->setEndpoint(null);
         $this->logger->debug("Auth with token");
         $info = new AuthInfo();
         $info->setProvider($this->session->getType()->getProvider());
         $token = new JWT();
         $token->setContents($this->session->getToken());
         $token->setUnknown2(59);
         $info->setToken($token);
         $env->setAuthInfo($info);
     }
     $env->setUnknown12(989);
     foreach ($reqs as $req) {
         $env->addRequests($req->toProto());
     }
     $URL = $this->session->hasEndpoint() ? $this->session->getEndpoint() : self::APIURL;
     Signature::sign($this->session, $env, $reqs);
     $resp = $this->client->post($URL, ["body" => $env->toStream()->getContents()]);
     $respEnv = new ResponseEnvelope($resp->getBody()->getContents());
     if ($respEnv->hasAuthTicket()) {
         $ticket = new AuthTicket($respEnv->getAuthTicket());
         $this->logger->info("Received auth ticket, expires in " . $ticket->getTimeToExpire() . "s");
         $this->session->setAuthTicket($ticket);
     }
     if ($respEnv->hasApiUrl()) {
         $endpoint = $respEnv->getApiUrl();
         $this->logger->info("Received API endpoint " . $endpoint);
         $this->session->setEndpoint("https://" . $endpoint . "/rpc");
     }
     $statusCode = $respEnv->getStatusCode();
     $this->logger->debug("Status code " . $statusCode);
     if ($statusCode == 102) {
         // Some auth problem
         throw new Exception("Received status code 102: invalid auth");
     } elseif ($statusCode == 53) {
         // Wrong endpoint
         if ($redo) {
             sleep(3);
             $this->execute($reqs, $createEndpoint, false);
             return;
         } else {
             throw new Exception("Received status code 53: wrong endpoint");
         }
     } elseif ($statusCode == 3) {
         // Possible ban
         throw new Exception("Received status code 3: account possibly banned");
     }
     if (!$respEnv->hasReturnsList()) {
         throw new Exception("No responses given");
     }
     $returns = $respEnv->getReturnsList();
     $countResponses = $returns->count();
     if ($countResponses != count($reqs)) {
         throw new Exception("Invalid responses found");
     }
     $responses = [];
     foreach ($returns as $return) {
         $responses[] = $return;
     }
     array_map(function (Request $req, $response) {
         $req->setRawResponse($response);
     }, $reqs, $responses);
 }