fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

BkacjiosApplication update checker7e0ab3b

v2.1.3
3.7 KiB146 linesraw
1----------------------------------------------------------------------------
2-- LuaSec 1.2.0
3-- Copyright (C) 2009-2022 PUC-Rio
4--
5-- Author: Pablo Musa
6-- Author: Tomas Guisasola
7---------------------------------------------------------------------------
8
9local socket	= require("socket")
10local ssl		= require("ssl")
11local ltn12		= require("ltn12")
12local http		= require("socket.http")
13local url		= require("socket.url")
14
15local try		= socket.try
16
17--
18-- Module
19--
20local _M = {
21	_VERSION	= "1.2.0",
22	_COPYRIGHT	= "LuaSec 1.2.0 - Copyright (C) 2009-2022 PUC-Rio",
23	PORT	= 443,
24	TIMEOUT	= 60
25}
26
27-- TLS configuration
28local cfg = {
29	protocol	= "any",
30	options		= {"all", "no_sslv2", "no_sslv3", "no_tlsv1"},
31	verify		= "none",
32}
33
34--------------------------------------------------------------------
35-- Auxiliar Functions
36--------------------------------------------------------------------
37
38-- Insert default HTTPS port.
39local function default_https_port(u)
40	return url.build(url.parse(u, {port = _M.PORT}))
41end
42
43-- Convert an URL to a table according to Luasocket needs.
44local function urlstring_totable(url, body, result_table)
45	url = {
46		url = default_https_port(url),
47		method = body and "POST" or "GET",
48		sink = ltn12.sink.table(result_table)
49	}
50	if body then
51		url.source = ltn12.source.string(body)
52		url.headers = {
53			["content-length"] = #body,
54			["content-type"] = "application/json",
55		}
56	end
57	return url
58end
59
60-- Forward calls to the real connection object.
61local function reg(conn)
62	local mt = getmetatable(conn.sock).__index
63	for name, method in pairs(mt) do
64		if type(method) == "function" then
65			conn[name] = function (self, ...)
66				return method(self.sock, ...)
67			end
68		end
69	end
70end
71
72-- Return a function which performs the SSL/TLS connection.
73local function tcp(params)
74	params = params or {}
75	-- Default settings
76	for k, v in pairs(cfg) do 
77		params[k] = params[k] or v
78	end
79	-- Force client mode
80	params.mode = "client"
81	-- 'create' function for LuaSocket
82	return function ()
83		local conn = {}
84		conn.sock = try(socket.tcp())
85		local st = getmetatable(conn.sock).__index.settimeout
86		function conn:settimeout(...)
87			 return st(self.sock, _M.TIMEOUT)
88		end
89		-- Replace TCP's connection function
90		function conn:connect(host, port)
91			 try(self.sock:connect(host, port))
92			 self.sock = try(ssl.wrap(self.sock, params))
93			 self.sock:sni(host)
94			 self.sock:settimeout(_M.TIMEOUT)
95			 try(self.sock:dohandshake())
96			 reg(self)
97			 return 1
98		end
99		return conn
100	end
101end
102
103--------------------------------------------------------------------
104-- Main Function
105--------------------------------------------------------------------
106
107-- Make a HTTP request over secure connection.  This function receives
108--  the same parameters of LuaSocket's HTTP module (except 'proxy' and
109--  'redirect') plus LuaSec parameters.
110--
111-- @param url mandatory (string or table)
112-- @param body optional (string)
113-- @return (string if url == string or 1), code, headers, status
114--
115local function request(url, body)
116	local result_table = {}
117	local stringrequest = type(url) == "string"
118	if stringrequest then
119		url = urlstring_totable(url, body, result_table)
120	else
121		url.url = default_https_port(url.url)
122	end
123	if http.PROXY or url.proxy then
124		return nil, "proxy not supported"
125	elseif url.redirect then
126		return nil, "redirect not supported"
127	elseif url.create then
128		return nil, "create function not permitted"
129	end
130	-- New 'create' function to establish a secure connection
131	url.create = tcp(url)
132	local res, code, headers, status = http.request(url)
133	if res and stringrequest then
134		return table.concat(result_table), code, headers, status
135	end
136	return res, code, headers, status
137end
138
139--------------------------------------------------------------------------------
140-- Export module
141--
142
143_M.request = request
144_M.tcp = tcp
145
146return _M