forked from cernodile/reEgg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eihandler.go
171 lines (147 loc) · 4.42 KB
/
eihandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"encoding/base64"
"errors"
"fmt"
"log"
"net/http"
"net/http/httputil"
"strings"
ei "biehdc.reegg/eggpb"
"google.golang.org/protobuf/proto"
)
func (egg *eggstore) path_get_periodicals(decoded []byte) []byte {
getperiodicals := ei.GetPeriodicalsRequest{}
err := proto.Unmarshal(decoded, &getperiodicals)
if err != nil {
log.Printf("cant unmarshal GetPeriodicalsRequest: %s", err)
return nil
}
if getperiodicals.UserId != nil {
log.Printf("get_periodicals: %s", *getperiodicals.UserId)
}
perresp := ei.PeriodicalsResponse{
Contracts: egg.currentContracts(&getperiodicals),
Events: egg.currentEvents(&getperiodicals),
Gifts: egg.serverGifts(&getperiodicals),
}
resp, err := proto.Marshal(&perresp)
if err != nil {
log.Printf("failed to marshal get_periodicals: %s", err.Error())
return nil
}
return resp
}
func (egg *eggstore) handlepath_ei(w http.ResponseWriter, req *http.Request) error {
var err error
action := req.PathValue("subpath")
req.ParseForm()
var decoded []byte
havedata := len(req.Form["data"])
if havedata > 1 {
log.Panicf("subpath is %q: we have a package with multiple data entries which we do not handle at this point", action)
}
if havedata > 0 {
// strings.ReplaceAll is due to android bug wrong padding according to op
decoded, err = base64.StdEncoding.DecodeString(strings.ReplaceAll(req.Form["data"][0], " ", "+"))
if err != nil {
log.Printf("cant base64 decode: %s", err)
return errors.New("do not send me broken base64 strings")
}
}
var resp []byte
switch action {
// userdata stuff
case "first_contact":
resp = egg.path_first_contact(decoded)
case "save_backup":
resp = egg.path_save_backup(decoded)
case "user_data_info":
resp = egg.path_user_data_info(decoded)
// gameplay stuff
case "get_periodicals":
resp = egg.path_get_periodicals(decoded)
case "daily_gift_info":
resp = path_daily_gift_info(decoded)
case "get_contracts":
resp = egg.path_get_contracts(decoded)
// coop stuff
case "query_coop":
resp = egg.path_query_coop(decoded)
case "create_coop":
resp = egg.path_create_coop(decoded)
case "coop_status":
resp = egg.path_coop_status(decoded)
case "update_coop_status":
resp = egg.path_update_coop_status(decoded)
case "join_coop":
resp = egg.path_join_coop(decoded)
case "auto_join_coop":
resp = egg.path_auto_join_coop(decoded)
case "leave_coop":
resp = egg.path_leave_coop(decoded)
case "update_coop_permissions":
resp = egg.path_update_coop_permissions(decoded)
case "gift_player_coop":
resp = egg.path_gift_player_coop(decoded)
case "kick_player_coop":
resp = egg.path_kick_player_coop(decoded)
//misc stuff
case "get_ad_config":
//noop and ignored
return errors.New("we dont advertise anymore")
default:
// something we arent dealing with yet
return handleUnhandled("POST /ei/", action, req)
}
if resp != nil {
buf := make([]byte, base64.StdEncoding.EncodedLen(len(resp)))
base64.StdEncoding.Encode(buf, resp)
//log.Printf("replying: %q", string(buf))
w.Write(buf)
}
return nil
}
func (egg *eggstore) handlepath_eidata(w http.ResponseWriter, req *http.Request) error {
var err error
action := req.PathValue("subpath")
req.ParseForm()
var decoded []byte
havedata := len(req.Form["data"])
if havedata > 1 {
log.Panicf("subpath is %q: we have a package with multiple data entries which we do not handle at this point", action)
}
if havedata > 0 {
// strings.ReplaceAll is due to android bug wrong padding according to op
decoded, err = base64.StdEncoding.DecodeString(strings.ReplaceAll(req.Form["data"][0], " ", "+"))
if err != nil {
log.Printf("cant base64 decode: %s", err)
return errors.New("do not send me broken base64 strings")
}
}
switch action {
case "log_action":
gena := ei.GenericAction{}
err := proto.Unmarshal(decoded, &gena)
if err != nil {
log.Printf("cant unmarshal GenericAction: %s", err)
return nil
}
log.Printf("log_action: %s", gena.String())
default:
return handleUnhandled("POST /ei_data/", action, req)
}
return nil
}
func handleUnhandled(path, action string, req *http.Request) error {
log.Printf("subpath is %q", action)
log.Println("unhandled action:", action)
dump, err := httputil.DumpRequest(req, true)
if err != nil {
log.Printf("failed to dump request: %s", err)
return errors.New("bad request")
}
log.Printf("Got %s path\n>>>\n%s\n<<<", path, strings.TrimSpace(string(dump)))
fmt.Println()
return nil
}