Skip to content

Commit

Permalink
Added unix socket support
Browse files Browse the repository at this point in the history
  • Loading branch information
lanstat committed May 14, 2024
1 parent bcc0fd3 commit ea92f99
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 12 deletions.
26 changes: 22 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,21 @@ void ParserArguments(int argc, char** argv) {
Settings::CacheDir = tmp.substr(11);
continue;
}
if (strcmp(argv[i], "-ipv6") == 0) {
Settings::IPv6Mode = true;
if (memcmp(argv[i], "-listen-mode=", 13) == 0) {
std::string tmp(argv[i]);
std::string mode = tmp.substr(13);
if (mode == "unix") {
Settings::ListenMode = 3;
} else if (mode == "ipv6") {
Settings::ListenMode = 2;
} else {
Settings::ListenMode = 1;
}
continue;
}
if (memcmp(argv[i], "-unix-path=", 11) == 0) {
std::string tmp(argv[i]);
Settings::UnixPath = tmp.substr(11);
continue;
}
if (memcmp(argv[i], "-proxy=", 7) == 0) {
Expand Down Expand Up @@ -107,8 +120,13 @@ int main(int argc, char** argv) {
engine_ = new Engine();
engine_->SetupListeningSocket(Settings::ServerPort);

Log(__FILE__, __LINE__)
<< "Started server on localhost:" << Settings::ServerPort;
if (Settings::ListenMode == 3) {
Log(__FILE__, __LINE__)
<< "Started server on " << Settings::UnixPath;
} else {
Log(__FILE__, __LINE__)
<< "Started server on localhost:" << Settings::ServerPort;
}

signal(SIGINT, SigIntHandler);
signal(SIGPIPE, SIG_IGN);
Expand Down
54 changes: 49 additions & 5 deletions src/Engine.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include "Engine.hpp"

#include "EventType.hpp"
#include "HttpClient.hpp"
#include <sys/socket.h>
#include <sys/un.h>

#include "AstraHttpClient.hpp"
#include "EventType.hpp"
#include "HLSStream.hpp"
#include "HttpClient.hpp"
#include "HttpsClient.hpp"
#include "Logger.hpp"
#include "Settings.hpp"
Expand Down Expand Up @@ -67,7 +70,7 @@ void PrintRequestType(int type) {
type_str = "EVENT_TYPE_CACHE_CLOSE";
break;
}
Log(__FILE__, __LINE__, Log::kDebug) << type_str;
Log(__FILE__, __LINE__, Log::kDebug) << "[" << type << "] " << type_str;
}

Engine::Engine() {
Expand Down Expand Up @@ -118,8 +121,10 @@ void Engine::FatalError(const char *syscall) {
* the web server.
* */
void Engine::SetupListeningSocket(int port) {
if (Settings::IPv6Mode) {
if (Settings::ListenMode == 2) {
ListenIpv6(port);
} else if (Settings::ListenMode == 3) {
ListenUnixSocket();
} else {
ListenIpv4(port);
}
Expand Down Expand Up @@ -158,6 +163,43 @@ void Engine::ListenIpv4(int port) {
socket_ = sock;
}

void Engine::ListenUnixSocket() {
int sock;
struct sockaddr_un srv_addr;

sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1) {
FatalError("socket()");
}

srv_addr.sun_family = AF_UNIX;
strcpy(srv_addr.sun_path, Settings::UnixPath.c_str());
unlink(srv_addr.sun_path);
int len = strlen(srv_addr.sun_path) + sizeof(srv_addr.sun_family);

if (bind(sock, (const struct sockaddr *)&srv_addr, len) != 0) {
FatalError("bind()");
}

if (listen(sock, 4096) < 0) {
FatalError("listen()");
}

int flags, s;
flags = fcntl(sock, F_GETFL, 0);
if (flags == -1) {
FatalError("FCNTL get flags error");
} else {
flags |= O_NONBLOCK;
s = fcntl(sock, F_SETFL, flags);
if (s == -1) {
FatalError("FCNTL set flags error");
}
}

socket_ = sock;
}

void Engine::ListenIpv6(int port) {
int sock;
struct sockaddr_in6 srv_addr;
Expand Down Expand Up @@ -238,7 +280,7 @@ void Engine::Run() {

request->is_processing = false;

if (request->event_type != EVENT_TYPE_DNS_VERIFY) {
if (request->event_type != EVENT_TYPE_DNS_VERIFY && request->event_type != EVENT_TYPE_CACHE_CLEAN) {
PrintRequestType(request->event_type);
}

Expand All @@ -260,10 +302,12 @@ void Engine::Run() {
} break;
case EVENT_TYPE_SERVER_WRITE_COMPLETE:
server_->HandleWrite(request, response);
break;
case EVENT_TYPE_SERVER_WRITE_PARTIAL:
// If failed to write to socket
if (server_->HandleWriteStream(request, response) == 1) {
stream_->RemoveRequest(request);

Utils::ReleaseRequest(request);
Log(__FILE__, __LINE__) << "Remove client closed";
} else {
Expand Down
1 change: 1 addition & 0 deletions src/Engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ class Engine {

void ListenIpv4(int port);
void ListenIpv6(int port);
void ListenUnixSocket();
};
#endif
4 changes: 3 additions & 1 deletion src/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ bool Settings::HLSMode = false;

bool Settings::AstraMode = false;

bool Settings::IPv6Mode = false;
int Settings::ListenMode = 1; // 1=IPv4 2=IPv6 3=unix

std::string Settings::Proxy = "";

std::string Settings::BaseUrl = "";

std::string Settings::HostFile = "";

std::string Settings::UnixPath = "/tmp/cdn.sock";
3 changes: 2 additions & 1 deletion src/Settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ class Settings {
static int StreamingBufferSize;
static bool HLSMode;
static bool AstraMode;
static bool IPv6Mode;
static int ListenMode;
static std::string Proxy;
static std::string BaseUrl;
static std::string UnixPath;
static std::string HostFile;
};
#endif
4 changes: 4 additions & 0 deletions src/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ void Stream::ReleaseResource(uint64_t resource_id) {
int Stream::RemoveRequest(struct Request *request) {
auto resource_id = request->resource_id;

if (resources_.find(resource_id) == resources_.end()) {
return 1;
}

struct Mux *mux = resources_.at(resource_id);
std::vector<struct Request *> requests = mux->requests;
int pointer = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct Request *Utils::HttpEntryRequest() { return CreateRequest(3); }
*
* 0 = char* bytes with the error response
*/
struct Request *Utils::HttpErrorRequest() { return CreateRequest(1); }
struct Request *Utils::HttpErrorRequest() { return CreateRequest(1, 100); }

/*
* Object for http external request
Expand Down Expand Up @@ -137,6 +137,7 @@ void Utils::ReleaseRequest(struct Request *request) {
free(request->iov[i].iov_base);
}
}
std::cout<< "LAN_[" << __FILE__ << ":" << __LINE__ << "] "<< request->event_type << std::endl;
free(request);
}

Expand Down

0 comments on commit ea92f99

Please sign in to comment.