网上没什么方案,最靠谱的方案是使用插件,调用
chrome.privacy.network.webRTCIPHandlingPolicy.set({
value: 'default_public_interface_only'
});
改变WebRTC策略。
一般通过WebRTC获取IP的代码如下:
function get_webrtc() {
//get the IP addresses associated with an account
function getIPs(callback){
var ip_dups = {};
//compatibility for firefox and chrome
var RTCPeerConnection = window.RTCPeerConnection
|| window.mozRTCPeerConnection
|| window.webkitRTCPeerConnection
|| window.msRTCPeerConnection;
//bypass naive webrtc blocking using an iframe
if(!RTCPeerConnection){
// SINGLE IFRAME
$('body').append('');
var win = document.getElementById('iframe').contentWindow;
RTCPeerConnection = win.RTCPeerConnection
|| win.mozRTCPeerConnection
|| win.webkitRTCPeerConnection
|| win.msRTCPeerConnection;
}
//minimal requirements for data connection
var mediaConstraints = {
optional: [{RtpDataChannels: true}]
};
var servers = {iceServers: [{urls: "stun:stun.l.google.com:19302?transport=udp"}]};
//construct a new RTCPeerConnection
//var pc = new RTCPeerConnection(servers, mediaConstraints);
try {
var pc = new RTCPeerConnection(servers, mediaConstraints);
} catch (e) {
return;
}
function handleCandidate(candidate){
//match just the IP address
var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/;
if(ip_regex.exec(candidate) && ip_regex.exec(candidate).length > 1) {
var ip_addr = ip_regex.exec(candidate)[1];
//remove duplicates
if(ip_dups[ip_addr] === undefined)
callback(ip_addr);
ip_dups[ip_addr] = true;
}
}
//listen for candidate events
//pc.onicecandidate = function(ice){
//console.log('ice.candidate.candidate='+ice.candidate.candidate);
//skip non-candidate events
//ice.candidate && handleCandidate(ice.candidate.candidate);
//};
//create a bogus data channel
//pc.createDataChannel("");
try {
pc.createDataChannel("bl");
} catch (k) {}
try {
pc.createOffer().then(function(result) {
pc.setLocalDescription(result);
});
}
catch(e) {
//create an offer sdp
pc.createOffer(function(result){
//trigger the stun server request
pc.setLocalDescription(result, function(){}, function(){});
}, function(){});
}
//wait for a while to let everything done
setTimeout(function(){
//read candidate info from local description
pc.localDescription.sdp.split('\n').forEach(function(line){
if(line.indexOf('a=candidate:') === 0) {
//console.log('line='+line);
handleCandidate(line);
}
});
}, 1000);
}
//insert IP addresses into the page
getIPs(function(ip){
//console.log(ip);
if(!$('#webrtc_loc').length) {
$('#webrtc').removeClass('disabled').html(
''+dic['loc_ip']+':' +
''+dic['pub_ip']+':' +
''
);
}
//local IPs
if (ip.match(/^(192\.168\.|169\.254\.|10\.|127\.|172\.(1[6-9]|2\d|3[01])\.)/)) {
$('#webrtc_loc').append('
'+ip+'');
}
//assume the rest are public IPs
else {
$.getJSON("/ip2loc/?i="+ip, function(data) {
//console.log(data);
$('#webrtc_pub').append('
'+data.ip+'
'+data.cn+(data.ci!=null?', '+data.ci:'')+'');
});
}
});
}
由于Cefsharp 添加浏览器插件 挺麻烦,最后想到一招。注入js,修改掉
var RTCPeerConnection = window.RTCPeerConnection
|| window.mozRTCPeerConnection
|| window.webkitRTCPeerConnection
|| window.msRTCPeerConnection;
这个对象。
即注入脚本,重定义RTCPeerConnection这个对象,让获取ip的代码不能正常运行。
console.log("Disable WebRTC Begin......");
window.RTCPeerConnection = {};
window.webkitRTCPeerConnection = {};
window.msRTCPeerConnection = {};
window.mozRTCPeerConnection = {};
console.log("Disable WebRTC End......");
// 实现ChromiumWebBrowser的FrameLoadStart事件
public event EventHandler FrameLoadStart;
// 实现代码为
void wb_FrameLoadStart(object sender, CefSharp.FrameLoadStartEventArgs e)
{
if (AppSetting.FrameLoadStartScript != null && AppSetting.FrameLoadStartScript.Length > 0)
{
e.Frame.ExecuteJavaScriptAsync(AppSetting.FrameLoadStartScript);
}
}