summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadREST/Transport/Socks5/Socks5Handshake.swift
blob: 34c10e7153751b17f7674f01d3b0820165e21c60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
//  Socks5Handshake.swift
//  MullvadTransport
//
//  Created by pronebird on 19/10/2023.
//

import Foundation

/// Handshake initiation message.
struct Socks5Handshake {
    /// Authentication methods supported by the client.
    /// Defaults to `.notRequired` when empty.
    var methods: [Socks5AuthenticationMethod] = []

    /// The byte representation in socks protocol.
    var rawData: Data {
        var data = Data()
        var methods = methods

        // Make sure to provide at least one supported authentication method.
        if methods.isEmpty {
            methods.append(.notRequired)
        }

        // Append socks version
        data.append(Socks5Constants.socksVersion)

        // Append number of supported authentication methods supported.
        data.append(UInt8(methods.count))

        // Append authentication methods
        data.append(contentsOf: methods.map { $0.rawValue })

        return data
    }
}

/// Handshake reply message.
struct Socks5HandshakeReply {
    /// The authentication method accepted by the socks proxys.
    var method: Socks5AuthenticationMethod
}