Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
add IOException to TorSocket Constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
JesusMcCloud committed Jun 6, 2018
1 parent fe7298f commit 61ee9d5
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 60 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ As of you, simply add `tor.native` as dependency to your project (using JitPack)
<dependency>
<groupId>com.github.JesusMcCloud.netlayer</groupId>
<artifactId>tor.native</artifactId>
<version>0.4.6</version>
<version>0.4.7</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.berndpruenster.netlayer</groupId>
<artifactId>parent</artifactId>
<version>0.4.6</version>
<version>0.4.7</version>
<packaging>pom</packaging>

<name>Netlayer</name>
Expand Down
2 changes: 1 addition & 1 deletion tor.external/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.berndpruenster.netlayer</groupId>
<artifactId>parent</artifactId>
<version>0.4.6</version>
<version>0.4.7</version>
</parent>

<artifactId>tor.external</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion tor.native/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.berndpruenster.netlayer</groupId>
<artifactId>parent</artifactId>
<version>0.4.6</version>
<version>0.4.7</version>
</parent>

<artifactId>tor.native</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public static void main(String[] args) throws IOException, TorCtlException {
//set default instance, so it can be omitted whenever creating Tor (Server)Sockets
//This will take some time
Tor.setDefault(new NativeTor(/*Tor installation destination*/
new File("tor-demo"), /*bridge configuration*/
parseBridgeLines(demo.pathBridges)));
new File("tor-demo"),
/*bridge configuration*/ parseBridgeLines(demo.pathBridges)));

System.out.println("Tor has been bootstrapped");

Expand All @@ -43,21 +43,28 @@ public static void main(String[] args) throws IOException, TorCtlException {
System.out.println("Hidden Service " + socket + " is ready");
new Thread(() -> {
System.err.println("we'll try and connect to the just-published hidden service");
new TorSocket(socket.getSocketAddress(), "Foo");
System.err.println("Connected to $socket. closing socket...");
socket.close();
try {
new TorSocket(socket.getSocketAddress(), "Foo");
System.err.println("Connected to $socket. closing socket...");
socket.close();
} catch (Exception e) {
System.err.println("This should have worked");
}
//retry connecting
try {

new TorSocket(socket.getServiceName(), socket.getHiddenServicePort(), "Foo");
} catch (Exception e) {
System.err.println("As exptected, connection to $socket failed!");
System.err.println("As exptected, connection to " + socket + " failed!");
}
try {
//let's connect to some regular domains using different streams
new TorSocket("www.google.com", 80, "FOO");
new TorSocket("www.cnn.com", 80, "BAR");
new TorSocket("www.google.com", 80, "BAZ");
} catch (Exception e) {
System.err.println("This should have worked");
}
//let's connect to some regular domains using different streams
new TorSocket("www.google.com", 80, "FOO");
new TorSocket("www.cnn.com", 80, "BAR");
new TorSocket("www.google.com", 80, "BAZ");


System.exit(0);

Expand Down
2 changes: 1 addition & 1 deletion tor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.berndpruenster.netlayer</groupId>
<artifactId>parent</artifactId>
<version>0.4.6</version>
<version>0.4.7</version>
</parent>

<artifactId>tor</artifactId>
Expand Down
129 changes: 85 additions & 44 deletions tor/src/main/kotlin/org/berndpruenster/netlayer/tor/TorSockets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,30 @@ data class HiddenServiceSocketAddress(val serviceName: String, val hiddenService
override fun toString(): String = "HiddenServiceSocket[addr=$serviceName,port=$hiddenServicePort]"
}

class TorSocket @JvmOverloads constructor(private val destination: String,
port: Int,
streamId: String? = null,
numTries: Int = 5,
tor: Tor? = null) : Socket() {
@JvmOverloads
constructor(socketAddress: HiddenServiceSocketAddress, streamId: String? = null,
numTries: Int = 5,
tor: Tor? = null) : this(socketAddress.serviceName, socketAddress.hiddenServicePort, streamId, numTries, tor)
class TorSocket @JvmOverloads @Throws(IOException::class) constructor(private val destination: String,
port: Int,
streamId: String? = null,
numTries: Int = 5,
tor: Tor? = null) : Socket() {
@JvmOverloads @Throws(IOException::class) constructor(socketAddress: HiddenServiceSocketAddress,
streamId: String? = null,
numTries: Int = 5,
tor: Tor? = null) : this(socketAddress.serviceName,
socketAddress.hiddenServicePort,
streamId,
numTries,
tor)

private val socket = setup(destination, port, numTries, streamId, tor)
@Throws(IOException::class) override fun connect(addr: SocketAddress) = throw IOException("DONT!")

@Throws(IOException::class) override fun connect(addr: SocketAddress, port: Int) = throw IOException("DONT!")
@Throws(IOException::class)
override fun connect(addr: SocketAddress) = throw IOException("DONT!")

@Throws(IOException::class) override fun bind(addr: SocketAddress) {
@Throws(IOException::class)
override fun connect(addr: SocketAddress, port: Int) = throw IOException("DONT!")

@Throws(IOException::class)
override fun bind(addr: SocketAddress) {
socket.bind(addr)
}

Expand All @@ -63,67 +71,101 @@ class TorSocket @JvmOverloads constructor(private val destination: String,
override fun isClosed(): Boolean = socket.isClosed
override fun isInputShutdown(): Boolean = socket.isInputShutdown
override fun isOutputShutdown(): Boolean = socket.isOutputShutdown
@Throws(IOException::class) override fun getInputStream(): InputStream = socket.getInputStream()
@Throws(IOException::class) override fun getOutputStream(): OutputStream = socket.getOutputStream()
@Throws(SocketException::class) override fun getTcpNoDelay(): Boolean = socket.tcpNoDelay
@Throws(SocketException::class) override fun getOOBInline(): Boolean = socket.oobInline
@Throws(SocketException::class) override fun getSoTimeout(): Int = socket.soTimeout
@Throws(SocketException::class) override fun getSendBufferSize(): Int = socket.sendBufferSize
@Throws(SocketException::class) override fun getReceiveBufferSize(): Int = socket.receiveBufferSize
@Throws(SocketException::class) override fun getTrafficClass(): Int = socket.trafficClass
@Throws(SocketException::class) override fun getKeepAlive(): Boolean = socket.keepAlive
@Throws(SocketException::class) override fun getReuseAddress(): Boolean = socket.reuseAddress
@Throws(SocketException::class) override fun getSoLinger(): Int = socket.soLinger

@Throws(SocketException::class) override fun setTcpNoDelay(arg0: Boolean) {
@Throws(IOException::class)
override fun getInputStream(): InputStream = socket.getInputStream()

@Throws(IOException::class)
override fun getOutputStream(): OutputStream = socket.getOutputStream()

@Throws(SocketException::class)
override fun getTcpNoDelay(): Boolean = socket.tcpNoDelay

@Throws(SocketException::class)
override fun getOOBInline(): Boolean = socket.oobInline

@Throws(SocketException::class)
override fun getSoTimeout(): Int = socket.soTimeout

@Throws(SocketException::class)
override fun getSendBufferSize(): Int = socket.sendBufferSize

@Throws(SocketException::class)
override fun getReceiveBufferSize(): Int = socket.receiveBufferSize

@Throws(SocketException::class)
override fun getTrafficClass(): Int = socket.trafficClass

@Throws(SocketException::class)
override fun getKeepAlive(): Boolean = socket.keepAlive

@Throws(SocketException::class)
override fun getReuseAddress(): Boolean = socket.reuseAddress

@Throws(SocketException::class)
override fun getSoLinger(): Int = socket.soLinger

@Throws(SocketException::class)
override fun setTcpNoDelay(arg0: Boolean) {
socket.tcpNoDelay = arg0
}

@Throws(SocketException::class) override fun setSoLinger(arg0: Boolean, arg1: Int) {
@Throws(SocketException::class)
override fun setSoLinger(arg0: Boolean, arg1: Int) {
socket.setSoLinger(arg0, arg1)
}

@Throws(IOException::class) override fun sendUrgentData(arg0: Int) {
@Throws(IOException::class)
override fun sendUrgentData(arg0: Int) {
socket.sendUrgentData(arg0)
}

@Throws(SocketException::class) override fun setOOBInline(arg0: Boolean) {
@Throws(SocketException::class)
override fun setOOBInline(arg0: Boolean) {
socket.oobInline = arg0
}

@Throws(SocketException::class) override fun setSoTimeout(arg0: Int) {
@Throws(SocketException::class)
override fun setSoTimeout(arg0: Int) {
socket.soTimeout = arg0
}

@Throws(SocketException::class) override fun setSendBufferSize(arg0: Int) {
@Throws(SocketException::class)
override fun setSendBufferSize(arg0: Int) {
socket.sendBufferSize = arg0
}

@Throws(SocketException::class) override fun setReceiveBufferSize(arg0: Int) {
@Throws(SocketException::class)
override fun setReceiveBufferSize(arg0: Int) {
socket.receiveBufferSize = arg0
}

@Throws(SocketException::class) override fun setKeepAlive(arg0: Boolean) {
@Throws(SocketException::class)
override fun setKeepAlive(arg0: Boolean) {
socket.keepAlive = arg0
}

@Throws(SocketException::class) override fun setTrafficClass(arg0: Int) {
@Throws(SocketException::class)
override fun setTrafficClass(arg0: Int) {
socket.trafficClass = arg0
}

@Throws(SocketException::class) override fun setReuseAddress(arg0: Boolean) {
@Throws(SocketException::class)
override fun setReuseAddress(arg0: Boolean) {
socket.reuseAddress = arg0
}

@Throws(IOException::class) override fun close() {
@Throws(IOException::class)
override fun close() {
socket.close()
}

@Throws(IOException::class) override fun shutdownInput() {
@Throws(IOException::class)
override fun shutdownInput() {
socket.shutdownInput()
}

@Throws(IOException::class) override fun shutdownOutput() {
@Throws(IOException::class)
override fun shutdownOutput() {
socket.shutdownOutput()
}

Expand All @@ -143,9 +185,10 @@ class HiddenServiceSocket @JvmOverloads constructor(internalPort: Int,

val socketAddress: HiddenServiceSocketAddress
val serviceName: String

init {
val (name, handler) = mgr.publishHiddenService(hiddenServiceDir, hiddenServicePort, internalPort)
serviceName= name
serviceName = name
socketAddress = HiddenServiceSocketAddress(name, hiddenServicePort)
bind(InetSocketAddress(LOCAL_IP, internalPort))
handler.attachReadyListeners(this, listeners)
Expand All @@ -171,11 +214,8 @@ class HiddenServiceSocket @JvmOverloads constructor(internalPort: Int,

}

@Throws(IOException::class) private fun setup(onionUrl: String,
port: Int,
numTries: Int,
streamID: String?,
tor: Tor?): Socket {
@Throws(IOException::class)
private fun setup(onionUrl: String, port: Int, numTries: Int, streamID: String?, tor: Tor?): Socket {

val before = Calendar.getInstance().timeInMillis
val mgr = getTorInstance(tor)
Expand All @@ -201,4 +241,5 @@ class HiddenServiceSocket @JvmOverloads constructor(internalPort: Int,
throw IOException("Cannot connect to HS")
}

private fun getTorInstance(tor: Tor?): Tor = tor ?: Tor.default ?: throw IOException("No default Tor Instance configured")
private fun getTorInstance(tor: Tor?): Tor = tor ?: Tor.default
?: throw IOException("No default Tor Instance configured")

0 comments on commit 61ee9d5

Please sign in to comment.