forked from heroiclabs/nakama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_rpc.go
315 lines (288 loc) · 9.9 KB
/
api_rpc.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright 2018 The Nakama Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package server
import (
"context"
"encoding/json"
"io"
"net/http"
"strings"
"time"
"github.com/gofrs/uuid/v5"
"github.com/gorilla/mux"
grpcgw "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/heroiclabs/nakama-common/api"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
var (
authTokenInvalidBytes = []byte(`{"error":"Auth token invalid","message":"Auth token invalid","code":16}`)
httpKeyInvalidBytes = []byte(`{"error":"HTTP key invalid","message":"HTTP key invalid","code":16}`)
noAuthBytes = []byte(`{"error":"Auth token or HTTP key required","message":"Auth token or HTTP key required","code":16}`)
rpcIDMustBeSetBytes = []byte(`{"error":"RPC ID must be set","message":"RPC ID must be set","code":3}`)
rpcFunctionNotFoundBytes = []byte(`{"error":"RPC function not found","message":"RPC function not found","code":5}`)
internalServerErrorBytes = []byte(`{"error":"Internal Server Error","message":"Internal Server Error","code":13}`)
badJSONBytes = []byte(`{"error":"json: cannot unmarshal object into Go value of type string","message":"json: cannot unmarshal object into Go value of type string","code":3}`)
requestBodyTooLargeBytes = []byte(`{"code":3, "message":"http: request body too large"}`)
)
func (s *ApiServer) RpcFuncHttp(w http.ResponseWriter, r *http.Request) {
// Check first token then HTTP key for authentication, and add user info to the context.
queryParams := r.URL.Query()
var isTokenAuth bool
var userID uuid.UUID
var username string
var vars map[string]string
var expiry int64
if httpKey := queryParams.Get("http_key"); httpKey != "" {
if httpKey != s.config.GetRuntime().HTTPKey {
// HTTP key did not match.
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
_, err := w.Write(httpKeyInvalidBytes)
if err != nil {
s.logger.Debug("Error writing response to client", zap.Error(err))
}
return
}
} else if auth := r.Header["Authorization"]; len(auth) >= 1 {
if httpKey, _, ok := parseBasicAuth(auth[0]); ok {
if httpKey != s.config.GetRuntime().HTTPKey {
// HTTP key did not match.
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
_, err := w.Write(httpKeyInvalidBytes)
if err != nil {
s.logger.Debug("Error writing response to client", zap.Error(err))
}
return
}
} else {
var token string
userID, username, vars, expiry, token, isTokenAuth = parseBearerAuth([]byte(s.config.GetSession().EncryptionKey), auth[0])
if !isTokenAuth || !s.sessionCache.IsValidSession(userID, expiry, token) {
// Auth token not valid or expired.
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
_, err := w.Write(authTokenInvalidBytes)
if err != nil {
s.logger.Debug("Error writing response to client", zap.Error(err))
}
return
}
}
} else {
// No authentication present.
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
_, err := w.Write(noAuthBytes)
if err != nil {
s.logger.Debug("Error writing response to client", zap.Error(err))
}
return
}
start := time.Now()
var success bool
var recvBytes, sentBytes int
var err error
var id string
// After this point the RPC will be captured in metrics.
defer func() {
s.metrics.ApiRpc(id, time.Since(start), int64(recvBytes), int64(sentBytes), !success)
}()
// Check the RPC function ID.
maybeID, ok := mux.Vars(r)["id"]
if !ok || maybeID == "" {
// Missing RPC function ID.
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusBadRequest)
sentBytes, err = w.Write(rpcIDMustBeSetBytes)
if err != nil {
s.logger.Debug("Error writing response to client", zap.Error(err))
}
return
}
id = strings.ToLower(maybeID)
// Find the correct RPC function.
fn := s.runtime.Rpc(id)
if fn == nil {
// No function registered for this ID.
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusNotFound)
sentBytes, err = w.Write(rpcFunctionNotFoundBytes)
if err != nil {
s.logger.Debug("Error writing response to client", zap.Error(err))
}
return
}
// Check if we need to mimic existing GRPC Gateway behaviour or expect to receive/send unwrapped data.
// Any value for this query parameter, including the parameter existing with an empty value, will
// indicate that raw behaviour is expected.
_, unwrap := queryParams["unwrap"]
// Prepare input to function.
var payload string
if r.Method == "POST" {
b, err := io.ReadAll(r.Body)
if err != nil {
// Request body too large.
if err.Error() == "http: request body too large" {
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusBadRequest)
sentBytes, err = w.Write(requestBodyTooLargeBytes)
if err != nil {
s.logger.Debug("Error writing response to client", zap.Error(err))
}
return
}
// Other error reading request body.
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
sentBytes, err = w.Write(internalServerErrorBytes)
if err != nil {
s.logger.Debug("Error writing response to client", zap.Error(err))
}
return
}
recvBytes = len(b)
// Maybe attempt to decode to a JSON string to mimic existing GRPC Gateway behaviour.
if recvBytes > 0 && !unwrap {
err = json.Unmarshal(b, &payload)
if err != nil {
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusBadRequest)
sentBytes, err = w.Write(badJSONBytes)
if err != nil {
s.logger.Debug("Error writing response to client", zap.Error(err))
}
return
}
} else {
payload = string(b)
}
}
queryParams.Del("http_key")
uid := ""
if isTokenAuth {
uid = userID.String()
}
clientIP, clientPort := extractClientAddressFromRequest(s.logger, r)
// Extract http headers
headers := make(map[string][]string)
for k, v := range r.Header {
if k == "Grpc-Timeout" {
continue
}
headers[k] = make([]string, 0, len(v))
headers[k] = append(headers[k], v...)
}
// Execute the function.
result, fnErr, code := fn(r.Context(), headers, queryParams, uid, username, vars, expiry, "", clientIP, clientPort, "", payload)
if fnErr != nil {
response, _ := json.Marshal(map[string]interface{}{"error": fnErr, "message": fnErr.Error(), "code": code})
w.Header().Set("content-type", "application/json")
w.WriteHeader(grpcgw.HTTPStatusFromCode(code))
sentBytes, err = w.Write(response)
if err != nil {
s.logger.Debug("Error writing response to client", zap.Error(err))
}
return
}
// Return the successful result.
var response []byte
if !unwrap {
// GRPC Gateway equivalent behaviour.
var err error
response, err = json.Marshal(map[string]interface{}{"payload": result})
if err != nil {
// Failed to encode the wrapped response.
s.logger.Error("Error marshaling wrapped response to client", zap.Error(err))
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
sentBytes, err = w.Write(internalServerErrorBytes)
if err != nil {
s.logger.Debug("Error writing response to client", zap.Error(err))
}
return
}
} else {
// "Unwrapped" response.
response = []byte(result)
}
if unwrap {
if contentType := r.Header["Content-Type"]; len(contentType) > 0 {
// Assume the request input content type is the same as the expected response.
w.Header().Set("content-type", contentType[0])
} else {
// Don't know payload content-type.
w.Header().Set("content-type", "text/plain")
}
} else {
// Fall back to default response content type application/json.
w.Header().Set("content-type", "application/json")
}
w.WriteHeader(http.StatusOK)
sentBytes, err = w.Write(response)
if err != nil {
s.logger.Debug("Error writing response to client", zap.Error(err))
return
}
success = true
}
func (s *ApiServer) RpcFunc(ctx context.Context, in *api.Rpc) (*api.Rpc, error) {
if in.Id == "" {
return nil, status.Error(codes.InvalidArgument, "RPC ID must be set")
}
id := strings.ToLower(in.Id)
fn := s.runtime.Rpc(id)
if fn == nil {
return nil, status.Error(codes.NotFound, "RPC function not found")
}
headers := make(map[string][]string, 0)
queryParams := make(map[string][]string, 0)
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Error(codes.Internal, "RPC function could not get incoming context")
}
for k, vs := range md {
// Only process the keys representing custom query parameters.
if strings.HasPrefix(k, "q_") {
queryParams[k[2:]] = vs
} else {
headers[k] = vs
}
}
uid := ""
username := ""
var vars map[string]string
expiry := int64(0)
if u := ctx.Value(ctxUserIDKey{}); u != nil {
uid = u.(uuid.UUID).String()
}
if u := ctx.Value(ctxUsernameKey{}); u != nil {
username = u.(string)
}
if v := ctx.Value(ctxVarsKey{}); v != nil {
vars = v.(map[string]string)
}
if e := ctx.Value(ctxExpiryKey{}); e != nil {
expiry = e.(int64)
}
clientIP, clientPort := extractClientAddressFromContext(s.logger, ctx)
result, fnErr, code := fn(ctx, headers, queryParams, uid, username, vars, expiry, "", clientIP, clientPort, "", in.Payload)
if fnErr != nil {
return nil, status.Error(code, fnErr.Error())
}
return &api.Rpc{Payload: result}, nil
}