daily pastebin goal
37%
SHARE
TWEET

Untitled

a guest Jun 20th, 2018 72 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.util.Arrays;
  8. import java.util.Random;
  9.  
  10. import org.ice4j.StunException;
  11. import org.ice4j.TransportAddress;
  12. import org.ice4j.attribute.Attribute;
  13. import org.ice4j.attribute.XorMappedAddressAttribute;
  14. import org.ice4j.message.Message;
  15. import org.ice4j.message.MessageFactory;
  16. import org.ice4j.message.Request;
  17.  
  18. public class NatTester {
  19.     private static final InetSocketAddress TEST1 = new InetSocketAddress("videostun1.mycdn.me", 80);
  20.     private static final InetSocketAddress TEST2 = new InetSocketAddress("videostun1.mycdn.me", 3478);
  21.     private static final InetSocketAddress TEST3 = new InetSocketAddress("videostun2.mycdn.me", 80);
  22.     private static final InetSocketAddress TEST4 = new InetSocketAddress("videostun2.mycdn.me", 3478);
  23.  
  24.     private static final Random rnd = new Random();
  25.     private static final byte[] buf = new byte[65536];
  26.     private static final DatagramPacket recvPacket = new DatagramPacket(buf, buf.length);
  27.  
  28.     public static void main(String[] args) throws Exception {
  29.         int port1 = rnd.nextInt(10000) + 20000;
  30.         int port2 = rnd.nextInt(10000) + 40000;
  31.         System.out.println("port1=" + port1 + " port2=" + port2);
  32.         try (DatagramSocket ds = new DatagramSocket(port1);
  33.              DatagramSocket ds2 = new DatagramSocket(port2)) {
  34.             ds.setSoTimeout(5000);
  35.             ds2.setSoTimeout(5000);
  36.             System.out.println(sendRequest(ds, TEST1));
  37.             System.out.println(sendRequest(ds, TEST2));
  38.             System.out.println(sendRequest(ds, TEST3));
  39.             System.out.println(sendRequest(ds, TEST4));
  40.             System.out.println(sendRequest(ds2, TEST1));
  41.             System.out.println(sendRequest(ds2, TEST2));
  42.             System.out.println(sendRequest(ds2, TEST3));
  43.             System.out.println(sendRequest(ds2, TEST4));
  44.         }
  45.     }
  46.  
  47.     private static TransportAddress sendRequest(DatagramSocket socket, InetSocketAddress address)
  48.             throws StunException, IOException {
  49.         Request request = MessageFactory.createBindingRequest();
  50.         byte[] tId = new byte[12];
  51.         rnd.nextBytes(tId);
  52.         request.setTransactionID(tId);
  53.         byte[] data1 = request.encode(null);
  54.         socket.send(new DatagramPacket(data1, data1.length, address));
  55.         socket.receive(recvPacket);
  56.         Message decoded = Message.decode(recvPacket.getData(), (char) recvPacket.getOffset(),
  57.                 (char) recvPacket.getLength());
  58.         if (decoded.getMessageType() != Message.BINDING_SUCCESS_RESPONSE
  59.                 || !Arrays.equals(tId, decoded.getTransactionID())) {
  60.             return null;
  61.         }
  62.         for (Attribute attr : decoded.getAttributes()) {
  63.             if (attr instanceof XorMappedAddressAttribute) {
  64.                 return ((XorMappedAddressAttribute) attr).getAddress(tId);
  65.             }
  66.         }
  67.         return null;
  68.     }
  69. }
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