Java implementation of arbitrary switch VPN to change the Internet area

  • 2020-04-01 03:58:31
  • OfStack

In many cases, the requirements of some web applications will require the impersonators to visit websites and applications in different locations. Therefore, switching IP arises at the right moment. However, IP, as a scarce resource, is not freely available. Hence the idea of applications switching VPNS to reach different parts of the country. Hence the following scheme.

Premise:

1. Windows operating system
2. Manually create a new network connection


package com.selenium.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ConnectNetWork {

  
  public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    connAdsl("VPN_Test","test", "test"); 
    Thread.sleep(1000); 
    cutAdsl("VPN_Test"); 
    Thread.sleep(1000); 
  }

   
  public static String executeCmd(String strCmd) throws Exception {
    System.out.println("cmd /c " + strCmd);
    Process p = Runtime.getRuntime().exec("cmd /c " + strCmd); 
    StringBuilder sbCmd = new StringBuilder();
    //Here is very important, set GB2312 to solve messy code!!
    //If the program default code is GB2312, can not write
    //I use UTF8 by default for NetBeans
    BufferedReader br = new BufferedReader(new InputStreamReader(p 
        .getInputStream(), "GB2312")); 
    String line; 
    while ((line = br.readLine()) != null) { 
      sbCmd.append(line + "n"); 
    }
    return sbCmd.toString(); 
    
    /*
    //And if I do the whole process this way, it's a little bit clearer. GetInputStream takes the original stream of bytes,
    //CMD returns a byte stream encoded in double-byte GB2312
    InputStream in = p.getInputStream();
    byte[] b = new byte[2000];
    in.read(b);
    String msg = new String(b, "GB2312");
    //Interpret this stack of bytes in terms of GB2312, and you can assemble a normal String
    //If I don't write GB2312 up here, that's equal to this UTF8 assembly, same thing
    return msg;
    */
  } 
 
   
  public static boolean connAdsl(String adslTitle, String adslName, String adslPass) throws Exception { 
    System.out.println(" Making connections ."); 
    String adslCmd = "rasdial " + adslTitle + " " + adslName + " " + adslPass; 
    String tempCmd = executeCmd(adslCmd); 
    //String tempCmd = executeCmd("ping www.youku.com"); 
    
    //Determine if the connection was successful
    if (tempCmd.indexOf(" The connected ") > 0) { 
      System.out.println(" The connection was successfully established ."); 
      return true; 
    } else { 
      System.err.println(tempCmd); 
      System.err.println(" Connection setup failed "); 
      return false; 
    } 
  } 
 
   
  public static boolean cutAdsl(String adslTitle) throws Exception { 
    String cutAdsl = "rasdial " + adslTitle + " /disconnect"; 
    String result = executeCmd(cutAdsl); 
     
    if (result.indexOf(" No connection ")!=-1){ 
      System.err.println(adslTitle + " Connection does not exist !"); 
      return false; 
    } else { 
      System.out.println(" Connection disconnected "); 
      return true; 
    } 
  } 
}

Note: the above code snippet is actually calling the Windows rasdial command. So the main credit is still rasdial, Java is just a shell. Once again the curve saved the country. Do you have a better way? Let me know if there's a better way, ^_^


Related articles: