-
Notifications
You must be signed in to change notification settings - Fork 192
fix: bahn.de API 403 errors #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix: bahn.de API 403 errors #131
Conversation
| @@ -1,5 +1,6 @@ | |||
| /// <reference types="next" /> | |||
| /// <reference types="next/image-types/global" /> | |||
| /// <reference path="./.next/types/routes.d.ts" /> | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why Next.js automatically added this. 🤷
Somebody please double-check this for correctness.
| } | ||
| ignoreBuildErrors: true, // temporarily, since some type errors still exists and are ambiguous | ||
| }, | ||
| serverExternalPackages: ["puppeteer-extra", "puppeteer-extra-plugin-stealth"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those must be stated as external to avoid bundling.
| export type ValidatedVendoJourney = z.infer<typeof validatedVendoJourneySchema>; | ||
|
|
||
| export const vbidSchema = z.object({ | ||
| export const VerbindungResponseSchema = z.object({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This schema has nothing to do with the Bahn's VBIDs.
It is about the HTTP response from their API at /web/api/angebote/verbindung/<vbid>.
The VBID is just the sole parameter of the route, but the schema should be named after the route itself acting as a descriptor.
| export type VbidSchema = z.infer<typeof vbidSchema>; | ||
| export type VerbindungResponse = z.infer<typeof VerbindungResponseSchema>; | ||
|
|
||
| const reconLegSchema = z.object({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These were over at parseHinfahrtRecon.ts which got deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer need to reverse-engineer the Recon logic as we use the actual implementation by the Bahn itself via our browser-based approach, which executes client-side JS as a normal user would.
6ea49a8 to
e12662a
Compare
|
I don't know how I feel with going back to a headless browser / puppeteer when it was a small milestone to get rid of it, making setup and run requirements much leaner. I doubt that a headless browser will circumvent scrape protection from cloud provider IPs. I'm expecting the same type of proof of this actually fixing #57 as detailed here #98 (comment) I'll only consider this PR if affected users confirm that this branch/fork solves their issue. |
Generally I agree such extras should be avoided wherever possible. In our case though, where we're avoiding bot detection, we most likely will need to make our program mimic an actual user to avoid getting caught. I've even received error pages from the website itself until I added random user agents and viewports. But let's see... |
I understand, but we should try our best to avoid adding a headless browser (again). In the end, almost everything a browser does - from the perspective of backends - can be emulated without running one. And not having a headless browser makes the application more lightweight, easier to deploy, and faster. |
chore: remove some comments and add spacing
ecb1634 to
ee13b1d
Compare
|
Works for me! |
|
@alexanderroidl after by running the docker image. Are your changes incorporated or do I need to rebuild the image? Anyway...still got the "Fehler: Server error (500). Diese Problem ist uns bekannt und wir arbeiten daran, es zu beheben. Ein Status über den Fehler finden Sie unter #57" |
Dang, I forgot to adjust the Docker setup. Right now it's failing because it's missing Chromium. Can you try it without Docker until I take care of it in the next couple of days, so we can see whether it generally works? |
|
This PR fixed it for me. I am running on a residential IP but I seemed to be blocked anyway... Although, I would prefer things to work without a whole chromium instance tbh |
|
This PR is stale because it has been open for 30 days with no activity. It will be closed in 14 days if there is no activity. |
|
could this be merged soon? |
I won't merge it for the time being because only 2 users report that this works and 1 reports an error. This PR massively changes the build and deployment requirements for Betterbahn and imo we need more data (i.e. more reports by affected users) to justify this change. To give a concrete number, I'd say if we get 10 reports by different users and it fixes #57 for at least 5 of them, this could be a worthwhile change. Otherwise if the interest is too low or those affected don't want to test this PR, I won't be able to help because I'm not affected since I don't have cloud infrastructure to run Betterbahn on. |
|
Tried it locally on a residental IP and doesn‘t work. |
This MR aims to fix the 403 errors which were causing HTTP responses with status 500 on our end (as described in #57).
The Bahn-API was responding 403 errors as it deemed our requests forbidden. This MR solves this consistently as I was able to verify over several days of testing.
Therefore instead of doing plain HTTP requests via the fetch API this MR aims to replace it by:
https://www.bahn.de/buchung/start?vbid=<vbid>/web/api/angebote/reconand/web/api/angebote/verbindung/<vbid>. This is not done by us but expected behavior of the page itself, as it would happen for an actual user.fetch).Side-effects of this approach:
Preview: