XChange icon indicating copy to clipboard operation
XChange copied to clipboard

How to use Proxies

Open wodepig opened this issue 3 years ago • 1 comments

5.0.14-SNAPSHOT 1.

 ExchangeSpecification exSpec = new AscendexExchange().getDefaultExchangeSpecification();
        exSpec.setSslUri("https://asdx.me");
        exSpec.setApiKey("api");
        exSpec.setSecretKey("secret");
        exSpec.setProxyHost("ip");
       exSpec.setProxyPort(1572);
        Exchange exchange = ExchangeFactory.INSTANCE.createExchange(exSpec);

This setup does not work 2.

   ClientConfig clientConfig = ExchangeRestProxyBuilder.createClientConfig(exSpec);
        ClientConfig clientConfig1 = ClientConfigUtil.addBasicAuthCredentials(clientConfig, "username", "password");
        Exchange build = ExchangeRestProxyBuilder.forInterface( AscendexExchange.class,exSpec).clientConfig(clientConfig1).build();
thorw:
Exception in thread "main" java.lang.NullPointerException
	at si.mazi.rescu.RestInvocationHandler.<init>(RestInvocationHandler.java:59)
	at si.mazi.rescu.RestProxyFactory.createProxy(RestProxyFactory.java:47)
	at si.mazi.rescu.RestProxyFactoryImpl.createProxy(RestProxyFactoryImpl.java:9)
	at org.knowm.xchange.client.ExchangeRestProxyBuilder.build(ExchangeRestProxyBuilder.java:80)
	at cn.iocoder.yudao.module.asdx.service.api.apiTest.main(apiTest.java:51)
Because :
![image](https://user-images.githubusercontent.com/42103087/185853366-07a45846-5b8b-4b20-a5bb-fd0b7bf8f630.png)
![image](https://user-images.githubusercontent.com/42103087/185853528-f3667188-bac3-44af-ac62-3eb76cda71f7.png)


I don't know how to use it correctly, hope to get help

wodepig avatar Aug 22 '22 06:08 wodepig

I think I solved that problem

1.find <yourExchange>BaseService

---other code----
ExchangeSpecification specWithAccountGroup = exchange.getDefaultExchangeSpecification();
      specWithAccountGroup.setSslUri(exchange.getExchangeSpecification().getSslUri());
      String proxyUsername =  getExchangeSpecificParametersItem("proxyUsername");
      String proxyPassword =  getExchangeSpecificParametersItem("proxyPassword");
      Proxy.Type proxyType = getExchangeSpecificParametersItem("proxyType");
      // create ClientConfig
      ClientConfig clientConfig = ExchangeRestProxyBuilder.createClientConfig(exchange.getExchangeSpecification());
      clientConfig.setProxyType(proxyType);
      // set password
      ClientConfig addPassword = ClientConfigUtil.addBasicAuthCredentials(clientConfig, proxyUsername, proxyPassword);
      // apply clientConfig
      ascendexAuthenticated =
          ExchangeRestProxyBuilder.forInterface(IAscendexAuthenticated.class, specWithAccountGroup)
                  .clientConfig(addPassword)
              .build();
----other code----
 public <T> T getExchangeSpecificParametersItem(String key){
    try {
      return (T)exchange.getExchangeSpecification().getExchangeSpecificParametersItem(key);
    } catch (Exception e) {
    return null;
    }
  }

2.add ExchangeSpecificParametersItem

 ExchangeSpecification exSpec = new AscendexExchange().getDefaultExchangeSpecification();
        // proxy
        exSpec.setProxyHost("49.70.95.173");
        exSpec.setProxyPort(4256);
     exSpec.setExchangeSpecificParametersItem("proxyUsername","proxyUsername");
        exSpec.setExchangeSpecificParametersItem("proxyPassword","proxyPassword");
       exSpec.setExchangeSpecificParametersItem("proxyType", Proxy.Type.SOCKS);
        Exchange exchange = ExchangeFactory.INSTANCE.createExchange(exSpec);
        AccountInfo accountInfo = exchange.getAccountService().getAccountInfo();
        System.out.println(accountInfo);

3.over

--- http(no Auth) https(no Auth) socks(no Auth) socks(Auth)
proxyUsername --- --- --- ---
proxyPassword --- --- --- ---
proxyType --- --- --- ---
status deny normal normal normal

----other

I did a simple test and found that the agent works well with or without authentication.(maybe bug) Meanwhile, my centos builds SS5, but they don't work when you fill in the configuration, although it works fine elsewhere .(Whether authentication is enabled or not) image

wodepig avatar Aug 22 '22 10:08 wodepig