Skip to content

Commit

Permalink
lssl添加
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed Apr 11, 2024
1 parent b0dd36d commit 9f30dc3
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 63 deletions.
2 changes: 1 addition & 1 deletion core/luabus/src/lua_socket_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int lua_socket_node::call_data(lua_State* L) {
if (m_codec) {
size_t data_len = 0;
char* data = (char*)m_codec->encode(L, 1, &data_len);
if (data_len <= SOCKET_PACKET_MAX){
if (data_len > 0 && data_len <= SOCKET_PACKET_MAX){
m_mgr->send(m_token, data, data_len);
lua_pushinteger(L, data_len);
return 1;
Expand Down
6 changes: 3 additions & 3 deletions core/luabus/src/socket_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void socket_stream::stream_send(const char* data, size_t data_len)
while (data_len > 0) {
int send_len = ::send(m_socket, data, (int)data_len, 0);
if (send_len == 0) {
on_error("connection-lost");
on_error("connection-send-lost");
return;
}
if (send_len == SOCKET_ERROR) {
Expand Down Expand Up @@ -369,7 +369,7 @@ void socket_stream::do_send(size_t max_len, bool is_eof) {
return;
}
if (send_len == 0) {
on_error("connection-lost");
on_error("connection-send-lost");
return;
}
total_send += send_len;
Expand Down Expand Up @@ -411,7 +411,7 @@ void socket_stream::do_recv(size_t max_len, bool is_eof) {
return;
}
if (recv_len == 0) {
on_error("connection-lost");
on_error("connection-recv-lost");
return;
}
total_recv += recv_len;
Expand Down
1 change: 1 addition & 0 deletions extend/lssl/src/ssl/lssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ namespace lssl {
kit_state.new_class<tlscodec>(
"init_tls", &tlscodec::init_tls,
"set_cert", &tlscodec::set_cert,
"isfinish", &tlscodec::isfinish,
"set_ciphers", &tlscodec::set_ciphers
);
return luassl;
Expand Down
95 changes: 50 additions & 45 deletions extend/lssl/src/ssl/lssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,17 @@ namespace lssl {
if (ctx) SSL_CTX_free(ctx);
}



virtual int load_packet(size_t data_len) {
if (!m_slice) return 0;
return data_len;
}

virtual uint8_t* encode(lua_State* L, int index, size_t* len) {
if (!is_handshake) {
return (uint8_t*)lua_tolstring(L, index, len);
uint8_t* data = (uint8_t*)lua_tolstring(L, index, len);
if (*len > 0) bio_write(L, data, *len);
tls_handshake(L);
return m_buf->data(len);
}
size_t slen = 0;
uint8_t* body = m_hcodec->encode(L, index, &slen);
Expand All @@ -184,11 +185,17 @@ namespace lssl {
}

virtual size_t decode(lua_State* L) {
size_t sz = m_slice->size();
if (!is_handshake) {
return handshake(L);
int top = lua_gettop(L);
lua_pushstring(L, "TLS");
lua_push_object(L, this);
lua_pushlstring(L, (const char*)m_slice->head(), sz);
m_slice->erase(sz);
m_packet_len = sz;
return lua_gettop(L) - top;
}
m_buf->clean();
bio_write(L, m_slice->size());
bio_write(L, m_slice->head(), sz);
do {
uint8_t* outbuff = m_buf->peek_space(SSL_TLS_READ_SIZE);
int read = SSL_read(ssl, outbuff, sizeof(SSL_TLS_READ_SIZE));
Expand All @@ -198,12 +205,19 @@ namespace lssl {
ERR_clear_error();
throw lua_exception("SSL_read error:%d", err);
}
m_buf->pop_space(SSL_TLS_READ_SIZE);
m_buf->pop_space(read);
} while (true);
m_slice->erase(sz);
m_packet_len = sz;
m_hcodec->set_slice(m_buf->get_slice());
return m_hcodec->decode(L);
}

bool isfinish() {
is_handshake = SSL_is_init_finished(ssl);
return is_handshake;
}

void set_codec(codec_base* codec) {
m_hcodec = codec;
}
Expand Down Expand Up @@ -253,60 +267,51 @@ namespace lssl {
}

protected:
size_t handshake(lua_State* L) {
int top = lua_gettop(L);
size_t sz = m_slice->size();
void tls_handshake(lua_State* L) {
m_buf->clean();
is_handshake = SSL_is_init_finished(ssl);
lua_pushstring(L, "TLS");
lua_pushinteger(L, is_handshake);
if (!is_handshake) {
bio_write(L, sz);
if (int ret = SSL_do_handshake(ssl) > 0) {
int err = SSL_get_error(ssl, ret);
ERR_clear_error();
throw lua_exception("SSL_do_handshake error:%d ret:%d", err, ret);
} else if (ret < 0) {
int err = SSL_get_error(ssl, ret);
ERR_clear_error();
if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
if (size_t len = bio_read(L) > 0) {
lua_pushlstring(L, (const char*)m_buf->head(), len);
}
}
if (is_handshake) return;
int ret = SSL_do_handshake(ssl);
if (ret == 1) {
return;
}
if (ret < 0) {
bool dshake = SSL_is_init_finished(ssl);
int err = SSL_get_error(ssl, ret);
ERR_clear_error();
if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
bio_read(L);
}
return;
}
m_slice->erase(sz);
return lua_gettop(L) - top;
bool dshake = SSL_is_init_finished(ssl);
int err = SSL_get_error(ssl, ret);
ERR_clear_error();
luaL_error(L, "SSL_do_handshake error:%d ret:%d", err, ret);
}

void bio_write(lua_State* L, size_t sz) {
char* p = (char*)m_slice->head();
void bio_write(lua_State* L, uint8_t* data, size_t sz) {
while (sz > 0) {
int written = BIO_write(in_bio, p, sz);
int written = BIO_write(in_bio, data, sz);
if (written <= 0 || written > sz) {
throw lua_exception("BIO_write error:%d", written);
}
sz -= written;
p += written;
data += written;
}
}

size_t bio_read(lua_State* L) {
void bio_read(lua_State* L) {
int pending = BIO_ctrl_pending(out_bio);
if (pending > 0) {
m_buf->clean();
while (pending > 0) {
uint8_t* outbuff = m_buf->peek_space(SSL_TLS_READ_SIZE);
int read = BIO_read(out_bio, outbuff, SSL_TLS_READ_SIZE);
if (read <= 0 || read > SSL_TLS_READ_SIZE) {
throw lua_exception("BIO_read error:%d", read);
}
m_buf->pop_space(SSL_TLS_READ_SIZE);
pending = BIO_ctrl_pending(out_bio);
while (pending > 0) {
uint8_t* outbuff = m_buf->peek_space(SSL_TLS_READ_SIZE);
int read = BIO_read(out_bio, outbuff, SSL_TLS_READ_SIZE);
if (read <= 0 || read > SSL_TLS_READ_SIZE) {
luaL_error(L, "BIO_read error:%d", read);
}
return m_buf->size();
m_buf->pop_space(read);
pending = BIO_ctrl_pending(out_bio);
}
return 0;
}

protected:
Expand Down
23 changes: 9 additions & 14 deletions script/network/http_client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,20 @@ 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, ...)
return self:on_handshake(socket, ...)
end
return self:on_http_recv(socket.token, ...)
log_debug("[HttpClient][on_socket_recv] client(proto:{}) message({})!", proto, {...})
return self:on_http_recv(socket, ...)
end

function HttpClient:on_handshake(token, is_handshake, message)
log_debug("[HttpClient][on_handshake] client(token:{}) message({})!", token, message)
local client = self.clients[token]
if not client then
return nil, "Illegal socket!"
end
if is_handshake then
thread_mgr:response(client.session_id, true, client)
return
end
function HttpClient:on_handshake(socket, codec, message)
log_debug("[HttpClient][on_handshake] message({})!", #message)
if message then
client:send_data(message)
socket:send_data(message)
end
if codec.isfinish() then
thread_mgr:response(socket.session_id, socket)
end
end

Expand Down

0 comments on commit 9f30dc3

Please sign in to comment.