/*!********************************************************************* * This Source Code Form is copyright of 51 Degrees Mobile Experts Limited. * Copyright 2022 51 Degrees Mobile Experts Limited, Davidson House, * Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU. * * This Source Code Form is the subject of the following patents and patent * applications, owned by 51 Degrees Mobile Experts Limited: * United Kingdom Patent Application No. 1905888.2. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. * * If a copy of the MPL was not distributed with this file, You can obtain * one at http://mozilla.org/MPL/2.0/. * * This Source Code Form is "Incompatible With Secondary Licenses", as * defined by the Mozilla Public License, v. 2.0. ********************************************************************** */ /** * ============================================================================ * RENDERER DI 51DEGREES — Le impronte dei chip Apple e l'albero che le legge * ============================================================================ * * DA DOVE VIENE. `getRenderer` di https://github.com/51Degrees/Renderer * (renderer.js, VERSION 1.657942, MPL 2.0, v. LICENSE.md in questa cartella). * Activetheory.net usa lo stesso codice, copiato nel suo bundle; qui si porta * dall'originale, controllato riga per riga contro la loro copia il 4 * novembre 2026. Questo file e [albero](albero.ts) restano MPL 2.0: chi lo * modifica tiene questa intestazione, e il sorgente di questi due file va reso * disponibile a chi riceve il sito (MPL 2.0, sezione 3.2). Il resto del sito * no: la MPL vale file per file. Il sorgente pubblicato sta in * public/licenze/51degrees e lo linka la pagina privacy: dopo ogni modifica a * questi file va ricopiato (v. il test licenza-51degrees, che lo controlla). * * COSA FA. Ogni chip disegna lo stesso piccolo disegno in modo leggermente * diverso, quindi l'hash dei pixel e' un'impronta del chip: * - `impronta2d` (hash): canvas 67x67, "*51Degrees*" in Arial 10pt ruotato, * ombre, un'ellisse semitrasparente, `toDataURL`, hash FNV-1a a 32 bit * (`fnv`: gli shift sono la moltiplicazione per 16777619, il primo di FNV); * - `impronta3d` (hash3d): WebGL 1 67x67, una sfera 50x50 con diffuso per * vertice in `mediump`, `toDataURL`, FNV. Le matrici sono di gl-matrix; * - `valutaAlbero` (evaluateNode e resolveNode): dalla radice ogni nodo * misura una cosa (`misuraDelBrowser`: famiglia dallo UA, altezza dello * schermo in pixel fisici, densita', gamut, le due impronte) e scende nel * figlio che ha quel valore; se nessun figlio ce l'ha rimisura dopo 10 ms; * finiti i tentativi vale l'insieme di chip del nodo. Una misura che fallisce * (0) si ferma al nodo. * * MODIFICHE ALL'ORIGINALE (MPL 2.0 le permette; v. sezione 2.3 per il * brevetto, che copre il codice di 51Degrees e non le modifiche): * - TypeScript, e `valutaAlbero` restituisce una promessa invece di chiamare * `complete`; l'attesa fra i tentativi si puo' passare da fuori (i test); * - i tentativi sono 3: nell'originale `maxRetries` non e' definito da * nessuna parte, e al primo valore senza figlio il codice lancia. Tre e' * quello che ha messo Active Theory nella sua copia; * - il contesto WebGL dell'impronta 3D si libera con `WEBGL_lose_context`; * - le coordinate di texture della sfera, calcolate e mai usate, non ci sono; * - il benchmark di CPU in un worker (`takUrl`, tak.js) non c'e', come nella * copia di Active Theory; le misure `width` e `reportedrenderer`, che * l'albero non usa, nemmeno. * ============================================================================ */ export type Misura = 'famiglia' | 'altezza' | 'densita' | 'gamut' | 'hash2d' | 'hash3d'; export interface Nodo { x: string; m?: Misura; n?: number[]; v?: (string | number)[]; } export interface Albero { versione: string; data: string; nodi: Nodo[]; } const TENTATIVI = 3; const attesa = (ms: number) => new Promise((avanti) => setTimeout(avanti, ms)); export function fnv(testo: string): number { let h = 0x811c9dc5; for (let i = 0; i < testo.length; ++i) { h ^= testo.charCodeAt(i); h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24); } return h >>> 0; } export async function valutaAlbero( albero: Albero, misura: (m: Misura) => string | number, aspetta: (ms: number) => Promise = attesa, ): Promise { let nodo = albero.nodi[0]!; let tentativi = 0; for (;;) { if (!nodo.m) return nodo.x; const valore = misura(nodo.m); if (!valore && valore !== '') return nodo.x; const figlio = (nodo.n ?? []).map((i) => albero.nodi[i]!).find((c) => c.v?.includes(valore)); if (figlio) { nodo = figlio; tentativi = 0; continue; } if ((nodo.n ?? []).length > 0 && tentativi < TENTATIVI) { tentativi++; await aspetta(10); continue; } return nodo.x; } } export function impronta2d(): number { const canvas = document.createElement('canvas'); canvas.width = 67; canvas.height = 67; const ctx = canvas.getContext('2d', { alpha: true }); if (!ctx) return 0; ctx.imageSmoothingQuality = 'low'; ctx.imageSmoothingEnabled = true; ctx.globalCompositeOperation = 'source-over'; ctx.globalAlpha = 1; ctx.miterLimit = Infinity; ctx.filter = 'none'; ctx.lineCap = 'butt'; ctx.lineDashOffset = 0; ctx.lineJoin = 'miter'; ctx.font = '10pt Arial'; ctx.lineWidth = 2; if (ctx.setLineDash !== undefined) ctx.setLineDash([10, 20]); ctx.shadowColor = 'black'; ctx.shadowOffsetX = -3; ctx.shadowOffsetY = -5; ctx.translate(canvas.width / 2, canvas.height / 2); ctx.rotate(0.8901179); ctx.fillStyle = 'green'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText('*51Degrees*', 0, 0); ctx.beginPath(); ctx.shadowColor = 'yellow'; ctx.shadowBlur = 1; ctx.shadowOffsetX = 1; ctx.shadowOffsetY = 1; ctx.strokeStyle = 'red'; ctx.fillStyle = 'rgba(0, 0, 255, 0.6)'; if (ctx.ellipse === undefined) ctx.arc(0, 0, 25, 0, 2 * Math.PI); else ctx.ellipse(0, 0, 25, 15, Math.PI / 4, 0, 2 * Math.PI); ctx.fill(); ctx.stroke(); return fnv(canvas.toDataURL()); } type Mat4 = number[]; const mat4 = { create(): Mat4 { const out = new Array(16); for (let i = 0; i < 16; i++) out[i] = i % 5 === 0 ? 1 : 0; return out; }, perspective(out: Mat4, fovy: number, aspect: number, near: number, far: number): Mat4 { const f = 1 / Math.tan(fovy / 2); out[0] = f / aspect; out[1] = 0; out[2] = 0; out[3] = 0; out[4] = 0; out[5] = f; out[6] = 0; out[7] = 0; out[8] = 0; out[9] = 0; out[11] = -1; out[12] = 0; out[13] = 0; out[15] = 0; if (far !== Infinity) { const nf = 1 / (near - far); out[10] = (far + near) * nf; out[14] = 2 * far * near * nf; } else { out[10] = -1; out[14] = -2 * near; } return out; }, identity(out: Mat4): Mat4 { for (let i = 0; i < 16; i++) out[i] = i % 5 === 0 ? 1 : 0; return out; }, lookAt(out: Mat4, eye: number[], center: number[], up: number[]): Mat4 { const [eyex, eyey, eyez] = eye as [number, number, number]; const [upx, upy, upz] = up as [number, number, number]; const [centerx, centery, centerz] = center as [number, number, number]; if (Math.abs(eyex - centerx) < 1e-6 && Math.abs(eyey - centery) < 1e-6 && Math.abs(eyez - centerz) < 1e-6) { return mat4.identity(out); } let z0 = eyex - centerx; let z1 = eyey - centery; let z2 = eyez - centerz; let len = 1 / Math.hypot(z0, z1, z2); z0 *= len; z1 *= len; z2 *= len; let x0 = upy * z2 - upz * z1; let x1 = upz * z0 - upx * z2; let x2 = upx * z1 - upy * z0; len = Math.hypot(x0, x1, x2); if (len) { len = 1 / len; x0 *= len; x1 *= len; x2 *= len; } else { x0 = 0; x1 = 0; x2 = 0; } let y0 = z1 * x2 - z2 * x1; let y1 = z2 * x0 - z0 * x2; let y2 = z0 * x1 - z1 * x0; len = Math.hypot(y0, y1, y2); if (len) { len = 1 / len; y0 *= len; y1 *= len; y2 *= len; } else { y0 = 0; y1 = 0; y2 = 0; } out[0] = x0; out[1] = y0; out[2] = z0; out[3] = 0; out[4] = x1; out[5] = y1; out[6] = z1; out[7] = 0; out[8] = x2; out[9] = y2; out[10] = z2; out[11] = 0; out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez); out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez); out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez); out[15] = 1; return out; }, multiply(out: Mat4, a: Mat4, b: Mat4): Mat4 { const [a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33] = a as [ number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, ]; for (let colonna = 0; colonna < 4; colonna++) { const b0 = b[colonna * 4]!; const b1 = b[colonna * 4 + 1]!; const b2 = b[colonna * 4 + 2]!; const b3 = b[colonna * 4 + 3]!; out[colonna * 4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; out[colonna * 4 + 1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; out[colonna * 4 + 2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; out[colonna * 4 + 3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; } return out; }, }; const VERTEX = 'attribute vec3 c,d; uniform vec4 e; uniform vec3 f,g;uniform mat4 h,i;varying vec3 j;void main(){vec3 a=normalize(d);vec4 b=h*vec4(c,1.);vec3 k=normalize(vec3(e-b));j=g*f*max(dot(k,a),0.),gl_Position=i*vec4(c,1.);}'; const FRAGMENT = '#ifdef GL_ES\nprecision mediump float;\n#endif\nvarying vec3 j;void main(){gl_FragColor = vec4(j, 1.0);}'; function sfera(gl: WebGLRenderingContext, program: WebGLProgram): number { const posizioni: number[] = []; const normali: number[] = []; const indici: number[] = []; for (let lat = 0; lat <= 50; ++lat) { const theta = (lat * Math.PI) / 50; const sinTheta = Math.sin(theta); const cosTheta = Math.cos(theta); for (let lon = 0; lon <= 50; ++lon) { const phi = (2 * lon * Math.PI) / 50; const x = Math.cos(phi) * sinTheta; const y = cosTheta; const z = Math.sin(phi) * sinTheta; posizioni.push(2 * x, 2 * y, 2 * z); normali.push(x, y, z); } } for (let lat = 0; lat < 50; ++lat) { for (let lon = 0; lon < 50; ++lon) { const primo = 51 * lat + lon; const secondo = primo + 50 + 1; indici.push(primo, secondo, primo + 1, secondo, secondo + 1, primo + 1); } } gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer()); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(posizioni), gl.STATIC_DRAW); const posizione = gl.getAttribLocation(program, 'c'); gl.vertexAttribPointer(posizione, 3, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(posizione); gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer()); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(normali), gl.STATIC_DRAW); const normale = gl.getAttribLocation(program, 'd'); gl.vertexAttribPointer(normale, 3, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(normale); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, gl.createBuffer()); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indici), gl.STATIC_DRAW); return indici.length; } export function impronta3d(): number { const canvas = document.createElement('canvas'); canvas.width = 67; canvas.height = 67; const gl = (canvas.getContext('webgl') ?? canvas.getContext('experimental-webgl')) as WebGLRenderingContext | null; if (!gl) return 0; try { gl.viewport(0, 0, 67, 67); gl.clearColor(0, 0, 0, 1); gl.clear(gl.COLOR_BUFFER_BIT); const vertex = gl.createShader(gl.VERTEX_SHADER)!; gl.shaderSource(vertex, VERTEX); gl.compileShader(vertex); const fragment = gl.createShader(gl.FRAGMENT_SHADER)!; gl.shaderSource(fragment, FRAGMENT); gl.compileShader(fragment); const program = gl.createProgram()!; gl.attachShader(program, vertex); gl.attachShader(program, fragment); gl.linkProgram(program); gl.detachShader(program, vertex); gl.detachShader(program, fragment); gl.deleteShader(vertex); gl.deleteShader(fragment); gl.useProgram(program); const n = sfera(gl, program); gl.clearColor(0, 0, 0, 1); gl.enable(gl.DEPTH_TEST); const proiezione = mat4.perspective(mat4.create(), Math.PI / 6, 1, 0.1, 100); const vista = mat4.lookAt(mat4.create(), [0, 0, 10], [0, 0, 0], [0, 1, 0]); const mvp = mat4.multiply(mat4.create(), proiezione, vista); gl.uniformMatrix4fv(gl.getUniformLocation(program, 'h'), false, vista); gl.uniformMatrix4fv(gl.getUniformLocation(program, 'i'), false, mvp); gl.uniform4fv(gl.getUniformLocation(program, 'e'), [10, 10, 10, 1]); gl.uniform3fv(gl.getUniformLocation(program, 'f'), [0.9, 0.5, 0.3]); gl.uniform3fv(gl.getUniformLocation(program, 'g'), [1, 1, 1]); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); gl.drawElements(gl.TRIANGLES, n, gl.UNSIGNED_SHORT, 0); gl.useProgram(null); gl.deleteProgram(program); return fnv(canvas.toDataURL()); } finally { gl.getExtension('WEBGL_lose_context')?.loseContext(); } } function gamut(): string { for (const valore of ['p3', 'srgb']) if (window.matchMedia(`(color-gamut: ${valore})`).matches) return valore; return 'n/a'; } export function misuraDelBrowser(m: Misura): string | number { switch (m) { case 'famiglia': return /iPhone|iPad|Macintosh/.exec(navigator.userAgent)?.[0] ?? ''; case 'altezza': return window.screen.height * window.devicePixelRatio; case 'densita': return window.devicePixelRatio; case 'gamut': return gamut(); case 'hash2d': return impronta2d(); case 'hash3d': return impronta3d(); } }