Skip to content

Commit

Permalink
feat: providing configured patches
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoooqq committed Nov 18, 2021
1 parent ebb22d9 commit 17c80b7
Show file tree
Hide file tree
Showing 34 changed files with 599 additions and 853 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fakebrowser",
"version": "0.0.55",
"version": "0.0.60",
"description": "馃 Fake fingerprints to bypass anti-bot systems. Simulate mouse and keyboard operations to make behavior like a real person.",
"repository": {
"type": "git",
Expand Down
57 changes: 44 additions & 13 deletions src/core/BrowserBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// noinspection JSUnusedGlobalSymbols

import * as path from 'path'

import {
ConnectParameters,
DriverParameters,
Expand All @@ -15,29 +17,53 @@ import {FakeBrowser, kDefaultWindowsDD} from './FakeBrowser'

export class BrowserBuilder {

private readonly _driverParams: DriverParameters
public readonly driverParams: DriverParameters

constructor() {
this._driverParams = {
this.driverParams = {
doNotHook: false,
deviceDesc: kDefaultWindowsDD,
userDataDir: '',
evasionPaths: [
'chrome.app',
'chrome.csi',
'chrome.loadTimes',
'chrome.runtime',
'window.history.length',
'window.matchMedia',
'navigator.webdriver',
'sourceurl',
'navigator.plugins-native',
'webgl',
'mimeTypes',
'navigator.mediaDevices',
'bluetooth',
'navigator.permissions',
'navigator.batteryManager',
'webrtc',
'canvas.fingerprint',
'user-agent-override',
'iframe.contentWindow',
'iframe.src',
'properties.getter',
'font.fingerprint',
'emoji.fingerprint',
'window.speechSynthesis',
'workers',
'keyboard',
].map(e => path.resolve(__dirname, `../plugins/evasions/${e}`)),
}
}

get driverParams(): DriverParameters {
return this._driverParams
}

get launchParams(): LaunchParameters {
const result = this._driverParams as LaunchParameters
const result = this.driverParams as LaunchParameters
result.launchOptions = result.launchOptions || {}

return result
}

get connectParams(): ConnectParameters {
const result = this._driverParams as ConnectParameters
const result = this.driverParams as ConnectParameters
result.connectOptions = result.connectOptions || {}

return result
Expand All @@ -54,27 +80,27 @@ export class BrowserBuilder {
}

deviceDescriptor(value: DeviceDescriptor) {
this._driverParams.deviceDesc = value
this.driverParams.deviceDesc = value
return this
}

displayUserActionLayer(value: boolean) {
this._driverParams.displayUserActionLayer = value
this.driverParams.displayUserActionLayer = value
return this
}

userDataDir(value: string) {
this._driverParams.userDataDir = value
this.driverParams.userDataDir = value
return this
}

log(value: boolean) {
this._driverParams.log = value
this.driverParams.log = value
return this
}

proxy(value: ProxyServer) {
this._driverParams.proxy = value
this.driverParams.proxy = value
return this
}

Expand All @@ -88,6 +114,11 @@ export class BrowserBuilder {
return this
}

evasionPaths(value: string[]) {
this.driverParams.evasionPaths = value
return this
}

async launch(): Promise<FakeBrowser> {
if ('undefined' === typeof this.launchParams.maxSurvivalTime) {
this.launchParams.maxSurvivalTime = FakeBrowser.globalConfig.defaultBrowserMaxSurvivalTime
Expand Down
1 change: 1 addition & 0 deletions src/core/BrowserLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class BrowserLauncher {
// or create it if it does not exist.

const userDataDir = params.userDataDir
assert(userDataDir)

if (!fs.existsSync(userDataDir)) {
// may throw
Expand Down
12 changes: 9 additions & 3 deletions src/core/DeviceDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,14 @@ export type IFontSalt = {
}

export interface FakeDeviceDescriptor extends DeviceDescriptor {
canvasSalt: number[],
canvasSalt?: number[],
// TODO: I should make a correspondence between user's existing fonts and required fonts,
// but I don't have time to do it for now.
// fakeFonts: Array<FakeFont>,
fontSalt: {
fontSalt?: {
[key: string]: IFontSalt
},
acceptLanguage?: string,
}

export default class DeviceDescriptorHelper {
Expand Down Expand Up @@ -471,13 +472,18 @@ export default class DeviceDescriptorHelper {
needsUpdate = true
}

// acceptLanguage
if (!fakeDD.acceptLanguage) {
fakeDD.acceptLanguage = this.buildAcceptLanguage(fakeDD)
}

return {
fakeDeviceDesc: fakeDD,
needsUpdate,
}
}

static buildAcceptLanguage(deviceDesc: DeviceDescriptor): string {
private static buildAcceptLanguage(deviceDesc: DeviceDescriptor): string {
// referer: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Accept-Language
// https://developer.mozilla.org/zh-CN/docs/Glossary/Quality_values

Expand Down
8 changes: 5 additions & 3 deletions src/core/Driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export interface DriverParameters {
displayUserActionLayer?: boolean,
log?: boolean,
proxy?: ProxyServer,
userDataDir: string,
userDataDir?: string,
evasionPaths: string[],
}

export interface LaunchParameters extends DriverParameters {
Expand Down Expand Up @@ -213,12 +214,13 @@ export default class Driver {
}

// browser language
const lang = DeviceDescriptorHelper.buildAcceptLanguage(fakeDD)
assert(fakeDD.acceptLanguage)
args.push(
`--lang=${lang}`,
`--lang=${fakeDD.acceptLanguage}`,
)

const userDataDir = launchParams.userDataDir
assert(userDataDir)
fs.mkdirSync(userDataDir, {recursive: true}) // throw exception

args.push(
Expand Down

0 comments on commit 17c80b7

Please sign in to comment.