scoop:scoop.asyncio:0.1.0
src/socks5_types.scoop
Selected production source · src/socks5_types.scoop
1
package scoop.asyncio3
import scoop.platform.net.SocketAddress5
/**6
* A SOCKS5 destination or bound endpoint represented by a domain name/port or an IP socket7
* address.8
*/9
public enum Socks5Address {10
/**11
* An already resolved IP endpoint.12
*/13
Ip(SocketAddress),14
/**15
* A domain name and network port, available for proxy-side resolution.16
*/17
Domain(String, Int) }18
/**19
* Chooses which side resolves a SOCKS5 domain destination.20
*/21
public enum Socks5Resolution {22
/**23
* Resolve the domain using the local event loop before requesting the proxy connection.24
*/25
Local,26
/**27
* Send the domain to the proxy for resolution.28
*/29
Proxy }30
/**31
* The SOCKS5 negotiation phase responsible for a failure.32
*/33
public enum Socks5Phase {34
/**35
* Authentication-method negotiation.36
*/37
Method,38
/**39
* Username/password authentication.40
*/41
Authentication,42
/**43
* The CONNECT request and proxy reply.44
*/45
Connect }47
/**48
* Copied username and password bytes for SOCKS5 username/password authentication.49
*/50
public class Socks5Credentials public constructor(username: List<Byte>, password: List<Byte>) {51
/**52
* The independent username byte snapshot.53
*/54
public val username: Array<Byte> = socks5CredentialBytes(username)55
/**56
* The independent password byte snapshot.57
*/58
public val password: Array<Byte> = socks5CredentialBytes(password)60
/**61
* UTF-8 convenience construction for SOCKS5 credentials.62
*/63
public companion object {64
/**65
* Encodes username and password as UTF-8 and validates protocol lengths.66
*/67
public fun fromUtf8(username: String, password: String): Socks5Credentials =68
Socks5Credentials(username.toByteArray(), password.toByteArray())69
}70
}72
private fun socks5CredentialBytes(bytes: List<Byte>): Array<Byte> {73
val count = bytes.size74
if (count < 1L || count > 255L) {75
throw IllegalArgumentException(Some("SOCKS5 credentials require 1..255 bytes per field"))76
}77
return Array<Byte>(count) { index -> bytes[index] }78
}80
/**81
* Reports a SOCKS5 negotiation failure with phase and optional proxy reply code.82
*/83
public class Socks5Exception internal constructor(84
/**85
* The negotiation phase that failed.86
*/87
public val phase: Socks5Phase,88
/**89
* The proxy's CONNECT reply code when available.90
*/91
public val replyCode: UByte?,92
message: String93
) : Exception(Some(message))95
internal fun requireSocks5Job(loop: EventLoop, cancellable: Boolean): Job {96
if (!hasEventLoop() || currentEventLoop() !== loop) {97
throw IllegalStateException(Some("SOCKS5 tunnel belongs to another event loop"))98
}99
val job = currentJobOrNone() ?: throw IllegalStateException(Some("SOCKS5 requires a Job"))100
if (job.isCompleted) throw IllegalStateException(Some("SOCKS5 requires an active Job"))101
if (cancellable) job.ensureActive()102
return job103
}