mirror of
https://github.com/OpenVPN/openvpn3.git
synced 2024-09-20 04:02:15 +02:00
82d4a24489
* Multi-profile support. * UI now only shows required fields for each profile. * Added support for server field. * Added support for static challenge/response. * Persist profile/server settings to preferences store.
184 lines
5.4 KiB
C++
184 lines
5.4 KiB
C++
// API for OpenVPN Client, intended to be wrapped by swig.
|
|
// Use ovpncli.i to wrap the API.
|
|
// The crux of the API is defined in OpenVPNClient (below)
|
|
// and TunBuilderBase.
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <openvpn/tun/builder/base.hpp>
|
|
|
|
namespace openvpn {
|
|
class OptionList;
|
|
|
|
namespace ClientAPI {
|
|
// Represents an OpenVPN server and its friendly name
|
|
struct ServerEntry {
|
|
std::string server;
|
|
std::string friendlyName;
|
|
};
|
|
|
|
// return properties of config
|
|
struct EvalConfig
|
|
{
|
|
EvalConfig() : error(false), staticChallengeEcho(false) {}
|
|
|
|
// true if error
|
|
bool error;
|
|
|
|
// if error, message given here
|
|
std::string message;
|
|
|
|
// this username must be used with profile
|
|
std::string userlockedUsername;
|
|
|
|
// profile name of config
|
|
std::string profileName;
|
|
|
|
// "friendly" name of config
|
|
std::string friendlyName;
|
|
|
|
// true: no creds required, false: username/password required
|
|
bool autologin;
|
|
|
|
// static challenge, may be empty, ignored if autologin
|
|
std::string staticChallenge;
|
|
|
|
// true if static challenge response should be echoed to UI, ignored if autologin
|
|
bool staticChallengeEcho;
|
|
|
|
// optional list of user-selectable VPN servers
|
|
std::vector<ServerEntry> serverList;
|
|
};
|
|
|
|
// used to pass credentials to VPN client
|
|
struct ProvideCreds
|
|
{
|
|
ProvideCreds() : replacePasswordWithSessionID(false) {}
|
|
|
|
std::string username;
|
|
std::string password;
|
|
|
|
// response to challenge
|
|
std::string response;
|
|
|
|
// OpenVPN server to connect to (if omitted, value from config file
|
|
// will be used)
|
|
std::string server;
|
|
|
|
// If true, on successful connect, we will replace the password
|
|
// with the session ID we receive from the server.
|
|
bool replacePasswordWithSessionID;
|
|
};
|
|
|
|
// OpenVPN config-file/profile
|
|
struct Config
|
|
{
|
|
std::string content;
|
|
};
|
|
|
|
// used to communicate VPN events such as connect, disconnect, etc.
|
|
struct Event
|
|
{
|
|
Event() : error(false) {}
|
|
bool error; // true if error
|
|
std::string name; // event name
|
|
std::string info; // additional event info
|
|
};
|
|
|
|
// returned by some methods as a status/error indication
|
|
struct Status
|
|
{
|
|
Status() : error(false) {}
|
|
bool error; // true if error
|
|
std::string message; // if error, message given here
|
|
};
|
|
|
|
// used to pass log lines
|
|
struct LogInfo
|
|
{
|
|
LogInfo(const std::string& str) : text(str) {}
|
|
std::string text; // log output (usually but not always one line)
|
|
};
|
|
|
|
namespace Private {
|
|
struct ClientState;
|
|
};
|
|
|
|
// Top-level OpenVPN client class that is wrapped by swig.
|
|
class OpenVPNClient : public TunBuilderBase {
|
|
public:
|
|
OpenVPNClient();
|
|
virtual ~OpenVPNClient();
|
|
|
|
// Parse config file and determine needed credentials statically.
|
|
static EvalConfig eval_config_static(const Config&);
|
|
|
|
// Parse OpenVPN configuration file.
|
|
EvalConfig eval_config(const Config&) const;
|
|
|
|
// Provide credentials. Call before connect() if needed_creds()
|
|
// indicates that credentials are needed.
|
|
void provide_creds(const ProvideCreds&);
|
|
|
|
// Callback to "protect" a socket from being routed through the tunnel.
|
|
// Will be called from the thread executing connect().
|
|
virtual bool socket_protect(int socket) = 0;
|
|
|
|
// Primary VPN client connect method, doesn't return until disconnect.
|
|
// Should be called by a worker thread. This method will make callbacks
|
|
// to event() and log() functions. Make sure to call eval_config()
|
|
// and possibly provide_creds() as well before this function.
|
|
Status connect();
|
|
|
|
// Stop the client. Only meaningful when connect() is running.
|
|
// May be called asynchronously from a different thread
|
|
// when connect() is running.
|
|
void stop();
|
|
|
|
// Pause the client -- useful to avoid continuous reconnection attempts
|
|
// when network is down. May be called from a different thread
|
|
// when connect() is running.
|
|
void pause();
|
|
|
|
// Resume the client after it has been paused. May be called from a
|
|
// different thread when connect() is running.
|
|
void resume();
|
|
|
|
// Do a disconnect/reconnect cycle n seconds from now. May be called
|
|
// from a different thread when connect() is running.
|
|
void reconnect(int seconds);
|
|
|
|
// Get stats/error info. May be called from a different thread
|
|
// when connect() is running.
|
|
|
|
// number of stats
|
|
static int stats_n();
|
|
|
|
// return a stats name, index should be >= 0 and < stats_n()
|
|
static std::string stats_name(int index);
|
|
|
|
// return a stats value, index should be >= 0 and < stats_n()
|
|
long long stats_value(int index) const;
|
|
|
|
// Callback for delivering events during connect() call.
|
|
// Will be called from the thread executing connect().
|
|
virtual void event(const Event&) = 0;
|
|
|
|
// Callback for logging.
|
|
// Will be called from the thread executing connect().
|
|
virtual void log(const LogInfo&) = 0;
|
|
|
|
private:
|
|
static void parse_config(const Config& config, EvalConfig& eval, OptionList& options);
|
|
|
|
// disable copy and assignment
|
|
OpenVPNClient(const OpenVPNClient&);
|
|
OpenVPNClient& operator=(const OpenVPNClient&);
|
|
|
|
Private::ClientState* state;
|
|
};
|
|
|
|
}
|
|
}
|