forked from humanmade/trafficator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.js
157 lines (133 loc) · 3.83 KB
/
session.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Worker file to run the browser session.
*/
const { expose } = require("threads/worker");
const puppeteer = require("puppeteer");
const { getConfig, resolve, getRandom, log } = require("./utils");
const UserAgent = require("user-agents");
expose(async (configFile, sessionId) => {
const config = getConfig(configFile);
const funnel = getRandom(config.funnels);
const browser = await puppeteer.launch({});
const page = await browser.newPage();
if (!funnel.entry) {
throw "funnel.entry must be defined and must be a valid URL.";
}
// Get start point and referer.
const entry = await resolve(getRandom(funnel.entry), page);
const referer = await resolve(
getRandom(funnel.referer || config.referer || ""),
page
);
// Set a random user agent.
const userAgent = new UserAgent(config.browsers || {});
await page.setUserAgent(userAgent.toString());
// Apply custom request headers.
Object.entries(config.headers || {}).map(async ([header, value]) => {
const content = await resolve(getRandom(value) || "");
await page.setExtraHTTPHeaders({
[header]: content
});
});
// Track steps.
let currentStep = 0;
// Handle session end.
let completer;
const completePromise = new Promise( ( resolve, reject ) => {
completer = { resolve, reject };
} );
let didDone = false;
const done = async () => {
if ( didDone ) {
return;
}
didDone = true;
log("session_end", sessionId);
const timeOnPage = await resolve(funnel.timeOnPage || 2000, page);
await page.waitForTimeout(timeOnPage);
await page.close({
runBeforeUnload: true
});
await browser.close();
completer.resolve();
return;
};
// Do steps.
const doStep = async () => {
try {
// Safety net.
if (!funnel.steps || !funnel.steps[currentStep]) {
await done();
return;
}
log("funnel_step_start", sessionId, currentStep);
const step = funnel.steps[currentStep];
const probability = await resolve(step.probability || 1, page);
// Finish session if probability not met.
if (Math.random() > probability) {
log("funnel_stop", sessionId, step.name || currentStep);
await done();
return;
}
log("funnel_step_action", sessionId, step.name || currentStep);
await step.action(page);
// Increment step.
currentStep++;
// If a page load won't be triggered then call next step.
if (step.willNotNavigate) {
await doStep();
}
} catch (error) {
log("funnel_step_error", sessionId, error);
await done();
}
};
// Navigation handler.
let didFail = false;
page.on("load", async () => {
if ( didFail ) {
await done();
return;
}
log("page_loaded", sessionId, page.url());
// Stay on page for configured time, default to 2 seconds.
const timeOnPage = await resolve(funnel.timeOnPage || 2000, page);
await page.waitForTimeout(timeOnPage);
// Proceed to next step.
await doStep();
});
page.on("console", msg => {
log("console:" + msg.type(), sessionId, msg.text(), msg.stackTrace());
});
page.on("error", e => {
log("error", e.message);
didFail = true;
});
page.on("requestfailed", e => {
if ( e.url() === page.url() ) {
log("request_failed", e.url(), page.url(), e.failure().errorText);
didFail = true;
}
});
page.on("requestfinished", e => {
if ( ! e.response().ok() ) {
log("response_error", e.response().status(), e.response().statusText());
didFail = true;
}
});
log("session_start", sessionId, {
entry,
referer
});
// First page view.
try {
await page.goto(entry, {
referer
});
} catch (error) {
log("session_start_error", sessionId, error);
await done();
} finally {
await completePromise;
}
});