diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java index 5511d97..bfc3979 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java @@ -1,10 +1,9 @@ package org.nico.ratel.landlords.client; -import io.netty.bootstrap.Bootstrap; -import io.netty.channel.Channel; -import io.netty.channel.EventLoopGroup; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.nio.NioSocketChannel; +import java.io.IOException; +import java.net.URL; +import java.util.List; + import org.nico.noson.Noson; import org.nico.noson.entity.NoType; import org.nico.ratel.landlords.client.handler.DefaultChannelInitializer; @@ -12,9 +11,11 @@ import org.nico.ratel.landlords.print.SimpleWriter; import org.nico.ratel.landlords.utils.StreamUtils; -import java.io.IOException; -import java.net.URL; -import java.util.List; +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.Channel; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioSocketChannel; public class SimpleClient { @@ -22,7 +23,12 @@ public class SimpleClient { public static String serverAddress; - public static int port; + public static int port = 80; + + private static String[] serverAddressSource = new String[] { + "https://raw.githubusercontent.com/ainilili/ratel/master/serverlist.json", + "https://gitee.com/ainilili/ratel/raw/master/serverlist.json" + }; public static void main(String[] args) throws InterruptedException, IOException { if(args != null && args.length > 0) { @@ -37,16 +43,19 @@ public static void main(String[] args) throws InterruptedException, IOException } } } - - if(serverAddress == null || port == 0){ - String serverInfo = StreamUtils.convertToString(new URL("https://raw.githubusercontent.com/ainilili/ratel/master/serverlist.json")); - List serverAddressList = Noson.convert(serverInfo, new NoType>() {}); + + if(serverAddress == null){ + List serverAddressList = getServerAddressList(); + if(serverAddressList == null || serverAddressList.size() == 0) { + throw new RuntimeException("Please use '-host' to setting server address."); + } + SimplePrinter.printNotice("Please select a server:"); for(int i = 0; i < serverAddressList.size(); i++) { SimplePrinter.printNotice((i+1) + ". " + serverAddressList.get(i)); } int serverPick = Integer.parseInt(SimpleWriter.write("option")); - while(serverPick<1 || serverPick>serverAddressList.size()){ + while(serverPick < 1 || serverPick > serverAddressList.size()){ try { SimplePrinter.printNotice("The server address does not exist!"); serverPick = Integer.parseInt(SimpleWriter.write("option")); @@ -70,7 +79,18 @@ public static void main(String[] args) throws InterruptedException, IOException } finally { group.shutdownGracefully().sync(); } + } + private static List getServerAddressList(){ + for(String serverAddressSource: serverAddressSource) { + try { + String serverInfo = StreamUtils.convertToString(new URL(serverAddressSource)); + return Noson.convert(serverInfo, new NoType>() {}); + } catch (IOException e) { + SimplePrinter.printNotice("Try connected " + serverAddressSource + " failed: " + e.getMessage()); + } + } + return null; } } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/utils/StreamUtils.java b/landlords-common/src/main/java/org/nico/ratel/landlords/utils/StreamUtils.java index eda30cf..c3db2dc 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/utils/StreamUtils.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/utils/StreamUtils.java @@ -41,6 +41,7 @@ public static String convertToString(InputStream inStream){ public static String convertToString(URL url) throws IOException { URLConnection con = url.openConnection(); con.setUseCaches(false); + con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36"); return convertToString(con.getInputStream()); }