Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.SocketTimeoutException;
- import java.util.UUID;
- import com.jcraft.jsch.*;
- public class Main {
- public static final String CMD_TYPE = "type";
- public static final String TIMESTAMP = "timestamp";
- public static final String USER_ID = "userId";
- public static final String PARTNER_ID = "partnerId";
- public static final String VOIP_USERNAME = "cryptoc";
- public static final String VOIP_PASSWORD = "password";
- public static final int VOIP_SSH_PORT = 22;
- public static final int CREATE_CONNECTION_TIMEOUT = 10000; // in sec
- private static Channel senderChannel;
- private static Session senderSession;
- public static void main(String[] args) {
- System.out.println("VoipDroid");
- testConnection();
- }
- private static void testConnection() {
- System.out.println("Testing connection");
- try {
- if (!connectToVoip())
- throw new Exception("Connection was not created.");
- System.out.println("Creating Streams");
- OutputStream out = senderChannel.getOutputStream();
- InputStream in = senderChannel.getInputStream();
- System.out.println("Swriting");
- out.write(createVoipCmd().getBytes("UTF-8"));
- out.flush();
- System.out.println("Waiting");
- byte[] waitByte = new byte[2];
- if (!(in.read(waitByte) == 1 && waitByte[0] == 1))
- throw new SocketTimeoutException("Partner not response");
- System.out.println("Confirming");
- } catch (Exception e) {
- System.out.println("Failed: " + e.getMessage());
- }
- closeConnect(senderChannel, senderSession);
- }
- private static boolean connectToVoip() {
- try {
- JSch mJsch = new JSch();
- senderSession = mJsch.getSession(VOIP_USERNAME, "app.vav.anx.sk", VOIP_SSH_PORT);
- System.out.println("session");
- senderSession.setUserInfo(new UserInfo() {
- @Override
- public String getPassphrase() {
- return null;
- }
- @Override
- public String getPassword() {
- return VOIP_PASSWORD;
- }
- @Override
- public boolean promptPassword(String message) {
- return true;
- }
- @Override
- public boolean promptPassphrase(String message) {
- return true;
- }
- @Override
- public boolean promptYesNo(String message) {
- return true;
- }
- @Override
- public void showMessage(String message) {
- }
- });
- senderSession.connect(CREATE_CONNECTION_TIMEOUT);
- System.out.println("mSession status: " + senderSession.isConnected());
- senderChannel = senderSession.getStreamForwarder("app.vav.anx.sk", 33000);
- senderChannel.connect(CREATE_CONNECTION_TIMEOUT);
- System.out.println("COnnection status: " + senderChannel.isConnected());
- return true;
- } catch (Exception e) {
- System.out.println(e.getMessage());
- return false;
- }
- }
- private static void closeConnect(Channel mChannel, Session mSession) {
- if (mChannel != null) {
- mChannel.disconnect();
- mChannel = null;
- }
- if (mSession != null) {
- mSession.disconnect();
- mSession = null;
- }
- }
- private static String createVoipCmd() {
- return "{\"" + USER_ID + "\": \"" + "de9a206d-3147-4aac-a9d4-b3e5c0514e0c" + "\", \"" + CMD_TYPE + "\": \""
- + "CREATE" + "\", \"" + PARTNER_ID + "\": \"" + "f61020f9-b582-401b-b7da-b3a9cfaefd35" + "\"" + "}";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement