bg.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. const apiPublic = [
  2. 'https://api.nnpaefp7pkadbxxkhz2agtbv2a4g5sgo2fbmv3i7czaua354334uqqad.onion/_/is_antitor.php',
  3. 'https://api.sercxi.eu.org/_/is_antitor.php',
  4. 'http://im5wixghmfmt7gf7wb4xrgdm6byx2gj26zn47da6nwo7xvybgxnqryid.onion/api/is_at.php',
  5. 'https://karma.clearnetonion.eu.org/api/is_at.php'
  6. ];
  7. let localUse = false,
  8. localDB = [],
  9. timr, memcache = {},
  10. forcePurge = false,
  11. apiurl = 'https://0.0.0.0/',
  12. customAEP = apiPublic[3];
  13. function is_hostile(f) {
  14. if (localUse) {
  15. return new Promise((g, b) => {
  16. g(localDB.includes(f) ? true : false);
  17. });
  18. }
  19. return new Promise((g, b) => {
  20. fetch(apiurl, {
  21. method: 'POST',
  22. mode: 'cors',
  23. headers: {
  24. 'Content-Type': 'application/x-www-form-urlencoded'
  25. },
  26. body: 'f=' + f
  27. }).then(r => r.json()).then(r => {
  28. if (r[0]) {
  29. g(r[1]);
  30. } else {
  31. b();
  32. }
  33. }).catch(b);
  34. });
  35. }
  36. function i_know_you(f) {
  37. if (!/^([a-z0-9_.-]{1,255})\.([a-z]{2,80})$/.test(f)) {
  38. return new Promise((g, b) => {
  39. g(200);
  40. });
  41. }
  42. let m;
  43. if (memcache[f] != undefined) {
  44. m = memcache[f];
  45. return new Promise((g, b) => {
  46. g(m);
  47. });
  48. }
  49. return new Promise((g, b) => {
  50. browser.storage.local.get([f]).then((ff) => {
  51. if (ff[f]) {
  52. if (ff[f] == 'y') {
  53. memcache[f] = 1;
  54. g(1);
  55. } else {
  56. memcache[f] = -1;
  57. g(-1);
  58. }
  59. } else {
  60. g(0);
  61. }
  62. }, () => {
  63. g(0);
  64. });
  65. });
  66. }
  67. function forget_cache(x) {
  68. if (x) {
  69. browser.storage.local.get(['ign1', 'obs', 'dbg', 'alt', 'cep', 'mul', 'opd', 'ldb', 'aep']).then(g => {
  70. browser.storage.local.clear();
  71. memcache = {};
  72. browser.storage.local.set({
  73. 'ign1': (g.ign1 == 'y' ? 'y' : 'n')
  74. });
  75. browser.storage.local.set({
  76. 'obs': (g.obs == 'y' ? 'y' : 'n')
  77. });
  78. browser.storage.local.set({
  79. 'dbg': (g.dbg == 'y' ? 'y' : 'n')
  80. });
  81. browser.storage.local.set({
  82. 'alt': (g.alt == 'y' ? 'y' : 'n')
  83. });
  84. browser.storage.local.set({
  85. 'cep': (g.cep != undefined ? g.cep : '3')
  86. });
  87. browser.storage.local.set({
  88. 'aep': (g.aep != undefined ? g.aep : apiPublic[3])
  89. });
  90. browser.storage.local.set({
  91. 'mul': (g.mul != undefined ? g.mul : 'eo')
  92. });
  93. browser.storage.local.set({
  94. 'opd': (g.opd == 'n' ? 'n' : 'y')
  95. });
  96. browser.storage.local.set({
  97. 'ldb': (g.ldb != undefined ? g.ldb : '[]')
  98. });
  99. browser.storage.local.set({
  100. 'lastU': Math.round((new Date()).getTime() / 1000)
  101. });
  102. browser.storage.local.set({
  103. 'lastV': (browser.runtime.getManifest()).version
  104. });
  105. });
  106. }
  107. clearTimeout(timr);
  108. timr = setTimeout(function () {
  109. forget_cache(true);
  110. }, 1814400000);
  111. }
  112. browser.storage.local.get(['lastU', 'lastV', 'ldb', 'opd', 'cep', 'aep']).then(g => {
  113. localUse = (g.opd == 'n') ? true : false;
  114. localDB = JSON.parse(g.ldb || '[]');
  115. customAEP = (g.cep == 9 && g.aep) ? g.aep : apiPublic[3];
  116. apiurl = (g.cep == 9) ? customAEP : apiPublic[(g.cep || 3)];
  117. if (g.lastU == undefined || Math.abs(Math.round((new Date()).getTime() / 1000) - g.lastU) > 1814400 || g.lastV != (browser.runtime.getManifest()).version || forcePurge) {
  118. forget_cache(true);
  119. } else {
  120. forget_cache(false);
  121. }
  122. });
  123. browser.runtime.onMessage.addListener((requests, sender, sendResponse) => {
  124. if (requests) {
  125. if (requests === 'clear') {
  126. forget_cache(true);
  127. return;
  128. }
  129. if (requests.indexOf('customaep,') === 0) {
  130. requests = requests.replace('customaep,', '');
  131. if (!/^http(|s):\/\/(.+)\/(.+)/.test(requests)) {
  132. requests = apiPublic[3];
  133. }
  134. browser.storage.local.set({
  135. 'aep': requests
  136. });
  137. customAEP = requests;
  138. browser.storage.local.get(['cep']).then(g => {
  139. if (g.cep == 9) {
  140. apiurl = customAEP;
  141. }
  142. });
  143. return;
  144. }
  145. if (requests.indexOf('dbmode,') === 0) {
  146. switch (requests) {
  147. case 'dbmode,s1':
  148. browser.storage.local.set({
  149. 'opd': 'y'
  150. });
  151. localUse = false;
  152. break;
  153. case 'dbmode,s0':
  154. browser.storage.local.set({
  155. 'opd': 'n'
  156. });
  157. localUse = true;
  158. break;
  159. case 'dbmode,cl':
  160. browser.storage.local.set({
  161. 'ldb': '[]'
  162. });
  163. localDB = [];
  164. break;
  165. case 'dbmode,rl':
  166. browser.storage.local.get(['ldb']).then(g => {
  167. localDB = JSON.parse(g.ldb || '[]');
  168. });
  169. break;
  170. }
  171. return;
  172. }
  173. if (requests.indexOf('urltype,') === 0) {
  174. switch (requests) {
  175. case 'urltype,0':
  176. browser.storage.local.set({
  177. 'cep': '0'
  178. });
  179. apiurl = apiPublic[0];
  180. break;
  181. case 'urltype,1':
  182. browser.storage.local.set({
  183. 'cep': '1'
  184. });
  185. apiurl = apiPublic[1];
  186. break;
  187. case 'urltype,2':
  188. browser.storage.local.set({
  189. 'cep': '2'
  190. });
  191. apiurl = apiPublic[2];
  192. break;
  193. case 'urltype,3':
  194. browser.storage.local.set({
  195. 'cep': '3'
  196. });
  197. apiurl = apiPublic[3];
  198. break;
  199. case 'urltype,9':
  200. browser.storage.local.set({
  201. 'cep': '9'
  202. });
  203. apiurl = customAEP;
  204. break;
  205. }
  206. return;
  207. }
  208. requests.forEach(request => {
  209. i_know_you(request).then((r) => {
  210. if (r == 1 || r == -1) {
  211. browser.tabs.sendMessage(sender.tab.id, [request, ((r == 1) ? true : false)]);
  212. }
  213. if (r == 0) {
  214. is_hostile(request).then((a) => {
  215. if (a) {
  216. browser.storage.local.set({
  217. [request]: 'y'
  218. });
  219. } else {
  220. browser.storage.local.set({
  221. [request]: 'n'
  222. });
  223. }
  224. if (Object.keys(memcache).length > 650) {
  225. memcache = {};
  226. }
  227. browser.tabs.sendMessage(sender.tab.id, [request, a]);
  228. }, () => {
  229. browser.tabs.sendMessage(sender.tab.id, [request, false]);
  230. });
  231. }
  232. }, () => {});
  233. });
  234. }
  235. return;
  236. });
  237. browser.runtime.onInstalled.addListener(g => {
  238. if (g.reason == 'install') {
  239. browser.tabs.create({
  240. url: 'http://about-isat.go.crimeflare.eu.org/'
  241. });
  242. }
  243. });