Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
profile_number
Browse files Browse the repository at this point in the history
new schema & migration script

portalNumber configured in config class. and passed  to dbconfig class to be used in Storage class
storage was updated to return portal_number in resultset for portal use (and future use as needed)
admin portal was adjusted to show portal number in (info, users)

connections, Stats and log works for current portal only.
need to consider creating new view for global stats
  • Loading branch information
samermassoud committed Aug 19, 2022
1 parent b08bc59 commit 0d68ee2
Show file tree
Hide file tree
Showing 9 changed files with 514 additions and 37 deletions.
2 changes: 2 additions & 0 deletions config/config.php.example
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ return [
// 'sessionExpiry' => 'P1D', // 1 day
// 'sessionExpiry' => 'PT12H', // 12 hours

// 'portalNumber' => 0,

// Portal Database Configuration
// NOTE: using any other database than SQLite requires *manual*
// initialization and migration!
Expand Down
98 changes: 98 additions & 0 deletions schema/2022022201_2022080901.migration
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

/* certificates */
ALTER TABLE certificates RENAME TO _certificates;

CREATE TABLE IF NOT EXISTS certificates(
portal_number BIGINT NOT NULL DEFAULT 0,
node_number BIGINT NOT NULL,
profile_id VARCHAR(255) NOT NULL,
display_name VARCHAR(255) NOT NULL,
common_name VARCHAR(255) UNIQUE NOT NULL,
created_at VARCHAR(255) NOT NULL,
expires_at VARCHAR(255) NOT NULL,
auth_key VARCHAR(255) REFERENCES oauth_authorizations(auth_key) ON DELETE CASCADE,
user_id VARCHAR(255) NOT NULL REFERENCES users(user_id) ON DELETE CASCADE
);

INSERT INTO certificates (node_number, profile_id, display_name, common_name, created_at, expires_at, auth_key, user_id)
SELECT (node_number, profile_id, display_name, common_name, created_at, expires_at, auth_key, user_id) FROM _certificates;

DROP TABLE _certificates;



/* wg_peers */
ALTER TABLE wg_peers RENAME TO _wg_peers;

CREATE TABLE IF NOT EXISTS wg_peers (
portal_number BIGINT NOT NULL DEFAULT 0,
node_number BIGINT NOT NULL,
profile_id VARCHAR(255) NOT NULL,
display_name VARCHAR(255) NOT NULL,
public_key VARCHAR(255) NOT NULL UNIQUE,
ip_four VARCHAR(255) NOT NULL UNIQUE,
ip_six VARCHAR(255) NOT NULL UNIQUE,
created_at VARCHAR(255) NOT NULL,
expires_at VARCHAR(255) NOT NULL,
auth_key VARCHAR(255) REFERENCES oauth_authorizations(auth_key) ON DELETE CASCADE,
user_id VARCHAR(255) NOT NULL REFERENCES users(user_id) ON DELETE CASCADE
);

INSERT INTO wg_peers (node_number, profile_id, display_name, public_key, ip_four, ip_six, created_at, expires_at, auth_key, user_id)
SELECT (node_number, profile_id, display_name, public_key, ip_four, ip_six, created_at, expires_at, auth_key, user_id) FROM _wg_peers;

DROP TABLE _wg_peers;

/* connection_log */
ALTER TABLE connection_log RENAME TO _connection_log;

CREATE TABLE IF NOT EXISTS connection_log(
portal_number BIGINT NOT NULL DEFAULT 0,
user_id VARCHAR(255) NOT NULL,
profile_id VARCHAR(255) NOT NULL,
vpn_proto VARCHAR(255) NOT NULL,
connection_id VARCHAR(255) NOT NULL,
ip_four VARCHAR(255) NOT NULL,
ip_six VARCHAR(255) NOT NULL,
connected_at VARCHAR(255) NOT NULL,
bytes_in BIGINT DEFAULT NULL,
bytes_out BIGINT DEFAULT NULL,
disconnected_at VARCHAR(255) DEFAULT NULL
);

INSERT INTO connection_log (user_id, profile_id, vpn_proto , connection_id, ip_four, ip_six, connected_at, bytes_in, bytes_out, disconnected_at)
SELECT (user_id, profile_id, vpn_proto , connection_id, ip_four, ip_six, connected_at, bytes_in, bytes_out, disconnected_at) FROM _connection_log;

DROP TABLE _connection_log;

/* live_stats */
ALTER TABLE live_stats RENAME TO _live_stats;

CREATE TABLE IF NOT EXISTS live_stats(
portal_number BIGINT NOT NULL DEFAULT 0,
date_time VARCHAR(255) NOT NULL,
profile_id VARCHAR(255) NOT NULL,
connection_count BIGINT NOT NULL
);

INSERT INTO live_stats (date_time, profile_id, connection_count)
SELECT (date_time, profile_id, connection_count) FROM _live_stats;

DROP TABLE _live_stats;


/* aggregate_stats */
ALTER TABLE aggregate_stats RENAME TO _aggregate_stats;

CREATE TABLE IF NOT EXISTS aggregate_stats(
portal_number BIGINT NOT NULL DEFAULT 0,
date VARCHAR(255) NOT NULL,
profile_id VARCHAR(255) NOT NULL,
max_connection_count BIGINT NOT NULL,
unique_user_count BIGINT NOT NULL
);

INSERT INTO aggregate_stats (date, profile_id, max_connection_count, unique_user_count)
SELECT (date, profile_id, max_connection_count, unique_user_count) FROM _aggregate_stats;

DROP TABLE _aggregate_stats;
77 changes: 77 additions & 0 deletions schema/2022080901.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
CREATE TABLE IF NOT EXISTS users(
user_id VARCHAR(255) NOT NULL PRIMARY KEY,
last_seen VARCHAR(255) NOT NULL,
permission_list TEXT NOT NULL,
auth_data TEXT DEFAULT NULL,
is_disabled BOOLEAN NOT NULL
);
CREATE TABLE IF NOT EXISTS local_users (
user_id VARCHAR(255) NOT NULL PRIMARY KEY,
password_hash VARCHAR(255) NOT NULL,
created_at VARCHAR(255) NOT NULL,
UNIQUE(user_id)
);
CREATE TABLE IF NOT EXISTS oauth_authorizations (
auth_key VARCHAR(255) NOT NULL PRIMARY KEY,
client_id VARCHAR(255) NOT NULL,
scope VARCHAR(255) NOT NULL,
authorized_at VARCHAR(255) NOT NULL,
expires_at VARCHAR(255) NOT NULL,
user_id VARCHAR(255) NOT NULL REFERENCES users(user_id) ON DELETE CASCADE,
UNIQUE(auth_key)
);
CREATE TABLE IF NOT EXISTS oauth_refresh_token_log (
auth_key VARCHAR(255) NOT NULL REFERENCES oauth_authorizations(auth_key) ON DELETE CASCADE,
refresh_token_id VARCHAR(255) NOT NULL,
UNIQUE(auth_key, refresh_token_id)
);
CREATE TABLE IF NOT EXISTS certificates(
portal_number BIGINT NOT NULL DEFAULT 0,
node_number BIGINT NOT NULL,
profile_id VARCHAR(255) NOT NULL,
display_name VARCHAR(255) NOT NULL,
common_name VARCHAR(255) UNIQUE NOT NULL,
created_at VARCHAR(255) NOT NULL,
expires_at VARCHAR(255) NOT NULL,
auth_key VARCHAR(255) REFERENCES oauth_authorizations(auth_key) ON DELETE CASCADE,
user_id VARCHAR(255) NOT NULL REFERENCES users(user_id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS wg_peers (
portal_number BIGINT NOT NULL DEFAULT 0,
node_number BIGINT NOT NULL,
profile_id VARCHAR(255) NOT NULL,
display_name VARCHAR(255) NOT NULL,
public_key VARCHAR(255) NOT NULL UNIQUE,
ip_four VARCHAR(255) NOT NULL UNIQUE,
ip_six VARCHAR(255) NOT NULL UNIQUE,
created_at VARCHAR(255) NOT NULL,
expires_at VARCHAR(255) NOT NULL,
auth_key VARCHAR(255) REFERENCES oauth_authorizations(auth_key) ON DELETE CASCADE,
user_id VARCHAR(255) NOT NULL REFERENCES users(user_id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS connection_log(
portal_number BIGINT NOT NULL DEFAULT 0,
user_id VARCHAR(255) NOT NULL,
profile_id VARCHAR(255) NOT NULL,
vpn_proto VARCHAR(255) NOT NULL,
connection_id VARCHAR(255) NOT NULL,
ip_four VARCHAR(255) NOT NULL,
ip_six VARCHAR(255) NOT NULL,
connected_at VARCHAR(255) NOT NULL,
bytes_in BIGINT DEFAULT NULL,
bytes_out BIGINT DEFAULT NULL,
disconnected_at VARCHAR(255) DEFAULT NULL
);
CREATE TABLE IF NOT EXISTS live_stats(
portal_number BIGINT NOT NULL DEFAULT 0,
date_time VARCHAR(255) NOT NULL,
profile_id VARCHAR(255) NOT NULL,
connection_count BIGINT NOT NULL
);
CREATE TABLE IF NOT EXISTS aggregate_stats(
portal_number BIGINT NOT NULL DEFAULT 0,
date VARCHAR(255) NOT NULL,
profile_id VARCHAR(255) NOT NULL,
max_connection_count BIGINT NOT NULL,
unique_user_count BIGINT NOT NULL
);
6 changes: 6 additions & 0 deletions src/Cfg/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public function dbConfig(string $baseDir): DbConfig
array_merge(
[
'baseDir' => $baseDir,
'portalNumber' => $this->portalNumber(),
],
$this->s('Db')->toArray()
)
Expand Down Expand Up @@ -221,4 +222,9 @@ public static function fromFile(string $configFile): self

return new self(require $configFile);
}

public function portalNumber(): int
{
return $this->requireInt('portalNumber', 0);
}
}
6 changes: 6 additions & 0 deletions src/Cfg/DbConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ public function dbPass(): ?string
{
return $this->optionalString('dbPass');
}


public function portalNumber(): int
{
return $this->requireInt('portalNumber');
}
}
Loading

0 comments on commit 0d68ee2

Please sign in to comment.