Skip to content

Commit

Permalink
avoid crash when url is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
pablouser1 committed Jun 30, 2024
1 parent 32bc4c9 commit 040625f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ RUN apk add --no-cache \
COPY package.json package-lock.json ./
RUN npm ci

# Delete dev deps
RUN apk del python3 g++ make cairo-dev pango-dev

# Copy required files for server
COPY ./js ./js
COPY ./src ./src
COPY ./api ./api
COPY ./js ./js

RUN apk del python3 g++ make cairo-dev pango-dev

EXPOSE 8080
CMD [ "node", "api/index.js" ]
4 changes: 3 additions & 1 deletion api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const server = http.createServer(async (req, res) => {
const url = Buffer.concat(buffers).toString();

const data = signer.sign(url);
console.log("Sent data from request with url: " + url);
if (data !== null) {
console.log("Sending data from request with url: " + url);
}
res.write(Utils.makePayload(data, signer.navigator()));
} else {
res.write(
Expand Down
32 changes: 19 additions & 13 deletions src/Signer.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,27 @@ class Signer {
/**
* Sign a TikTok URL
* @param {string} url_str Unsigned URL
* @returns Object with signed data
* @returns Object with signed data or null if error
*/
sign(url_str) {
const url = new URL(url_str);
const signature = this.signature(url.toString());
url.searchParams.append('_signature', signature);
const bogus = this.bogus(url.searchParams.toString());
url.searchParams.append('X-Bogus', bogus);
const xttparams = this.xttparams(url.searchParams.toString());
return {
signature: signature,
signed_url: url.toString(),
"x-tt-params": xttparams,
"X-Bogus": bogus
};
try {
const url = new URL(url_str);
const signature = this.signature(url.toString());
url.searchParams.append('_signature', signature);
const bogus = this.bogus(url.searchParams.toString());
url.searchParams.append('X-Bogus', bogus);
const xttparams = this.xttparams(url.searchParams.toString());
return {
signature: signature,
signed_url: url.toString(),
"x-tt-params": xttparams,
"X-Bogus": bogus
};
} catch (_e) {
return null;
} finally {

}
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/Utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
class Utils {
static makePayload(data, navigator) {
if (data === null) {
return JSON.stringify({
status: "error",
data: "There was an error processing your request! Is your URL valid?"
})
}

return JSON.stringify({
status: "ok",
data: {
Expand Down

0 comments on commit 040625f

Please sign in to comment.