Skip to content

Commit

Permalink
ssl扩展
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed Apr 11, 2024
1 parent 8e165e4 commit b0dd36d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 29 deletions.
3 changes: 1 addition & 2 deletions core/luabus/src/socket_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,7 @@ void socket_stream::do_send(size_t max_len, bool is_eof) {
}
}

void socket_stream::do_recv(size_t max_len, bool is_eof)
{
void socket_stream::do_recv(size_t max_len, bool is_eof) {
size_t total_recv = 0;
while (total_recv < max_len && m_link_status == elink_status::link_connected) {
auto* space = m_recv_buffer->peek_space(SOCKET_RECV_LEN);
Expand Down
20 changes: 10 additions & 10 deletions extend/lssl/src/ssl/lssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,13 @@ namespace lssl {
m_hcodec = codec;
}

int init_tls(lua_State* L, bool is_server = false) {
int init_tls(lua_State* L, bool is_client) {
ctx = SSL_CTX_new(SSLv23_method());
if (!ctx) {
char buf[256];
ERR_error_string_n(ERR_get_error(), buf, sizeof(buf));
luaL_error(L, "SSL_CTX_new faild. %s\n", buf);
}
ssl = SSL_new(ctx);
if (!ssl) luaL_error(L, "SSL_new faild");
in_bio = BIO_new(BIO_s_mem());
Expand All @@ -218,16 +224,10 @@ namespace lssl {
BIO_set_mem_eof_return(in_bio, -1);
BIO_set_mem_eof_return(out_bio, -1);
SSL_set_bio(ssl, in_bio, out_bio);
ctx = SSL_CTX_new(SSLv23_method());
if (!ctx) {
char buf[256];
ERR_error_string_n(ERR_get_error(), buf, sizeof(buf));
luaL_error(L, "SSL_CTX_new faild. %s\n", buf);
}
if (is_server) {
SSL_set_accept_state(ssl);
} else {
if (is_client) {
SSL_set_connect_state(ssl);
} else {
SSL_set_accept_state(ssl);
}
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion script/driver/socket.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ end
function Socket:send_data(...)
if self.alive then
local send_len = self.session.call_data(...)
return send_len > 0
return send_len >= 0
end
return false, "socket not alive"
end
Expand Down
53 changes: 37 additions & 16 deletions script/network/http_client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,24 @@ function HttpClient:on_hour()
end

function HttpClient:on_socket_recv(socket, proto, ...)
log_debug("[HttpClient][on_socket_recv] client(proto:{}) message({})!", proto, {...})
if proto == "TLS" then
self:on_handshake(socket.token, ...)
end
return self:on_http_recv(socket.token, ...)
end

function HttpClient:on_handshake(token, message)
function HttpClient:on_handshake(token, is_handshake, message)
log_debug("[HttpClient][on_handshake] client(token:{}) message({})!", token, message)
local client = self.clients[token]
if client and message then
if not client then
return nil, "Illegal socket!"
end
if is_handshake then
thread_mgr:response(client.session_id, true, client)
return
end
if message then
client:send_data(message)
end
end
Expand All @@ -78,20 +87,9 @@ function HttpClient:send_request(url, timeout, querys, headers, method, datas)
log_err("[HttpClient][send_request] failed : {}", port)
return false, port
end
local socket = Socket(self)
local ok, cerr = socket:connect(host, port, proto_text)
if not ok then
return false, cerr
end
if proto == "https" then
local codec = tlscodec(self.hcodec)
if not codec then
return false, "tls codec create failed!"
end
codec:init_tls()
socket:set_codec(codec)
else
socket:set_codec(self.hcodec)
local socket, err = self:init_http_socket(host, port, proto)
if not socket then
return false, err
end
if not headers then
headers = {["Content-Type"] = "text/plain" }
Expand All @@ -107,6 +105,28 @@ function HttpClient:send_request(url, timeout, querys, headers, method, datas)
return thread_mgr:yield(session_id, url, timeout or HTTP_TIMEOUT)
end

function HttpClient:init_http_socket(host, port, proto)
local socket = Socket(self)
local ok, cerr = socket:connect(host, port, proto_text)
if not ok then
return nil, cerr
end
if proto == "https" then
local codec = tlscodec(self.hcodec)
if not codec then
return nil, "tls codec create failed!"
end
codec:init_tls()
socket:set_codec(codec)
socket:send_data()
local session_id = thread_mgr:build_session_id()
socket.session_id = session_id
return thread_mgr:yield(session_id, host, HTTP_TIMEOUT)
end
socket:set_codec(self.hcodec)
return socket
end

--get接口
function HttpClient:call_get(url, querys, headers, datas, timeout)
return self:send_request(url, timeout, querys, headers, "GET", datas)
Expand Down Expand Up @@ -163,6 +183,7 @@ function HttpClient:parse_url_addr(url)
local ip = self.domains[host]
if not ip then
local ips = busdns(host)
log_debug("[HttpClient][parse_url_addr] host:{} ips {}!", host, ips)
if not ips or #ips == 0 then
return nil, "ip addr parse failed!"
end
Expand Down

0 comments on commit b0dd36d

Please sign in to comment.