Advertisement
Guest User

Untitled

a guest
Dec 4th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.28 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.io.OutputStream;
  3. import java.net.SocketTimeoutException;
  4. import java.util.UUID;
  5.  
  6. import com.jcraft.jsch.*;
  7.  
  8. public class Main {
  9.     public static final String CMD_TYPE = "type";
  10.     public static final String TIMESTAMP = "timestamp";
  11.     public static final String USER_ID = "userId";
  12.     public static final String PARTNER_ID = "partnerId";
  13.  
  14.     public static final String VOIP_USERNAME = "cryptoc";
  15.     public static final String VOIP_PASSWORD = "password";
  16.     public static final int VOIP_SSH_PORT = 22;
  17.  
  18.     public static final int CREATE_CONNECTION_TIMEOUT = 10000; // in sec
  19.  
  20.     private static Channel senderChannel;
  21.     private static Session senderSession;
  22.  
  23.     public static void main(String[] args) {
  24.         System.out.println("VoipDroid");
  25.         testConnection();
  26.     }
  27.  
  28.     private static void testConnection() {
  29.         System.out.println("Testing connection");
  30.  
  31.         try {
  32.             if (!connectToVoip())
  33.                 throw new Exception("Connection was not created.");
  34.  
  35.             System.out.println("Creating Streams");
  36.             OutputStream out = senderChannel.getOutputStream();
  37.             InputStream in = senderChannel.getInputStream();
  38.  
  39.             System.out.println("Swriting");
  40.             out.write(createVoipCmd().getBytes("UTF-8"));
  41.             out.flush();
  42.  
  43.             System.out.println("Waiting");
  44.             byte[] waitByte = new byte[2];
  45.             if (!(in.read(waitByte) == 1 && waitByte[0] == 1))
  46.                 throw new SocketTimeoutException("Partner not response");
  47.  
  48.             System.out.println("Confirming");
  49.  
  50.         } catch (Exception e) {
  51.             System.out.println("Failed: " + e.getMessage());
  52.         }
  53.  
  54.         closeConnect(senderChannel, senderSession);
  55.     }
  56.  
  57.     private static boolean connectToVoip() {
  58.         try {
  59.             JSch mJsch = new JSch();
  60.             senderSession = mJsch.getSession(VOIP_USERNAME, "app.vav.anx.sk", VOIP_SSH_PORT);
  61.             System.out.println("session");
  62.             senderSession.setUserInfo(new UserInfo() {
  63.                 @Override
  64.                 public String getPassphrase() {
  65.                     return null;
  66.                 }
  67.  
  68.                 @Override
  69.                 public String getPassword() {
  70.                     return VOIP_PASSWORD;
  71.                 }
  72.  
  73.                 @Override
  74.                 public boolean promptPassword(String message) {
  75.                     return true;
  76.                 }
  77.  
  78.                 @Override
  79.                 public boolean promptPassphrase(String message) {
  80.                     return true;
  81.                 }
  82.  
  83.                 @Override
  84.                 public boolean promptYesNo(String message) {
  85.                     return true;
  86.                 }
  87.  
  88.                 @Override
  89.                 public void showMessage(String message) {
  90.                 }
  91.             });
  92.             senderSession.connect(CREATE_CONNECTION_TIMEOUT);
  93.             System.out.println("mSession status: " + senderSession.isConnected());
  94.  
  95.             senderChannel = senderSession.getStreamForwarder("app.vav.anx.sk", 33000);
  96.             senderChannel.connect(CREATE_CONNECTION_TIMEOUT);
  97.  
  98.             System.out.println("COnnection status: " + senderChannel.isConnected());
  99.             return true;
  100.         } catch (Exception e) {
  101.             System.out.println(e.getMessage());
  102.             return false;
  103.         }
  104.     }
  105.  
  106.     private static void closeConnect(Channel mChannel, Session mSession) {
  107.         if (mChannel != null) {
  108.             mChannel.disconnect();
  109.             mChannel = null;
  110.         }
  111.         if (mSession != null) {
  112.             mSession.disconnect();
  113.             mSession = null;
  114.         }
  115.     }
  116.  
  117.     private static String createVoipCmd() {
  118.         return "{\"" + USER_ID + "\": \"" + "de9a206d-3147-4aac-a9d4-b3e5c0514e0c" + "\", \"" + CMD_TYPE + "\": \""
  119.                 + "CREATE" + "\", \"" + PARTNER_ID + "\": \"" + "f61020f9-b582-401b-b7da-b3a9cfaefd35" + "\"" + "}";
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement