-
Notifications
You must be signed in to change notification settings - Fork 35
/
client.lua
221 lines (191 loc) · 6.07 KB
/
client.lua
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
function SaveX(sErr)
if (sErr) then
s.err = sErr
end
file.remove("s.txt")
file.open("s.txt","w+")
print("Saving new config:")
for k, v in pairs(s) do
print(k .. "=" .. v)
file.writeline(k .. "=" .. v)
end
file.close()
collectgarbage()
end
function mysplit(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
-- Forces the files to contain a dot in the name.
if string.match(str, "%.") then
t[i] = str
i = i + 1
end
end
return t
end
function dwn()
-- body
n = n + 1
v = data[n]
if v == nil then
--dofile(data[1]..".lc")
bootfile= string.gsub(data[1], '\.lua$','') --string.gsub(s, '\....$','')
s.boot = bootfile..".lc"
SaveX("No error")
Reboot("File loaded Reboot...")
else
print("Filename: "..v)
filename=v
file.remove(v);
file.open(v, "w+")
payloadFound = false
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)
if (payloadFound == true) then
file.write(payload)
file.flush()
else
if (string.find(payload,"\r\n\r\n") ~= nil) then
file.write(string.sub(payload,string.find(payload,"\r\n\r\n") + 4))
file.flush()
payloadFound = true
end
end
payload = nil
collectgarbage()
end)
conn:on("disconnection", function(conn)
conn = nil
file.close()
ext = string.sub(v, -3)
if (ext == "lua") then
node.compile(filename)
end
dwn()
end)
conn:on("connection", function(conn)
cmd = "GET /"..s.path.."/uploads/"..id.."/"..v.." HTTP/1.0\r\n"..
"Host: "..s.host.."\r\n"..
"Connection: close\r\n"..
"Accept-Charset: utf-8\r\n"..
"Accept-Encoding: \r\n"..
"User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"..
"Accept: */*\r\n\r\n"
print("Request_HTTP:"..cmd)
conn:send(cmd)
end)
conn:connect(80,s.host)
end
end
function Reboot(msg)
print("Restarting: "..msg)
node.restart()
end
function FileList(sck,c)
print "initialized"
local nStart, nEnd = string.find(c, "\n\n")
if (nEnde == nil) then
nStart, nEnd = string.find(c, "\r\n\r\n")
end
c = string.sub(c,nEnd+1)
print("length: "..string.len(c))
data = mysplit(c, "\n") -- fill the field with filenames
--for k,v in pairs(data) do
n = 1
v = data[n]
if v == nil then Reboot("No file to download") end
print("Filename: "..v)
filename=v
file.remove(v);
file.open(v, "w+")
payloadFound = false
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)
if (payloadFound == true) then
file.write(payload)
file.flush()
else
if (string.find(payload,"\r\n\r\n") ~= nil) then
file.write(string.sub(payload,string.find(payload,"\r\n\r\n") + 4))
file.flush()
payloadFound = true
end
end
payload = nil
collectgarbage()
end)
conn:on("disconnection", function(conn)
conn = nil
file.close()
ext = string.sub(v, -3)
if (ext == "lua") then
node.compile(v)
end
dwn()
end)
conn:on("connection", function(conn)
cmd = "GET /"..s.path.."/uploads/"..id.."/"..v.." HTTP/1.0\r\n"..
"Host: "..s.host.."\r\n"..
"Connection: close\r\n"..
"Accept-Charset: utf-8\r\n"..
"Accept-Encoding: \r\n"..
"User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"..
"Accept: */*\r\n\r\n"
print("Request_HTTP:"..cmd)
conn:send(cmd)
end)
conn:connect(80,s.host)
--end
collectgarbage()
end
print("fetch lua..")
data = {}
filename=nil
LoadX()
wifi.setmode (wifi.STATION)
station_cfg={}
if s.ssid ~= nil or s.ssid ~= '' then
station_cfg.ssid=s.ssid
station_cfg.save=true
end
if s.pwd ~= nil or s.pwd ~= '' then
station_cfg.pwd=s.pwd
end
wifi.sta.config(station_cfg)
wifi.sta.autoconnect (1)
iFail = 20 -- trying to connect to AP in 20sec, if not then reboot
tmr.alarm (1, 1000, 1, function ( )
iFail = iFail -1
print(iFail)
if (iFail == 0) then
msg = "could not access "..s.ssid
SaveX(msg)
Reboot(msg)
end
if wifi.sta.getip ( ) == nil then
print(s.ssid..": "..iFail)
else
print ("ip: " .. wifi.sta.getip ( ))
tmr.stop (1)
-- get list of files
sk=net.createConnection(net.TCP, 0)
cmd = "GET /".. s.path .."/node.php?id="..id.."&list"..
" HTTP/1.1\r\n"..
"Host: "..s.domain.."\r\n"..
"Accept: */*\r\n"..
"User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)"..
"\r\n\r\n"
print("Request_HTTP:"..cmd)
sk:on("connection",function(conn, payload)
sk:send(cmd)
end)
sk:on("receive", FileList)
--sGet = "GET /".. s.path .. " HTTP/1.1\r\nHost: " .. s.domain .. "\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n"
sk:connect(80,s.host)
end
collectgarbage()
end)
print(collectgarbage("count").." kB used")