Improve tor logging (#4853)

* Improve tor logging to so its easier to see where we are failing to connect

* Add logging for success case
This commit is contained in:
Chris Stewart 2022-10-21 09:48:40 -05:00 committed by GitHub
parent 1d1af1d52e
commit 89a4c9e13e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 14 deletions

View File

@ -12,6 +12,7 @@ import akka.stream.scaladsl.{BidiFlow, Flow, Keep}
import akka.stream.stage._
import akka.stream.{Attributes, BidiShape, Inlet, Outlet}
import akka.util.ByteString
import grizzled.slf4j.Logging
import org.bitcoins.core.util.NetworkUtil
import java.net.{InetSocketAddress, URI}
@ -113,7 +114,8 @@ class Socks5ProxyGraphStage(
targetPort: Int,
proxyParams: Socks5ProxyParams)
extends GraphStage[
BidiShape[ByteString, ByteString, ByteString, ByteString]] {
BidiShape[ByteString, ByteString, ByteString, ByteString]]
with Logging {
val bytesIn: Inlet[ByteString] = Inlet("OutgoingTCP.in")
val bytesOut: Outlet[ByteString] = Outlet("OutgoingTCP.out")
@ -140,7 +142,7 @@ class Socks5ProxyGraphStage(
private val connectMessage = socks5ConnectionRequest(
InetSocketAddress.createUnresolved(targetHostName, targetPort))
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = {
new GraphStageLogic(shape) with StageLogging {
private var state: State = Greeting
@ -201,6 +203,9 @@ class Socks5ProxyGraphStage(
passAlong(socks5In, bytesOut, doFinish = false, doPull = true)
pull(bytesIn)
case Failure(ex) =>
logger.error(
s"Failed to connect $targetHostName:$targetPort via tor",
ex)
failStage(ex)
}
case _ =>
@ -225,5 +230,6 @@ class Socks5ProxyGraphStage(
setHandler(bytesOut, eagerTerminateOutput)
setHandler(socks5Out, eagerTerminateOutput)
}
}
}

View File

@ -3,11 +3,12 @@ package org.bitcoins.tor
import akka.actor.{Actor, ActorLogging, ActorRef, Props, Terminated}
import akka.io.Tcp
import akka.util.ByteString
import grizzled.slf4j.Logging
import org.bitcoins.crypto.CryptoUtil
import org.bitcoins.tor.Socks5Connection.{Credentials, Socks5Connect}
import java.net.{Inet4Address, Inet6Address, InetAddress, InetSocketAddress}
import scala.util.Try
import scala.util.{Failure, Success, Try}
/** Simple socks 5 client. It should be given a new connection, and will
*
@ -19,9 +20,10 @@ import scala.util.Try
class Socks5Connection(
connection: ActorRef,
credentialsOpt: Option[Credentials],
command: Socks5Connect)
target: Socks5Connect)
extends Actor
with ActorLogging {
with ActorLogging
with Logging {
import Socks5Connection._
@ -48,7 +50,7 @@ class Socks5Connection(
connection ! Tcp.ResumeReading
} else {
context become connectionRequest
connection ! Tcp.Write(socks5ConnectionRequest(command.address))
connection ! Tcp.Write(socks5ConnectionRequest(target.address))
connection ! Tcp.ResumeReading
}
}
@ -56,16 +58,24 @@ class Socks5Connection(
def authenticate: Receive = { case Tcp.Received(data) =>
if (parseAuth(data)) {
context become connectionRequest
connection ! Tcp.Write(socks5ConnectionRequest(command.address))
connection ! Tcp.Write(socks5ConnectionRequest(target.address))
connection ! Tcp.ResumeReading
}
}
def connectionRequest: Receive = { case Tcp.Received(data) =>
val connectedAddress = parseConnectedAddress(data)
context become connected
context.parent ! Socks5Connected(connectedAddress)
isConnected = true
val connectedAddressT = tryParseConnectedAddress(data)
connectedAddressT match {
case Success(connectedAddress) =>
logger.info(
s"Tor connection request succeeded. target=$target connectedAddress=$connectedAddress")
context become connected
context.parent ! Socks5Connected(connectedAddress)
isConnected = true
case Failure(err) =>
logger.error(s"Tor connection request failed to $target", err)
}
}
def connected: Receive = { case Tcp.Register(handler, _, _) =>
@ -87,7 +97,7 @@ class Socks5Connection(
super.postStop()
connection ! Tcp.Close
if (!isConnected) {
context.parent ! command.failureMessage
context.parent ! target.failureMessage
}
}
@ -209,8 +219,9 @@ object Socks5Connection {
} else {
val status = data(1)
if (status != 0) {
throw Socks5Error(
connectErrors.getOrElse(status, s"Unknown SOCKS5 error $status"))
val errMsg =
connectErrors.getOrElse(status, s"Unknown SOCKS5 error $status")
throw Socks5Error(errMsg + s" data=$data")
}
data(3) match {
case 0x01 =>