lua https request 调用
网上资料
引用ssl.https 包
local https = require("ssl.https")
之后按同http一样调用。
但是,这种只最基本的实现了访问https服务的要求,却没有验证数字证书的示例说明。
数字证书的调用
类似
wget --private-key /root/client.key --certificate /root/client.crt --ca-certificate /root/ca.crt https://www.test.com -O wgetssl curl --key /root/client.key --cert /root/client.crt --cacert /root/ca.crt https://www.test.com
必须要传入证书文件
再搜 资料很少
最有用的是
http://notebook.kulchenko.com/programming/https-ssl-calls-with-lua-and-luasec
讲的是socket 建立连接,和https差了一层。
就差一步了……
再也没找到任何资料。
所以,查lua包源码
https的部分内容
local ssl = require("ssl")
function request(url, body)
local result_table = {}
local stringrequest = type(url) == "string"
if stringrequest then
url = urlstring_totable(url, body, result_table)
else
url.url = default_https_port(url.url)
end
if http.PROXY or url.proxy then
return nil, "proxy not supported"
elseif url.redirect then
return nil, "redirect not supported"
elseif url.create then
return nil, "create function not permitted"
end
-- New 'create' function to establish a secure connection
url.create = tcp(url)
local res, code, headers, status = http.request(url)
if res and stringrequest then
return table.concat(result_table), code, headers, status
end
return res, code, headers, status
end
-- Return a function which performs the SSL/TLS connection.
local function tcp(params)
params = params or {}
-- Default settings
for k, v in pairs(cfg) do
params[k] = params[k] or v
end
-- Force client mode
params.mode = "client"
-- 'create' function for LuaSocket
return function ()
local conn = {}
conn.sock = try(socket.tcp())
local st = getmetatable(conn.sock).__index.settimeout
function conn:settimeout(...)
return st(self.sock, ...)
end
-- Replace TCP's connection function
function conn:connect(host, port)
try(self.sock:connect(host, port))
self.sock = try(ssl.wrap(self.sock, params))
try(self.sock:dohandshake())
reg(self, getmetatable(self.sock))
return
end
return conn
end
end
https.request
url.create = tcp(url)
会调用tcp函数。
params = params or {}
-- Default settings
for k, v in pairs(cfg) do
params[k] = params[k] or v
end
self.sock = try(ssl.wrap(self.sock, params))
而tcp函数又用requset传入的参数创建名为 params的table类对象,传入params调用ssl.warp函数
好吧,再去ssl函数看源码
function newcontext(cfg)
local succ, msg, ctx
-- Create the context
ctx, msg = context.create(cfg.protocol)
if not ctx then return nil, msg end
-- Mode
succ, msg = context.setmode(ctx, cfg.mode)
if not succ then return nil, msg end
-- Load the key
if cfg.key then
succ, msg = context.loadkey(ctx, cfg.key, cfg.password)
if not succ then return nil, msg end
end
-- Load the certificate
if cfg.certificate then
succ, msg = context.loadcert(ctx, cfg.certificate)
if not succ then return nil, msg end
end
-- Load the CA certificates
if cfg.cafile or cfg.capath then
succ, msg = context.locations(ctx, cfg.cafile, cfg.capath)
if not succ then return nil, msg end
end
-- Set the verification options
succ, msg = optexec(context.setverify, cfg.verify, ctx)
if not succ then return nil, msg end
-- Set SSL options
succ, msg = optexec(context.setoptions, cfg.options, ctx)
if not succ then return nil, msg end
-- Set the depth for certificate verification
if cfg.depth then
succ, msg = context.setdepth(ctx, cfg.depth)
if not succ then return nil, msg end
end
return ctx
end
--
--
--
function wrap(sock, cfg)
local ctx, msg
if type(cfg) == "table" then
ctx, msg = newcontext(cfg)
if not ctx then return nil, msg end
else
ctx = cfg
end
local s, msg = core.create(ctx)
if s then
core.setfd(s, sock:getfd())
sock:setfd(core.invalidfd)
return s
end
return nil, msg
end
眼前一亮,看到熟悉的证书参数了,key,password,ca...
懂的看代码就该如何作了。
首先wrap调用newcontext
而newcontext应用 之前request传入的参数
那把key,password,ca等,写入https.request就全通了。
写demo
测试通过。
不传证书
#!/usr/bin/lua
require("socket")
local https = require("ssl.https") local one, code, headers, status = https.request{
url = "https://www.test.com"
}
print(code)
print(header)
print(status)
print(one)
结果为
root@LeWiFi:~# lua luahttps.test
nil
nil
nil
nil
传入证书
#!/usr/bin/lua
require("socket")
local https = require("ssl.https")
local one, code, headers, status = https.request{
url = "https://www.test.com",
key = "/root/client.key",
certificate="/root/client.crt",
cafile="/root/ca.crt"
}
print(code)
print(header)
print(status)
print(one)
结果
root@LeWiFi:~# lua luahttps.test nil
HTTP/1.1 OK
success
lua https request 调用的更多相关文章
- tengine lua 开源一 调用内部接口高效发送文件
tengine lua 开源一 调用内部接口高效发送文件 开源自己封装的sendfile 模块,可以高效的通过lua发送文件 源码地址:https://github.com/weinyzhou/Lu ...
- C程序与Lua脚本相互调用
Lua脚本是一种可用于C程序开发/测试的工具,本篇介绍一下C程序与Lua脚本如何进行相互调用,更加详细的操作参见<Programing in Lua>.本文分为3个部分:1.Windows ...
- 使用soapUI代替WSDL2JAVA生成cxf HTTPS 客户端调用代码
如果直接用cxf下面的wsdl2java生成https服务调用代码,会报https证书的错误.在你不想导入证书的情况下,可以使用soapUI进行客户端代码的生成,步骤如下: 1.设置CXF,如下图: ...
- InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised.解决办法
最近使用requests进行get请求的时候,控制台输出如下错误. InsecureRequestWarning: Unverified HTTPS request is being made. Ad ...
- ubuntu配置lua环境,并进行c与lua的相互调用
1.安装lua环境 先查看一下apt可获取的lua版本 我们选择lua5.1版本进行安装 sudo apt install lua5.1 安装完之后测试一下是否安装成功,如果可以正常使用,则lua环境 ...
- InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings In
InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is s ...
- pytest文档46-关于https请求警告问题(InsecureRequestWarning: Unverified HTTPS request is being made)
前言 使用 pytest 执行 https 请求用例的时候,控制台会出现警告:InsecureRequestWarning: Unverified HTTPS request is being mad ...
- (原)lua使用ffi调用c程序的函数
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5812763.html 参考网址: http://luajit.freelists.narkive.co ...
- openresty+lua做接口调用权限限制
说明:openresty可以理解为一个服务器它将nginx的核心包含了过来,并结合lua脚本语言实现一些对性能要求高的功能,该篇文章介绍了使用openresty 1.purview.lua --调用j ...
随机推荐
- UVALive 2678 利用序列的前缀来减少时间复杂度
题意很简单,在一串正整数序列中找一个连续的子序列使该序列和大于等于一个已知量S,但要求序列长度最短,通常喜欢暴力枚举 这个题目跟大白书之前的一个题目很像,在数列A中 求 Ai-Aj最大 并且 i< ...
- java反射使用详细例子
1. 概念 反射,一种计算机处理方式.是程序可以访问.检测和修改它本身状态或行为的一种能力. 2. 反射机制的作用 通过反机制访问java类的属性,方法,构造方法等: 3.反射机制中的类 (1) ja ...
- 计量经济与时间序列_ACF与PACF标准差(均标准误)的计算(含代码)
1 我们对于acf和pacf值计算完毕之后,在需要计算两个数值的标准差. 2 acf和pacf的标准差计算略有不同.acf的标准差是一个移动过程,而pacf是一个相对固定过程. 3 我们继 ...
- Django知识点_梳理
- Python基础学习四
Python基础学习四 1.内置函数 help()函数:用于查看内置函数的用途. help(abs) isinstance()函数:用于判断变量类型. isinstance(x,(int,float) ...
- 关于汽车诊断OBD的理解(ISO15031-5)(转发)
1.OBD用来做什么 2.OBD和UDS的区别 3.OBD硬件接口简介 4.OBD的9大模式介绍 OBD(On-Board Diagnostic)指的是在线诊断系统,是汽车上的一种用于监控车辆状况以及 ...
- 论文翻译——Dynamic Pooling and Unfolding Recursive Autoencoders for Paraphrase Detection
Dynamic Pooling and Unfolding Recursive Autoencoders for Paraphrase Detection 动态池和展开递归自动编码器的意译检测 论文地 ...
- linux上一些常用的命令
1.时时查看tomcat启动信息,首先切换到tomcat的logs目录下 tail -f catalina.out 2.在tomcat的bin目录下启动无权限,需要赋权 chmod 777 *.sh ...
- 嵌入式c语言编码规范
学习嵌入式的同学应该首先掌握嵌入式编码规范,这样才能更好的嵌入式系统. 下面就从这几个方面讲解一下嵌入式c编码规范. 注释风格.排版风格.头文件风格.变量定义.宏定义.函数 1 注释风格 1.1 注 ...
- fatal: remote origin already exists.
解决方法: 先删除, 再添加 1. git remote rm origin 2. git remote add origin https://github.com/zjulanjian/eshop. ...