forestwanglin / okx-v5-java

OKX V5 SDK for JAVA
MIT License
20 stars 8 forks source link

How to use socks5 proxy #6

Closed qbot955 closed 3 months ago

qbot955 commented 3 months ago

Api have to bind an ip, I need to use proxy for debugging

forestwanglin commented 3 months ago

Which type do you use, websocket or rest API?

qbot955 commented 3 months ago

both

Wang Lin @.***>于2024年7月22日 周一19:28写道:

Which type do you use, websocket or rest API?

— Reply to this email directly, view it on GitHub https://github.com/forestwanglin/okx-v5-java/issues/6#issuecomment-2242729917, or unsubscribe https://github.com/notifications/unsubscribe-auth/BJ3R4QM4M2NFEITH2C4FD5LZNTUHTAVCNFSM6AAAAABLH6JQZSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENBSG4ZDSOJRG4 . You are receiving this because you authored the thread.Message ID: @.***>

forestwanglin commented 3 months ago

You can initial http client via proxy as below.

Websocket:

    private OkxWsApiService getOkxWsService() {
        Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 7890));
        OkHttpClient client = defaultClient(Duration.ofMillis(300000))
                .newBuilder()
                .proxy(proxy)
                .build();
        return new OkxWsApiService(client, true);
    }

Rest API:

    private OkxApiService getOkxService() {
        String apiKey = System.getenv("API_KEY");
        String passphrase = System.getenv("PASSPHRASE");
        String secretKey = System.getenv("SECRET_KEY");
        Duration defaultTimeout = Duration.ofSeconds(30);
        boolean simulated = true;
        Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 7890));
        OkHttpClient client = defaultClient(apiKey, secretKey, passphrase, simulated, defaultTimeout)
                .newBuilder()
                .proxy(proxy)
                .build();
        return new OkxApiService(buildApi(apiKey, secretKey, passphrase, simulated, defaultTimeout), client, simulated);
    }
qbot955 commented 3 months ago

You can initial http client via proxy as below.

Websocket:

    private OkxWsApiService getOkxWsService() {
        Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 7890));
        OkHttpClient client = defaultClient(Duration.ofMillis(300000))
                .newBuilder()
                .proxy(proxy)
                .build();
        return new OkxWsApiService(client, true);
    }

Rest API:

    private OkxApiService getOkxService() {
        String apiKey = System.getenv("API_KEY");
        String passphrase = System.getenv("PASSPHRASE");
        String secretKey = System.getenv("SECRET_KEY");
        Duration defaultTimeout = Duration.ofSeconds(30);
        boolean simulated = true;
        Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 7890));
        OkHttpClient client = defaultClient(apiKey, secretKey, passphrase, simulated, defaultTimeout)
                .newBuilder()
                .proxy(proxy)
                .build();
        return new OkxApiService(buildApi(apiKey, secretKey, passphrase, simulated, defaultTimeout), client, simulated);
    }

Thanks, it works