Skip to content
This repository has been archived by the owner on Nov 16, 2019. It is now read-only.

Commit

Permalink
fixes for new encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
femot committed Nov 8, 2016
1 parent ec6bfc9 commit ec8a3f2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 58 deletions.
31 changes: 0 additions & 31 deletions api/crypto.go

This file was deleted.

58 changes: 31 additions & 27 deletions api/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package api
import (
"context"
"crypto/rand"
"errors"
"fmt"
"log"
"time"

"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"

protos "github.com/pogodevorg/POGOProtos-go"
"github.com/pogodevorg/pgoapi-go/auth"
"gitlab.com/pidgeyfinder/pokecrypt"
)

const defaultURL = "https://pgorelease.nianticlabs.com/plfe/rpc"
Expand All @@ -20,7 +21,6 @@ const downloadSettingsHash = "05daf51635c82611d1aac95c0b051d3ec088a930"
// Session is used to communicate with the Pokémon Go API
type Session struct {
feed Feed
crypto Crypto
location *Location
rpc *RPC
url string
Expand All @@ -43,15 +43,14 @@ func getTimestamp(t time.Time) uint64 {
}

// NewSession constructs a Pokémon Go RPC API client
func NewSession(provider auth.Provider, location *Location, feed Feed, crypto Crypto, debug bool) *Session {
func NewSession(provider auth.Provider, location *Location, feed Feed, debug bool) *Session {
return &Session{
location: location,
rpc: NewRPC(),
provider: provider,
debug: debug,
debugger: &jsonpb.Marshaler{Indent: "\t"},
feed: feed,
crypto: crypto,
started: time.Now(),
hasTicket: false,
hash: make([]byte, 32),
Expand Down Expand Up @@ -118,51 +117,52 @@ func (s *Session) Call(ctx context.Context, requests []*protos.Request) (*protos
}
}

if s.crypto.Enabled() && s.hasTicket {
if s.hasTicket {
t := getTimestamp(time.Now())

ticket, _ := proto.Marshal(s.ticket)
requestHash := make([]uint64, len(requests))

for idx, request := range requests {
hash, err := generateRequestHash(s.ticket, request)
req, err := proto.Marshal(request)
if err != nil {
return nil, err
}
requestHash[idx] = hash
}

locationHash1, err := generateLocation1(s.ticket, s.location)
if err != nil {
return nil, err
requestHash[idx] = pokecrypt.HashRequest(ticket, req)
}

locationHash2, err := generateLocation2(s.location)
if err != nil {
return nil, err
}
locationHash1 := pokecrypt.HashLoaction1(ticket, s.location.Lat, s.location.Lon, s.location.Alt)
locationHash2 := pokecrypt.HashLocation2(s.location.Lat, s.location.Lon, s.location.Alt)

signature := &protos.Signature{
RequestHash: requestHash,
LocationHash1: locationHash1,
LocationHash2: locationHash2,
RequestHash: requestHash,
LocationHash1: int32(locationHash1),
LocationHash2: int32(locationHash2),
ActivityStatus: &protos.Signature_ActivityStatus{
Stationary: true,
},
DeviceInfo: &protos.Signature_DeviceInfo{
DeviceId: "<device_id>",
DeviceBrand: "Apple",
DeviceModel: "iPhone",
DeviceModelBoot: "Iphone7,2",
HardwareManufacturer: "Apple",
HardwareModel: "N66AP",
FirmwareBrand: "iPhone OS",
FirmwareType: "9.3.3",
},
SessionHash: s.hash,
Timestamp: t,
TimestampSinceStart: (t - getTimestamp(s.started)),
Unknown25: pokecrypt.Hash25(),
}

signatureProto, err := proto.Marshal(signature)
if err != nil {
return nil, ErrFormatting
}

iv := s.crypto.CreateIV()
encryptedSignature, err := s.crypto.Encrypt(signatureProto, iv)
if err != nil {
return nil, ErrFormatting
}

requestMessage, err := proto.Marshal(&protos.SendEncryptedSignatureRequest{
EncryptedSignature: encryptedSignature,
EncryptedSignature: pokecrypt.Encrypt(signatureProto, uint32(signature.TimestampSinceStart)),
})
if err != nil {
return nil, ErrFormatting
Expand Down Expand Up @@ -265,6 +265,7 @@ func (s *Session) Announce(ctx context.Context) (mapObjects *protos.GetMapObject
{RequestType: protos.RequestType_CHECK_AWARDED_BADGES},
{protos.RequestType_DOWNLOAD_SETTINGS, settingsMessage},
{protos.RequestType_GET_MAP_OBJECTS, getMapObjectsMessage},
{RequestType: protos.RequestType_CHECK_CHALLENGE},
}

response, err := s.Call(ctx, requests)
Expand All @@ -273,6 +274,9 @@ func (s *Session) Announce(ctx context.Context) (mapObjects *protos.GetMapObject
}

mapObjects = &protos.GetMapObjectsResponse{}
if len(response.Returns) < 5 {
return nil, errors.New("Empty response")
}
err = proto.Unmarshal(response.Returns[5], mapObjects)
if err != nil {
return nil, &ErrResponse{err}
Expand Down

0 comments on commit ec8a3f2

Please sign in to comment.