Skip to content

Commit

Permalink
TLS Client Fingerprint
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBarba committed Nov 10, 2022
1 parent 9050c49 commit d802053
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wasm-5.7.1
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ docc:

github-pages:
echo "compute-runtime.swift.cloud" > docs/CNAME

demo:
swift build -c debug --triple wasm32-unknown-wasi
fastly compute serve --skip-build --file ./.build/debug/*.wasm
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ let package = Package(
],
targets: [
.target(name: "Compute", dependencies: ["ComputeRuntime"]),
.target(name: "ComputeRuntime")
.target(name: "ComputeRuntime"),
.executableTarget(name: "Demo", dependencies: ["Compute"])
]
)
7 changes: 7 additions & 0 deletions Sources/Compute/IncomingRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,10 @@ extension IncomingRequest {
}
}
}

extension IncomingRequest {

public func clientFingerprint() -> String? {
return try? request.tlsJa3Md5()
}
}
9 changes: 9 additions & 0 deletions Sources/Compute/Runtime/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,12 @@ extension Request {
return bytes.split { $0 == 0 }.compactMap { String(bytes: $0, encoding: .utf8) }
}
}

extension Request {

public func tlsJa3Md5() throws -> String? {
return try wasiString(maxBufferLength: 16) { buffer, _, written in
fastly_http_req__downstream_tls_ja3_md5(buffer, &written)
}
}
}
2 changes: 2 additions & 0 deletions Sources/Compute/Runtime/Stubs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ func fastly_http_req__redirect_to_grip_proxy(_ backend: UnsafePointer<CChar>!, _

func fastly_http_req__register_dynamic_backend(_ name: UnsafePointer<CChar>!, _ name_len: Int, _ target: UnsafePointer<CChar>!, _ target_len: Int, _ backend_config_mask: UInt32, _ backend_configuration: UnsafeMutablePointer<DynamicBackendConfig>!) -> Int32 { fatalError() }

func fastly_http_req__downstream_tls_ja3_md5(_ value: UnsafeMutablePointer<UInt8>!, _ nwritten: UnsafeMutablePointer<Int>!) -> Int32 { fatalError() }

/* FASTLY_HTTP_RESP */

func fastly_http_resp__new(_ handle: UnsafeMutablePointer<WasiHandle>!) -> Int32 { fatalError() }
Expand Down
3 changes: 3 additions & 0 deletions Sources/ComputeRuntime/include/ComputeRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ int fastly_http_req__redirect_to_websocket_proxy(const char *backend, size_t bac
WASM_IMPORT("fastly_http_req", "redirect_to_grip_proxy")
int fastly_http_req__redirect_to_grip_proxy(const char *backend, size_t backend_len);

WASM_IMPORT("fastly_http_req", "downstream_tls_ja3_md5")
int fastly_http_req__downstream_tls_ja3_md5(uint8_t *value, size_t *nwritten);

WASM_IMPORT("fastly_http_req", "register_dynamic_backend")
int fastly_http_req__register_dynamic_backend(const char *name,
size_t name_len,
Expand Down
10 changes: 10 additions & 0 deletions Sources/Demo/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Compute

let router = Router()

router.get("/") { req, res in
print("fingerprint:", req.clientFingerprint() ?? "(null)")
try await res.status(200).send("Hello, Swift")
}

try await router.listen()
2 changes: 2 additions & 0 deletions fastly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
language = "swift"
manifest_version = 2

0 comments on commit d802053

Please sign in to comment.