Skip to content
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

Add Proxy Support #2

Closed
kamuridesu opened this issue Jan 10, 2024 · 5 comments
Closed

Add Proxy Support #2

kamuridesu opened this issue Jan 10, 2024 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@kamuridesu
Copy link
Contributor

kamuridesu commented Jan 10, 2024

Hello! Please, add proxy support. I think is not currently supported by native fetch, so I'm using it with axios, like:

import {NekosAPI} from "nekosapi/v3/index.js";
import { SocksProxyAgent } from "socks-proxy-agent";
import axios, { AxiosResponse } from "axios";

type ProxyAuth = {
    username: string;
    password: string;
}

type CustomProxy = {
    protocol: string;
    host: string;
    port: number;
    auth: ProxyAuth;
}

export const Socks5Proxy: CustomProxy = {
    protocol: 'socks5',
    host: process.env.PROXY_HOST!,
    port: parseFloat(process.env.PROXY_PORT!),
    auth: {
      username: process.env.PROXY_USER != undefined ? process.env.PROXY_USER : "",
      password: process.env.PROXY_PASS != undefined ? process.env.PROXY_PASS : ""
    }
}

function checkResponseCode(response: AxiosResponse<any, any>) {
    if ((response.status > 200 && response.status <= 300)) {
        throw new Error(`An error occurred while fetching data from the server. ${response.statusText}. Status: ${response.status}. ${response.request?.url}`)
    }
}

async function fetchResponse<T>(url: URL): Promise<T> {
    const proxy = process.env.PROXY_HOST != undefined ? Socks5Proxy : false;
    let agent;
    if (proxy) {
        const proxyURL = `${proxy.protocol}://${proxy.auth.username}:${proxy.auth.password}@${proxy.host}:${proxy.port}`;
        agent = new SocksProxyAgent(proxyURL);
    }
    const response = await axios.get(
        url.toString(),
        {
            httpsAgent: agent,
            responseType: "json"
        }
    )
    checkResponseCode(response);
    return <T> await response.data;
}

export class NekosAPIAxiosProxy extends NekosAPI {
    constructor() {
        super();
        (this as any).checkResponseCode = checkResponseCode;
        (this as any).fetchResponse = fetchResponse;
    }
}
@cataclym cataclym self-assigned this Jan 13, 2024
@cataclym
Copy link
Owner

cataclym commented Jan 13, 2024

Hi! Thanks for creating this issue
This is certainly something I would not mind implementing, however as you say, native fetch does not support proxies.
I am thinking of using undici or axios, and currently axios seems like the fastest option.

@cataclym cataclym added the enhancement New feature or request label Jan 13, 2024
@cataclym
Copy link
Owner

In 073a7d8 I added (untested) preliminary support.
I am not sure how to test the proxy setup, so if you have the time to test it, I would appreciate that :) npm i https://github.com/cataclym/nekosapi.git

It's setup to read the environment variables same as yours.

@cataclym
Copy link
Owner

@kamuridesu :)

@kamuridesu
Copy link
Contributor Author

Thank you, sorry for the late response, I'm traveling right now. I'll test it as soon as possible!

@kamuridesu
Copy link
Contributor Author

All working! Sorry for taking so long to validate, it's all working fine now, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants