CLI options added: Address to broadcast games to (defaults to localhost) and port to run webserver on (defaults to 8333)

This commit is contained in:
2025-04-17 21:14:04 +10:00
parent 77dc7a5310
commit dbca745f05
5 changed files with 51 additions and 17 deletions

View File

@@ -1,2 +1,2 @@
rm cls/connect2/*.class
javac -source 1.6 -target 1.6 -classpath src -d cls src/connect2/Main.java && java -classpath cls connect2.Main
javac -source 1.8 -target 1.8 -classpath src -d cls src/connect2/Main.java && java -classpath cls connect2.Main $*

View File

@@ -40,17 +40,27 @@ public class ECHost implements Runnable {
boolean terminated = false;
public ECHost(int war3Version, String name, String sessionKey) {
public ECHost(int war3Version, String name, String sessionKey)
{
this(war3Version, name, sessionKey, "");
}
public ECHost(int war3Version, String name, String sessionKey, String broadcastIp) {
this.war3version = war3Version;
this.name = name;
this.sessionKey = sessionKey;
udpTargets = new ArrayList<SocketAddress>();
try {
udpTargets.add(new InetSocketAddress(InetAddress.getLocalHost(), 6112));
udpTargets.add(new InetSocketAddress("255.255.255.255", 6112));
} catch(UnknownHostException uhe) {
System.out.println("[ECHost] UDP broadcast target error: " + uhe.getLocalizedMessage());
if (broadcastIp!="")
{
udpTargets.add(new InetSocketAddress(broadcastIp, 6112));
}
else
{
try {
udpTargets.add(new InetSocketAddress(InetAddress.getLocalHost(), 6112));
} catch(UnknownHostException uhe) {
System.out.println("[ECHost] UDP broadcast target error: " + uhe.getLocalizedMessage());
}
}
this.games = new HashMap<Integer, GameInfo>();

View File

@@ -9,8 +9,15 @@ import java.net.URI;
public class GuiApp implements ActionListener {
JButton launchBtn, exitBtn;
int port;
public GuiApp(){
public GuiApp()
{
this(8333);
}
public GuiApp(int port)
{
this.port=port;
JFrame frame = new JFrame("WC3Connect-Java");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.launchBtn = new JButton("Launch");
@@ -22,7 +29,7 @@ public class GuiApp implements ActionListener {
if(!Desktop.isDesktopSupported() || !Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)){
this.launchBtn.setEnabled(false);
this.launchBtn.setText("Open your webbrowser and go to: 127.0.0.1:8033");
this.launchBtn.setText("Open your webbrowser and go to: http://localhost:"+this.port+"/");
}
frame.getContentPane().add(this.launchBtn, BorderLayout.PAGE_START);
@@ -34,7 +41,7 @@ public class GuiApp implements ActionListener {
private void launch(){
try {
Desktop.getDesktop().browse(URI.create("http://127.0.0.1:8333"));
Desktop.getDesktop().browse(URI.create("http://localhost:"+this.port+"/"));
} catch (IOException e1) {
e1.printStackTrace();
}

View File

@@ -10,16 +10,21 @@ public class Main {
public static int Version = 2018102602;
public static void main(String[] args) throws java.io.IOException {
int port;
String broadcastIp;
if (args.length<2) {broadcastIp="";} else {broadcastIp=args[1];}
if (args.length<1) {port=8333;} else {port=Integer.parseInt(args[0]);}
ThirdPartyBotMap.put("192.99.6.98", "MMH-USA");
ThirdPartyBotMap.put("85.10.199.252", "MMH-Euro");
new Thread(new LoadThirdPartyThread()).start();
new Web();
new Web(port,broadcastIp);
if(!GraphicsEnvironment.isHeadless()){
new GuiApp();
new GuiApp(port);
} else {
System.out.println("Open your webbrowser and go to: 127.0.0.1:8033");
System.out.println("Open your web browser and go to: http://localhost:"+port+"/");
}
}

View File

@@ -24,7 +24,17 @@ public class Web {
private ECHost host = null;
private ECList list = null;
public Web() throws IOException {
public String broadcastIp;
public Web() throws IOException
{
this(8333);
}
public Web(int port) throws IOException
{
this(port,"");
}
public Web(int port, String broadcastIp) throws IOException {
if(Main.Debug) {
Logger logger = Logger.getLogger("com.sun.net.httpserver");
ConsoleHandler ch = new ConsoleHandler();
@@ -32,7 +42,7 @@ public class Web {
ch.setLevel(Level.FINER);
logger.addHandler(ch);
}
HttpServer server = HttpServer.create(new InetSocketAddress(8333), 0);
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
server.createContext("/", new FileHandler());
server.createContext("/login", new LoginHandler(this));
server.createContext("/signup", new SignupHandler());
@@ -44,7 +54,9 @@ public class Web {
server.createContext("/validate", new ValidateHandler(this));
server.setExecutor(null);
server.start();
System.out.println("[web] started on :8333");
System.out.println("[web] started on :"+port);
this.broadcastIp=broadcastIp;
}
static Map<String, String> getPostForm(HttpExchange httpExchange) throws IOException {
@@ -239,7 +251,7 @@ public class Web {
synchronized(web) {
web.loginResult = new LoginResult(obj.getInt("war3version"), obj.getString("name"), obj.getString("session_key2"));
if(web.host == null) {
web.host = new ECHost(web.loginResult.war3Version, web.loginResult.name, web.loginResult.sessionKey);
web.host = new ECHost(web.loginResult.war3Version, web.loginResult.name, web.loginResult.sessionKey, web.broadcastIp);
web.host.init();
} else {
web.host.update(web.loginResult.war3Version, web.loginResult.name, web.loginResult.sessionKey);