Zekfad / nhentai-api

Node.JS client for nhentai.net undocumented API.
https://zekfad.github.io/nhentai-api/
ISC License
42 stars 6 forks source link

API: 403 with unencoded URI #18

Closed Ducote closed 2 years ago

Ducote commented 2 years ago

Hello

08/29/2019 : 11PM UTC +2 Works flawlessly

09/29/2019 : 11PM UTC +2 Having an error

/home/raphael/Programmes/Got you that/node_modules/nhentai-api/dist/cjs/bundle.js:402
   */request(options){let{net:net,agent:agent}=this;return new Promise((resolve,reject)=>{Object.assign(options,{agent:agent,headers:{"User-Agent":`nhentai-api-client/3.0.2 Node.js/${process.versions.node}`}}),net.get(options,response=>{const{statusCode:statusCode}=response,contentType=response.headers["content-type"];let error;if(200!==statusCode?error=new Error(`Request failed with status code ${statusCode}`):/^application\/json/.test(contentType)||(error=new Error(`Invalid content-type - expected application/json but received ${contentType}`)),error)return response.resume(),void reject(error);response.setEncoding("utf8");let rawData="";response.on("data",chunk=>rawData+=chunk),response.on("end",()=>{try{resolve(JSON.parse(rawData))}catch(error){reject(error)}})}).on("error",error=>reject(error))})}
                                                                                                                                                                                                                                                                                                                                                                    ^

Error: Request failed with status code 404
    at ClientRequest.<anonymous> (/home/raphael/Programmes/Got you that/node_modules/nhentai-api/dist/cjs/bundle.js:402:357)
    at Object.onceWrapper (node:events:510:26)
    at ClientRequest.emit (node:events:390:28)
    at HTTPParser.parserOnIncomingClient (node:_http_client:621:27)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
    at TLSSocket.socketOnData (node:_http_client:487:22)
    at TLSSocket.emit (node:events:390:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at TLSSocket.Readable.push (node:internal/streams/readable:228:10)

Nota Bene : the ^ is set under the on.

Strangely, the website works flawlessly with my browser and because on TLS, I'm not able to see much with a DPI. Just seing that the DNS response is correct (104.27.194.88 and 104.27.195.88).

Thanks for your hardwork, You're breathtaking !

Zekfad commented 2 years ago

The example provided in readme works for me (I've made clean container with sudo docker run -it --rm node:slim /bin/bash). Could you provide a minimal working example with such error?

Ducote commented 2 years ago

I identified that the problem come from a specific manga : #374874

If this manga shows up, it broke the search.

The error shows up with this code for me : (updated npm and dependencies yesterday)

const { API, } = require('nhentai-api');
const nhentai = new API();

var recherche = '"SeFri+Ijou"+"Konyakusha+Miman"+"More+Than+A+Sex+Friend"+"Less+Than+A+Fiancée"';
nhentai.search(recherche).then(async search => {
    if (search.books.length == 0){
        console.log('Nothing');
    }
    else{
        console.log('Something');
    }

});

The other thing that tickles is me is that when I use THIS specific research, it gives a code 403. But the search my bot use gives a 404 :

var recherche = "english+<a bunch of removed tags>+pages%3A>15+uploaded%3A>24h";
Zekfad commented 2 years ago

I can confirm this behavior. Locally I've tested connecting through proxies and requesting target url with curl. Looks like some sort of filtering by user agent. I'll investigate this tomorrow. If you have spare time, you can try to compile master branch with different user agent and test if that works. User agent is setting here: https://github.com/Zekfad/nhentai-api/blob/5f638265d5aab7035654fcbbb68948493435bddb/src/api.js#L149 The endpoint that fails is /api/galleries/search?query=%22SeFri+Ijou%22+%22Konyakusha+Miman%22+%22More+Than+A+Sex+Friend%22+%22Less+Than+A+Fiancée%22&page=1, you can also experiment with curl and headers to test that.

Zekfad commented 2 years ago

I've investigated the issue. Turns out that I pass search string as is to query (I remember that I had a reason for that). To fix your case, simply wrap your query with encodeURIComponent like follows:

- var recherche = '"SeFri+Ijou"+"Konyakusha+Miman"+"More+Than+A+Sex+Friend"+"Less+Than+A+Fiancée"';
+ var recherche = encodeURIComponent('"SeFri+Ijou"+"Konyakusha+Miman"+"More+Than+A+Sex+Friend"+"Less+Than+A+Fiancée"');