diff --git a/GunbotProxyCommunity.iml b/GunbotProxyCommunity.iml index bb2bc37..5b50ba4 100644 --- a/GunbotProxyCommunity.iml +++ b/GunbotProxyCommunity.iml @@ -7,7 +7,6 @@ - diff --git a/configuration.properties b/configuration.properties new file mode 100644 index 0000000..02fe70a --- /dev/null +++ b/configuration.properties @@ -0,0 +1 @@ +sellOnlyMode=true \ No newline at end of file diff --git a/src/main/java/nl/komtek/gpi/controllers/CheckSetupController.java b/src/main/java/nl/komtek/gpi/controllers/CheckSetupController.java index 20ef12f..b43f722 100644 --- a/src/main/java/nl/komtek/gpi/controllers/CheckSetupController.java +++ b/src/main/java/nl/komtek/gpi/controllers/CheckSetupController.java @@ -7,9 +7,11 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import java.io.File; +import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.HashMap; @@ -19,17 +21,35 @@ import java.util.Map; * Created by Elroy on 10-7-2017. */ @Controller -@RequestMapping("/checkSetup/**") + public class CheckSetupController { @Autowired private GunbotProxyService gunbotProxyService; @Autowired private Util util; - private Map setupData = new HashMap<>(); - @RequestMapping + @RequestMapping("/checkSetup/**") public ModelAndView checkSetup(ModelMap modelMap) { + modelMap.put("setupData", setupData()); + return new ModelAndView("setupData", modelMap); + } + + @ResponseBody + @RequestMapping("/checkSetupLinux/**") + public String checkSetupLinux() throws IOException { + StringBuilder stringData = new StringBuilder(); + stringData.append("\n\n"); + Map setupData = setupData(); + for (Map.Entry entry : setupData.entrySet()) { + stringData.append(String.format("%s -- %s \n", entry.getKey(), entry.getValue())); + } + return stringData.toString(); + } + + private Map setupData() { + Map setupData = new HashMap<>(); + try { String keyName = "default_apiKey"; String secretName = "default_apiSecret"; @@ -69,9 +89,9 @@ public class CheckSetupController { try { InetAddress address = InetAddress.getByName("poloniex.com"); if (address.getHostAddress().equals("127.0.0.1")) { - setupData.put("hostfile", "Looking good!"); + setupData.put(String.format("hostfile (%s)", address.getHostAddress()), "Looking good!"); } else { - setupData.put("hostfile", "poloniex.com is not pointing to 127.0.0.1"); + setupData.put(String.format("hostfile (%s)", address.getHostAddress()), "poloniex.com is not pointing to 127.0.0.1"); } } catch (UnknownHostException e) { setupData.put("hostfile", e.getMessage()); @@ -80,9 +100,9 @@ public class CheckSetupController { try { InetAddress address = InetAddress.getByName("www.poloniex.com"); if (!address.getHostAddress().equals("127.0.0.1")) { - setupData.put("www.poloniex.com", "Looking good!"); + setupData.put(String.format("www.poloniex.com (%s)", address.getHostAddress()), "Looking good!"); } else { - setupData.put("www.poloniex.com", "www.poloniex.com should not point to 127.0.0.1"); + setupData.put(String.format("www.poloniex.com (%s)", address.getHostAddress()), "www.poloniex.com should not point to 127.0.0.1"); } } catch (UnknownHostException e) { setupData.put("www.poloniex.com", e.getMessage()); @@ -91,20 +111,18 @@ public class CheckSetupController { String gunbotLocation = util.getEnvProperty("gunbot.location"); if (!StringUtils.isEmpty(gunbotLocation.length())) { if (!gunbotLocation.startsWith("file://")) { - setupData.put("Gunbot location", "Your file location should start with 'file://'"); + setupData.put("Gunbot location (Optional)", "Your file location should start with 'file://'"); } else { - File file = new File(gunbotLocation.replace("file://","")); + File file = new File(gunbotLocation.replace("file://", "")); if (file.exists()) { - setupData.put("Gunbot location", "Looking good!"); + setupData.put("Gunbot location (Optional)", "Looking good!"); } else { - setupData.put("Gunbot location", "The specified location does not exist"); + setupData.put("Gunbot location (Optional)", "The specified location does not exist"); } } } else { - setupData.put("Gunbot location", "You cannot use monitoring without this"); + setupData.put("Gunbot location (Optional)", "You cannot use monitoring without this"); } - - modelMap.put("setupData", setupData); - return new ModelAndView("setupData", modelMap); + return setupData; } } diff --git a/src/main/java/nl/komtek/gpi/controllers/GunbotProxyController.java b/src/main/java/nl/komtek/gpi/controllers/GunbotProxyController.java index e2a0390..f74fcfe 100644 --- a/src/main/java/nl/komtek/gpi/controllers/GunbotProxyController.java +++ b/src/main/java/nl/komtek/gpi/controllers/GunbotProxyController.java @@ -8,6 +8,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import nl.komtek.gpi.services.GunbotProxyService; +import nl.komtek.gpi.utils.Util; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -18,6 +19,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.math.BigDecimal; import java.time.ZoneOffset; import java.util.List; @@ -35,6 +37,8 @@ public class GunbotProxyController { private boolean doubleBuyProtection; @Value("${doubleBuyProtectionSeconds:0}") private int doubleBuyProtectionSeconds; + @Autowired + private Util util; private Logger logger = LogManager.getLogger(GunbotProxyController.class); private PoloniexDataMapper mapper = new PoloniexDataMapper(); @@ -138,10 +142,20 @@ public class GunbotProxyController { @RequestMapping(value = "/tradingApi/**", params = "command=buy") @ResponseBody public String tradingRequestBuy(HttpServletRequest request, + HttpServletResponse response, @RequestParam String currencyPair, @RequestParam BigDecimal rate, @RequestParam BigDecimal amount) { + boolean globalSellOnlyMode = Boolean.parseBoolean(util.getConfigurationProperty("sellOnlyMode")); + boolean pairSellOnlyMode = Boolean.parseBoolean(util.getConfigurationProperty(String.format("%s_sellOnlyMode", currencyPair))); + if (globalSellOnlyMode || pairSellOnlyMode) { + String message = String.format("You are not allowed to buy. Sell Only mode is active for %s", currencyPair); + logger.info(message); + response.setStatus(403); + return message; + } + String key = request.getHeader("key"); if (doubleBuyProtection || doubleBuyProtectionSeconds > 0) { return gunbotProxyService.buyOrderWithProtection(key, currencyPair, rate, amount); diff --git a/src/main/java/nl/komtek/gpi/services/GunbotProxyService.java b/src/main/java/nl/komtek/gpi/services/GunbotProxyService.java index fb0bf2c..37912c9 100644 --- a/src/main/java/nl/komtek/gpi/services/GunbotProxyService.java +++ b/src/main/java/nl/komtek/gpi/services/GunbotProxyService.java @@ -13,6 +13,7 @@ import nl.komtek.gpi.utils.Util; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; @@ -40,6 +41,8 @@ public class GunbotProxyService { private ApplicationContext applicationContext; @Autowired private Util util; + @Value("${connection.maxRetries:6}") + private int maxRetries; private Map poloniexDefaultAPIClients = new HashMap<>(); private Map poloniexTradingAPIClients = new HashMap<>(); private Map marketMapping = new HashMap<>(); @@ -81,7 +84,7 @@ public class GunbotProxyService { retryPolicy = new RetryPolicy() .retryOn(failure -> failure instanceof Exception) .withDelay(500, TimeUnit.MILLISECONDS) - .withMaxRetries(6); + .withMaxRetries(maxRetries); } private boolean createMarketDefaultApiClients(String market) { diff --git a/src/main/java/nl/komtek/gpi/utils/CaseLessProperties.java b/src/main/java/nl/komtek/gpi/utils/CaseLessProperties.java new file mode 100644 index 0000000..5f761d2 --- /dev/null +++ b/src/main/java/nl/komtek/gpi/utils/CaseLessProperties.java @@ -0,0 +1,24 @@ +package nl.komtek.gpi.utils; + +/** + * Created by Elroy on 13-7-2017. + */ +import java.util.Properties; + +public class CaseLessProperties extends Properties { + + public Object put(Object key, Object value) { + String lowercase = ((String) key).toLowerCase(); + return super.put(lowercase, value); + } + + public String getProperty(String key) { + String lowercase = key.toLowerCase(); + return super.getProperty(lowercase); + } + + public String getProperty(String key, String defaultValue) { + String lowercase = key.toLowerCase(); + return super.getProperty(lowercase, defaultValue); + } +} \ No newline at end of file diff --git a/src/main/java/nl/komtek/gpi/utils/Util.java b/src/main/java/nl/komtek/gpi/utils/Util.java index 1b6cca3..9e6865b 100644 --- a/src/main/java/nl/komtek/gpi/utils/Util.java +++ b/src/main/java/nl/komtek/gpi/utils/Util.java @@ -1,10 +1,16 @@ package nl.komtek.gpi.utils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; +import java.io.FileInputStream; +import java.io.InputStream; +import java.util.Properties; + /** * Created by Elroy on 10-7-2017. */ @@ -13,9 +19,23 @@ public class Util { @Autowired private Environment environment; - + private Logger logger = LogManager.getLogger(Util.class); + public String getEnvProperty(String key) { String value = StringUtils.trimAllWhitespace(environment.getProperty(key)); return StringUtils.replace(value, "\"", ""); } + + public String getConfigurationProperty(String key) { + CaseLessProperties prop = new CaseLessProperties(); + String value = null; + try (InputStream input = new FileInputStream("configuration.properties")) { + prop.load(input); + value = prop.getProperty(key); + logger.debug(String.format("reading property key %s -- value %s"), key, value); + } catch (Exception e) { + logger.error("Error reading configuration file", e.getMessage()); + } + return value; + } }