Skip to content
Snippets Groups Projects
Unverified Commit 8e2c566e authored by Bensong Liu's avatar Bensong Liu
Browse files

working on framework. some trials

parent a4c57edc
No related branches found
No related tags found
No related merge requests found
......@@ -5,5 +5,16 @@
extern rlib::logger rlog;
// epoll buffer size.
constexpr size_t EPOLL_MAX_EVENTS = 32;
// DGRAM packet usually smaller than 1400B.
constexpr size_t DGRAM_BUFFER_SIZE = 20480;
// Change a connection on every n seconds,
// to reset the GFW deep-packet-inspection process.
// ( Only if server side is encrypted, so nothing happens
// to the real openvpn server.
constexpr size_t SERVER_ENCRYPT_CONNECTION_TIMEOUT_SECONDS = 60;
#endif
......@@ -12,7 +12,7 @@ struct ConnectionMapping {
std::unordered_multimap<fd_t, string> server2client;
static string clientInfoAsKey(string ip, uint16_t port) {
// Also works for ipv6. We just want to eliminate duplication, rather than make it easy to read.
return ip + "@" + port;
return ip + '@' + port;
}
};
......
......@@ -6,23 +6,32 @@
using std::string;
namespace Protocols {
class BaseHandler : rlib::noncopyable {
// Handler holds the senderId=>nextHopFd mapping.
// senderId is "$ip@$port", for example, `fe80:8100::1@1080`.
// Misc protocol may use duplicateSenderId to work on port migration.
// Any listener may use removeSenderId to disconnect a sender.
// Note: this interface works for both TCP and UDP.
struct BaseHandler : rlib::noncopyable {
BaseHandler(string outboundConfig) {
loadConfig(outboundConfig);
}
virtual ~BaseOutboundHandler = default;
virtual ~BaseHandler = default;
// Interfaces
virtual loadConfig(string config) = 0;
virtual handleMessage(string binaryMessage) = 0;
virtual void loadConfig(string config) = 0;
virtual void handleMessage(string binaryMessage, string senderId) = 0;
virtual void duplicateSenderId(string newSenderId, string oldSenderId) = 0;
virtual void removeSenderId(string senderId) = 0;
};
class BaseListener : rlib::noncopyable {
struct BaseListener : rlib::noncopyable {
BaseListener(string inboundConfig) {
loadConfig(inboundConfig);
}
virtual loadConfig(string config) = 0;
virtual listenForever(BaseHandler *nextHop) = 0;
virtual ~BaseListener = default;
virtual void loadConfig(string config) = 0;
virtual void listenForever(BaseHandler *nextHop) = 0;
};
}
......
......@@ -16,10 +16,14 @@ namespace Protocols {
// listenPort = ar[2].as<uint16_t>();
psk = ar[3];
// listen these ports.
}
virtual listenForever(BaseHandler* nextHop) override {
// if message arrives:
// send to handler.
}
private:
......
......@@ -4,6 +4,8 @@
#include <protocols/base.hpp>
#include <rlib/sys/sio.hpp>
#include <rlib/string.hpp>
#include <utils.hpp>
#include <common.hpp>
namespace Protocols {
class PlainInboundListener : public BaseListener {
......@@ -12,17 +14,33 @@ namespace Protocols {
auto ar = rlib::string(config).split('@'); // Also works for ipv6.
if (ar.size() != 3)
throw std::invalid_argument("Wrong parameter string for protocol 'plain'. Example: plain@fe00:1e10:ce95:1@10809");
auto listenAddr = ar[1];
auto listenPort = ar[2].as<uint16_t>();
listenAddr = ar[1];
listenPort = ar[2].as<uint16_t>();
listenFd = rlib::quick_listen(listenAddr, listenPort, true);
}
virtual listenForever(BaseHandler* nextHop) override {
auto listenFd = rlib::quick_listen(listenAddr, listenPort, true);
rlib_defer([&] {close(listenFd);});
auto epollFd = epoll_create1(0);
if(epollFd == -1)
throw std::runtime_error("Failed to create epoll fd.");
epoll_add_fd(epollFd, listenFd);
epoll_event events[MAX_EVENTS];
char buffer[DGRAM_BUFFER_SIZE];
// WARN: If you want to modify this program to work for both TCP and UDP, PLEASE use rlib::sockIO::recv instead of fixed buffer.
rlog.info("PlainListener listening [{}]:{} ...", listenAddr, listenPort);
while (true) {
// ...
}
}
private:
fd_t listenFd;
string listenAddr;
uint16_t listenPort;
};
using PlainOutboundListener = PlainInboundListener;
......
......@@ -9,6 +9,7 @@
#include <wepoll.h>
#endif
inline void epoll_add_fd(fd_t epollFd, fd_t fd) {
epoll_event event {
.events = EPOLLIN,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment