1、通过RTCPeerConnection获取用户ip

关于RTCPeerConnection
兼容情况

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
function getUserIP(onNewIP) { //  onNewIp - your listener function for new IPs
//compatibility for firefox and chrome
var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new myPeerConnection({
iceServers: []
}),
noop = function() {},
localIPs = {},
ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
key;

function iterateIP(ip) {
if (!localIPs[ip]) onNewIP(ip);
localIPs[ip] = true;
}

//create a bogus data channel
pc.createDataChannel("");

// create offer and set local description
pc.createOffer().then(function(sdp) {
sdp.sdp.split('\n').forEach(function(line) {
if (line.indexOf('candidate') < 0) return;
line.match(ipRegex).forEach(iterateIP);
});

pc.setLocalDescription(sdp, noop, noop);
}).catch(function(reason) {
// An error occurred, so handle the failure to connect
});

//sten for candidate events
pc.onicecandidate = function(ice) {
if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
};
}

// Usage

getUserIP(function(ip){
alert("Got IP! :" + ip);
});

2、根据浏览器生成唯一uuid

安装依赖库

1
npm install fingerprintjs

使用

1
var fingerprint = new Fingerprint().get();

如果你想使用 Canvas FingerPrinting,则如下调用:

1
var fingerprint = new Fingerprint({canvas: true}).get();

如果你想使用屏幕分辨率计算指纹,则需如下调用:

1
var fingerprint = new Fingerprint({screen_resolution: true}).get();

使用自定义的哈希函数

1
2
var my_hasher = new function(value, seed){ return value.length % seed; };
var fingerprint = new Fingerprint({hasher: my_hasher}).get();

或者直接传递方法:

1
2
var my_hasher = new function(value, seed){ return value.length % seed; };
var fingerprint = new Fingerprint(my_hasher).get();

更新(跨浏览器识别)

一项可以跨浏览器获取唯一识别码的技术
通过不同算法,让浏览器进行一系列计算,最后根据不同的场景和计算过程中的纹路信息得出唯一识别码(其核心概念在于硬件的唯一性)

demo

论文地址