-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aebfb51
commit 4834897
Showing
12 changed files
with
125 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package net.logandhillon.icx.common; | ||
|
||
import java.net.InetAddress; | ||
import java.security.SecureRandom; | ||
import java.util.Base64; | ||
|
||
public class SNVS { | ||
private static final SecureRandom RANDOM = new SecureRandom(); | ||
public static final String SEPARATOR = "\u001f"; | ||
|
||
/** | ||
* @return 256-bit (32 char) b64 token | ||
*/ | ||
public static String genToken() { | ||
byte[] key = new byte[24]; // 24 chars = 32 char b64 | ||
RANDOM.nextBytes(key); | ||
|
||
return Base64.getUrlEncoder().withoutPadding().encodeToString(key); | ||
} | ||
|
||
public record InetToken(InetAddress registrant, String token) { | ||
} | ||
|
||
public record Token(String name, String token) { | ||
@Override | ||
public String toString() { | ||
return name + SEPARATOR + token; | ||
} | ||
|
||
public static boolean validate(String snvs) { | ||
return snvs != null && !snvs.isBlank() && !snvs.contains(SEPARATOR) && !snvs.contains("$"); | ||
} | ||
|
||
public boolean validate() { | ||
return SNVS.Token.validate(this.name); | ||
} | ||
|
||
public static Token fromString(String payload) throws IllegalArgumentException { | ||
String[] snvs = payload.split(SEPARATOR, 2); | ||
if (snvs[1].contains(SEPARATOR) || snvs[1].contains("$")) | ||
throw new IllegalArgumentException("Invalid screen name"); | ||
if (snvs[0] == null || snvs[0].isEmpty() || snvs[1].isEmpty()) | ||
throw new IllegalArgumentException("Malformed SNVS"); | ||
return new Token(snvs[0], snvs[1]); | ||
} | ||
|
||
public Token withoutToken() { | ||
return new Token(name, null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 15 additions & 11 deletions
26
src/main/java/net/logandhillon/icx/server/NameRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,32 @@ | ||
package net.logandhillon.icx.server; | ||
|
||
import net.logandhillon.icx.common.SNVS; | ||
import org.apache.logging.log4j.Logger; | ||
import org.apache.logging.log4j.core.LoggerContext; | ||
|
||
import java.net.InetAddress; | ||
import java.util.HashMap; | ||
import java.util.Objects; | ||
|
||
public class NameRegistry { | ||
private static final Logger LOG = LoggerContext.getContext().getLogger(NameRegistry.class); | ||
private static final HashMap<String, InetAddress> REGISTRY = new HashMap<>(); | ||
private static final HashMap<String, SNVS.InetToken> REGISTRY = new HashMap<>(); | ||
public static final SNVS.Token SERVER = new SNVS.Token("SERVER", SNVS.genToken()); | ||
|
||
public void registerName(String name, InetAddress registrant) { | ||
if (REGISTRY.containsKey(name)) throw new RuntimeException("Name taken"); | ||
LOG.info("Registering '{}' to {}", name, registrant); | ||
REGISTRY.put(name, registrant); | ||
public void registerName(SNVS.Token snvs, InetAddress registrant) { | ||
if (REGISTRY.containsKey(snvs.name())) throw new RuntimeException("Name taken"); | ||
if (!snvs.validate()) throw new RuntimeException("Malformed or invalid SNVS"); | ||
LOG.info("Registering '{}' to {}", snvs.name(), registrant); | ||
REGISTRY.put(snvs.name(), new SNVS.InetToken(registrant, snvs.token())); | ||
} | ||
|
||
public void releaseName(String name) { | ||
if (name == null) return; | ||
LOG.info("Releasing '{}'", name); | ||
REGISTRY.remove(name); | ||
public void releaseName(SNVS.Token snvs) { | ||
if (snvs == null || snvs.name() == null) return; | ||
LOG.info("Releasing '{}'", snvs.name()); | ||
REGISTRY.remove(snvs.name()); | ||
} | ||
|
||
public boolean verifyName(String name, InetAddress addr) { | ||
return REGISTRY.containsKey(name) && REGISTRY.get(name) == addr; | ||
public boolean verifyName(SNVS.Token snvs, InetAddress origin) { | ||
return REGISTRY.containsKey(snvs.name()) && REGISTRY.get(snvs.name()).registrant() == origin && Objects.equals(REGISTRY.get(snvs.name()).token(), snvs.token()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.