{"action":"created","issue":{"url":"https://api.github.com/repos/xetorthio/jedis/issues/1023","labels_url":"https://api.github.com/repos/xetorthio/jedis/issues/1023/labels{/name}","comments_url":"https://api.github.com/repos/xetorthio/jedis/issues/1023/comments","events_url":"https://api.github.com/repos/xetorthio/jedis/issues/1023/events","html_url":"https://github.com/xetorthio/jedis/issues/1023","id":87392383,"number":1023,"title":"Automatically change localhost references in HostAndPort to the address of the current machine","user":{"login":"allanwax","id":8459774,"avatar_url":"https://avatars.githubusercontent.com/u/8459774?v=3","gravatar_id":"","url":"https://api.github.com/users/allanwax","html_url":"https://github.com/allanwax","followers_url":"https://api.github.com/users/allanwax/followers","following_url":"https://api.github.com/users/allanwax/following{/other_user}","gists_url":"https://api.github.com/users/allanwax/gists{/gist_id}","starred_url":"https://api.github.com/users/allanwax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/allanwax/subscriptions","organizations_url":"https://api.github.com/users/allanwax/orgs","repos_url":"https://api.github.com/users/allanwax/repos","events_url":"https://api.github.com/users/allanwax/events{/privacy}","received_events_url":"https://api.github.com/users/allanwax/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-06-11T15:52:18Z","updated_at":"2015-06-12T16:12:56Z","closed_at":null,"body":"First, apologies as I'm still sick (today=2015-06-11) so if there are stupid errors below, please forgive.\r\n\r\nMany people get caught specifying localhost/127.0.0.1 in configuration values for jedis cluster.  It is fine for jedis.  I myself got caught when I first started using it.  I propose to help by automatically replacing localhost references to the IP address of the local machine.  Thus as information gets passed around it will contain an absolute address rather than a relative one.\r\n\r\nHere's the suggested new class.  NOTE `169.254` is a Microsoft special address sometimes used if the machine can't get to the internet.  I.e. the connections window will show a connection to the router but none to the internet.\r\n\r\n```java\r\npackage redis.clients.jedis;\r\n\r\nimport java.net.InetAddress;\r\n\r\npublic class HostAndPort {\r\n\tpublic static final String LOCALHOST_STR ;\r\n\t\r\n\tstatic {\r\n\t\ttry {\r\n\t\t\tLOCALHOST_STR = InetAddress.getLocalHost().getAddress();\r\n\t\t}\r\n\t\tcatch (Exception e) {\r\n\t\t\tLOCALHOST_STR = \"localhost\";\r\n\t\t}\r\n\t}\r\n\r\n\tprivate String host;\r\n\tprivate int port;\r\n\r\n\tpublic HostAndPort(String host, int port) {\r\n\t\tthis.host = host;\r\n\t\tthis.port = port;\r\n\t}\r\n\r\n\tpublic String getHost() {\r\n\t\treturn host;\r\n\t}\r\n\r\n\tpublic int getPort() {\r\n\t\treturn port;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic boolean equals(Object obj) {\r\n                if (obj == null) return false;\r\n\r\n\t\tif (obj instanceof HostAndPort) {\r\n\t\t\tHostAndPort hp = (HostAndPort) obj;\r\n\t\t\tString thisHost = convertHost(host);\r\n\t\t\tString hpHost = convertHost(hp.host);\r\n\t\t\treturn port == hp.port && thisHost.equals(hpHost);\r\n\t\t}\r\n\r\n\t\treturn false;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic int hashCode() {\r\n\t\treturn 31 * convertHost(host).hashCode() + port;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic String toString() {\r\n\t\treturn host + \":\" + port;\r\n\t}\r\n\r\n\tprivate String convertHost(String host) {\r\n\t\t if (host.equals(\"127.0.0.1\") || host.startsWith(\"localhost\") || host.equals(\"0.0.0.0\") || host.startsWith(\"169.254\"))\r\n\t\t\t return LOCALHOST_STR;\r\n         else if (host.startsWith(\"::1\") || host.startsWith(\"0:0:0:0:0:0:0:1\"))\r\n\t\t\treturn LOCALHOST_STR;\r\n\r\n\t\treturn host;\r\n\t}\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/xetorthio/jedis/issues/comments/111539832","html_url":"https://github.com/xetorthio/jedis/issues/1023#issuecomment-111539832","issue_url":"https://api.github.com/repos/xetorthio/jedis/issues/1023","id":111539832,"user":{"login":"allanwax","id":8459774,"avatar_url":"https://avatars.githubusercontent.com/u/8459774?v=3","gravatar_id":"","url":"https://api.github.com/users/allanwax","html_url":"https://github.com/allanwax","followers_url":"https://api.github.com/users/allanwax/followers","following_url":"https://api.github.com/users/allanwax/following{/other_user}","gists_url":"https://api.github.com/users/allanwax/gists{/gist_id}","starred_url":"https://api.github.com/users/allanwax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/allanwax/subscriptions","organizations_url":"https://api.github.com/users/allanwax/orgs","repos_url":"https://api.github.com/users/allanwax/repos","events_url":"https://api.github.com/users/allanwax/events{/privacy}","received_events_url":"https://api.github.com/users/allanwax/received_events","type":"User","site_admin":false},"created_at":"2015-06-12T16:12:56Z","updated_at":"2015-06-12T16:12:56Z","body":"Missed it.  Try this change\r\n\r\n```java\r\npublic HostAndPort(String host, int port) {\r\n    this.host = convertHost(host);\r\n    this.port = port;\r\n  }\r\n```\r\n\r\nthen every time a HostAndPort is created it will be processed before being stored."}}