Skip to content

Instantly share code, notes, and snippets.

@gizmomogwai
Forked from CyberShadow/.gitignore
Created July 1, 2023 18:39
Show Gist options
  • Save gizmomogwai/38cdd961784c7bf4112d5a884da6f537 to your computer and use it in GitHub Desktop.
Save gizmomogwai/38cdd961784c7bf4112d5a884da6f537 to your computer and use it in GitHub Desktop.
nntp-md-adder
/nntp-md-adder
name "nntp-md-adder"
targetType "executable"
dependency "ae" version="==0.0.3063"
sourceFiles "proxy.d"
{
"fileVersion": 1,
"versions": {
"ae": "0.0.3063"
}
}
import std.exception;
import std.string;
import ae.net.asockets;
import ae.sys.log;
enum targetHost = "news.digitalmars.com";
enum targetPort = 119;
enum listenPort = 4119;
void main()
{
auto log = createLogger("NNTPProxy");
auto server = new TcpServer();
server.handleAccept = (TcpConnection serverTcp) {
log("Accepted connection from " ~ serverTcp.remoteAddressStr);
auto clientTcp = new TcpConnection();
IConnection clientConn, serverConn;
serverConn = new LineBufferedAdapter(serverTcp);
clientConn = new LineBufferedAdapter(clientTcp);
bool inHeaders;
clientConn.handleConnect = {
log("Connected to " ~ clientTcp.remoteAddressStr);
clientConn.handleReadData = (Data data) {
serverConn.send(data);
};
serverConn.handleReadData = (Data data) {
auto contents = cast(string)data.contents;
if (!inHeaders)
{
if (contents == "POST")
inHeaders = true;
}
else
{
enum replaceFrom = "Content-Type: text/plain;";
enum replaceTo = replaceFrom ~ " markup=markdown;";
if (contents.startsWith(replaceFrom))
data = Data(replaceTo) ~ data[replaceFrom.length .. $];
else
if (contents == "" || contents == ".")
inHeaders = false;
}
clientConn.send(data);
};
};
clientConn.handleDisconnect = (string reason, DisconnectType) { log("Client disconnected: " ~ reason); if (serverConn.state == ConnectionState.connected) serverConn.disconnect(reason); };
serverConn.handleDisconnect = (string reason, DisconnectType) { log("Server disconnected: " ~ reason); if (clientConn.state == ConnectionState.connected) clientConn.disconnect(reason); };
clientTcp.connect(targetHost, targetPort);
};
server.listen(listenPort);
log("Proxy started");
socketManager.loop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment