Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Commit

Permalink
feat: prevent the bot from being detected; fix #53
Browse files Browse the repository at this point in the history
  • Loading branch information
wdzeng committed Jul 11, 2023
1 parent f447001 commit c962e26
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
1 change: 1 addition & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ RUN wget -qO /tmp/chrome.deb https://dl.google.com/linux/chrome/deb/pool/main/g/
RUN wget -qO /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/${CHROME_VERSION}/chromedriver_linux64.zip \
&& unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/ \
&& rm /tmp/chromedriver.zip
RUN sed -i s/cdc_/fan_/g /usr/local/bin/chromedriver
# Set display port to avoid crash
ENV DISPLAY=:99
ENV PATH /usr/local/bin:$PATH
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ ARG BASE_IMAGE
FROM alpine:3.17.2 AS alpine-base
RUN apk add --no-cache chromium chromium-chromedriver nodejs tini \
&& adduser -u 1000 -H -D bot
# Replace cdc token to prevent begin detected
RUN sed -i s/cdc_/fan_/g /usr/bin/chromedriver
ENV CHROME_BIN=/usr/bin/chromium-browser CHROME_PATH=/usr/lib/chromium/


Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
"packageManager": "pnpm@7.29.3",
"dependencies": {
"@types/node": "^18.15.3",
"@types/selenium-webdriver": "^4.1.13",
"@types/selenium-webdriver": "^4.1.14",
"@vercel/ncc": "^0.36.1",
"commander": "^10.0.0",
"commander": "^10.0.1",
"loglevel": "^1.8.1",
"selenium-webdriver": "^4.8.1",
"selenium-webdriver": "^4.9.2",
"typescript": "^4.9.5"
},
"devDependencies": {
Expand Down
29 changes: 14 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 21 additions & 5 deletions src/tw-shopee-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
Browser,
Builder,
By,
ChromiumWebDriver,
error,
IWebDriverOptionsCookie,
until,
WebDriver
until
} from 'selenium-webdriver'
import chrome from 'selenium-webdriver/chrome'
import { ExitCode } from './exit-code'
Expand All @@ -25,7 +25,7 @@ interface ShopeeCredential {
}

export default class TaiwanShopeeBot {
private driver!: WebDriver
private driver!: ChromiumWebDriver

constructor(
private username: string | undefined,
Expand Down Expand Up @@ -355,15 +355,31 @@ export default class TaiwanShopeeBot {
.addArguments('--disable-dev-shm-usage')
.addArguments('--disable-gpu')
.addArguments('--lang=zh-TW')
// Shopee server now detects if the bot is running in headless mode.
.addArguments(
'--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'
)
.addArguments('--disable-blink-features')
.addArguments('--disable-blink-features=AutomationControlled')
.excludeSwitches('enable-automation')
.excludeSwitches('useAutomationExtension')

if (process.env['DEBUG']) {
logger.debug('Open debug port on 9222.')
options.addArguments('--remote-debugging-port=9222')
}

this.driver = await new Builder()
this.driver = (await new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options)
.build()
.build()) as ChromiumWebDriver

await this.driver.sendDevToolsCommand(
'Page.addScriptToEvaluateOnNewDocument',
{
source: `Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) `
}
)
}

private async runBot(
Expand Down

0 comments on commit c962e26

Please sign in to comment.