Report external ip address to api

This commit is contained in:
Electroid 2017-07-26 17:21:36 -07:00
parent 736803709c
commit 50ef439922
2 changed files with 23 additions and 2 deletions

View File

@ -50,7 +50,8 @@ public interface ServerDoc {
}
@Serialize
interface CurrentPort extends Partial {
interface Ip extends Partial {
String ip();
Integer current_port();
}
@ -95,7 +96,7 @@ public interface ServerDoc {
* Startup info sent to the API
*/
@Serialize
interface Startup extends Online, CurrentPort {
interface Startup extends Online, Ip {
@Nullable DeployInfo deploy_info();
Map<String, String> plugin_versions();
Set<Integer> protocol_versions();

View File

@ -1,9 +1,14 @@
package tc.oc.api.minecraft.servers;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.file.Path;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import javax.inject.Inject;
@ -51,10 +56,25 @@ public class StartupServerDocument implements ServerDoc.Startup {
}
});
private final Lazy<String> ip = Lazy.from(() -> {
try {
URL url = new URL("http://checkip.amazonaws.com");
return new BufferedReader(new InputStreamReader(url.openStream())).readLine();
} catch(IOException e) {
logger.log(Level.SEVERE, "Unable to find external ip", e);
return minecraftServer.getAddress().getHostName();
}
});
@Override public boolean online() {
return true;
}
@Override
public String ip() {
return ip.get();
}
@Override public Integer current_port() {
return minecraftServer.getAddress().getPort();
}