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

fixes for new encryption #29

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions api/crypto.go

This file was deleted.

2 changes: 1 addition & 1 deletion api/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package api
import "testing"

func BenchmarkGetBytes(b *testing.B) {
l := Location{0.0, 0.0, 0.0}
l := Location{0.0, 0.0, 0.0, 0.0}
for n := 0; n < b.N; n++ {
l.GetBytes()
}
Expand Down
65 changes: 38 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"
"gopkg.in/pokelibs/go-pokelib.v43"
)

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 @@ -32,6 +32,8 @@ type Session struct {
started time.Time
provider auth.Provider
hash []byte

deviceInfo *protos.Signature_DeviceInfo
}

func generateRequests() []*protos.Request {
Expand All @@ -43,18 +45,31 @@ 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, deviceInfo *protos.Signature_DeviceInfo, debug bool) *Session {
if deviceInfo == nil{
// Default deviceInfo
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",
}
}
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),
deviceInfo:deviceInfo,
}
}

Expand Down Expand Up @@ -118,51 +133,43 @@ 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
requestHash[idx] = pokelib.HashRequest(ticket, req)
}

locationHash1, err := generateLocation1(s.ticket, s.location)
if err != nil {
return nil, err
}

locationHash2, err := generateLocation2(s.location)
if err != nil {
return nil, err
}
locationHash1 := pokelib.HashLocation1(ticket, s.location.Lat, s.location.Lon, s.location.Alt)
locationHash2 := pokelib.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: s.deviceInfo,
SessionHash: s.hash,
Timestamp: t,
TimestampSinceStart: (t - getTimestamp(s.started)),
Unknown25: pokelib.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: pokelib.Encrypt(signatureProto, uint32(signature.TimestampSinceStart)),
})
if err != nil {
return nil, ErrFormatting
Expand Down Expand Up @@ -265,6 +272,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 +281,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
78 changes: 0 additions & 78 deletions api/util.go

This file was deleted.

11 changes: 3 additions & 8 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package cli

import (
"github.com/pogodevorg/pgoapi-go/api"
"github.com/urfave/cli"
)
import "github.com/urfave/cli"

// Run interprets arguments and performs actions
func Run(crypto api.Crypto, args []string) {
func Run(args []string) {

w := wrapper{
crypto: crypto,
}
w := wrapper{}

app := cli.NewApp()
app.Name = "pgoapi-go"
Expand Down
5 changes: 2 additions & 3 deletions cli/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ type wrapper struct {
alt float64
accuracy float64

debug bool
crypto api.Crypto
debug bool
}

func (w *wrapper) wrap(action func(context.Context, *api.Session, auth.Provider) error) func(*cli.Context) error {
Expand All @@ -40,7 +39,7 @@ func (w *wrapper) wrap(action func(context.Context, *api.Session, auth.Provider)
Accuracy: w.accuracy,
}

client := api.NewSession(provider, location, &api.VoidFeed{}, w.crypto, w.debug)
client := api.NewSession(provider, location, &api.VoidFeed{}, nil, w.debug)

return action(ctx, client, provider)
}
Expand Down
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package main
import (
"os"

"github.com/pogodevorg/pgoapi-go/api"
"github.com/pogodevorg/pgoapi-go/cli"
)

func main() {
crypto := &api.DefaultCrypto{}
cli.Run(crypto, os.Args)
cli.Run(os.Args)
}