lego11

AFTP

Aug 23rd, 2014
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1.  
  2. function enc(str, key) -- Function
  3. local tProp = {} -- Make table
  4. for i = 1, str:len() do -- loop for str length
  5. local val = tonumber((str:byte(i) * tonumber(key) + (12.6 * key)) / 2.8)--Byte str times keyadd 12.6 times key and divide 2.8
  6. table.insert(tProp, val) -- rest is to return the data
  7. end
  8. return tostring(table.concat(tProp,"|"))
  9. end
  10.  
  11. function dec(str, key)
  12. local data = {}
  13. for word in string.gmatch(str, "[^|]+") do
  14. local rdata = ((tonumber(word)*2.8)-(12.6*key))/key
  15. table.insert(data, math.floor(rdata+0.5))
  16. end
  17. return string.char(unpack(data))
  18. end
  19.  
  20.  
  21. --config
  22.  
  23. side = "top" --modem
  24. rand = false --more secure not yet implemented
  25. btlp = false --not yet implement
  26. stpmsg = "end" --msg that indicates end of file
  27. shtdown = false -- if true will shutdown at end of file transfer
  28. gfb = true -- return current file size and other usefull data
  29. pmsg = "ping" --default ping msg
  30. dc = false --basic encryption method (eta will be wrong)
  31. vsn = 1.4 -- version
  32. cp = true -- keep pinging if failed connection
  33.  
  34. --code
  35. rednet.open(side)
  36. term.clear()
  37. term.setCursorPos(1,1)
  38. print("Arpanet FTP "..vsn.." |")
  39. print("(C) Aperture Labs 1970 |")
  40. print("---------------------#")
  41. print('Type help for command list')
  42.  
  43. while true do
  44. ct = false
  45. write("action> ")
  46. p = read()
  47.  
  48. if p == "help" then
  49. print[[help -cmd list
  50. exit -leave program
  51. id -computer id
  52. files -send file
  53. filer -receive file]]
  54. ct = true
  55. end
  56.  
  57. if p == "id" then
  58. print(os.getComputerID())
  59. ct = true
  60. end
  61.  
  62. if p == "filer" then
  63. write("ID> ")
  64. cid = read()
  65. stop = false
  66. cid = cid + 0
  67. write("save as: ")
  68. nami = read()
  69. h = fs.open(nami,"w")
  70. write("ping msg> ")
  71. rednet.send(cid,read())
  72.  
  73. while stop == false do
  74. icid, pack = rednet.receive(2)
  75. if cp == false then
  76. if icid == nil then
  77. print("no connection established!")
  78. sleep(1.5)
  79. os.reboot()
  80. end
  81. else
  82. if icid == nil then
  83. print("no connection established.")
  84. sleep(1.5)
  85. end
  86. end
  87. if icid == cid then
  88. print("Received "..pack.." Saving to file")
  89.  
  90. if pack ~= stpmsg then
  91. h.write(pack)
  92. if gfb == true then
  93. print(fs.getSize(nami))
  94. end
  95. else
  96. print("file complete!")
  97. if dc == true then
  98. write("key> ")
  99. keys = read()
  100. h.close()
  101. h = fs.open(nami,'r')
  102. nonen = h.readAll()
  103. h.close()
  104. h = fs.open(nami,'w')
  105. h.write(dec(nonen,keys))
  106. end
  107. if dc == false then
  108. h.close()
  109. end
  110. stop = true
  111. if shtdown == true then
  112. os.shutdown()
  113. else
  114. os.reboot()
  115. end
  116. end
  117.  
  118. elseif icid ~= nil then
  119. print("Blocked Conection from ID: "..icid)
  120. end
  121. sleep(0)
  122. end
  123.  
  124. ct = true
  125. end
  126.  
  127. if p == "files" then
  128. write("file> ")
  129. nami = read()
  130. if dc == true then
  131. write("key> ")
  132. keys = read()
  133. end
  134. h = fs.open(nami,"r")
  135. if dc == false then
  136. wpack = h.readAll()
  137. else
  138. wpack = enc(h.readAll(), keys)
  139. end
  140. wpackt = {}
  141.  
  142. print("Dividing into Packets may take a while!")
  143. for char in string.gmatch(wpack, ".") do
  144. table.insert(wpackt, char)
  145. sleep(0)
  146. end
  147.  
  148. if gfb == true then
  149. print("hosting on "..os.getComputerID())
  150. eta = #wpackt * .01
  151. print("Estimated transfer time in seconds")
  152. print(eta)
  153. end
  154.  
  155. while true do
  156.  
  157. cid, auth = rednet.receive()
  158. stop = false
  159. c = 1
  160. wpn = #wpackt + 1
  161. if auth == "sup" then
  162. print("Seeding ID:"..cid)
  163. while c < wpn do
  164. rednet.send(cid, wpackt[c])
  165. c = c + 1
  166. sleep(0.01)
  167. end
  168. rednet.send(cid, stpmsg)
  169. print("Seed Complete!")
  170.  
  171. elseif auth == pmsg then
  172. rednet.send(cid,nami)
  173. end
  174. sleep(0)
  175. end
  176.  
  177. ct = true
  178. end
  179.  
  180. if p == "exit" then
  181. print("Thankyou for using Arpanet FTP.")
  182. sleep(1.5)
  183. os.reboot()
  184. ct = true
  185. end
  186.  
  187. if ct == false then
  188. print("ERR Unknown command.")
  189. end
  190. sleep(0)
  191. end
Advertisement
Add Comment
Please, Sign In to add comment