Advertisement
Terrah

speed mesurescript

Aug 25th, 2016
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.00 KB | None | 0 0
  1.  
  2. --[[Packet fields (IP Header):
  3. string destination
  4. string source
  5. int checksum
  6. int length
  7. int id
  8. string protocol (tcp,udp,icmp,unkown)
  9. int ttl
  10. int type
  11. int ihl (ip header length)
  12. int version
  13. int flags
  14. int fragment
  15. table/string/nil data (if unknown this is a string)
  16. ]]
  17.  
  18. --[[ICMP Data fields:
  19.  
  20. https://tools.ietf.org/html/rfc777
  21.  
  22. int checksum
  23. int code
  24. int type (0=reply,3=unreachable,8=request,11=timeout)
  25. table original (contains the original stripped IP header)
  26.  
  27. ]]
  28.  
  29. --[[TCP Data fields:
  30.  
  31. int ack
  32. int checksum
  33. int destination_port
  34. int seq
  35. int source_port
  36. int urgent
  37. int window
  38. table flags (array of bools keys: urg,ack,psh,rst,syn,fin)
  39. string data
  40.  
  41. ]]
  42.  
  43. --[[UDP Data fields:
  44.  
  45. int checksum
  46. int conver_checksum
  47. int destination_port
  48. int source_port
  49. string data
  50.  
  51. ]]
  52.  
  53. --IP if its a table it'll attempt listening to the interfaces (adapters)
  54. --with their respective IP
  55. --IP = {};
  56. --IP[0] = "localhost";
  57. --IP[1] = GetHostName();
  58.  
  59.  
  60. --IP can also be a single address
  61. --IP="127.0.0.1";
  62.  
  63. --IP can also be nil, then it'll listen to the first non loopback interface
  64. --IP=nil;
  65.  
  66. --If this is true then the program will wait for user input on failure
  67. PAUSE=true;
  68.  
  69. --Time in miliseconds between the Tick function is proc-ed (Tick())
  70. TICK=500;
  71.  
  72. --If this is set it'll modify the socket-buffer to a new size
  73. --this is usable if you got high traffic on your socket
  74. --increase if your CPU is too shit to handle the amount of data you're reciving
  75. --uses more ram!
  76. --Default: 8192
  77. BUFFER=8192;
  78.  
  79. --Scroll to the bottom for the event function defs
  80. --void Recv(packet, interface); = runs when a packet is recived
  81. --bool Tick(); = runs every tick as defined by TICK, if this returns true the progam will die
  82.  
  83. local Data = {};
  84. local CollectedDown = {};
  85. CollectedDown.byte=0;
  86. CollectedDown.kilo=0;
  87. CollectedDown.mega=0;
  88. CollectedDown.giga=0;
  89. CollectedDown.tera=0;
  90. CollectedDown.all=0;
  91.  
  92. local CollectedUp = {};
  93. CollectedUp.byte=0;
  94. CollectedUp.kilo=0;
  95. CollectedUp.mega=0;
  96. CollectedUp.giga=0;
  97. CollectedUp.tera=0;
  98. CollectedUp.all=0;
  99.  
  100. function AddToCollected(bytes,Collected)
  101.  
  102.     Collected.all = Collected.all + bytes;
  103.  
  104.     Collected.byte = Collected.byte + bytes;
  105.  
  106.     while Collected.byte > 1000 do
  107.         Collected.kilo = Collected.kilo + 1;
  108.         Collected.byte = Collected.byte - 1000;
  109.     end
  110.  
  111.     while Collected.kilo > 1000 do
  112.         Collected.mega = Collected.mega + 1;
  113.         Collected.kilo = Collected.kilo - 1000;
  114.     end
  115.  
  116.     while Collected.mega > 1000 do
  117.         Collected.giga = Collected.giga + 1;
  118.         Collected.mega = Collected.mega - 1000;
  119.     end
  120.  
  121.     while Collected.giga > 1000 do
  122.         Collected.tera = Collected.tera + 1;
  123.         Collected.giga = Collected.giga - 1000;
  124.     end
  125. end
  126.  
  127. function Recv(packet,interface)
  128.  
  129.     local p = Data[interface];
  130.  
  131.     packet.Time = os.clock();
  132.     table.insert(p,packet);
  133. end
  134.  
  135. function ColorPrint(text,backgroud,foreground,pleft,pright)
  136.  
  137.     local b,f = GetTextColor();
  138.     SetTextColor(backgroud,foreground);
  139.  
  140.     if pleft and pleft > 0 then
  141.  
  142.         for n=1,pleft do
  143.             io.write(" ");
  144.         end
  145.     end
  146.  
  147.     io.write(text);
  148.  
  149.     if pright and pright > 0 then
  150.  
  151.         pright = pright - text:len();
  152.  
  153.         if pright > 0 then
  154.  
  155.             for n=1,pright do
  156.                 io.write(" ");
  157.             end
  158.         end
  159.     end
  160.     SetTextColor(b,f);
  161. end
  162.  
  163. local function GetSpeedString(data)
  164.  
  165.     --10 seconds
  166.     data = data / 10;
  167.  
  168.     local speed = data/1000;
  169.     local notation = "B/sec";
  170.  
  171.     if speed > 1.0 then
  172.  
  173.         notation = "KB/sec";
  174.         data = speed;
  175.  
  176.         speed = data/1000;
  177.         if speed > 1.0 then
  178.  
  179.             notation = "MB/sec";
  180.             data = speed;
  181.  
  182.             speed = data/1000;
  183.             if speed > 1.0 then
  184.                 notation = "GB/sec";
  185.                 data = speed;
  186.             end
  187.         end
  188.     end
  189.     return tostring(data),notation;
  190. end
  191.  
  192. local updatecnt = 2;
  193. local cnt=updatecnt;
  194.  
  195. local function Ticker()
  196.  
  197.     if cnt >= updatecnt then
  198.         cnt=0;
  199.     else
  200.         cnt = cnt + 1;
  201.         return;
  202.     end
  203.  
  204.     local t = os.clock();
  205.     local cdata = {};
  206.     local up,down;
  207.     local udpup,udpdown;
  208.     local tcpup,tcpdown;
  209.     local othertcpup,othertcpdown;
  210.     local speed,note;
  211.     local tup,tdown = 0,0;
  212.  
  213.     for k,v in pairs(Data)do
  214.  
  215.         cdata[k]={};
  216.         up=0;
  217.         down=0;
  218.         udpup=0;
  219.         udpdown=0;
  220.         tcpup=0;
  221.         tcpdown=0;
  222.         othertcpup=0;
  223.         othertcpdown=0;
  224.  
  225.         for kk,vv in pairs(v)do
  226.  
  227.             if t-vv.Time > 10.0 then
  228.                 v[kk]=nil;
  229.             else
  230.                 if vv.destination==k then
  231.  
  232.                     down = down + vv.length;
  233.                     AddToCollected(vv.length,CollectedDown);
  234.  
  235.                     if vv.protocol=="tcp" then
  236.                         tcpdown = tcpdown + vv.length;
  237.                     elseif vv.protocol=="udp" then
  238.                         udpdown = udpdown + vv.length;
  239.                     else
  240.                         othertcpdown = othertcpdown + vv.length;
  241.                     end
  242.                 else
  243.  
  244.                     up = up + vv.length;
  245.                     AddToCollected(vv.length,CollectedUp);
  246.  
  247.                     if vv.protocol=="tcp" then
  248.                         tcpup = tcpup + vv.length;
  249.                     elseif vv.protocol=="udp" then
  250.                         udpup = udpup + vv.length;
  251.                     else
  252.                         othertcpup = othertcpup + vv.length;
  253.                     end
  254.                 end
  255.             end
  256.         end
  257.  
  258.         tup = tup + up;
  259.         tdown = tdown + down;
  260.  
  261.         cdata[k].Up = up;
  262.         cdata[k].Down = down;
  263.  
  264.         cdata[k].TcpUp = tcpup;
  265.         cdata[k].TcpDown = tcpdown;
  266.  
  267.         cdata[k].UdpUp = udpup;
  268.         cdata[k].UdpDown = udpdown;
  269.  
  270.         cdata[k].OtherUp = othertcpup;
  271.         cdata[k].OtherDown = othertcpdown;
  272.  
  273.     end
  274.  
  275.     cls();
  276.     for k,v in pairs(cdata)do
  277.  
  278.         ColorPrint(k,1,0xD,0,15);
  279.  
  280.         speed,note = GetSpeedString(v.Down);
  281.  
  282.         ColorPrint("DOWN",1,7,2,5);
  283.         ColorPrint(tostring(speed),1,0x2,2,10);
  284.         ColorPrint(note,1,0x2,1,8);
  285.  
  286.         ColorPrint("|",1,7,0,0);
  287.  
  288.         speed,note = GetSpeedString(v.Up);
  289.  
  290.         ColorPrint("UP",1,7,2,5);
  291.         ColorPrint(tostring(speed),1,0xc,2,10);
  292.         ColorPrint(note,1,0xc,1,16);
  293.  
  294.         ColorPrint("-TCP:",0,0xb,0,15);
  295.  
  296.         speed,note = GetSpeedString(v.TcpDown);
  297.  
  298.         ColorPrint("DOWN",0,7,2,5);
  299.         ColorPrint(tostring(speed),0,0x2,2,10);
  300.         ColorPrint(note,0,0x2,1,8);
  301.  
  302.         io.write("|");
  303.  
  304.         speed,note = GetSpeedString(v.TcpUp);
  305.  
  306.         ColorPrint("UP",0,7,2,5);
  307.         ColorPrint(tostring(speed),0,0xc,2,10);
  308.         ColorPrint(note,0,0xc,1,5);
  309.  
  310.         io.write("\n");
  311.  
  312.         ColorPrint("-UDP:",0,0xe,0,15);
  313.  
  314.         speed,note = GetSpeedString(v.UdpDown);
  315.  
  316.         ColorPrint("DOWN",0,7,2,5);
  317.         ColorPrint(tostring(speed),0,0x2,2,10);
  318.         ColorPrint(note,0,0x2,1,8);
  319.  
  320.         io.write("|");
  321.  
  322.         speed,note = GetSpeedString(v.UdpUp);
  323.  
  324.         ColorPrint("UP",0,7,2,5);
  325.         ColorPrint(tostring(speed),0,0xc,2,10);
  326.         ColorPrint(note,0,0xc,1,5);
  327.  
  328.         io.write("\n");
  329.  
  330.         ColorPrint("-OTHER:",0,0xf,0,15);
  331.  
  332.         speed,note = GetSpeedString(v.OtherDown);
  333.  
  334.         ColorPrint("DOWN",0,7,2,5);
  335.         ColorPrint(tostring(speed),0,0x2,2,10);
  336.         ColorPrint(note,0,0x2,1,8);
  337.  
  338.         io.write("|");
  339.  
  340.         speed,note = GetSpeedString(v.OtherUp);
  341.  
  342.         ColorPrint("UP",0,7,2,5);
  343.         ColorPrint(tostring(speed),0,0xc,2,10);
  344.         ColorPrint(note,0,0xc,1,5);
  345.         io.write("\n\n");
  346.  
  347.     end
  348.  
  349.     speed,note = GetSpeedString(tdown);
  350.  
  351.     TITLE = "TOTAL DOWN/UP: "..tostring(speed).." "..note.." | ";
  352.  
  353.     speed,note = GetSpeedString(tup);
  354.  
  355.     TITLE = TITLE .. tostring(speed).." "..note;
  356.  
  357.     io.write("\n\n");
  358.  
  359.     --ColorPrint(text,backgroud,foreground,pleft,pright)
  360.  
  361.     local offset = 40;
  362.  
  363.     ColorPrint("DOWNLOADED BYTES: " .. tostring(CollectedDown.all),0,0x2,0,offset);
  364.     ColorPrint("UPLOADED BYTES: " .. tostring(CollectedUp.all).."\n\n",0,0xc,0,0);
  365.  
  366.     ColorPrint("TB: "..tostring(CollectedDown.tera),0,0x2,0,offset);
  367.     ColorPrint("TB: "..tostring(CollectedUp.tera).."\n",0,0xc,0,0);
  368.  
  369.     ColorPrint("GB: "..tostring(CollectedDown.giga),0,0x2,0,offset);
  370.     ColorPrint("GB: "..tostring(CollectedUp.giga).."\n",0,0xc,0,0);
  371.  
  372.     ColorPrint("MB: "..tostring(CollectedDown.mega),0,0x2,0,offset);
  373.     ColorPrint("MB: "..tostring(CollectedUp.mega).."\n",0,0xc,0,0);
  374.  
  375.     ColorPrint("KB: "..tostring(CollectedDown.kilo),0,0x2,0,offset);
  376.     ColorPrint("KB: "..tostring(CollectedUp.kilo).."\n",0,0xc,0,0);
  377.  
  378.     ColorPrint(" B: "..tostring(CollectedDown.byte),0,0x2,0,offset);
  379.     ColorPrint(" B: "..tostring(CollectedUp.byte).."\n",0,0xc,0,0);
  380.  
  381.     return false;
  382. end
  383.  
  384. function Tick()
  385.  
  386.     print(tostring(IP));
  387.  
  388.     if type(IP)=="string" then
  389.         Data[IP]={};
  390.         print(IP);
  391.     else
  392.         for k,v in pairs(IP)do
  393.             Data[v]={};
  394.             print(k,v);
  395.         end
  396.     end
  397.  
  398.     Tick = Ticker;
  399. end
  400.  
  401.  
  402.  
  403. print("Lua startup script run!\n");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement