-
Notifications
You must be signed in to change notification settings - Fork 3
/
E2_Proxy_old.txt
264 lines (221 loc) · 6.71 KB
/
E2_Proxy_old.txt
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
@name E2 Proxy
@inputs [Reset]
@outputs State
@persist URL:string URLHash
@persist HttpData:string HttpDataLen #interval HTTP variables
@persist DataToWrite:string DataToWriteLen
@persist FileLoaded FileRead:string FileReadLength
@persist FilePartCount FileCurrentPart
@persist [FileCurrentSubStart FileCurrentSubLen] [FileCurrentSub]:string
@outputs FileMultiplePartsToWrite FileCurrentPartName:string
@persist [PROGRAM_NAME]:string #constants
@persist FILE_MAXSIZE [FILE_OUT_PREFIX, FILE_COMM]:string #constants (file-related)
# Unused variables
#@persist FileCurrentSubHash HttpDataHash DataToWriteHash
#@persist [DO_OPTIMISE]
#@persist [NEWLINE URL_INVALID]:string
#@persist E:entity
# Rewritten in 2018, 2023
# Rev3
#THIS IS A BETA BUILD, BUGS EASILY MAY BE PRESENT
#Current limitations:
# * Very limited hashing (as E2 inbuilt hashing is slow)
# * No silent-mode
# * No concmd optimisations inbuilt (not sure if needed)
# SIGNALS
# 0 - OK
# 2 - Success: URL access
# 4 - Bad URL (short for error 404)
# 5 - Segmentation fault (server error)
# 9 - Success: Overall task
runOnFile(1)
runOnHTTP(1)
interval(1)
if(first())
{
#E = entity()
#E:setAlpha(0)
PROGRAM_NAME = "E2XY" #EE-two-xie
#URL_INVALID = "0.0.0.0"
#NEWLINE = toChar(10)
FILE_OUT_PREFIX = "proxy_out"
FILE_COMM = "proxy_comm.txt"
FILE_MAXSIZE = fileMaxSize() * 1024 #1024, not 1000. More info on https://github.com/wiremod/wire/blob/master/lua/entities/gmod_wire_expression2/core/files.lua
#DO_OPTIMISE = 1 #Allow in-game local variables manipulation for optimisation
function setState(N)
{
State = N
}
function incState(){setState(State+1)}
function printStd(Prefix:string, Msg:string)
{
print("["+PROGRAM_NAME+":"+Prefix+"] "+Msg)
}
function reload()
{
setState(0)
FilePartCount = 0
FileMultiplePartsToWrite = 0 #by default
Reset=0
}
function sendSignal(S:string)
{
fileWrite(FILE_COMM, S)
}
function string num2hex(N) # Number to 32-bit hex
{
return format("%08x", N)
}
function string generate_FN_Part_Name(Base:string, BaseHash, CurrentPart, MaxPart)
{
if(MaxPart < 1)
{
printStd("Error", "Invalid parts amount, things can be very wrong!")
}
return format("%s.%08x.%x-%x.txt",
Base, BaseHash, CurrentPart, MaxPart
)
}
reload()
}
if(Reset){reload()}
autoPerf(1)
switch(State)
{
case 0,
#fileCanWrite will be very useful in the next few cases
if(fileCanLoad() & fileCanWrite())
{
fileLoad(FILE_COMM)
incState()
}
break
case 1,
FileLoaded = fileClk()
if(FileLoaded)
{
FileRead = fileRead()
FileReadLength = FileRead:length()
if(FileReadLength > 1) # something is for input, and it's not a signal
{
URL = FileRead
URLHash = hash(URL)
incState()
}
elseif (!fileLoaded() & !fileLoading())
{
sendSignal("0")
printStd("All good", "Comm file was not present, so we created it")
reload()
}
else #no input (yet)
{
printStd("Idle", "No input (yet)")
reload()
}
}
break
case 2,
#Run the internet request
if(httpCanRequest()){
httpRequest(URL)
incState()
}
break
case 3,
#Wait for the response
HttpData = httpData()
HttpDataLen = HttpData:length()
if (HttpDataLen)
{
printStd("Process", "Data retrieved, now saving...")
sendSignal("2")
incState()
}
elseif (!HttpDataLen & httpCanRequest()) #If loading error
{
printStd("Error", "Page not found")
sendSignal("4")
reload()
}
break
case 4,
#generate stats for the retrieved data, could be very laggy
DataToWrite = httpUrlEncode(HttpData) #Binary-to-text, E2 internal implementation
DataToWriteLen = DataToWrite:length() #this variable will be VERY useful in the future
incState()
break
case 5,
#generate stats for file parts (1 layer down)
FileCurrentPart = 1
FilePartCount = ceil(DataToWriteLen/FILE_MAXSIZE) #ceil, not floor!
FileMultiplePartsToWrite = FilePartCount > 1 #not necessary per se, just to make my life easier
incState()
break
case 6, #generate stats for the current file part
if(FileMultiplePartsToWrite)
{
# I reckon this WILL cause heavy lags since we manipulate large strings
# but what can we do?
#REMEMBER: sub() starts counting from 1
#Syntax: string:Sub(StartingPoint, AmountOfChars)
FileCurrentSubStart = ((FileCurrentPart-1)*FILE_MAXSIZE)+1
FileCurrentSubLen = min( (FileCurrentPart * FILE_MAXSIZE), DataToWriteLen )
FileCurrentSub = DataToWrite:sub(FileCurrentSubStart, FileCurrentSubLen)
}
incState()
break
case 7, #wait until we are ready for the next step
if(fileCanWrite())
{
if (FileMultiplePartsToWrite)
{
FileCurrentPartName = generate_FN_Part_Name(FILE_OUT_PREFIX, URLHash,
FileCurrentPart, FilePartCount
)
}
else
{
FileCurrentPartName = generate_FN_Part_Name(FILE_OUT_PREFIX, URLHash,
1, 1
)
}
incState()
}
break
case 8, #write to the file part
if (FileMultiplePartsToWrite)
{
if(FileCurrentPart <= FilePartCount)
{
fileWrite(FileCurrentPartName, FileCurrentSub)
FileCurrentPart++
}
#this is a post check, so the next time we
#don't wait for the fileCanWrite to become 1,
#which could take another 3-5 seconds
if(FileCurrentPart > FilePartCount)
{
incState()
}
else
{
setState(State-2) # To: generate stats for the current file part
}
}
else
{
fileWrite(FileCurrentPartName, DataToWrite)
incState()
}
break
case 9, #finishing up
if(fileCanWrite())
{
sendSignal("9")
printStd("Notification", "Task completed")
reload()
}
default,
break
}