Main.java
1 // OpenVPN -- An application to securely tunnel IP networks 2 // over a single port, with support for SSL/TLS-based 3 // session authentication and key exchange, 4 // packet encryption, packet authentication, and 5 // packet compression. 6 // 7 // Copyright (C) 2012- OpenVPN Inc. 8 // 9 // SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception 10 // 11 12 // TESTING_ONLY 13 14 import java.io.*; 15 import java.nio.charset.Charset; 16 17 public class Main { 18 // utility method to read a file and return as a String 19 public static String readFile(String filename) throws IOException { 20 return readStream(new FileInputStream(filename)); 21 } 22 23 private static String readStream(InputStream stream) throws IOException { 24 // No real need to close the BufferedReader/InputStreamReader 25 // as they're only wrapping the stream 26 try { 27 Reader reader = new BufferedReader(new InputStreamReader(stream)); 28 StringBuilder builder = new StringBuilder(); 29 char[] buffer = new char[4096]; 30 int read; 31 while ((read = reader.read(buffer, 0, buffer.length)) > 0) { 32 builder.append(buffer, 0, read); 33 } 34 return builder.toString(); 35 } finally { 36 // Potential issue here: if this throws an IOException, 37 // it will mask any others. Normally I'd use a utility 38 // method which would log exceptions and swallow them 39 stream.close(); 40 } 41 } 42 43 public static void main(String[] args) throws IOException, Client.ConfigError, Client.CredsUnspecifiedError { 44 if (args.length >= 1) 45 { 46 // load config file 47 String config = readFile(args[0]); 48 49 // get creds 50 String username = ""; 51 String password = ""; 52 if (args.length >= 3) 53 { 54 username = args[1]; 55 password = args[2]; 56 } 57 58 // instantiate client object 59 final Client client = new Client(config, username, password); 60 61 // catch signals 62 final Thread mainThread = Thread.currentThread(); 63 Runtime.getRuntime().addShutdownHook(new Thread() { 64 public void run() { 65 client.stop(); 66 try { 67 mainThread.join(); 68 } 69 catch (InterruptedException e) { 70 } 71 } 72 }); 73 74 // execute client session 75 client.connect(); 76 77 // show stats before exit 78 client.show_stats(); 79 } 80 else 81 { 82 System.err.println("OpenVPN Java client"); 83 System.err.println("Usage: java Client <client.ovpn> [username] [password]"); 84 System.exit(2); 85 } 86 } 87 }