SHARE
TWEET

Untitled

a guest Jun 22nd, 2018 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ru.bloof.stun;
  2.  
  3. import java.io.IOException;
  4. import java.net.DatagramPacket;
  5. import java.net.DatagramSocket;
  6. import java.net.InetSocketAddress;
  7. import java.net.SocketTimeoutException;
  8. import java.util.Arrays;
  9. import java.util.Random;
  10.  
  11. import org.ice4j.StunException;
  12. import org.ice4j.TransportAddress;
  13. import org.ice4j.attribute.Attribute;
  14. import org.ice4j.attribute.AttributeFactory;
  15. import org.ice4j.attribute.ChangeRequestAttribute;
  16. import org.ice4j.attribute.MappedAddressAttribute;
  17. import org.ice4j.attribute.XorMappedAddressAttribute;
  18. import org.ice4j.message.Message;
  19. import org.ice4j.message.MessageFactory;
  20. import org.ice4j.message.Request;
  21.  
  22. public class NatTypeTester {
  23.     private static final byte[] buf = new byte[65536];
  24.     private static final DatagramPacket recvPacket = new DatagramPacket(buf, buf.length);
  25.     private static final Random rnd = new Random();
  26.     private static final InetSocketAddress TEST = new InetSocketAddress("videostuntest.mycdn.me", 3478);
  27.  
  28.     public static void main(String[] args) throws Exception {
  29.         try (DatagramSocket ds = new DatagramSocket(new InetSocketAddress(args[0], rnd.nextInt(10000) + 40000))) {
  30.             ds.setSoTimeout(5000);
  31.             Request test1 = createRequest();
  32.             byte[] encoded = test1.encode(null);
  33.             ds.send(new DatagramPacket(encoded, encoded.length, TEST));
  34.             InetSocketAddress localAddress = (InetSocketAddress) ds.getLocalSocketAddress();
  35.             System.out.println(localAddress);
  36.             Message message;
  37.             try {
  38.                 message = receive(ds, recvPacket, test1);
  39.             } catch (SocketTimeoutException e) {
  40.                 System.out.println("No UDP for you");
  41.                 return;
  42.             }
  43.             TransportAddress ta = extractAddress(message);
  44.             if (ta == null) {
  45.                 System.err.println("Invalid reponse");
  46.                 return;
  47.             } else {
  48.                 System.out.println("Test1 mapped " + ta);
  49.             }
  50.             InetSocketAddress test1Address = (InetSocketAddress) recvPacket.getSocketAddress();
  51.             if (localAddress.getPort() == ta.getPort() && localAddress.getAddress().equals(ta.getAddress())) {
  52.                 Request test2 = createRequest();
  53.                 ChangeRequestAttribute changeAttribute = AttributeFactory.createChangeRequestAttribute(true, true);
  54.                 test2.putAttribute(changeAttribute);
  55.                 byte[] test2Encoded = test2.encode(null);
  56.                 ds.send(new DatagramPacket(test2Encoded, test2Encoded.length, TEST));
  57.                 try {
  58.                     Message receive = receive(ds, recvPacket, test2);
  59.                     System.out.println("Test2 mapped " + extractAddress(receive));
  60.                     InetSocketAddress test2Address = (InetSocketAddress) recvPacket.getSocketAddress();
  61.                     if (test1Address.getAddress().equals(test2Address.getAddress())
  62.                             || test1Address.getPort() == test2Address.getPort()) {
  63.                         System.out.println("Server fooled us! Src=" + test1Address + " test2=" + test2Address);
  64.                     }
  65.                     System.out.println("Open internet");
  66.                 } catch (SocketTimeoutException e) {
  67.                     System.out.println("Symmetric NAT, also changing MAPPED-ADDRESS");
  68.                 }
  69.             } else {
  70.                 Request test2 = createRequest();
  71.                 ChangeRequestAttribute changeAttribute = AttributeFactory.createChangeRequestAttribute(true, true);
  72.                 test2.putAttribute(changeAttribute);
  73.                 byte[] test2Encoded = test2.encode(null);
  74.                 ds.send(new DatagramPacket(test2Encoded, test2Encoded.length, TEST));
  75.                 try {
  76.                     Message receive = receive(ds, recvPacket, test2);
  77.                     System.out.println("Test2 mapped " + extractAddress(receive));
  78.                     InetSocketAddress test2Address = (InetSocketAddress) recvPacket.getSocketAddress();
  79.                     if (test1Address.getAddress().equals(test2Address.getAddress())
  80.                             || test1Address.getPort() == test2Address.getPort()) {
  81.                         System.out.println("Server fooled us! Src=" + test1Address + " test2=" + test2Address);
  82.                     }
  83.                     System.out.println("NAT, no filter");
  84.                     return;
  85.                 } catch (SocketTimeoutException e) {
  86.                 }
  87.  
  88.                 Request test3 = createRequest();
  89.                 ChangeRequestAttribute portChangeAttribute = AttributeFactory.createChangeRequestAttribute(false,
  90.                         true);
  91.                 test3.putAttribute(portChangeAttribute);
  92.                 byte[] test3Encode = test3.encode(null);
  93.                 ds.send(new DatagramPacket(test3Encode, test3Encode.length, TEST));
  94.                 try {
  95.                     Message receive = receive(ds, recvPacket, test3);
  96.                     System.out.println("Test3 mapped " + extractAddress(receive));
  97.                     InetSocketAddress test3Address = (InetSocketAddress) recvPacket.getSocketAddress();
  98.                     if (test1Address.getPort() == test3Address.getPort()) {
  99.                         System.out.println("Server fooled us! Src=" + test1Address + " test3=" + test3Address);
  100.                     }
  101.                     System.out.println("NAT, address-sensitive filter");
  102.                 } catch (SocketTimeoutException e) {
  103.                     System.out.println("NAT, port-sensitive filter");
  104.                 }
  105.             }
  106.         }
  107.     }
  108.  
  109.     private static Request createRequest() throws StunException {
  110.         Request request = MessageFactory.createBindingRequest();
  111.         byte[] tId = new byte[12];
  112.         rnd.nextBytes(tId);
  113.         request.setTransactionID(tId);
  114.         return request;
  115.     }
  116.  
  117.     private static TransportAddress extractAddress(Message message) {
  118.         Attribute attr = message.getAttribute(Attribute.MAPPED_ADDRESS);
  119.         if (attr != null) {
  120.             return ((MappedAddressAttribute) attr).getAddress();
  121.         }
  122.         attr = message.getAttribute(Attribute.XOR_MAPPED_ADDRESS);
  123.         if (attr != null) {
  124.             return ((XorMappedAddressAttribute) attr).getAddress(message.getTransactionID());
  125.         }
  126.         return null;
  127.     }
  128.  
  129.     private static Message receive(DatagramSocket ds, DatagramPacket packet, Request request)
  130.             throws IOException, StunException {
  131.         while (true) {
  132.             ds.receive(packet);
  133.             Message decoded = Message.decode(packet.getData(), (char) packet.getOffset(), (char) packet.getLength());
  134.             if (Arrays.equals(decoded.getTransactionID(), request.getTransactionID())) {
  135.                 System.out.println(
  136.                         "Received success " + (decoded.getMessageType() == Message.BINDING_SUCCESS_RESPONSE));
  137.                 return decoded;
  138.             }
  139.         }
  140.     }
  141. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top