Last active
July 1, 2023 18:39
-
-
Save CyberShadow/ccb3813d5953e5e7f2c53fe43275f986 to your computer and use it in GitHub Desktop.
nntp-md-adder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/nntp-md-adder |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name "nntp-md-adder" | |
targetType "executable" | |
dependency "ae" version="==0.0.3063" | |
sourceFiles "proxy.d" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"fileVersion": 1, | |
"versions": { | |
"ae": "0.0.3063" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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