Alyssa

Shop_Server

Mar 18th, 2015
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.38 KB | None | 0 0
  1. debug = true
  2. authTime = 100 --This might work better if it's changed
  3. mSide = "top" -- modem side
  4. scrx,scry =term.getSize()
  5. cur = term.current()
  6. local oPull = os.pullEvent
  7. os.pullEvent = os.pullEventRaw
  8. doExits = true --Maybe change this.
  9. tChan = 1337 --Channel for turtle communication, should probably be changed
  10. cChan = 31337 --Channel for client communication, also should be changed.
  11. sAddr = "kvse3lmart" --Address to send to here
  12. toAddr = "klukereedk" --Profits go here
  13. iWindowX = 0
  14. iWindowY = 0
  15. iWindowZ = 0
  16.  
  17. if not fs.exists('dateApi') then
  18.     if fs.exists("dateApi") then
  19.         fs.delete("dateApi")
  20.     end
  21.     print("Installing Date API")
  22.     shell.run("pastebin","get","21kyJN5A","dateApi")
  23.     os.loadAPI("dateApi")
  24. else
  25.     os.loadAPI("dateApi")
  26. end
  27.  
  28. if fs.exists("/authKey") then
  29.     if debug then
  30.         print("Found auth file")
  31.     end
  32.     f = fs.open("/authKey","r")
  33.     authKey = f.readAll()
  34.     f.close()
  35. else
  36.     error("authKey file does not exist.")
  37. end
  38.  
  39. if fs.exists("/pkey") then
  40.     f = fs.open("/pkey","r")
  41.     pKey = f.readAll()
  42.     f.close()
  43. else
  44.     error("No private key file, put the master/private key (The hash ending with -000) for the store address in '/pkey'")
  45. end
  46.  
  47. if not fs.isDir("/coupons") then
  48.     if fs.exists("/coupons") then
  49.         fs.delete("/coupons")
  50.     end
  51.     fs.makeDir("/coupons")
  52. end
  53.  
  54. if not fs.exists("/stock") then
  55.     f = fs.open("/stock","w")
  56.     f.write(textutils.serialize({}))
  57.     f.close()
  58. end
  59.  
  60. local MOD = 2^32
  61. local MODM = MOD-1
  62.  
  63. local function memoize(f)
  64.         local mt = {}
  65.         local t = setmetatable({}, mt)
  66.         function mt:__index(k)
  67.                 local v = f(k)
  68.                 t[k] = v
  69.                 return v
  70.         end
  71.         return t
  72. end
  73.  
  74. local function make_bitop_uncached(t, m)
  75.         local function bitop(a, b)
  76.                 local res,p = 0,1
  77.                 while a ~= 0 and b ~= 0 do
  78.                         local am, bm = a % m, b % m
  79.                         res = res + t[am][bm] * p
  80.                         a = (a - am) / m
  81.                         b = (b - bm) / m
  82.                         p = p*m
  83.                 end
  84.                 res = res + (a + b) * p
  85.                 return res
  86.         end
  87.         return bitop
  88. end
  89.  
  90. local function make_bitop(t)
  91.         local op1 = make_bitop_uncached(t,2^1)
  92.         local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  93.         return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  94. end
  95.  
  96. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  97.  
  98. local function bxor(a, b, c, ...)
  99.         local z = nil
  100.         if b then
  101.                 a = a % MOD
  102.                 b = b % MOD
  103.                 z = bxor1(a, b)
  104.                 if c then z = bxor(z, c, ...) end
  105.                 return z
  106.         elseif a then return a % MOD
  107.         else return 0 end
  108. end
  109.  
  110. local function band(a, b, c, ...)
  111.         local z
  112.         if b then
  113.                 a = a % MOD
  114.                 b = b % MOD
  115.                 z = ((a + b) - bxor1(a,b)) / 2
  116.                 if c then z = bit32_band(z, c, ...) end
  117.                 return z
  118.         elseif a then return a % MOD
  119.         else return MODM end
  120. end
  121.  
  122. local function bnot(x) return (-1 - x) % MOD end
  123.  
  124. local function rshift1(a, disp)
  125.         if disp < 0 then return lshift(a,-disp) end
  126.         return math.floor(a % 2 ^ 32 / 2 ^ disp)
  127. end
  128.  
  129. local function rshift(x, disp)
  130.         if disp > 31 or disp < -31 then return 0 end
  131.         return rshift1(x % MOD, disp)
  132. end
  133.  
  134. local function lshift(a, disp)
  135.         if disp < 0 then return rshift(a,-disp) end
  136.         return (a * 2 ^ disp) % 2 ^ 32
  137. end
  138.  
  139. local function rrotate(x, disp)
  140.     x = x % MOD
  141.     disp = disp % 32
  142.     local low = band(x, 2 ^ disp - 1)
  143.     return rshift(x, disp) + lshift(low, 32 - disp)
  144. end
  145.  
  146. local k = {
  147.         0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  148.         0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  149.         0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  150.         0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  151.         0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  152.         0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  153.         0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  154.         0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  155.         0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  156.         0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  157.         0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  158.         0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  159.         0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  160.         0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  161.         0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  162.         0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  163. }
  164.  
  165. local function str2hexa(s)
  166.         return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  167. end
  168.  
  169. local function num2s(l, n)
  170.         local s = ""
  171.         for i = 1, n do
  172.                 local rem = l % 256
  173.                 s = string.char(rem) .. s
  174.                 l = (l - rem) / 256
  175.         end
  176.         return s
  177. end
  178.  
  179. local function s232num(s, i)
  180.         local n = 0
  181.         for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  182.         return n
  183. end
  184.  
  185. local function preproc(msg, len)
  186.         local extra = 64 - ((len + 9) % 64)
  187.         len = num2s(8 * len, 8)
  188.         msg = msg .. "\128" .. string.rep("\0", extra) .. len
  189.         assert(#msg % 64 == 0)
  190.         return msg
  191. end
  192.  
  193. local function initH256(H)
  194.         H[1] = 0x6a09e667
  195.         H[2] = 0xbb67ae85
  196.         H[3] = 0x3c6ef372
  197.         H[4] = 0xa54ff53a
  198.         H[5] = 0x510e527f
  199.         H[6] = 0x9b05688c
  200.         H[7] = 0x1f83d9ab
  201.         H[8] = 0x5be0cd19
  202.         return H
  203. end
  204.  
  205. local function digestblock(msg, i, H)
  206.         local w = {}
  207.         for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  208.         for j = 17, 64 do
  209.                 local v = w[j - 15]
  210.                 local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  211.                 v = w[j - 2]
  212.                 w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  213.         end
  214.  
  215.         local a, b, c, d, e, f, g, h = H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]
  216.         for i = 1, 64 do
  217.                 local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  218.                 local maj = bxor(band(a, b), band(a, c), band(b, c))
  219.                 local t2 = s0 + maj
  220.                 local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  221.                 local ch = bxor (band(e, f), band(bnot(e), g))
  222.                 local t1 = h + s1 + ch + k[i] + w[i]
  223.                 h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  224.         end
  225.  
  226.         H[1] = band(H[1] + a)
  227.         H[2] = band(H[2] + b)
  228.         H[3] = band(H[3] + c)
  229.         H[4] = band(H[4] + d)
  230.         H[5] = band(H[5] + e)
  231.         H[6] = band(H[6] + f)
  232.         H[7] = band(H[7] + g)
  233.         H[8] = band(H[8] + h)
  234. end
  235.  
  236. local function sha256(msg)
  237.         msg = preproc(msg, #msg)
  238.         local H = initH256({})
  239.         for i = 1, #msg, 64 do digestblock(msg, i, H) end
  240.         return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  241.                 num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  242. end
  243.  
  244. function readStock()
  245.     f = fs.open("/stock","r")
  246.     fc = textutils.unserialize(f.readAll())
  247.     f.close()
  248.     return fc
  249. end
  250.  
  251. function updateStock(nFile)
  252.     f = fs.open("/stock","w")
  253.     f.write(textutils.serialize(nFile))
  254.     f.close()
  255.     return true
  256. end
  257.  
  258.  
  259. function getData(id,name)
  260.     sFile = readStock()
  261.     if not sFile[id] then
  262.         return false
  263.     end
  264.     if not sFile[id][name] then
  265.         return false
  266.     end
  267.     return sFile[id][name]
  268. end
  269.  
  270. function setData(id,name,value)
  271.     sFile = readStock()
  272.     if not sFile[id] then
  273.         return false --The entry needs to exist
  274.     end
  275.     sFile[id][name] = value
  276.     updateStock(sFile)
  277.     return true
  278. end
  279.  
  280. function getEntry(id)
  281.     sFile = readStock()
  282.     if not sFile[id] then
  283.         return false
  284.     end
  285.     return sFile[id]
  286. end
  287.  
  288. function addEntry(tValues)
  289.     sFile = readStock()
  290.     table.insert(sFile,tValues)
  291.     updateStock(sFile)
  292.     return #sFile
  293. end
  294.  
  295. function addLog(toAdd)
  296.     term.redirect(logs)
  297.     cTime = textutils.formatTime(os.time(),false)
  298.     d,m,y = dateApi.genDate(os.day())
  299.     date = m.."/"..d.."/"..y
  300.     print("["..cTime.." "..date.."] "..toAdd)
  301.     return true
  302. end
  303.  
  304. function runCommand(cmd)
  305.     cArgs = {}
  306.     for w in cmd:gmatch("[^ ]+") do
  307.         table.insert(cArgs,w)
  308.     end
  309.     if string.lower(cArgs[1]) == "echo" then
  310.         if #cmd > 5 then
  311.             addLog(cmd:sub(6,#cmd))
  312.             return true
  313.         else
  314.             addLog("Usage: "..cArgs[1].." <What to echo>")
  315.             return false
  316.         end
  317.     elseif string.lower(cArgs[1]) == "exit" then
  318.         addLog("Exiting program.")
  319.         term.redirect(cur)
  320.         os.pullEvent = oPull
  321.         runServ = false
  322.     end
  323. end
  324.  
  325. function doCheck(rec)
  326.     --Got an OK/COMPLETE message
  327.     curWait = httpWait
  328.     if rec == "OK" then
  329.         checkOK = "COMPLETE"
  330.         oCheck = toCheck
  331.         toCheck = nil --Wait for a complete
  332.     elseif rec == "COMPLETE" then
  333.         if oCheck[1] == "get_chest" then
  334.             checkOK = "OK"
  335.             toCheck = {"deliver_item",iWindowX,iWindowY,iWindowZ}
  336.         else
  337.             checkOK = false
  338.         end
  339.     end
  340. end
  341.  
  342. function genValid(time) --Generate something to detect if the turtle is legit.
  343.     if not time then
  344.         time = os.time()
  345.     end
  346.     tHash = authKey.." "..tostring(math.floor(math.floor(time() * authTime) / (authTime / 10)))
  347.     vKey = sha256(tHash)
  348.     return vKey:sub(1,10) --Just because
  349. end
  350.  
  351. modem = peripheral.wrap(mSide)
  352. modem.open(tChan)
  353. modem.open(cChan)
  354.  
  355. ce = ""
  356. sce = 1
  357. txtLog = {}
  358. tls = 0
  359. TID = os.startTimer(2.5) --Delay between checking recent transactions
  360. stime = os.time() * 1000 --Measure time in ticks
  361. hst = os.time() * 1000
  362. cvs = {}
  363. checkOK = false
  364. nBal = 0
  365. httpWait = 2.5
  366. curWait = 2.5
  367. logs = window.create(cur, 1, 1,scrx,scry-1,true)
  368. rTxt = window.create(cur,1,scry,scrx,1,true)
  369. term.redirect(rTxt)
  370. term.setCursorBlink(true)
  371. runServ = true
  372.  
  373. while runServ do
  374.     --Event 1, Side 2, Channel 3, Reply Channel 4, Message 5, Distance 6
  375.     evnt = {os.pullEvent()}
  376.     etime = os.time() * 1000
  377.     te = etime - stime
  378.     tes = te / 20
  379.     htes = (((os.time() * 1000) - hst) / 20)
  380.     if htes >= httpWait then
  381.         suc = http.request("http://65.26.252.225/krist/?getbalance="..sAddr)
  382.         hst = os.time() * 1000
  383.     end
  384.     if tes >= curWait then --Check if curWait seconds have passed.
  385.         if checkOK then
  386.             if toCheck then
  387.                 toSend = genValid(os.time()).." "
  388.                 for i = 1, #toCheck do
  389.                     toSend = toSend..toCheck[i].." "
  390.                 end
  391.                 toSend = toSend:sub(1,#toSend-1)
  392.                 table.insert(cvs,1,toSend:sub(1,10))
  393.                 modem.transmit(tChan, 10000, toSend)
  394.             end
  395.         end
  396.         TID = os.startTimer(curWait) --Reboot the timer,so we get events
  397.         stime = os.time() * 1000 --Reboot the measuring system
  398.     end
  399.     if evnt[1] == "http_success" then
  400.         cBal = tonumber(evnt[3].readAll())
  401.         if cBal > 0 then
  402.             if nBal then
  403.                 if cBal == nBal then
  404.                     --Give items
  405.                 end
  406.             else
  407.                 --Refund money
  408.             end
  409.         end
  410.     elseif evnt[1] == "modem_message" then
  411.         curTime = os.time()
  412.         cKey = genValid(curTime)
  413.         msg = evnt[5]
  414.         if evnt[5]:sub(1,10) == cKey then
  415.             --Received a message that may, or may not be a checkOK
  416.             args = {}
  417.             for w in msg:gmatch("[^ ]+") do
  418.                 table.insert(args,w)
  419.             end
  420.             if args[2] == checkOK then
  421.                 doCheck(args[2])
  422.             end
  423.         elseif checkOK then
  424.             for i = 1, #cvs do
  425.                 if cvs[1] == evnt[5]:sub(1,10) then
  426.                     args = {}
  427.                     for w in msg:gmatch("[^ ]+") do
  428.                         table.insert(args,w)
  429.                     end
  430.                     if args[2] == checkOK then
  431.                         doCheck(args[2])
  432.                     end
  433.                 end
  434.             end
  435.         elseif evnt[3] == cChan then
  436.             --Client
  437.         end
  438.     elseif evnt[1] == "key" then
  439.         if evnt[2] == keys.left then
  440.             if sce > 1 then
  441.                 sce = sce-1
  442.             end
  443.             term.setCursorPos(sce,1)
  444.         elseif evnt[2] == keys.right then
  445.             if sce < #ce +1 then
  446.                 sce = sce+1
  447.             end
  448.             term.setCursorPos(sce,1)
  449.         elseif evnt[2] == 199 then
  450.             sce = 1
  451.             term.setCursorPos(sce,1)
  452.         elseif evnt[2] == 207 then
  453.             sce = #ce +1
  454.             term.setCursorPos(scr,1)
  455.         elseif evnt[2] == keys.backspace then
  456.             ce = ce:sub(0,sce-2) .. ce:sub(sce,#ce)
  457.             sce = sce-1
  458.             if sce < 1 then
  459.                 sce = 1
  460.             end
  461.             term.redirect(rTxt)
  462.             term.clearLine()
  463.             term.setCursorPos(1,1)
  464.             write(ce)
  465.             term.setCursorPos(sce,1)
  466.         elseif evnt[2] == keys.enter then
  467.             runCommand(ce)
  468.             table.insert(txtLog,1,ce)
  469.             sce = 1
  470.             ce = ""
  471.             tls = 0
  472.             term.redirect(rTxt)
  473.             term.clearLine()
  474.             term.setCursorPos(1,1)
  475.             write(ce)
  476.             term.setCursorPos(sce,1)
  477.         elseif evnt[2] == keys.up then
  478.             if txtLog[tls+1] then
  479.                 tls = tls+1
  480.                 ce = txtLog[tls]
  481.                 sce = #ce +1
  482.                 term.redirect(rTxt)
  483.                 term.clearLine()
  484.                 term.setCursorPos(1,1)
  485.                 write(ce)
  486.                 term.setCursorPos(sce,1)
  487.             end
  488.         elseif evnt[2] == keys.down then
  489.             if txtLog[tls-1] then
  490.                 tls = tls-1
  491.                 ce = txtLog[tls]
  492.                 sce = #ce +1
  493.                 term.redirect(rTxt)
  494.                 term.clearLine()
  495.                 term.setCursorPos(1,1)
  496.                 write(ce)
  497.                 term.setCursorPos(sce,1)
  498.             else
  499.                 tls = 0
  500.                 ce = ""
  501.                 sce = 1
  502.                 term.redirect(rTxt)
  503.                 term.clearLine()
  504.                 term.setCursorPos(1,1)
  505.                 write(ce)
  506.                 term.setCursorPos(sce,1)
  507.             end
  508.         end
  509.     elseif evnt[1] == "char" then
  510.         ce = ce:sub(0,sce-1) .. evnt[2] .. ce:sub(sce,#ce)
  511.         sce = sce+1
  512.         term.redirect(rTxt)
  513.         term.clearLine()
  514.         term.setCursorPos(1,1)
  515.         write(ce)
  516.         term.setCursorPos(sce,1)
  517.     elseif evnt[1] == "terminate" then
  518.         if doExits then
  519.             term.redirect(cur)
  520.             os.pullEvent = oPull
  521.             break
  522.         end
  523.     end
  524. end
Advertisement
Add Comment
Please, Sign In to add comment