Skip to content

Commit

Permalink
AI chat: Attempt to allow for https endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
mostlikely4r committed Nov 26, 2024
1 parent 0bc3bde commit 8cda754
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
3 changes: 2 additions & 1 deletion playerbot/PlayerbotAIConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ inline ParsedUrl parseUrl(const std::string& url) {

ParsedUrl parsed;
parsed.hostname = match[2];
parsed.port = match[4].length() ? std::stoi(match[4]) : 80;
parsed.https = match[1] == "https";
parsed.port = parsed.https ? 443 : (match[4].length() ? std::stoi(match[4]) : 80);
parsed.path = match[5].length() ? match[5] : std::string("/");
return parsed;
}
Expand Down
1 change: 1 addition & 0 deletions playerbot/PlayerbotAIConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ struct ParsedUrl {
std::string hostname;
std::string path;
int port;
bool https;
};

class PlayerbotAIConfig
Expand Down
54 changes: 52 additions & 2 deletions playerbot/PlayerbotLLMInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <sstream>
#include <regex>
#include <chrono>
#include <openssl/ssl.h>
#include <openssl/err.h>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
Expand Down Expand Up @@ -214,6 +216,48 @@ std::string PlayerbotLLMInterface::Generate(const std::string& prompt, std::vect

freeaddrinfo(res);

SSL_CTX* ctx = nullptr;
SSL* ssl = nullptr;

if (parsedUrl.https)
{
// Initialize OpenSSL
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
const SSL_METHOD* method = TLS_client_method();
ctx = SSL_CTX_new(method);
if (!ctx) {
if (debug)
debugLines.push_back("Failed to create SSL context");
sLog.outError("BotLLM: Failed to create SSL context");
#ifdef _WIN32
closesocket(sock);
WSACleanup();
#else
close(sock);
#endif
return "";
}

ssl = SSL_new(ctx);
SSL_set_fd(ssl, sock);
if (SSL_connect(ssl) <= 0) {
if (debug)
debugLines.push_back("SSL connection failed");
sLog.outError("BotLLM: SSL connection failed");
SSL_free(ssl);
SSL_CTX_free(ctx);
#ifdef _WIN32
closesocket(sock);
WSACleanup();
#else
close(sock);
#endif
return "";
}
}

std::ostringstream request;
request << "POST " << parsedUrl.path << " HTTP/1.1\r\n";
request << "Host: " << parsedUrl.hostname << "\r\n";
Expand All @@ -228,11 +272,17 @@ std::string PlayerbotLLMInterface::Generate(const std::string& prompt, std::vect
if (debug)
debugLines.push_back("Send the request" + request.str());

if (send(sock, request.str().c_str(), request.str().size(), 0) < 0) {
bool write = parsedUrl.https ? (SSL_write(ssl, request.str().c_str(), request.str().size()) <= 0) : (send(sock, request.str().c_str(), request.str().size(), 0) < 0);
if (write) {
if (debug)
debugLines.push_back("Failed to send request");

sLog.outError("BotLLM: Failed to send request");

if (parsedUrl.https)
{
SSL_free(ssl);
SSL_CTX_free(ctx);
}
#ifdef _WIN32
closesocket(sock);
WSACleanup();
Expand Down

0 comments on commit 8cda754

Please sign in to comment.