From a600075abf8515977c7728b35e8677d7d5f1756c Mon Sep 17 00:00:00 2001 From: "Tyler St. Onge" Date: Sat, 22 Aug 2020 01:53:25 -0400 Subject: ability to accept files over passive connection --- .../tylerstonge/honeypot/ftp/FtpFileReceiver.scala | 27 +++++++++++++++------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'src/main/scala/com/tylerstonge/honeypot/ftp/FtpFileReceiver.scala') diff --git a/src/main/scala/com/tylerstonge/honeypot/ftp/FtpFileReceiver.scala b/src/main/scala/com/tylerstonge/honeypot/ftp/FtpFileReceiver.scala index 74ca594..9f88b06 100644 --- a/src/main/scala/com/tylerstonge/honeypot/ftp/FtpFileReceiver.scala +++ b/src/main/scala/com/tylerstonge/honeypot/ftp/FtpFileReceiver.scala @@ -1,9 +1,11 @@ package com.tylerstonge.honeypot.ftp +import java.io.File import java.net.InetSocketAddress -import java.nio.file.{Files, OpenOption, Paths, StandardOpenOption} +import java.nio.file.{Files, Paths, StandardOpenOption} +import java.util.UUID -import akka.actor.{Actor, Props} +import akka.actor.{Actor, ActorRef, Props} import akka.event.{Logging, LoggingAdapter} import akka.io.Tcp._ import akka.io.{IO, Tcp} @@ -11,30 +13,39 @@ import akka.util.{ByteString, ByteStringBuilder} object FtpFileReceiver { - def props(port: Int): Props = Props(new FtpFileReceiver(port)) + def props(port: Int, controller: ActorRef): Props = Props(new FtpFileReceiver(port, controller)) } -class FtpFileReceiver(port: Int) extends Actor { +class FtpFileReceiver(port: Int, controller: ActorRef) extends Actor { val log: LoggingAdapter = Logging(context.system, this) - IO(Tcp)(context.system) ! Bind(self, new InetSocketAddress("localhost", port)) + IO(Tcp)(context.system) ! Bind(self, new InetSocketAddress("127.0.0.1", port)) val fileData: ByteStringBuilder = ByteString.newBuilder + val name: String = UUID.randomUUID().toString + + override def postStop { + log.debug("shutting down") + } override def receive: Receive = { case Bound(localAddress) => log.info("listening on {}", localAddress) case CommandFailed(_: Bind) => context.stop(self) case Connected(_, _) => - log.info("peer connected") + val connection = sender() + connection ! Register(self) context.become { case Received(data) => + log.info("read {} bytes", data.length) fileData.addAll(data) case PeerClosed => - log.info("peer closed connection, writing file to disk") - val out = Files.newByteChannel(Paths.get("testfile.txt"), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE) + log.debug("peer closed connection, writing file to disk") + val out = Files.newByteChannel(new File("C:\\Users\\dropkick\\Documents\\dev\\honeypot\\" + this.name).toPath, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE) out.write(fileData.result().toByteBuffer) out.close() + controller.tell(Write(ByteString.apply("226 File transferred.\n")), context.parent) context.stop(self) + case msg@_ => log.warning("unknown message: {}", msg) } } } -- cgit v1.1