/ javacli / Client.java
Client.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  public class Client implements OpenVPNClientThread.EventReceiver {
 15      private OpenVPNClientThread client_thread;
 16  
 17      public static class ConfigError extends Exception {
 18  	public ConfigError(String msg) { super(msg); }
 19      }
 20  
 21      public static class CredsUnspecifiedError extends Exception {
 22  	public CredsUnspecifiedError(String msg) { super(msg); }
 23      }
 24  
 25      // Load OpenVPN core (implements ClientAPI_OpenVPNClient) from shared library 
 26      static {
 27  	System.loadLibrary("ovpncli");
 28  	String test = ClientAPI_OpenVPNClient.crypto_self_test();
 29  	System.out.format("CRYPTO SELF TEST: %s", test);
 30      }
 31  
 32      public Client(String config_text, String username, String password) throws ConfigError, CredsUnspecifiedError {
 33  	// init client implementation object
 34  	client_thread = new OpenVPNClientThread();
 35  
 36  	// load/eval config
 37  	ClientAPI_Config config = new ClientAPI_Config();
 38  	config.setContent(config_text);
 39  	config.setCompressionMode("yes");
 40  	ClientAPI_EvalConfig ec = client_thread.eval_config(config);
 41  	if (ec.getError())
 42  	    throw new ConfigError("OpenVPN config file parse error: " + ec.getMessage());
 43  
 44  	// handle creds
 45  	ClientAPI_ProvideCreds creds = new ClientAPI_ProvideCreds();
 46  	if (!ec.getAutologin())
 47  	    {
 48  		if (username.length() > 0)
 49  		    {
 50  			creds.setUsername(username);
 51  			creds.setPassword(password);
 52  			creds.setReplacePasswordWithSessionID(true);
 53  		    }
 54  		else
 55  		    throw new CredsUnspecifiedError("OpenVPN config file requires username/password but none provided");
 56  	    }
 57  	client_thread.provide_creds(creds);
 58      }
 59  
 60      public void connect() {
 61  	// connect
 62  	client_thread.connect(this);
 63  
 64  	// wait for worker thread to exit
 65  	client_thread.wait_thread_long();
 66      }
 67  
 68      public void stop() {
 69  	client_thread.stop();
 70      }
 71  
 72      public void show_stats() {
 73  	int n = client_thread.stats_n();
 74  	for (int i = 0; i < n; ++i)
 75  	    {
 76  		String name = client_thread.stats_name(i);
 77  		long value = client_thread.stats_value(i);
 78  		if (value > 0)
 79  		    System.out.format("STAT %s=%s%n", name, value);
 80  	    }
 81      }
 82  
 83      @Override
 84      public void event(ClientAPI_Event event) {
 85  	boolean error = event.getError();
 86  	String name = event.getName();
 87  	String info = event.getInfo();
 88  	System.out.format("EVENT: err=%b name=%s info='%s'%n", error, name, info);
 89      }
 90  
 91      // Callback to get a certificate
 92      @Override
 93      public void external_pki_cert_request(ClientAPI_ExternalPKICertRequest req) {
 94  	req.setError(true);
 95  	req.setErrorText("cert request failed: external PKI not implemented");
 96      }
 97  
 98      // Callback to sign data
 99      @Override
100      public void external_pki_sign_request(ClientAPI_ExternalPKISignRequest req) {
101  	req.setError(true);
102  	req.setErrorText("sign request failed: external PKI not implemented");
103      }
104  
105      @Override
106      public void log(ClientAPI_LogInfo loginfo) {
107  	String text = loginfo.getText();
108  	System.out.format("LOG: %s", text);
109      }
110  
111      @Override
112      public void done(ClientAPI_Status status) {
113  	System.out.format("DONE ClientAPI_Status: err=%b msg='%s'%n", status.getError(), status.getMessage());
114      }
115  
116      @Override
117      public boolean socket_protect(int socket)
118      {
119  	return false;
120      }
121  
122      @Override
123      public boolean pause_on_connection_timeout()
124      {
125  	return false;
126      }
127  
128      @Override
129      public OpenVPNClientThread.TunBuilder tun_builder_new()
130      {
131  	return null;
132      }
133   }