use of com.pokegoapi.util.hash.Hash in project PokeGOAPI-Java by Grover-c13.
the class PokeHashProvider method provide.
/**
* Provides a hash for the given arguments
*
* @param timestamp timestamp to hash
* @param latitude latitude to hash
* @param longitude longitude to hash
* @param altitude altitude to hash
* @param authTicket auth ticket to hash
* @param sessionData session data to hash
* @param requests request data to hash
* @return the hash provider
* @throws HashException if an exception occurs while providing this hash
*/
@Override
public Hash provide(long timestamp, double latitude, double longitude, double altitude, byte[] authTicket, byte[] sessionData, byte[][] requests) throws HashException {
if (key.hasTested()) {
if (awaitRequests) {
try {
key.await();
} catch (InterruptedException e) {
throw new HashException(e);
}
} else {
long time = System.currentTimeMillis();
long timeLeft = time - key.getRatePeriodEnd();
if (key.getRequestsRemaining() <= 0 && timeLeft > 0) {
throw new HashLimitExceededException("Exceeded hash request limit! Period ends in " + timeLeft + "ms");
}
}
}
Request request = new Request(latitude, longitude, altitude, timestamp, authTicket, sessionData, requests);
try {
HttpURLConnection connection = (HttpURLConnection) new URL(endpoint).openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("X-AuthToken", key.getKey());
connection.setRequestProperty("content-type", "application/json");
connection.setRequestProperty("User-Agent", "PokeGOAPI-Java");
connection.setDoOutput(true);
String requestJSON = MOSHI.adapter(Request.class).toJson(request);
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(requestJSON);
out.flush();
out.close();
int responseCode = connection.getResponseCode();
this.key.setProperties(connection);
String error = getError(connection);
switch(responseCode) {
case HttpURLConnection.HTTP_OK:
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder builder = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
builder.append(line);
}
in.close();
Response response = MOSHI.adapter(Response.class).fromJson(builder.toString());
long locationAuth = response.getLocationAuthHash();
long location = response.getLocationHash();
int locationAuthHash = (int) ((locationAuth & 0xFFFFFFFFL) ^ (locationAuth >>> 32));
int locationHash = (int) ((location & 0xFFFFFFFFL) ^ (location >>> 32));
return new Hash(locationAuthHash, locationHash, response.getRequestHashes());
case HttpURLConnection.HTTP_BAD_REQUEST:
if (error.length() > 0) {
throw new HashException(error);
}
throw new HashException("Bad hash request!");
case HttpURLConnection.HTTP_UNAUTHORIZED:
if (error.length() > 0) {
throw new HashException(error);
}
throw new HashException("Unauthorized hash request!");
case 429:
if (awaitRequests) {
try {
key.await();
return provide(timestamp, latitude, longitude, altitude, authTicket, sessionData, requests);
} catch (InterruptedException e) {
throw new HashException(e);
}
} else {
if (error.length() > 0) {
throw new HashLimitExceededException(error);
}
throw new HashLimitExceededException("Exceeded hash limit!");
}
case 404:
throw new HashException("Unknown hashing endpoint! \"" + this.endpoint + "\"");
default:
if (error.length() > 0) {
throw new HashException(error + " (" + responseCode + ")");
}
throw new HashException("Received unknown response code! (" + responseCode + ")");
}
} catch (IOException e) {
throw new HashException("Failed to perform PokeHash request", e);
}
}
use of com.pokegoapi.util.hash.Hash in project PokeGOAPI-Java by Grover-c13.
the class Signature method setSignature.
/**
* Given a fully built request, set the signature correctly.
*
* @param api the api
* @param builder the RequestEnvelope builder
* @throws RequestFailedException if an invalid request is sent
*/
public static void setSignature(PokemonGo api, RequestEnvelope.Builder builder) throws RequestFailedException {
boolean usePtr8 = false;
byte[][] requestData = new byte[builder.getRequestsCount()][];
for (int i = 0; i < builder.getRequestsCount(); i++) {
requestData[i] = builder.getRequests(i).toByteArray();
RequestType requestType = builder.getRequests(i).getRequestType();
if (requestType == RequestType.GET_PLAYER) {
usePtr8 |= api.isFirstGP();
api.setFirstGP(false);
} else if (requestType == RequestType.GET_MAP_OBJECTS) {
usePtr8 |= !api.isFirstGMO();
api.setFirstGMO(false);
}
}
double latitude = api.getLatitude();
double longitude = api.getLongitude();
double accuracy = api.getAccuracy();
if (Double.isNaN(latitude)) {
latitude = 0.0;
}
if (Double.isNaN(longitude)) {
longitude = 0.0;
}
if (Double.isNaN(accuracy)) {
accuracy = 0.0;
}
byte[] authTicket;
if (builder.hasAuthTicket()) {
authTicket = builder.getAuthTicket().toByteArray();
} else {
authTicket = builder.getAuthInfo().toByteArray();
}
long currentTimeMillis = api.currentTimeMillis();
byte[] sessionHash = api.getSessionHash();
HashProvider provider = api.getHashProvider();
Hash hash = provider.provide(currentTimeMillis, latitude, longitude, accuracy, authTicket, sessionHash, requestData);
long timeSinceStart = currentTimeMillis - api.getStartTime();
SignatureOuterClass.Signature.Builder signatureBuilder = SignatureOuterClass.Signature.newBuilder().setLocationHash1(hash.getLocationAuthHash()).setLocationHash2(hash.getLocationHash()).setSessionHash(ByteString.copyFrom(sessionHash)).setTimestamp(currentTimeMillis).setTimestampSinceStart(timeSinceStart).setDeviceInfo(api.getDeviceInfo()).addAllLocationFix(LocationFixes.getDefault(api, builder, currentTimeMillis, RANDOM)).setActivityStatus(api.getActivitySignature(RANDOM)).setUnknown25(provider.getUNK25());
final SignatureOuterClass.Signature.SensorInfo sensorInfo = SensorInfo.getDefault(api, currentTimeMillis, RANDOM);
if (sensorInfo != null)
signatureBuilder.addSensorInfo(sensorInfo);
List<Long> requestHashes = hash.getRequestHashes();
for (int i = 0; i < builder.getRequestsCount(); i++) signatureBuilder.addRequestHash(requestHashes.get(i));
Crypto crypto = provider.getCrypto();
SignatureOuterClass.Signature signature = signatureBuilder.build();
byte[] signatureByteArray = signature.toByteArray();
byte[] encrypted = crypto.encrypt(signatureByteArray, timeSinceStart).toByteBuffer().array();
ByteString signatureBytes = SendEncryptedSignatureRequest.newBuilder().setEncryptedSignature(ByteString.copyFrom(encrypted)).build().toByteString();
RequestEnvelope.PlatformRequest signatureRequest = RequestEnvelope.PlatformRequest.newBuilder().setType(PlatformRequestType.SEND_ENCRYPTED_SIGNATURE).setRequestMessage(signatureBytes).build();
builder.addPlatformRequests(signatureRequest);
if (usePtr8) {
ByteString ptr8 = UnknownPtr8RequestOuterClass.UnknownPtr8Request.newBuilder().setMessage("90f6a704505bccac73cec99b07794993e6fd5a12").build().toByteString();
builder.addPlatformRequests(RequestEnvelope.PlatformRequest.newBuilder().setType(PlatformRequestType.UNKNOWN_PTR_8).setRequestMessage(ptr8).build());
}
}
Aggregations