-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server code rewritten to akka-streams
- Loading branch information
Showing
7 changed files
with
100 additions
and
439 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.karasiq.proxychain.app | ||
|
||
import java.net.InetSocketAddress | ||
|
||
import akka.actor.ActorSystem | ||
import akka.event.Logging | ||
import akka.stream.ActorMaterializer | ||
import akka.stream.scaladsl.Tcp.OutgoingConnection | ||
import akka.stream.scaladsl.{Flow, Keep, Sink, Source} | ||
import akka.util.ByteString | ||
import akka.{Done, NotUsed} | ||
import com.karasiq.proxy.server.ProxyConnectionRequest | ||
import com.karasiq.proxy.{ProxyChain, ProxyException} | ||
import com.karasiq.proxychain.AppConfig | ||
|
||
import scala.concurrent.duration._ | ||
import scala.concurrent.{Future, Promise} | ||
import scala.language.postfixOps | ||
|
||
private [app] object Connector { | ||
def apply(cfg: AppConfig)(implicit as: ActorSystem, am: ActorMaterializer) = new Connector(cfg) | ||
} | ||
|
||
private[app] class Connector(cfg: AppConfig)(implicit actorSystem: ActorSystem, actorMaterializer: ActorMaterializer) { | ||
import actorSystem.dispatcher | ||
private val log = Logging(actorSystem, "Proxy") | ||
|
||
def connect(request: ProxyConnectionRequest, clientAddress: InetSocketAddress): Future[(OutgoingConnection, Flow[ByteString, ByteString, NotUsed])] = { | ||
val chains = cfg.proxyChainsFor(request.address) | ||
log.debug("Trying connect to {} through chains: {}", request.address, chains) | ||
|
||
val promise = Promise[(OutgoingConnection, Flow[ByteString, ByteString, NotUsed])] | ||
val futures = chains.map { chain ⇒ | ||
val ((proxyInput, (connFuture, proxyFuture)), proxyOutput) = Source.asSubscriber[ByteString] | ||
.initialTimeout(10 seconds) | ||
.idleTimeout(5 minutes) | ||
.viaMat(ProxyChain.connect(request.address, chain, Some(AppConfig.tlsContext())))(Keep.both) | ||
.toMat(Sink.asPublisher[ByteString](fanout = false))(Keep.both) | ||
.run() | ||
|
||
(for (_ ← proxyFuture; proxyConnection ← connFuture) yield { | ||
if (promise.trySuccess((proxyConnection, Flow.fromSinkAndSource(Sink.fromSubscriber(proxyInput), Source.fromPublisher(proxyOutput))))) { | ||
if (chain.isEmpty) log.warning("Proxy chain not defined, direct connection to {} opened", request.address) | ||
else log.info("Opened connection through proxy chain {} to {}", chain.mkString("[", " -> ", "]"), request.address) | ||
} | ||
Done | ||
}).recover { case _ ⇒ Done } | ||
} | ||
|
||
Future.sequence(futures).onComplete { completed ⇒ | ||
promise.tryFailure(new ProxyException(s"Connection to ${request.address} through chains $chains failed")) | ||
} | ||
promise.future | ||
} | ||
} |
240 changes: 0 additions & 240 deletions
240
src/main/scala/com/karasiq/proxychain/app/Handler.scala
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.