|
| 1 | +import BaseController, { BaseConfig, BaseState } from './BaseController'; |
| 2 | + |
| 3 | +const DEFAULT_PHISHING_RESPONSE = require('eth-phishing-detect/src/config.json'); |
| 4 | +const PhishingDetector = require('eth-phishing-detect/src/detector'); |
| 5 | + |
| 6 | +/** |
| 7 | + * @type EthPhishingResponse |
| 8 | + * |
| 9 | + * Configuration response from the eth-phishing-detect package |
| 10 | + * consisting of approved and unapproved website origins |
| 11 | + * |
| 12 | + * @property blacklist - List of unapproved origins |
| 13 | + * @property fuzzylist - List of fuzzy-matched unapproved origins |
| 14 | + * @property tolerance - Fuzzy match tolerance level |
| 15 | + * @property version - Versin number of this configuration |
| 16 | + * @property whitelist - List of approved origins |
| 17 | + */ |
| 18 | +export interface EthPhishingResponse { |
| 19 | + blacklist: string[]; |
| 20 | + fuzzylist: string[]; |
| 21 | + tolerance: number; |
| 22 | + version: number; |
| 23 | + whitelist: string[]; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * @type PhishingConfig |
| 28 | + * |
| 29 | + * Phishing controller configuration |
| 30 | + * |
| 31 | + * @property interval - Polling interval used to fetch new block / approve lists |
| 32 | + */ |
| 33 | +export interface PhishingConfig extends BaseConfig { |
| 34 | + interval: number; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * @type PhishingState |
| 39 | + * |
| 40 | + * Phishing controller state |
| 41 | + * |
| 42 | + * @property phishing - eth-phishing-detect configuration |
| 43 | + */ |
| 44 | +export interface PhishingState extends BaseState { |
| 45 | + phishing: EthPhishingResponse; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Controller that passively polls on a set interval for approved and unapproved website origins |
| 50 | + */ |
| 51 | +export class PhishingController extends BaseController<PhishingState, PhishingConfig> { |
| 52 | + private detector: any; |
| 53 | + private handle?: NodeJS.Timer; |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates a PhishingController instance |
| 57 | + * |
| 58 | + * @param state - Initial state to set on this controller |
| 59 | + * @param config - Initial options used to configure this controller |
| 60 | + */ |
| 61 | + constructor(state?: Partial<PhishingState>, config?: Partial<PhishingConfig>) { |
| 62 | + super(state, config); |
| 63 | + this.defaultConfig = { interval: 180000 }; |
| 64 | + this.defaultState = { phishing: DEFAULT_PHISHING_RESPONSE }; |
| 65 | + this.detector = new PhishingDetector(this.defaultState.phishing); |
| 66 | + this.initialize(); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Sets a new polling interval |
| 71 | + * |
| 72 | + * @param interval - Polling interval used to fetch new exchange rates |
| 73 | + */ |
| 74 | + set interval(interval: number) { |
| 75 | + this.handle && clearInterval(this.handle); |
| 76 | + this.updatePhishingLists(); |
| 77 | + this.handle = setInterval(() => { |
| 78 | + this.updatePhishingLists(); |
| 79 | + }, interval); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Determines if a given origin is unapproved |
| 84 | + * |
| 85 | + * @param origin - Domain origin of a website |
| 86 | + * @returns - True if the origin is an unapproved origin |
| 87 | + */ |
| 88 | + test(origin: string) { |
| 89 | + return this.detector.check(origin).result; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Updates lists of approved and unapproved website origins |
| 94 | + */ |
| 95 | + async updatePhishingLists() { |
| 96 | + let phishing; |
| 97 | + if (this.disabled) { |
| 98 | + return; |
| 99 | + } |
| 100 | + try { |
| 101 | + const response = await fetch('https://api.infura.io/v2/blacklist'); |
| 102 | + const json = await response.json(); |
| 103 | + phishing = json && json.whitelist ? json : /* istanbul ignore next */ null; |
| 104 | + } catch (error) { |
| 105 | + /* tslint:disable-next-line:no-empty */ |
| 106 | + } |
| 107 | + this.detector = new PhishingDetector(phishing); |
| 108 | + phishing && this.update({ phishing }); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +export default PhishingController; |
0 commit comments