-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Socket #2618
Closed
Closed
Add Socket #2618
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b15be0e
Add Internet
SoniEx2 7bfdb6f
Clean up
SoniEx2 cab1b31
Clean up Checkstyle
SoniEx2 c2c8939
Separate whitelists, configurable.
SoniEx2 6edad37
Cleanup static analysis warnings
SoniEx2 1afca03
Rename Internet -> Socket
SoniEx2 52c3b24
Add SandboxException
SoniEx2 f92ccbd
Fix thread safety issues
SoniEx2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
150 changes: 150 additions & 0 deletions
150
engine/src/main/java/org/terasology/config/SocketConfig.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 |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* Copyright 2016 MovingBlocks | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.terasology.config; | ||
|
||
import com.google.common.collect.Maps; | ||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonNull; | ||
import com.google.gson.JsonParseException; | ||
import com.google.gson.JsonSerializationContext; | ||
import com.google.gson.JsonSerializer; | ||
import gnu.trove.set.TIntSet; | ||
import gnu.trove.set.hash.TIntHashSet; | ||
import org.terasology.naming.Name; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
|
||
/** | ||
* Configuration file for all the socket BS you can think of. | ||
*/ | ||
// TODO make thread safe and stuff | ||
public class SocketConfig { | ||
/** | ||
* All the TCP whitelists. Keyed by module ID. | ||
*/ | ||
private Map<Name, Hosts> tcpWhitelists = Maps.newTreeMap(); | ||
|
||
/** | ||
* Retrieves the TCP whitelists. | ||
* | ||
* @return The TCP whitelists. | ||
*/ | ||
public Map<Name, Hosts> getTcpWhitelists() { | ||
return tcpWhitelists; | ||
} | ||
|
||
/** | ||
* POJO representing the hosts a module is allowed to access. | ||
*/ | ||
public static class Hosts { | ||
/** | ||
* The hosts themselves. | ||
*/ | ||
private Map<String, Ports> hosts = Maps.newTreeMap(); | ||
|
||
/** | ||
* Retrieves the hosts. | ||
* | ||
* @return The hosts. | ||
*/ | ||
public Map<String, Ports> getHosts() { | ||
return hosts; | ||
} | ||
|
||
public static class Handler implements JsonSerializer<Hosts>, JsonDeserializer<Hosts> { | ||
|
||
@Override | ||
public Hosts deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||
// We need this for the treemap. | ||
Map<String, Ports> map; | ||
try { | ||
// Ugh java | ||
map = context.deserialize(json, Hosts.class.getDeclaredField("hosts").getGenericType()); | ||
} catch (Exception e) { | ||
// Who cares | ||
throw new JsonParseException(e); | ||
} | ||
if (map == null) { | ||
return null; | ||
} | ||
Hosts h = new Hosts(); | ||
h.hosts = map; | ||
return h; | ||
} | ||
|
||
@Override | ||
public JsonElement serialize(Hosts src, Type typeOfSrc, JsonSerializationContext context) { | ||
if (src == null) { | ||
return JsonNull.INSTANCE; | ||
} | ||
return context.serialize(src.hosts); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* POJO representing the ports a module is allowed to access. | ||
*/ | ||
public static class Ports { | ||
/** | ||
* The ports themselves. | ||
*/ | ||
private TIntSet ports = new TIntHashSet(); | ||
|
||
/** | ||
* Retrieves the ports. | ||
* | ||
* @return The ports. | ||
*/ | ||
public TIntSet getPorts() { | ||
return ports; | ||
} | ||
|
||
public static class Handler implements JsonSerializer<Ports>, JsonDeserializer<Ports> { | ||
@Override | ||
public Ports deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||
// We need this custom deserialization (and this class) for 2 reasons: | ||
// 1. We need to control TIntHashSet's no_entry_value. | ||
// 2. We need to sanitize user input. | ||
int[] array = context.deserialize(json, int[].class); | ||
if (array == null) { | ||
return null; | ||
} | ||
Arrays.stream(array).filter(x -> (x < 1 || x > 65535) && x != -1).findAny().ifPresent(x -> { | ||
throw new IllegalStateException("Port must be in range 1 <= port <= 65535, or -1 for wildcard value: " + x); | ||
}); | ||
TIntHashSet hashSet = new TIntHashSet(10, .5f, Integer.MIN_VALUE); | ||
hashSet.addAll(array); | ||
Ports p = new Ports(); | ||
p.ports = hashSet; | ||
return p; | ||
} | ||
|
||
@Override | ||
public JsonElement serialize(Ports src, Type typeOfSrc, JsonSerializationContext context) { | ||
if (src == null) { | ||
return JsonNull.INSTANCE; | ||
} | ||
// Just defer to standard TIntHashSet serialization. This is fine. | ||
return context.serialize(src.ports); | ||
} | ||
} | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to keep the Context interface and the injection concept as simple as possible.
Some idea I could think of to simplify it a little bit would be to have
Context
have ainject
method so that we don't need to have a method that we must protect against all but one caller. Although I am not that happy with that idea.