0

I am trying to fetch a site: link here. If you click on the link, it shows JSON: {"error":"Socket Error"}. I am trying to fetch that website, and return the error.

However, I get a 403 Forbidden error instead. Is there a reason for this? I turned CORS off, but I don't think it did anything. Here is an example of what I have tried:

async function b(){
  error = await fetch('https://matchmaker.krunker.io/seek-game?hostname=krunker.io&region=us-ca-sv&game=SV%3A4jve9&autoChangeGame=false&validationToken=QR6beUGVKUKkzwIsKhbKXyaJaZtKmPN8Rwgykea5l5FkES04b6h1RHuBkaUMFnu%2B&dataQuery=%7B%7D', {mode:'no-cors'}).then(res=>res.json())
  console.log(JSON.stringify(error))
}
b()

Why doesn't anything seem to work? Please comment if there is anything I need to add, this is my first Stack Overflow post so I am still slightly confused by what makes a good question. Thanks for helping!!

NOTE: My environment is Node.JS (testing on Repl.it which I think uses the latest Node version).

imaginate
  • 567
  • 2
  • 13
  • Are you controlling the server side here? Maybe they only allow requests from certain servers – Luca Kiebel Nov 19 '20 at 18:37
  • @LucaKiebel, No I do not control the server side. However, I do know that CORS mode Access-Control-Allow-Origin is blocking me (at least I think). – imaginate Nov 19 '20 at 18:41
  • Then you can't really do anything against it. If they dont offer a public API, and they dont want you to access theirs, it wont work – Luca Kiebel Nov 19 '20 at 18:45
  • @LucaKiebel ok, thanks for helping! – imaginate Nov 19 '20 at 18:46
  • You might want to play with your (fetch) request headers to mimic your browser. Or simply to mimic curl headers (or use curl), which seems to work fine :-) – ΔO 'delta zero' Nov 19 '20 at 23:20
  • @ΔO'deltazero' Thanks for commenting! I thought of doing this, but couldn't seem to change my user-agent (which seems to be the problem). If you know of any way to do this, please write an answer. Thanks! – imaginate Nov 20 '20 at 02:54
  • @imaginate, check out my answer below. – ΔO 'delta zero' Nov 23 '20 at 21:50

2 Answers2

2

This particular host is protected width Cloudflare anti DDoS protection. The server doesn't accept requests made by fetch, but the do accept requests from curl. God knows why.

$ curl 'https://matchmaker.krunker.io/seek-game?hostname=krunker.io&region=us-ca-sv&game=SV%3A4jve9&autoChangeGame=false&validationToken=QR6beUGVKUKkzwIsKhbKXyaJaZtKmPN8Rwgykea5l5FkES04b6h1RHuBkaUMFnu%2B&dataQuery=%7B%7D'

// => {"error":"Socket Error"}

You can use curl in node.js with node-libcurl package.

const { curly } = require('node-libcurl')
const url = 'https://matchmaker.krunker.io/seek-game?hostname=krunker.io&region=us-ca-sv&game=SV%3A4jve9&autoChangeGame=false&validationToken=QR6beUGVKUKkzwIsKhbKXyaJaZtKmPN8Rwgykea5l5FkES04b6h1RHuBkaUMFnu%2B&dataQuery=%7B%7D'

curly.get(url)
    .then(({ statusCode, data }) => console.log(statusCode, data))

// => 400 { error: 'Socket Error' }

Works as expected :-)

ΔO 'delta zero'
  • 3,506
  • 1
  • 19
  • 31
  • Thanks so much! This solution worked for a couple of days, now it seems like I am getting blocked again. I will keep this as the answer, but could you possibly try to update it? Thanks so much! – imaginate Nov 28 '20 at 22:12
  • Might be that Cloudflare anti DDoS protection identified you as a bot and blocked your access. – ΔO 'delta zero' Nov 28 '20 at 22:42
  • See if that URL still works from a browser on the same IP. – ΔO 'delta zero' Nov 28 '20 at 22:43
  • It has to do with the origin. I went to `https://matchmaker.krunker.io` and ran the function `b()` (shown above) and I got the resulting `{ error: 'Socket Error' }`. I know that you can set the origin in cURL using a `-H` header (example [here](https://stackoverflow.com/questions/12173990/how-can-you-debug-a-cors-request-with-curl)) but don't know how to do this using `node-libcurl`. Thanks :) – imaginate Nov 28 '20 at 23:03
  • You can respond to my [new post](https://stackoverflow.com/questions/65054601/can-you-set-an-origin-header-using-node-libcurl) if you want. thanks again – imaginate Nov 28 '20 at 23:05
1

You can use a proxy such as allorigins.win which is a cors proxy that can retrieve the data from a URL in the form of json. You can fetch from this URL: https://api.allorigins.win/raw?url=https://matchmaker.krunker.io/game-list?hostname=krunker.io

  • Your answer must contain an example in code ... After all, this question is old ... – Paulo Boaventura May 29 '21 at 04:56
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Tyler2P Jun 05 '21 at 11:43