From d65506e420fbd7b32756f271fa9e1bc6cf851472 Mon Sep 17 00:00:00 2001 From: Marko Zajc Date: Sat, 2 Jan 2021 16:52:14 +0100 Subject: [PATCH] Support IPv6 addresses properly (#4225) * Support IPv6 addresses properly support the `[ipv6]:port` and `ipv6` formats silly cat forgot that ipv6 is a thing * unnecessary self promotion * less split more substring * also display ipv6 format properly * important security fixes --- core/assets/contributors | 3 ++- core/src/mindustry/ui/dialogs/JoinDialog.java | 21 ++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/core/assets/contributors b/core/assets/contributors index db86054e93..59f40b3ef2 100644 --- a/core/assets/contributors +++ b/core/assets/contributors @@ -111,4 +111,5 @@ Quick-Korx Catchears younggam simba-fs -RedRadiation \ No newline at end of file +RedRadiation +Marko Zajc diff --git a/core/src/mindustry/ui/dialogs/JoinDialog.java b/core/src/mindustry/ui/dialogs/JoinDialog.java index 2d9f565c7b..4f858a67ff 100644 --- a/core/src/mindustry/ui/dialogs/JoinDialog.java +++ b/core/src/mindustry/ui/dialogs/JoinDialog.java @@ -563,25 +563,32 @@ public class JoinDialog extends BaseDialog{ transient Host lastHost; void setIP(String ip){ - - //parse ip:port, if unsuccessful, use default values - if(ip.lastIndexOf(':') != -1 && ip.lastIndexOf(':') != ip.length() - 1){ - try{ + try{ + boolean isIpv6 = Strings.count(ip, ':') > 1; + if(isIpv6 && ip.lastIndexOf("]:") != -1 && ip.lastIndexOf("]:") != ip.length() - 1){ + int idx = ip.indexOf("]:"); + this.ip = ip.substring(1, idx); + this.port = Integer.parseInt(ip.substring(idx + 2, ip.length())); + }else if(!isIpv6 && ip.lastIndexOf(':') != -1 && ip.lastIndexOf(':') != ip.length() - 1){ int idx = ip.lastIndexOf(':'); this.ip = ip.substring(0, idx); this.port = Integer.parseInt(ip.substring(idx + 1)); - }catch(Exception e){ + }else{ this.ip = ip; this.port = Vars.port; } - }else{ + }catch(Exception e){ this.ip = ip; this.port = Vars.port; } } String displayIP(){ - return ip + (port != Vars.port ? ":" + port : ""); + if(Strings.count(ip, ':') > 1){ + return port != Vars.port ? "[" + ip + "]:" + port : ip; + }else{ + return ip + (port != Vars.port ? ":" + port : ""); + } } public Server(){