Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Proxy
- {
- private int listenPort = 25000;
- private int remotePort = 25565;
- private String remoteHost = "localhost";
- private String rulesFileName;
- private Action defaultAction = Action.ACCEPT;
- private ArrayList<Rule> rules = new ArrayList();
- private volatile boolean isWorking = true;
- private static DateTimeFormatter formatter;
- private boolean debug = false;
- private static enum Type
- {
- TCP, UDP;
- private Type() {}
- }
- private static enum Action
- {
- ACCEPT, DROP;
- private Action() {}
- }
- private class Rule
- {
- public Proxy.Type type = Proxy.Type.TCP;
- public Proxy.Action action = Proxy.Action.ACCEPT;
- public String ip;
- private Rule() {}
- public void parse(String s)
- throws IllegalArgumentException
- {
- String[] parts = s.trim().split(",", 3);
- if (parts.length < 3) {
- throw new IllegalArgumentException();
- }
- if ("UDP".equals(parts[0].toUpperCase())) {
- this.type = Proxy.Type.UDP;
- }
- if ("D".equals(parts[1].toUpperCase())) {
- this.action = Proxy.Action.DROP;
- }
- this.ip = parts[2];
- }
- public String toString()
- {
- return (this.type == Proxy.Type.TCP ? "TCP" : "UDP") + " " + (this.action == Proxy.Action.ACCEPT ? "accept" : "drop") + " " + this.ip;
- }
- }
- public static void main(String[] args)
- throws Exception
- {
- formatter = DateTimeFormat.forPattern("yyyy-MM-dd hh:mm:ss - ");
- Options options = new Options();
- Option conf = new Option("f", "rules", true, "rules file");
- conf.setRequired(true);
- options.addOption(conf);
- Option port = new Option("p", "port", true, "listen port, default 25000");
- port.setType(Integer.class);
- options.addOption(port);
- Option remote = new Option("r", "remote", true, "remote port");
- remote.setRequired(true);
- remote.setType(Integer.class);
- options.addOption(remote);
- Option host = new Option("h", "host", true, "remote host, default: localhost");
- options.addOption(host);
- Option def = new Option("d", "default-action", true, "default firewall action, default: accept");
- options.addOption(def);
- Option verbose = new Option("v", "verbose", false, "log debug info");
- options.addOption(verbose);
- CommandLineParser parser = new DefaultParser();
- HelpFormatter formatter = new HelpFormatter();
- try
- {
- cmd = parser.parse(options, args);
- }
- catch (ParseException e)
- {
- CommandLine cmd;
- System.out.println(e.getMessage());
- formatter.printHelp("utility-name", options);
- System.exit(1); return;
- }
- CommandLine cmd;
- Proxy proxy = new Proxy();
- proxy.listenPort = Integer.parseInt(cmd.getOptionValue("port", "25000"));
- proxy.remotePort = Integer.parseInt(cmd.getOptionValue("remote"));
- proxy.remoteHost = cmd.getOptionValue("host", "localhost");
- proxy.rulesFileName = cmd.getOptionValue("rules");
- proxy.debug = cmd.hasOption("verbose");
- proxy.run();
- }
- private static synchronized void log(String msg)
- {
- System.out.print(formatter.print(DateTime.now()));
- System.out.println(msg);
- }
- private void debug(String msg)
- {
- if (!this.debug) {
- return;
- }
- log("[DEBUG] - " + msg);
- }
- private void loadRules()
- throws IOException
- {
- String lines = new String(Files.readAllBytes(Paths.get(this.rulesFileName, new String[0])));
- String[] raw = lines.split("\n");
- ArrayList<Rule> rules = new ArrayList();
- for (String s : raw) {
- try
- {
- Rule rule = new Rule(null);
- rule.parse(s);
- rules.add(rule);
- }
- catch (IllegalArgumentException localIllegalArgumentException) {}
- }
- this.rules = rules;
- }
- private Rule findRule(Type type, String ip)
- {
- Rule r;
- for (Iterator localIterator = this.rules.iterator(); localIterator.hasNext(); return r)
- {
- r = (Rule)localIterator.next();
- if ((!ip.equals(r.ip)) || (r.type != type)) {}
- }
- return null;
- }
- private boolean canConnect(Type type, String ip)
- {
- Rule r = findRule(type, ip);
- if (this.defaultAction == Action.ACCEPT)
- {
- if (r == null) {
- return true;
- }
- if (r.action == Action.DROP) {
- return false;
- }
- return true;
- }
- if (r == null) {
- return false;
- }
- if (r.action == Action.ACCEPT) {
- return true;
- }
- return false;
- }
- /* Error */
- public void run()
- throws Exception
- {
- // Byte code:
- // 0: ldc 95
- // 2: invokestatic 5 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy:log (Ljava/lang/String;)V
- // 5: new 64 java/lang/StringBuilder
- // 8: dup
- // 9: invokespecial 65 java/lang/StringBuilder:<init> ()V
- // 12: ldc 96
- // 14: invokevirtual 67 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
- // 17: aload_0
- // 18: getfield 7 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy:listenPort I
- // 21: invokevirtual 97 java/lang/StringBuilder:append (I)Ljava/lang/StringBuilder;
- // 24: ldc 98
- // 26: invokevirtual 67 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
- // 29: aload_0
- // 30: getfield 2 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy:remoteHost Ljava/lang/String;
- // 33: invokevirtual 67 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
- // 36: ldc 99
- // 38: invokevirtual 67 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
- // 41: aload_0
- // 42: getfield 1 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy:remotePort I
- // 45: invokevirtual 97 java/lang/StringBuilder:append (I)Ljava/lang/StringBuilder;
- // 48: ldc 100
- // 50: invokevirtual 67 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
- // 53: aload_0
- // 54: getfield 71 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy:defaultAction Lpl/dbgsystem/titanaxe/minecraft/proxy/Proxy$Action;
- // 57: getstatic 70 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy$Action:ACCEPT Lpl/dbgsystem/titanaxe/minecraft/proxy/Proxy$Action;
- // 60: if_acmpne +8 -> 68
- // 63: ldc 101
- // 65: goto +5 -> 70
- // 68: ldc 102
- // 70: invokevirtual 67 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
- // 73: aload_0
- // 74: getfield 3 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy:debug Z
- // 77: ifeq +8 -> 85
- // 80: ldc 103
- // 82: goto +5 -> 87
- // 85: ldc 104
- // 87: invokevirtual 67 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
- // 90: ldc 105
- // 92: invokevirtual 67 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
- // 95: invokevirtual 68 java/lang/StringBuilder:toString ()Ljava/lang/String;
- // 98: invokestatic 5 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy:log (Ljava/lang/String;)V
- // 101: new 106 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy$1
- // 104: dup
- // 105: aload_0
- // 106: invokespecial 107 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy$1:<init> (Lpl/dbgsystem/titanaxe/minecraft/proxy/Proxy;)V
- // 109: invokevirtual 108 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy$1:start ()V
- // 112: new 109 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy$2
- // 115: dup
- // 116: aload_0
- // 117: invokespecial 110 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy$2:<init> (Lpl/dbgsystem/titanaxe/minecraft/proxy/Proxy;)V
- // 120: astore_1
- // 121: aload_1
- // 122: invokevirtual 111 java/lang/Thread:start ()V
- // 125: new 112 java/net/DatagramSocket
- // 128: dup
- // 129: aload_0
- // 130: getfield 7 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy:listenPort I
- // 133: invokespecial 113 java/net/DatagramSocket:<init> (I)V
- // 136: astore_2
- // 137: aconst_null
- // 138: astore_3
- // 139: new 112 java/net/DatagramSocket
- // 142: dup
- // 143: invokespecial 114 java/net/DatagramSocket:<init> ()V
- // 146: astore 4
- // 148: aconst_null
- // 149: astore 5
- // 151: ldc 115
- // 153: newarray <illegal type>
- // 155: astore 6
- // 157: new 116 java/net/DatagramPacket
- // 160: dup
- // 161: aload 6
- // 163: aload 6
- // 165: arraylength
- // 166: invokespecial 117 java/net/DatagramPacket:<init> ([BI)V
- // 169: astore 7
- // 171: aload_2
- // 172: aload 7
- // 174: invokevirtual 118 java/net/DatagramSocket:receive (Ljava/net/DatagramPacket;)V
- // 177: aload 7
- // 179: invokevirtual 119 java/net/DatagramPacket:getAddress ()Ljava/net/InetAddress;
- // 182: invokevirtual 120 java/net/InetAddress:toString ()Ljava/lang/String;
- // 185: ldc 121
- // 187: ldc 104
- // 189: invokevirtual 122 java/lang/String:replaceAll (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
- // 192: astore 8
- // 194: aload_0
- // 195: getstatic 123 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy$Type:UDP Lpl/dbgsystem/titanaxe/minecraft/proxy/Proxy$Type;
- // 198: aload 8
- // 200: invokespecial 4 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy:canConnect (Lpl/dbgsystem/titanaxe/minecraft/proxy/Proxy$Type;Ljava/lang/String;)Z
- // 203: ifne +29 -> 232
- // 206: new 64 java/lang/StringBuilder
- // 209: dup
- // 210: invokespecial 65 java/lang/StringBuilder:<init> ()V
- // 213: ldc 124
- // 215: invokevirtual 67 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
- // 218: aload 8
- // 220: invokevirtual 67 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
- // 223: invokevirtual 68 java/lang/StringBuilder:toString ()Ljava/lang/String;
- // 226: invokestatic 5 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy:log (Ljava/lang/String;)V
- // 229: goto -72 -> 157
- // 232: aload_0
- // 233: new 64 java/lang/StringBuilder
- // 236: dup
- // 237: invokespecial 65 java/lang/StringBuilder:<init> ()V
- // 240: ldc 125
- // 242: invokevirtual 67 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
- // 245: aload 8
- // 247: invokevirtual 67 java/lang/StringBuilder:append (Ljava/lang/String;)Ljava/lang/StringBuilder;
- // 250: invokevirtual 68 java/lang/StringBuilder:toString ()Ljava/lang/String;
- // 253: invokespecial 8 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy:debug (Ljava/lang/String;)V
- // 256: new 116 java/net/DatagramPacket
- // 259: dup
- // 260: aload 6
- // 262: aload 7
- // 264: invokevirtual 126 java/net/DatagramPacket:getLength ()I
- // 267: aload_0
- // 268: getfield 2 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy:remoteHost Ljava/lang/String;
- // 271: invokestatic 127 java/net/InetAddress:getByName (Ljava/lang/String;)Ljava/net/InetAddress;
- // 274: aload_0
- // 275: getfield 1 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy:remotePort I
- // 278: invokespecial 128 java/net/DatagramPacket:<init> ([BILjava/net/InetAddress;I)V
- // 281: astore 9
- // 283: aload 4
- // 285: aload 9
- // 287: invokevirtual 129 java/net/DatagramSocket:send (Ljava/net/DatagramPacket;)V
- // 290: aload 4
- // 292: sipush 2000
- // 295: invokevirtual 130 java/net/DatagramSocket:setSoTimeout (I)V
- // 298: new 116 java/net/DatagramPacket
- // 301: dup
- // 302: aload 6
- // 304: aload 6
- // 306: arraylength
- // 307: invokespecial 117 java/net/DatagramPacket:<init> ([BI)V
- // 310: astore 10
- // 312: aload 4
- // 314: aload 10
- // 316: invokevirtual 118 java/net/DatagramSocket:receive (Ljava/net/DatagramPacket;)V
- // 319: new 116 java/net/DatagramPacket
- // 322: dup
- // 323: aload 6
- // 325: aload 10
- // 327: invokevirtual 126 java/net/DatagramPacket:getLength ()I
- // 330: aload 7
- // 332: invokevirtual 119 java/net/DatagramPacket:getAddress ()Ljava/net/InetAddress;
- // 335: aload 7
- // 337: invokevirtual 131 java/net/DatagramPacket:getPort ()I
- // 340: invokespecial 128 java/net/DatagramPacket:<init> ([BILjava/net/InetAddress;I)V
- // 343: astore 9
- // 345: aload_2
- // 346: aload 9
- // 348: invokevirtual 129 java/net/DatagramSocket:send (Ljava/net/DatagramPacket;)V
- // 351: goto -194 -> 157
- // 354: astore 6
- // 356: aload 6
- // 358: astore 5
- // 360: aload 6
- // 362: athrow
- // 363: astore 11
- // 365: aload 4
- // 367: ifnull +33 -> 400
- // 370: aload 5
- // 372: ifnull +23 -> 395
- // 375: aload 4
- // 377: invokevirtual 133 java/net/DatagramSocket:close ()V
- // 380: goto +20 -> 400
- // 383: astore 12
- // 385: aload 5
- // 387: aload 12
- // 389: invokevirtual 134 java/lang/Throwable:addSuppressed (Ljava/lang/Throwable;)V
- // 392: goto +8 -> 400
- // 395: aload 4
- // 397: invokevirtual 133 java/net/DatagramSocket:close ()V
- // 400: aload 11
- // 402: athrow
- // 403: astore 4
- // 405: aload_0
- // 406: getfield 3 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy:debug Z
- // 409: ifeq +13 -> 422
- // 412: ldc -120
- // 414: invokestatic 5 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy:log (Ljava/lang/String;)V
- // 417: aload 4
- // 419: invokevirtual 137 java/lang/Exception:printStackTrace ()V
- // 422: aload_2
- // 423: ifnull +75 -> 498
- // 426: aload_3
- // 427: ifnull +21 -> 448
- // 430: aload_2
- // 431: invokevirtual 133 java/net/DatagramSocket:close ()V
- // 434: goto +64 -> 498
- // 437: astore 4
- // 439: aload_3
- // 440: aload 4
- // 442: invokevirtual 134 java/lang/Throwable:addSuppressed (Ljava/lang/Throwable;)V
- // 445: goto +53 -> 498
- // 448: aload_2
- // 449: invokevirtual 133 java/net/DatagramSocket:close ()V
- // 452: goto +46 -> 498
- // 455: astore 4
- // 457: aload 4
- // 459: astore_3
- // 460: aload 4
- // 462: athrow
- // 463: astore 13
- // 465: aload_2
- // 466: ifnull +29 -> 495
- // 469: aload_3
- // 470: ifnull +21 -> 491
- // 473: aload_2
- // 474: invokevirtual 133 java/net/DatagramSocket:close ()V
- // 477: goto +18 -> 495
- // 480: astore 14
- // 482: aload_3
- // 483: aload 14
- // 485: invokevirtual 134 java/lang/Throwable:addSuppressed (Ljava/lang/Throwable;)V
- // 488: goto +7 -> 495
- // 491: aload_2
- // 492: invokevirtual 133 java/net/DatagramSocket:close ()V
- // 495: aload 13
- // 497: athrow
- // 498: goto -373 -> 125
- // 501: astore_2
- // 502: ldc -118
- // 504: invokestatic 5 pl/dbgsystem/titanaxe/minecraft/proxy/Proxy:log (Ljava/lang/String;)V
- // 507: aload_2
- // 508: invokevirtual 137 java/lang/Exception:printStackTrace ()V
- // 511: bipush 100
- // 513: invokestatic 51 java/lang/System:exit (I)V
- // 516: goto -391 -> 125
- // Line number table:
- // Java source line #177 -> byte code offset #0
- // Java source line #178 -> byte code offset #5
- // Java source line #181 -> byte code offset #101
- // Java source line #197 -> byte code offset #109
- // Java source line #201 -> byte code offset #112
- // Java source line #214 -> byte code offset #121
- // Java source line #219 -> byte code offset #125
- // Java source line #220 -> byte code offset #139
- // Java source line #221 -> byte code offset #151
- // Java source line #225 -> byte code offset #157
- // Java source line #226 -> byte code offset #171
- // Java source line #229 -> byte code offset #177
- // Java source line #230 -> byte code offset #194
- // Java source line #231 -> byte code offset #206
- // Java source line #232 -> byte code offset #229
- // Java source line #234 -> byte code offset #232
- // Java source line #237 -> byte code offset #256
- // Java source line #238 -> byte code offset #283
- // Java source line #239 -> byte code offset #290
- // Java source line #242 -> byte code offset #298
- // Java source line #243 -> byte code offset #312
- // Java source line #246 -> byte code offset #319
- // Java source line #247 -> byte code offset #345
- // Java source line #248 -> byte code offset #351
- // Java source line #220 -> byte code offset #354
- // Java source line #249 -> byte code offset #363
- // Java source line #250 -> byte code offset #405
- // Java source line #251 -> byte code offset #412
- // Java source line #252 -> byte code offset #417
- // Java source line #255 -> byte code offset #422
- // Java source line #219 -> byte code offset #455
- // Java source line #255 -> byte code offset #463
- // Java source line #259 -> byte code offset #498
- // Java source line #255 -> byte code offset #501
- // Java source line #256 -> byte code offset #502
- // Java source line #257 -> byte code offset #507
- // Java source line #258 -> byte code offset #511
- // Java source line #259 -> byte code offset #516
- // Local variable table:
- // start length slot name signature
- // 0 519 0 this Proxy
- // 120 2 1 tcpServer Thread
- // 136 356 2 server java.net.DatagramSocket
- // 501 7 2 e Exception
- // 138 345 3 localThrowable5 Throwable
- // 146 250 4 clientSocket java.net.DatagramSocket
- // 403 15 4 e Exception
- // 437 4 4 localThrowable2 Throwable
- // 455 6 4 localThrowable3 Throwable
- // 149 237 5 localThrowable6 Throwable
- // 155 169 6 buf byte[]
- // 354 7 6 localThrowable Throwable
- // 169 167 7 packet java.net.DatagramPacket
- // 192 54 8 ip String
- // 281 66 9 sendPacket java.net.DatagramPacket
- // 310 16 10 receivePacket java.net.DatagramPacket
- // 363 38 11 localObject1 Object
- // 383 5 12 localThrowable1 Throwable
- // 463 33 13 localObject2 Object
- // 480 4 14 localThrowable4 Throwable
- // Exception table:
- // from to target type
- // 151 354 354 java/lang/Throwable
- // 151 365 363 finally
- // 375 380 383 java/lang/Throwable
- // 139 403 403 java/lang/Exception
- // 430 434 437 java/lang/Throwable
- // 139 422 455 java/lang/Throwable
- // 139 422 463 finally
- // 455 465 463 finally
- // 473 477 480 java/lang/Throwable
- // 125 498 501 java/lang/Exception
- }
- private class TCPProxyThread
- extends Thread
- {
- private Socket socket;
- public TCPProxyThread(Socket socket)
- {
- this.socket = socket;
- }
- public void run()
- {
- final byte[] request = new byte['?'];
- byte[] reply = new byte['?'];
- InetSocketAddress socketAddress = (InetSocketAddress)this.socket.getRemoteSocketAddress();
- InetAddress inetAddress = socketAddress.getAddress();
- String ip = inetAddress.getHostAddress();
- try
- {
- if (!Proxy.this.canConnect(Proxy.Type.TCP, ip))
- {
- Proxy.log("refused, TCP " + ip);
- this.socket.close();
- return;
- }
- if (Proxy.this.debug) {
- Proxy.this.debug("accepted, TCP " + ip);
- }
- final InputStream socketIn = this.socket.getInputStream();
- OutputStream socketOut = this.socket.getOutputStream();
- Socket remote = new Socket(Proxy.this.remoteHost, Proxy.this.remotePort);Throwable localThrowable3 = null;
- try
- {
- InputStream remoteIn = remote.getInputStream();
- final OutputStream remoteOut = remote.getOutputStream();
- Thread t = new Thread()
- {
- public void run()
- {
- try
- {
- int bytesRead;
- while ((bytesRead = socketIn.read(request)) != -1)
- {
- remoteOut.write(request, 0, bytesRead);
- remoteOut.flush();
- }
- }
- catch (IOException localIOException) {}
- try
- {
- remoteOut.close();
- }
- catch (IOException localIOException1) {}
- }
- };
- t.start();
- try
- {
- int bytesRead;
- while ((bytesRead = remoteIn.read(reply)) != -1)
- {
- socketOut.write(reply, 0, bytesRead);
- socketOut.flush();
- }
- }
- catch (IOException localIOException1) {}
- }
- catch (Throwable localThrowable1)
- {
- localThrowable3 = localThrowable1;throw localThrowable1;
- }
- finally
- {
- if (remote != null) {
- if (localThrowable3 != null) {
- try
- {
- remote.close();
- }
- catch (Throwable localThrowable2)
- {
- localThrowable3.addSuppressed(localThrowable2);
- }
- } else {
- remote.close();
- }
- }
- }
- try
- {
- this.socket.close();
- }
- catch (IOException localIOException2) {}
- if (!Proxy.this.debug) {
- return;
- }
- }
- catch (Exception e)
- {
- if (Proxy.this.debug) {
- e.printStackTrace();
- }
- }
- finally
- {
- try
- {
- this.socket.close();
- }
- catch (IOException localIOException4) {}
- }
- Proxy.this.debug("disconnected, TCP " + ip);
- }
- }
- public int listenPort()
- {
- return this.listenPort;
- }
- public void listenPort(int listenPort)
- {
- this.listenPort = listenPort;
- }
- public int remotePort()
- {
- return this.remotePort;
- }
- public void remotePort(int remotePort)
- {
- this.remotePort = remotePort;
- }
- public String remoteHost()
- {
- return this.remoteHost;
- }
- public void remoteHost(String remoteHost)
- {
- this.remoteHost = remoteHost;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement