Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TorCmd.OnionClientAuth.Add implementation #434

Merged
merged 4 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions library/runtime-core/api/runtime-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -3366,7 +3366,7 @@ public final class io/matthewnelson/kmp/tor/runtime/core/builder/IsolationFlagBu
}

public final class io/matthewnelson/kmp/tor/runtime/core/builder/OnionAddBuilder {
public final fun clientAuth (Lio/matthewnelson/kmp/tor/runtime/core/key/AuthKey$Public;)Lio/matthewnelson/kmp/tor/runtime/core/builder/OnionAddBuilder;
public final fun clientAuth (Lio/matthewnelson/kmp/tor/runtime/core/key/X25519$PublicKey;)Lio/matthewnelson/kmp/tor/runtime/core/builder/OnionAddBuilder;
public final fun flags (Lio/matthewnelson/kmp/tor/runtime/core/ThisBlock;)Lio/matthewnelson/kmp/tor/runtime/core/builder/OnionAddBuilder;
public final fun maxStreams (Lio/matthewnelson/kmp/tor/runtime/core/ThisBlock;)Lio/matthewnelson/kmp/tor/runtime/core/builder/OnionAddBuilder;
public final fun port (Lio/matthewnelson/kmp/tor/runtime/core/ThisBlock;)Lio/matthewnelson/kmp/tor/runtime/core/builder/OnionAddBuilder;
Expand All @@ -3378,6 +3378,15 @@ public final class io/matthewnelson/kmp/tor/runtime/core/builder/OnionAddBuilder
public field MaxStreamsCloseCircuit Ljava/lang/Boolean;
}

public final class io/matthewnelson/kmp/tor/runtime/core/builder/OnionClientAuthAddBuilder {
public field clientName Ljava/lang/String;
public final fun flags (Lio/matthewnelson/kmp/tor/runtime/core/ThisBlock;)Lio/matthewnelson/kmp/tor/runtime/core/builder/OnionClientAuthAddBuilder;
}

public final class io/matthewnelson/kmp/tor/runtime/core/builder/OnionClientAuthAddBuilder$FlagBuilder {
public field Permanent Ljava/lang/Boolean;
}

public final class io/matthewnelson/kmp/tor/runtime/core/builder/SocksFlagBuilder {
public field CacheDNS Ljava/lang/Boolean;
public field CacheIPv4DNS Ljava/lang/Boolean;
Expand Down Expand Up @@ -3668,10 +3677,10 @@ public final class io/matthewnelson/kmp/tor/runtime/core/ctrl/TorCmd$Onion {
}

public final class io/matthewnelson/kmp/tor/runtime/core/ctrl/TorCmd$Onion$Add : io/matthewnelson/kmp/tor/runtime/core/ctrl/TorCmd$Unprivileged {
public final field addressKey Lio/matthewnelson/kmp/tor/runtime/core/key/AddressKey$Private;
public final field clientAuth Ljava/util/Set;
public final field destroyKeyOnJobCompletion Z
public final field flags Ljava/util/Set;
public final field key Lio/matthewnelson/kmp/tor/runtime/core/key/AddressKey$Private;
public final field keyType Lio/matthewnelson/kmp/tor/runtime/core/key/KeyType$Address;
public final field maxStreams Lio/matthewnelson/kmp/tor/runtime/core/TorConfig$LineItem;
public final field ports Ljava/util/Set;
Expand All @@ -3694,6 +3703,12 @@ public final class io/matthewnelson/kmp/tor/runtime/core/ctrl/TorCmd$OnionClient
}

public final class io/matthewnelson/kmp/tor/runtime/core/ctrl/TorCmd$OnionClientAuth$Add : io/matthewnelson/kmp/tor/runtime/core/ctrl/TorCmd$Unprivileged {
public final field address Lio/matthewnelson/kmp/tor/runtime/core/address/OnionAddress;
public final field authKey Lio/matthewnelson/kmp/tor/runtime/core/key/AuthKey$Private;
public final field clientName Ljava/lang/String;
public final field flags Ljava/util/Set;
public fun <init> (Lio/matthewnelson/kmp/tor/runtime/core/address/OnionAddress$V3;Lio/matthewnelson/kmp/tor/runtime/core/key/X25519$PrivateKey;)V
public fun <init> (Lio/matthewnelson/kmp/tor/runtime/core/address/OnionAddress$V3;Lio/matthewnelson/kmp/tor/runtime/core/key/X25519$PrivateKey;Lio/matthewnelson/kmp/tor/runtime/core/ThisBlock;)V
}

public final class io/matthewnelson/kmp/tor/runtime/core/ctrl/TorCmd$OnionClientAuth$Remove : io/matthewnelson/kmp/tor/runtime/core/ctrl/TorCmd$Unprivileged {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ public class TorConfig private constructor(
public open class Builder private constructor(other: TorConfig?) {

@JvmField
protected val settings: MutableSet<Setting> = mutableSetOf()
protected val settings: MutableSet<Setting> = LinkedHashSet(1, 1.0f)
// For dealing with inherited disabled port
@JvmField
protected val inheritedDisabledPorts: MutableSet<Setting> = mutableSetOf()
protected val inheritedDisabledPorts: MutableSet<Setting> = LinkedHashSet(1, 1.0f)

/**
* Add an already configured [Setting].
Expand Down Expand Up @@ -210,7 +210,7 @@ public class TorConfig private constructor(

init {
if (other != null) {
val disabledPorts = mutableSetOf<Setting>()
val disabledPorts = LinkedHashSet<Setting>(1, 1.0f)

other.settings.forEach { setting ->
if (
Expand Down Expand Up @@ -316,7 +316,7 @@ public class TorConfig private constructor(

private var argument: String = AUTO
private var allowReassign = true
private val unixFlags = mutableSetOf<String>()
private val unixFlags = LinkedHashSet<String>(1, 1.0f)

@KmpTorDsl
public override fun asPort(
Expand Down Expand Up @@ -378,7 +378,7 @@ public class TorConfig private constructor(

private var argument: String = default
private var allowReassign: Boolean = true
private val isolationFlags = mutableSetOf<String>()
private val isolationFlags = LinkedHashSet<String>(1, 1.0f)

@KmpTorDsl
public override fun auto(): __DNSPort {
Expand Down Expand Up @@ -444,7 +444,7 @@ public class TorConfig private constructor(

private var argument: String = default
private var allowReassign: Boolean = true
private val isolationFlags = mutableSetOf<String>()
private val isolationFlags = LinkedHashSet<String>(1, 1.0f)

@KmpTorDsl
public override fun auto(): __HTTPTunnelPort {
Expand Down Expand Up @@ -510,9 +510,9 @@ public class TorConfig private constructor(

private var argument: String = default
private var allowReassign = true
private val socksFlags = mutableSetOf<String>()
private val unixFlags = mutableSetOf<String>()
private val isolationFlags = mutableSetOf<String>()
private val socksFlags = LinkedHashSet<String>(1, 1.0f)
private val unixFlags = LinkedHashSet<String>(1, 1.0f)
private val isolationFlags = LinkedHashSet<String>(1, 1.0f)

@KmpTorDsl
public override fun asPort(
Expand Down Expand Up @@ -597,7 +597,7 @@ public class TorConfig private constructor(

private var port: String = default
private var allowReassign: Boolean = true
private val isolationFlags = mutableSetOf<String>()
private val isolationFlags = LinkedHashSet<String>(1, 1.0f)

@KmpTorDsl
public override fun auto(): __TransPort {
Expand Down Expand Up @@ -963,7 +963,7 @@ public class TorConfig private constructor(
keyword = Companion,
) {

private val suffixes = mutableSetOf<String>()
private val suffixes = LinkedHashSet<String>(1, 1.0f)

/**
* Add a single suffix (e.g. ".exit")
Expand Down Expand Up @@ -1396,7 +1396,7 @@ public class TorConfig private constructor(
keyword = Companion,
) {

private val ports = mutableSetOf<LineItem>()
private val ports = LinkedHashSet<LineItem>(1, 1.0f)
private var version: LineItem? = null
private var allowUnknownPorts: LineItem = DEFAULT_ALLOW_UNKNOWN_PORTS
// private var exportCircuitID: LineItem = DEFAULT_EXPORT_CIRCUIT_ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import kotlin.jvm.JvmSynthetic
* // entry.privateKey will not be null because `DiscardPK`
* // flag was not defined when created above.
* runtime.executeAsync(TorCmd.Onion.Add(
* key = entry.privateKey!!,
* addressKey = entry.privateKey!!,
* destroyKeyOnJobCompletion = true,
* ) {
* port {
Expand All @@ -86,17 +86,18 @@ import kotlin.jvm.JvmSynthetic
* }
* })
*
* @see [TorCmd.Onion.Add]
* @see [HiddenServiceEntry]
* @see [TorConfig.HiddenServiceMaxStreams]
* @see [TorConfig.HiddenServicePort]
* */
@KmpTorDsl
public class OnionAddBuilder private constructor() {

private val clientAuth = mutableSetOf<AuthKey.Public>()
private val flags = mutableSetOf<String>()
private val clientAuth = LinkedHashSet<AuthKey.Public>(1, 1.0f)
private val flags = LinkedHashSet<String>(1, 1.0f)
private var maxStreams: TorConfig.LineItem? = null
private val ports = mutableSetOf<TorConfig.LineItem>()
private val ports = LinkedHashSet<TorConfig.LineItem>(1, 1.0f)

@KmpTorDsl
public fun port(
Expand All @@ -109,12 +110,9 @@ public class OnionAddBuilder private constructor() {

@KmpTorDsl
public fun clientAuth(
key: AuthKey.Public,
key: X25519.PublicKey,
): OnionAddBuilder {
when (key) {
is X25519.PublicKey -> "V3Auth"
}.let { flags.add(it) }

flags.add("V3Auth")
clientAuth.add(key)
return this
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2024 Matthew Nelson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
@file:Suppress("PropertyName")

package io.matthewnelson.kmp.tor.runtime.core.builder

import io.matthewnelson.immutable.collections.toImmutableSet
import io.matthewnelson.kmp.tor.core.api.annotation.KmpTorDsl
import io.matthewnelson.kmp.tor.runtime.core.ThisBlock
import io.matthewnelson.kmp.tor.runtime.core.apply
import io.matthewnelson.kmp.tor.runtime.core.ctrl.TorCmd
import kotlin.jvm.JvmField
import kotlin.jvm.JvmSynthetic

@KmpTorDsl
public class OnionClientAuthAddBuilder private constructor() {

private val flags = LinkedHashSet<String>(1, 1.0f)

/**
* Cannot exceed 16 characters in length and must not contain
* whitespace, otherwise tor will reject it.
* */
@JvmField
public var clientName: String? = null

@KmpTorDsl
public fun flags(
block: ThisBlock<FlagBuilder>,
): OnionClientAuthAddBuilder {
FlagBuilder.configure(flags, block)
return this
}

/**
* Configure flags specific to [TorCmd.OnionClientAuth.Add].
*
* - `null` - no action (default)
* - `true` - add the flag if not present
* - `false` - remove the flag if present
* */
@KmpTorDsl
public class FlagBuilder private constructor() {

@JvmField
public var Permanent: Boolean? = null

internal companion object {

@JvmSynthetic
internal fun configure(
flags: MutableSet<String>,
block: ThisBlock<FlagBuilder>,
) {
val b = FlagBuilder().apply(block)

b.Permanent?.let {
val flag = "Permanent"
if (it) flags.add(flag) else flags.remove(flag)
}
}
}
}
internal companion object {

@JvmSynthetic
internal fun configure(
block: ThisBlock<OnionClientAuthAddBuilder>,
): Arguments {
val b = OnionClientAuthAddBuilder().apply(block)

return Arguments(b.clientName, b.flags)
}
}

internal class Arguments internal constructor(
internal val clientName: String?,
flags: Set<String>,
) {

internal val flags = flags.toImmutableSet()

internal companion object {

internal val EMPTY = Arguments(null, emptySet())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ import kotlin.jvm.JvmStatic
* */
public class AddressMapping(from: String, to: String) {

public constructor(from: IPAddress, to: IPAddress): this(from.canonicalHostName(), to.canonicalHostName())
public constructor(from: IPAddress, to: String): this(from.canonicalHostName(), to)
public constructor(from: String, to: IPAddress): this(from, to.canonicalHostName())
public constructor(from: IPAddress, to: String): this(from.canonicalHostName(), to)
public constructor(from: IPAddress, to: IPAddress): this(from.canonicalHostName(), to.canonicalHostName())
public constructor(from: IPAddress, to: OnionAddress): this(from.canonicalHostName(), to.canonicalHostName())

/**
Expand Down Expand Up @@ -112,7 +112,7 @@ public class AddressMapping(from: String, to: String) {

/**
* Creates a [AddressMapping] that instructs tor to generate
* a random host value (e.g. 4lr2xdqckbl4nttj.virtual) and
* a random host value (e.g. `4lr2xdqckbl4nttj.virtual`) and
* map the provided string (host name) to it.
*
* If the string (host name) is already mapped, tor will return
Expand Down
Loading
Loading