-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcontinuity_manager.gd
More file actions
114 lines (86 loc) · 3.34 KB
/
Copy pathcontinuity_manager.gd
File metadata and controls
114 lines (86 loc) · 3.34 KB
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
class_name TaloContinuityManager extends Timer
var _client: TaloClient
var _requests: Array = []
const _CONTINUITY_PATH = "user://tc.bin"
const _CONTINUITY_TIMESTAMP_HEADER = "X-Talo-Continuity-Timestamp"
const _HEALTH_CHECK_ENDPOINT := "/public/health"
const _EXCLUDED_ENDPOINTS: Array[String] = [
_HEALTH_CHECK_ENDPOINT,
"/v1/players/auth",
"/v1/players/identify",
"/v1/socket-tickets"
]
func _ready() -> void:
name = "TaloContinuityManager"
_client = TaloClient.new("")
add_child(_client)
_requests = _read_requests()
ignore_time_scale = true
timeout.connect(_on_timeout)
start(10)
func push_request(method: HTTPClient.Method, url: String, body: Dictionary, headers: Array[String], timestamp: int):
_requests.push_back({
method = method,
url = url,
body = body.duplicate(true),
headers = headers.filter(func (h: String): return h.find("Authorization") == -1 and h.find("X-Talo-Signature") == -1),
timestamp = timestamp
})
_write_requests()
func _read_requests() -> Array:
if not FileAccess.file_exists(_CONTINUITY_PATH):
return []
var file := FileAccess.open_encrypted_with_pass(_CONTINUITY_PATH, FileAccess.READ, Talo.crypto_manager.get_key())
if file == null:
TaloCryptoManager.handle_undecryptable_file(_CONTINUITY_PATH, "continuity file")
return []
var json := JSON.new()
json.parse(file.get_as_text())
file.close()
return json.data
func _write_requests():
var file := FileAccess.open_encrypted_with_pass(_CONTINUITY_PATH, FileAccess.WRITE, Talo.crypto_manager.get_key())
file.store_line(JSON.stringify(_requests))
file.close()
func _on_timeout():
if _requests.is_empty() or not (await Talo.health_check.ping()):
return
for i in range(10):
if _requests.is_empty():
break
var req := _requests.pop_front()
_write_requests()
var headers: Array[String] = ["Authorization: Bearer %s" % Talo.settings.access_key]
headers.append_array(req.headers)
if not req.headers.any(func (h: String): return h.find(_CONTINUITY_TIMESTAMP_HEADER) != -1):
headers.append("%s: %s" % [_CONTINUITY_TIMESTAMP_HEADER, req.timestamp])
await _client.make_request(req.method, req.url, req.body, headers, true)
func request_can_be_replayed(method: HTTPClient.Method, url: String, res: TaloClient.TaloClientResponse) -> bool:
if not Talo.settings.continuity_enabled:
return false
if _EXCLUDED_ENDPOINTS.any(func (endpoint: String): return url.find(endpoint) != -1):
return false
var allowed_methods := [
HTTPClient.Method.METHOD_POST,
HTTPClient.Method.METHOD_PUT,
HTTPClient.Method.METHOD_PATCH,
HTTPClient.Method.METHOD_DELETE
]
if not allowed_methods.has(method):
return false
return res.result != HTTPRequest.RESULT_SUCCESS or res.response_code > 500
func clear_requests() -> void:
_requests.clear()
_write_requests()
func handle_post_response_healthcheck(url: String, res: TaloClient.TaloClientResponse):
if url.find(_HEALTH_CHECK_ENDPOINT) != -1:
return
var success := true if res.result == HTTPRequest.RESULT_SUCCESS else false
if success:
# if offline mode is enabled, check if it should be disabled
if Talo.health_check.get_last_status() == Talo.health_check.HealthCheckStatus.FAILED:
await Talo.health_check.ping()
else:
# if offline mode isn't enabled, check if it should be enabled
if Talo.health_check.get_last_status() != Talo.health_check.HealthCheckStatus.FAILED:
await Talo.health_check.ping()