禁用 WebRTC

禁用 WebRTC 防止泄露真实ip,默认禁用 WebRTC,脚本菜单选项用于启用/禁用当前域名的 WebRTC

当前为 2024-09-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name 禁用 WebRTC
  3. // @version 1.1
  4. // @description 禁用 WebRTC 防止泄露真实ip,默认禁用 WebRTC,脚本菜单选项用于启用/禁用当前域名的 WebRTC
  5. // @author ChatGPT
  6. // @match *://*/*
  7. // @grant GM_setValue
  8. // @grant GM_getValue
  9. // @grant GM_registerMenuCommand
  10. // @run-at document-start
  11. // @namespace https://greasyfork.org/users/452911
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. // 获取启用站点列表
  18. const enabledSites = GM_getValue('enabledSites', []);
  19. const currentURL = window.location.hostname;
  20.  
  21. // 设置菜单命令
  22. const menuCommandLabel = enabledSites.includes(currentURL)
  23. ? '设置当前域名禁用WebRTC'
  24. : '设置当前域名启用WebRTC';
  25.  
  26. GM_registerMenuCommand(menuCommandLabel, function() {
  27. if (enabledSites.includes(currentURL)) {
  28. GM_setValue('enabledSites', enabledSites.filter(site => site !== currentURL));
  29. } else {
  30. GM_setValue('enabledSites', [...enabledSites, currentURL]);
  31. }
  32. });
  33.  
  34. // 默认禁用 WebRTC
  35. function disableWebRTC() {
  36. const RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection || window.msRTCPeerConnection;
  37. if (RTCPeerConnection) {
  38. // 替换 RTCPeerConnection 为一个抛出错误的函数
  39. window.RTCPeerConnection = function() {
  40. throw new Error("RTCPeerConnection is disabled");
  41. };
  42. window.RTCPeerConnection.prototype = {};
  43. }
  44.  
  45. const getUserMedia = navigator.mediaDevices && navigator.mediaDevices.getUserMedia;
  46. if (getUserMedia) {
  47. // 替换 getUserMedia 为一个返回拒绝的 Promise
  48. navigator.mediaDevices.getUserMedia = function() {
  49. return Promise.reject(new Error("getUserMedia is disabled"));
  50. };
  51. }
  52. }
  53.  
  54. // 根据当前域名的状态启用或禁用 WebRTC
  55. if (!enabledSites.includes(currentURL)) {
  56. disableWebRTC();
  57. }
  58. })();