Skip to content

Commit

Permalink
support gitee's raw content.
Browse files Browse the repository at this point in the history
  • Loading branch information
ainilili committed Dec 19, 2020
1 parent 5993649 commit e249528
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
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;
import org.nico.ratel.landlords.print.SimplePrinter;
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 {

public static int id = -1;

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) {
Expand All @@ -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<String> serverAddressList = Noson.convert(serverInfo, new NoType<List<String>>() {});

if(serverAddress == null){
List<String> 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"));
Expand All @@ -70,7 +79,18 @@ public static void main(String[] args) throws InterruptedException, IOException
} finally {
group.shutdownGracefully().sync();
}
}

private static List<String> getServerAddressList(){
for(String serverAddressSource: serverAddressSource) {
try {
String serverInfo = StreamUtils.convertToString(new URL(serverAddressSource));
return Noson.convert(serverInfo, new NoType<List<String>>() {});
} catch (IOException e) {
SimplePrinter.printNotice("Try connected " + serverAddressSource + " failed: " + e.getMessage());
}
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down

0 comments on commit e249528

Please sign in to comment.