CapsAdmin

Untitled

Jul 24th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.61 KB | None | 0 0
  1. include("luasocket/ltn12.lua")
  2. include("luasocket/socket.lua")
  3. include("luasocket/mime.lua")
  4.  
  5. include("luasocket/socket/url.lua")
  6. include("luasocket/socket/http.lua")
  7. include("luasocket/socket/tp.lua")
  8. include("luasocket/socket/smtp.lua")
  9. include("luasocket/socket/ftp.lua")
  10.  
  11. luasocket = {}
  12.  
  13. luasocket.ltn12 = require("mime")
  14. luasocket.socket = require("socket")
  15. luasocket.mime = require("mime")
  16. luasocket.url = require("socket.url")
  17. luasocket.http = require("socket.http")
  18. luasocket.tp = require("socket.tp")
  19. luasocket.smtp = require("socket.smtp")
  20. luasocket.ftp = require("socket.ftp")
  21.  
  22. http = {}
  23.  
  24. function http.HeaderToTable(header)
  25.     local tbl = {}
  26.  
  27.     for key, line in pairs(header:Explode("\n")) do
  28.         if #line ~= 0 then
  29.             local key, value = line:match("(.+):%s+(.+)")
  30.             tbl[key] = value
  31.         end
  32.     end
  33.  
  34.     return tbl
  35. end
  36.  
  37. function http.TableToHeader(tbl)
  38.     local str = ""
  39.  
  40.     for key, value in pairs(tbl) do
  41.         str = str .. tostring(key) .. ": " .. tostring(value) .. "\n"
  42.     end
  43.  
  44.     return str
  45. end
  46.  
  47. function http.Get(url, callback, header)
  48.     check(url, "string")
  49.     check(header, "string", "nil")
  50.     check(callback, "function", "nil", "false")
  51.  
  52.     url = url:gsub("http://", "")
  53.     callback = callback or table.print
  54.  
  55.     local host, get = url:match("(.-)/(.+)")
  56.  
  57.     if not get then
  58.         host = url:gsub("/", "")
  59.         get = ""
  60.     end
  61.  
  62.     local socket, err = luasocket.socket.connect(host, 80)
  63.     if not socket then print("$4"..err) return end
  64.  
  65.     socket:send(F("GET /%s HTTP/1.1\n", get))
  66.     socket:send(F("Host: %s\n", host))
  67.     socket:send("User-Agent: oohh\n")
  68.     socket:send("\n")
  69.  
  70.     local header = ""
  71.     local content = ""
  72.     local status
  73.  
  74.     local isheader = true
  75.  
  76.     Thinker(function()
  77.         local line, err, rest = socket:receive("*line")
  78.         socket:settimeout(0)
  79.  
  80.         if err then
  81.             local ok, err = pcall(callback, {content = content, header = http.HeaderToTable(header), status = status})
  82.  
  83.             if err then
  84.                 print(err)
  85.             end
  86.  
  87.             socket:close()
  88.  
  89.             return true
  90.         end
  91.  
  92.         if not status then
  93.             status = line
  94.             return
  95.         end
  96.  
  97.         if isheader then
  98.             if #line == 0 then
  99.                 isheader = false
  100.             else
  101.                 header = header  .. line .. "\n"
  102.             end
  103.         else
  104.             content = content .. line .. "\n"
  105.         end
  106.     end)
  107. end
  108.  
  109. do -- tcp socket meta
  110.     local sockets = {}
  111.  
  112.     hook.Add("LuaClose", "socket_close", function()
  113.         for key, sock in pairs(sockets) do
  114.             if sock:IsValid() then
  115.                 sock:Remove()
  116.             else
  117.                 table.remove(sockets, key)
  118.             end
  119.         end
  120.     end)
  121.  
  122.     timer.Create("socket_think", 0, 0, function()
  123.         for key, sock in pairs(sockets) do
  124.             if sock:IsValid() then
  125.                 sock:Think()
  126.             else
  127.                 table.remove(sockets, key)
  128.             end
  129.         end
  130.     end)
  131.  
  132.     local function assert(res, err)
  133.         if not res then
  134.             error(res, 3)
  135.         end
  136.         return res
  137.     end
  138.  
  139.     local function new_socket(override, META, typ)
  140.         typ = typ or "tcp"
  141.         typ = typ:lower()
  142.  
  143.         if typ == "udp" or typ == "tcp" then
  144.             local self = {}
  145.  
  146.             self.socket = override or assert(luasocket.socket[typ]())
  147.             self.socket:settimeout(0)
  148.             self.socket_type = typ
  149.  
  150.             local obj = setmetatable(self, META)
  151.             obj:Initialize()
  152.             table.insert(sockets, obj)
  153.  
  154.             return obj
  155.         end
  156.     end
  157.  
  158.     local function remove_socket(self)
  159.         self.socket:close()
  160.         for key, sock in pairs(sockets) do
  161.             if sock == self then
  162.                 table.remove(sockets, key)
  163.                 break
  164.             end
  165.         end
  166.         MakeNULL(self)
  167.     end
  168.  
  169.     do -- client
  170.         local CLIENT = {}
  171.         CLIENT.__index = CLIENT
  172.  
  173.         function CLIENT:Initialize()
  174.             self.Buffer = {}
  175.         end
  176.  
  177.         function CLIENT:__tostring()
  178.             return string.format("client_%s[%s][%s]", self.socket_type, self:GetIP() or "none", self:GetPort() or "0")
  179.         end
  180.  
  181.         function CLIENT:DebugPrintf(fmt, ...)
  182.             if luasocket.debug then
  183.                 nospam_printf("%s - " .. fmt, self, ...)
  184.             end
  185.         end
  186.  
  187.         function CLIENT:Connect(ip, port)
  188.             check(ip, "string")
  189.             check(port, "number")
  190.  
  191.             self.socket:settimeout(0)
  192.             if self.socket_type == "tcp" then
  193.                 self.socket:connect(ip, port)
  194.             else
  195.                 self.socket:setpeername(ip, port)
  196.             end
  197.             self.socket:settimeout(0)
  198.  
  199.             self.connecting = true
  200.         end
  201.  
  202.         function CLIENT:Send(str)
  203.             if self.socket_type == "tcp" then
  204.                 table.insert(self.Buffer, str)
  205.             else
  206.                 self.socket:send(str)
  207.             end
  208.         end
  209.  
  210.         function CLIENT:Think()
  211.             local sock = self.socket
  212.             sock:settimeout(0)
  213.  
  214.             if self.connecting then
  215.                 local res, err = sock:getpeername()
  216.                 if res then
  217.                     -- ip, port = res, err
  218.  
  219.                     self.connected = true
  220.                     self.connecting = nil
  221.                     if self.OnConnect then
  222.                         self:OnConnect(res, err)
  223.                     end
  224.  
  225.                     self:DebugPrintf("connected to %s:%s", res, err)
  226.  
  227.                     self.Timeouts = 0
  228.                 elseif err == "timeout" or err == "getpeername failed" then
  229.                     self:Timeout()
  230.                 else
  231.                     if self.OnError then
  232.                         self:OnError(err)
  233.                     end
  234.                     self.connecting = nil
  235.                     self:DebugPrintf("errored: %s", err)
  236.                 end
  237.             end
  238.  
  239.             if self.socket_type == "tcp" and self.connected then
  240.                 local data = self.Buffer[1]
  241.                 if data then
  242.                     local bytes,b,c,d = sock:send(data)
  243.  
  244.                     if bytes then
  245.                         if self.OnSend then
  246.                             self:OnSend(data, bytes, b,c,d)
  247.                         end
  248.                         self:DebugPrintf("sucessfully sent %q", data)
  249.                         table.remove(self.Buffer, 1)
  250.                     end
  251.                 end
  252.  
  253.                 local data, err, partial = sock:receive()
  254.  
  255.                 if data then
  256.                     if self.OnReceive then
  257.                         self:OnReceive(data)
  258.                     end
  259.                     self:DebugPrintf("received %q", data)
  260.                     self.Timeouts = 0
  261.                 elseif err == "timeout" then
  262.                     self:Timeout()
  263.                 elseif err == "Socket is not connected" then
  264.                     --self.connected = false
  265.                     --self.connecting = true
  266.                 elseif err == "closed" then
  267.                     self:DebugPrintf("wants to close", client)
  268.                     if self.OnClose then
  269.                         self:OnClose()
  270.                     end
  271.                 elseif self.OnError then
  272.                     self:OnError(err)
  273.                     self:DebugPrintf("errored: %s", err)
  274.                 end
  275.             end
  276.         end
  277.  
  278.         CLIENT.MaxTimeouts = 100
  279.         CLIENT.Timeouts = 0
  280.  
  281.         function CLIENT:Timeout()
  282.             if not self.OnTimeout or self:OnTimeout(self.Timeouts) ~= false then
  283.                 self.Timeouts = self.Timeouts + 1
  284.                 if self.Timeouts > self.MaxTimeouts then
  285.                     if self.OnClose then
  286.                         self:OnClose()
  287.                     else
  288.                         self:Remove()
  289.                     end
  290.                 end
  291.             end
  292.         end
  293.  
  294.         function CLIENT:SetMaxTimeouts(num)
  295.             self.MaxTimeouts = num
  296.         end
  297.  
  298.         function CLIENT:Remove()
  299.             remove_socket(self)
  300.         end
  301.  
  302.         function CLIENT:OnClose()
  303.             self:Remove()
  304.         end
  305.  
  306.         function CLIENT:IsConnected()
  307.             return self.connected == true
  308.         end
  309.  
  310.         function CLIENT:IsSending()
  311.             return #self.Buffer > 0
  312.         end
  313.  
  314.         function CLIENT:GetIP()
  315.             local ip, port = self.socket:getpeername()
  316.             return ip
  317.         end
  318.  
  319.         function CLIENT:GetPort()
  320.             local ip, port = self.socket:getpeername()
  321.             return ip and port or nil
  322.         end
  323.  
  324.         function CLIENT:IsValid()
  325.             return true
  326.         end
  327.  
  328.         function luasocket.Client(typ)
  329.             return new_socket(nil, CLIENT, typ)
  330.         end
  331.  
  332.         luasocket.ClientMeta = CLIENT
  333.     end
  334.  
  335.     do -- server
  336.         local SERVER = {}
  337.         SERVER.__index = SERVER
  338.  
  339.         function SERVER:Initialize()
  340.             self.Clients = {}
  341.         end
  342.  
  343.         function SERVER:__tostring()
  344.             return string.format("server_%s[%s][%s]", self.socket_type, self:GetIP() or "nil", self:GetPort() or "nil")
  345.         end
  346.  
  347.         function SERVER:DebugPrintf(fmt, ...)
  348.             if luasocket.debug then
  349.                 printf("%s - " .. fmt, self, ...)
  350.             end
  351.         end
  352.  
  353.         function SERVER:GetClients()
  354.             local copy = {}
  355.             for key, client in pairs(self.Clients) do
  356.                 if client.IsValid and client:IsValid() then
  357.                     table.insert(copy, client)
  358.                 else
  359.                     table.remove(self.Clients, key)
  360.                 end
  361.             end
  362.             return copy
  363.         end
  364.  
  365.         function SERVER:Host(ip, port)
  366.             ip = ip or "*"
  367.             port = port or 0
  368.  
  369.             if self.socket_type == "tcp" then
  370.                 self.socket:settimeout(0)
  371.                 self.socket:setoption("reuseaddr", true)
  372.                 self.socket:bind(ip, port)
  373.                 self.socket:listen()
  374.                 self.socket:settimeout(0)
  375.                 self.ready = true
  376.             elseif self.socket_type == "udp" then
  377.                 self.socket:settimeout(0)
  378.                 self.socket:setsockname(ip, port)
  379.                 self.socket:settimeout(0)
  380.                 self.ready = true
  381.             end
  382.         end
  383.  
  384.         function SERVER:Send(data, ip, port)
  385.             check(ip, "string")
  386.  
  387.             if self.socket_type == "tcp" then
  388.                 for key, client in pairs(self:GetClients()) do
  389.                     if client:GetIP() == ip and (not port or (port == client:GetPort())) then
  390.                         client:Send(data)
  391.                         break
  392.                     end
  393.                 end
  394.             elseif self.socket_type == "udp" then
  395.                 check(port, "number")
  396.                 self.socket:sendto(data, ip, port)
  397.             end
  398.         end
  399.  
  400.         SERVER.Bind = SERVER.Host
  401.  
  402.         function SERVER:Think()
  403.             if not self.ready then return end
  404.  
  405.             if self.socket_type == "udp" then
  406.                 local data, ip, port = self.socket:receivefrom()
  407.  
  408.                 if data then
  409.                     if self.OnReceive then
  410.                         self:OnReceive(data, {GetIP = function() return ip end, GetPort = function() return port end})
  411.                     end
  412.  
  413.                     self:DebugPrintf("received %s from %s:%s", data, ip, port)
  414.                 elseif ip ~= "timeout" then
  415.                     self:DebugPrintf("%s errored: ", client, err)
  416.                 end
  417.             elseif self.socket_type == "tcp" then
  418.                 local sock = self.socket
  419.                 sock:settimeout(0)
  420.  
  421.                 local ls_client, err = sock:accept()
  422.  
  423.                 if ls_client then
  424.                     local client = new_socket(ls_client, luasocket.ClientMeta, "tcp")
  425.                     client.connected = true
  426.                     table.insert(self.Clients, client)
  427.  
  428.                     if self.OnClientConnected then
  429.                         if self:OnClientConnected(client, client:GetIP(), client:GetPort()) == false then
  430.                             client:Remove()
  431.                         end
  432.                     end
  433.                     self:DebugPrintf("%s connected", client)
  434.                 end
  435.  
  436.                 for _, client in pairs(self:GetClients()) do
  437.                     local data, err, partial = client.socket:receive()
  438.                     if data then
  439.  
  440.                         if self.OnReceive then
  441.                             self:OnReceive(data, client)
  442.                         end
  443.  
  444.                         if client.OnSend then
  445.                             client:OnSend(data)
  446.                         end
  447.  
  448.                         self:DebugPrintf("received %s from %s", data, client)
  449.  
  450.                     elseif err == "closed" then
  451.                         self:DebugPrintf("%s wants to close", client)
  452.                         if not self.OnClientClose or self:OnClientClose(client) ~= false then
  453.                             if client.OnClose then
  454.                                 client:OnClose()
  455.                             else
  456.                                 client:Remove()
  457.                             end
  458.                         end
  459.                     elseif err == "timeout" then
  460.                         --client:Timeout() -- hmm
  461.                     else
  462.                         if self.OnClientError then
  463.                             self:OnClientError(client, err)
  464.                         end
  465.                         if client.OnError then
  466.                             client:OnError(err)
  467.                         end
  468.                         self:DebugPrintf("%s errored: ", client, err)
  469.                     end
  470.                 end
  471.             end
  472.         end
  473.  
  474.         function SERVER:Remove()
  475.             remove_socket(self)
  476.         end
  477.  
  478.         function SERVER:IsValid()
  479.             return true
  480.         end
  481.  
  482.         function SERVER:GetIP()
  483.             local ip, port = self.socket:getsockname()
  484.             return ip
  485.         end
  486.  
  487.         function SERVER:GetPort()
  488.             local ip, port = self.socket:getsockname()
  489.             return ip and port or nil
  490.         end
  491.  
  492.         function luasocket.Server(typ)
  493.             return new_socket(nil, SERVER, typ)
  494.         end
  495.  
  496.         luasocket.ServerMeta = SERVER
  497.     end
  498.  
  499.     do return end
  500.  
  501.     timer.Simple(1, function()
  502.         print("go")
  503.  
  504.         do -- UDP
  505.             local server = luasocket.Server("udp")
  506.             server:Host("10.0.0.1", 888)
  507.  
  508.             function server:OnReceive(data, client)
  509.                 server:Send("hi", client:GetIP())
  510.             end
  511.  
  512.             local client = luasocket.Client("udp")
  513.                 client:Connect("10.0.0.1", 888)
  514.                 client:Send("hello")
  515.                 client:Send("hello")
  516.                 client:Send("hello")
  517.                 client:Send("hello")
  518.         end
  519.  
  520.         do -- TCP
  521.             local server = luasocket.Server("tcp")
  522.                 server:Host("10.0.0.1", 555)
  523.  
  524.                 function server:OnReceieve(data, client)
  525.                     self:Send("hi", client:GetIP())
  526.                 end
  527.  
  528.             local client = luasocket.Client("tcp")
  529.                 client:Connect("10.0.0.1", 555)
  530.                 client:Send("hello\n")
  531.                 client:Send("hello\n")
  532.                 client:Send("hello\n")
  533.                 client:Send("hello\n")
  534.         end
  535.     end)
  536.  
  537. end
Advertisement
Add Comment
Please, Sign In to add comment