BastliBridge

A bot framework bridgin multiple IM protocols, and mail
git clone git://xatko.vsos.ethz.ch/BastliBridge.git
Log | Files | Refs | Submodules

commit 429d8024bafabaababf8a78726c5a443758c5ef5
parent 754f913f30ce81b3f934dd5a1168b9bdf238abae
Author: Dominik Schmidt <das1993@hotmail.com>
Date:   Mon, 25 Sep 2017 22:42:13 +0200

Add a second target bastliproxy

This is a http-proxy which bridges into the telegram-space.

Diffstat:
Makefile | 10++++++++++
src/proxy.d | 103+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 113 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -7,6 +7,9 @@ SRC := src/bot.d $(wildcard src/bastlibridge/*.d) $(wildcard Dirk/source/*/*.d) TARGET = bastlibridge LIBS = ev +TARGET2 = bastliproxy +SRC2 = $(wildcard src/bastlibridge/*.d) $(wildcard Dirk/source/ssl/*.d Dirk/source/loader/*.d) src/proxy.d + _DFLAGS := $(addprefix -I, $(INCLUDES)) $(addprefix -L-l, $(LIBS)) $(DFLAGS) .PHONY: all shared static clean distclean @@ -19,6 +22,11 @@ $(TARGET): $(patsubst %.d, %.o, $(SRC)) $(TARGET)-debug: $(patsubst %.d, %-debug.o, $(SRC)) $(DMD) $^ $(_DFLAGS) $(DEBUG) -of$@ +$(TARGET2)-debug: $(patsubst %.d, %-debug.o, $(SRC2)) + $(DMD) $^ -I src $(DEBUG) -Xcc=-rdynamic -of$@ +$(TARGET2): $(patsubst %.d, %.o, $(SRC2)) + $(DMD) $^ -I src $(RELEASE) -of$@ + %.o:%.d $(DMD) $(_DFLAGS) $(RELEASE) -c -of$@ $< %-debug.o:%.d @@ -26,5 +34,7 @@ $(TARGET)-debug: $(patsubst %.d, %-debug.o, $(SRC)) clean: rm -f $(patsubst %.d, %.o, $(SRC)) $(patsubst %.d,%-debug.o,$(SRC)) + rm -f $(patsubst %.d, %.o, $(SRC2)) $(patsubst %.d,%-debug.o,$(SRC2)) distclean: clean rm -f $(TARGET){,-debug} + rm -f $(TARGET2){,-debug} diff --git a/src/proxy.d b/src/proxy.d @@ -0,0 +1,103 @@ +import std.socket; +import std.stdio; +import std.conv:to; +import std.typecons; +import std.experimental.logger; +import std.range; +import bastlibridge.http; +import bastlibridge.telegram; + +auto telegram=Nullable!Telegram.init; +ubyte[] buf=new ubyte[1024]; + +immutable string index=` + ____ ___ _____________ ____ + / __ )/ | / ___/_ __/ / / _/ + / __ / /| | \__ \ / / / / / / + / /_/ / ___ |___/ // / / /____/ / +/_____/_/ |_/____//_/ /_____/___/ +`; + +void bridge(Socket ins, Socket outs, ubyte[] buf){ + trace("Bridging between sockets"); + size_t total=0; + size_t ret=0; + do{ + ret=ins.receive(buf); + total+=ret; + outs.send(buf[0..ret]); + }while(ret==buf.length); + trace("Bridged a total of ", total, " bytes"); +} + +void handleClient(Socket newsock){ + scope(exit){ + newsock.close(); + } + trace("Handling client "~newsock.remoteAddress.to!string); + static HttpResponse!void res; + auto req=receiveHttpRequest(newsock, buf); + if(req.isNull){ + info("Socket gone"); + return; + } + writeln(req); + void not_found(){ + res.response(404, "Not Found"); + res.header("Connection","close"); + res.header("Content-Type","text/plain"); + res.data("Not Found"); + res.perform(newsock); + } + if(req.url=="/"){ + res.response(200, "OK"); + res.header("Content-Type","text/plain"); + res.header("Connection","close"); + res.data(index); + res.perform(newsock); + } + else if(req.url[1]!='?'){ + not_found(); + } + else{ + auto path=telegram.getFile(req.url[2..$]); + if(path.length==0){ + not_found(); + return; + } + static HttpRequest!void r2; + r2.request("GET", chain("file/bot", telegram.token, "/", path)); + r2.header("Host", telegram.ApiAddr); + r2.header("Connection", "close"); + r2.perform(telegram.sock); + ubyte[1024] buffer; + bridge(telegram.sock, newsock, buffer); + } +} + +void main(string[] args){ + if(args.length!=4){ + stderr.writeln("Usage: ", args[0], " <TELEGRAM_TOKEN> <IP> <PORT>"); + } + + Address addr=parseAddress(args[2], args[3].to!ushort); + telegram=Telegram(args[1]); + telegram.connect(); + scope(exit){ + telegram.disconnect(); + } + info("Listening on "~addr.toString); + + Socket server=new TcpSocket(); + info("Binding to Address"); + server.bind(addr); + scope(exit){ + server.close(); + } + info("Listening"); + server.listen(10); + while(true){ + auto newsock=server.accept(); + handleClient(newsock); + } +}