diff --git a/jargyle-client/src/main/java/com/github/jh3nd3rs0n/jargyle/client/socks5/MethodSubnegotiator.java b/jargyle-client/src/main/java/com/github/jh3nd3rs0n/jargyle/client/socks5/MethodSubnegotiator.java index 28d288ed2..dff82f5dc 100644 --- a/jargyle-client/src/main/java/com/github/jh3nd3rs0n/jargyle/client/socks5/MethodSubnegotiator.java +++ b/jargyle-client/src/main/java/com/github/jh3nd3rs0n/jargyle/client/socks5/MethodSubnegotiator.java @@ -5,6 +5,7 @@ import java.io.OutputStream; import java.net.Socket; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -196,6 +197,25 @@ public MethodEncapsulation subnegotiate( } } + + private static final class MethodSubnegotiators { + + private final Map methodSubnegotiatorsMap; + + public MethodSubnegotiators() { + this.methodSubnegotiatorsMap = + new HashMap(); + } + + public void add(final MethodSubnegotiator value) { + this.methodSubnegotiatorsMap.put(value.getMethod(), value); + } + + public Map toMap() { + return Collections.unmodifiableMap(this.methodSubnegotiatorsMap); + } + + } private static final class NoAcceptableMethodsMethodSubnegotiator extends MethodSubnegotiator { @@ -269,39 +289,26 @@ public MethodEncapsulation subnegotiate( } - private static final Map METHOD_SUBNEGOTIATOR_MAP; + private static final Map METHOD_SUBNEGOTIATORS_MAP; static { - METHOD_SUBNEGOTIATOR_MAP = new HashMap(); - MethodSubnegotiator gssapiMethodSubnegotiator = - new GssapiMethodSubnegotiator(); - METHOD_SUBNEGOTIATOR_MAP.put( - gssapiMethodSubnegotiator.getMethod(), - gssapiMethodSubnegotiator); - MethodSubnegotiator noAcceptableMethodsMethodSubnegotiator = - new NoAcceptableMethodsMethodSubnegotiator(); - METHOD_SUBNEGOTIATOR_MAP.put( - noAcceptableMethodsMethodSubnegotiator.getMethod(), - noAcceptableMethodsMethodSubnegotiator); - MethodSubnegotiator noAuthenticationRequiredMethodSubnegotiator = - new NoAuthenticationRequiredMethodSubnegotiator(); - METHOD_SUBNEGOTIATOR_MAP.put( - noAuthenticationRequiredMethodSubnegotiator.getMethod(), - noAuthenticationRequiredMethodSubnegotiator); - MethodSubnegotiator usernamePasswordMethodSubnegotiator = - new UsernamePasswordMethodSubnegotiator(); - METHOD_SUBNEGOTIATOR_MAP.put( - usernamePasswordMethodSubnegotiator.getMethod(), - usernamePasswordMethodSubnegotiator); + MethodSubnegotiators methodSubnegotiators = new MethodSubnegotiators(); + methodSubnegotiators.add(new GssapiMethodSubnegotiator()); + methodSubnegotiators.add(new NoAcceptableMethodsMethodSubnegotiator()); + methodSubnegotiators.add( + new NoAuthenticationRequiredMethodSubnegotiator()); + methodSubnegotiators.add(new UsernamePasswordMethodSubnegotiator()); + METHOD_SUBNEGOTIATORS_MAP = new HashMap( + methodSubnegotiators.toMap()); } public static MethodSubnegotiator getInstance(final Method meth) { - MethodSubnegotiator methodSubnegotiator = METHOD_SUBNEGOTIATOR_MAP.get( + MethodSubnegotiator methodSubnegotiator = METHOD_SUBNEGOTIATORS_MAP.get( meth); if (methodSubnegotiator != null) { return methodSubnegotiator; } - String str = METHOD_SUBNEGOTIATOR_MAP.keySet().stream() + String str = METHOD_SUBNEGOTIATORS_MAP.keySet().stream() .map(Method::toString) .collect(Collectors.joining(", ")); throw new IllegalArgumentException(String.format( diff --git a/jargyle-server/src/main/java/com/github/jh3nd3rs0n/jargyle/server/internal/server/socks5/MethodSubnegotiator.java b/jargyle-server/src/main/java/com/github/jh3nd3rs0n/jargyle/server/internal/server/socks5/MethodSubnegotiator.java index 719d0833f..ef7f36574 100644 --- a/jargyle-server/src/main/java/com/github/jh3nd3rs0n/jargyle/server/internal/server/socks5/MethodSubnegotiator.java +++ b/jargyle-server/src/main/java/com/github/jh3nd3rs0n/jargyle/server/internal/server/socks5/MethodSubnegotiator.java @@ -5,6 +5,7 @@ import java.io.OutputStream; import java.net.Socket; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -206,7 +207,26 @@ public MethodSubnegotiationResults subnegotiate( } } - + + private static final class MethodSubnegotiators { + + private final Map methodSubnegotiatorsMap; + + public MethodSubnegotiators() { + this.methodSubnegotiatorsMap = + new HashMap(); + } + + public void add(final MethodSubnegotiator value) { + this.methodSubnegotiatorsMap.put(value.getMethod(), value); + } + + public Map toMap() { + return Collections.unmodifiableMap(this.methodSubnegotiatorsMap); + } + + } + private static final class NoAcceptableMethodsMethodSubnegotiator extends MethodSubnegotiator { @@ -307,39 +327,26 @@ public MethodSubnegotiationResults subnegotiate( } - private static final Map METHOD_SUBNEGOTIATOR_MAP; + private static final Map METHOD_SUBNEGOTIATORS_MAP; static { - METHOD_SUBNEGOTIATOR_MAP = new HashMap(); - MethodSubnegotiator gssapiMethodSubnegotiator = - new GssapiMethodSubnegotiator(); - METHOD_SUBNEGOTIATOR_MAP.put( - gssapiMethodSubnegotiator.getMethod(), - gssapiMethodSubnegotiator); - MethodSubnegotiator noAcceptableMethodsMethodSubnegotiator = - new NoAcceptableMethodsMethodSubnegotiator(); - METHOD_SUBNEGOTIATOR_MAP.put( - noAcceptableMethodsMethodSubnegotiator.getMethod(), - noAcceptableMethodsMethodSubnegotiator); - MethodSubnegotiator noAuthenticationRequiredMethodSubnegotiator = - new NoAuthenticationRequiredMethodSubnegotiator(); - METHOD_SUBNEGOTIATOR_MAP.put( - noAuthenticationRequiredMethodSubnegotiator.getMethod(), - noAuthenticationRequiredMethodSubnegotiator); - MethodSubnegotiator usernamePasswordMethodSubnegotiator = - new UsernamePasswordMethodSubnegotiator(); - METHOD_SUBNEGOTIATOR_MAP.put( - usernamePasswordMethodSubnegotiator.getMethod(), - usernamePasswordMethodSubnegotiator); + MethodSubnegotiators methodSubnegotiators = new MethodSubnegotiators(); + methodSubnegotiators.add(new GssapiMethodSubnegotiator()); + methodSubnegotiators.add(new NoAcceptableMethodsMethodSubnegotiator()); + methodSubnegotiators.add( + new NoAuthenticationRequiredMethodSubnegotiator()); + methodSubnegotiators.add(new UsernamePasswordMethodSubnegotiator()); + METHOD_SUBNEGOTIATORS_MAP = new HashMap( + methodSubnegotiators.toMap()); } public static MethodSubnegotiator getInstance(final Method meth) { - MethodSubnegotiator methodSubnegotiator = METHOD_SUBNEGOTIATOR_MAP.get( + MethodSubnegotiator methodSubnegotiator = METHOD_SUBNEGOTIATORS_MAP.get( meth); if (methodSubnegotiator != null) { return methodSubnegotiator; } - String str = METHOD_SUBNEGOTIATOR_MAP.keySet().stream() + String str = METHOD_SUBNEGOTIATORS_MAP.keySet().stream() .map(Method::toString) .collect(Collectors.joining(", ")); throw new IllegalArgumentException(String.format(