disabling cache; it's not really necessary.

This commit is contained in:
brent saner
2024-12-20 01:29:56 -05:00
parent c0af14d890
commit 3b4d712722
24 changed files with 941 additions and 112 deletions

View File

@@ -1,31 +1,43 @@
PRAGMA foreign_keys=OFF;
PRAGMA foreign_keys= OFF;
PRAGMA journal_mode = WAL;
BEGIN TRANSACTION;
CREATE TABLE tunnels (
tun_id INTEGER NOT NULL PRIMARY KEY,
cksum_crc32 INTEGER NOT NULL,
"desc" TEXT,
server_v4 TEXT NOT NULL,
current_client_v4 TEXT NOT NULL,
tunnel_server_v6 TEXT NOT NULL,
tunnel_client_v6 TEXT NOT NULL,
prefix_64 TEXT NOT NULL,
prefix_48 TEXT,
rdns_1 TEXT,
rdns_2 TEXT,
rdns_3 TEXT,
rdns_4 TEXT,
rdns_5 TEXT,
created INTEGER NOT NULL,
checked INTEGER NOT NULL,
updated INTEGER
CREATE TABLE tunnels
(
tun_id INTEGER NOT NULL PRIMARY KEY,
cksum_crc32 INTEGER NOT NULL,
"desc" TEXT,
server_v4 TEXT NOT NULL,
current_client_v4 TEXT NOT NULL,
tunnel_server_v6 TEXT NOT NULL,
tunnel_client_v6 TEXT NOT NULL,
prefix_64 TEXT NOT NULL,
prefix_48 TEXT,
rdns_1 TEXT,
rdns_2 TEXT,
rdns_3 TEXT,
rdns_4 TEXT,
rdns_5 TEXT,
created TIMESTAMP NOT NULL,
checked TIMESTAMP NOT NULL,
updated TIMESTAMP
);
CREATE TABLE client_ips (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
tun_id INTEGER NOT NULL,
client_ip INTEGER NOT NULL,
when_set INTEGER NOT NULL, when_fetched INTEGER,
CONSTRAINT client_ips_tunnels_FK FOREIGN KEY (tun_id) REFERENCES tunnels(tun_id) ON DELETE CASCADE ON UPDATE CASCADE
CREATE TABLE client_ips
(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
tun_id INTEGER NOT NULL,
client_ip INTEGER NOT NULL,
when_set TIMESTAMP NOT NULL,
when_fetched TIMESTAMP,
CONSTRAINT client_ips_tunnels_FK FOREIGN KEY (tun_id) REFERENCES tunnels (tun_id) ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO sqlite_sequence
VALUES ('client_ips', 0);
CREATE TABLE metadata
(
key TEXT NOT NULL,
value TEXT,
created TIMESTAMP NOT NULL,
updated TIMESTAMP
);
INSERT INTO sqlite_sequence VALUES('client_ips',0);
COMMIT;
PRAGMA foreign_keys=ON;
PRAGMA foreign_keys= ON;

View File

@@ -8,8 +8,3 @@ var (
//go:embed "_static/cache.schema.sql"
schemaBytes []byte
)
const (
SelectTunnels string = ""
SelectTunnelById string = ""
)

View File

@@ -4,6 +4,9 @@ import (
`os`
`path/filepath`
`github.com/jmoiron/sqlx`
_ `github.com/mattn/go-sqlite3`
`r00t2.io/gobroke/conf`
`r00t2.io/sysutils/paths`
)
@@ -12,24 +15,59 @@ import (
It will be created if it doesn't exist for persistent caches.
*/
func NewCache(db string) (c *Cache, err error) {
func NewCache(db string, perms *conf.Perms) (c *Cache, err error) {
var cache Cache
var exists bool
switch db {
case ":memory:":
// NO-OP for now; exists should be false, but it is since it's zero-val.
default:
if perms == nil {
perms = new(conf.Perms)
if err = perms.SetMissing(); err != nil {
return
}
}
if exists, err = paths.RealPathExists(&db); err != nil {
return
}
if !exists {
if err = os.MkdirAll(filepath.Dir(db), 0700); err != nil {
if err = os.MkdirAll(filepath.Dir(db), *perms.ParentDir.Mode); err != nil {
return
}
if err = os.WriteFile()
if err = os.WriteFile(db, nil, *perms.File.Mode); err != nil {
return
}
}
if err = perms.Chown(filepath.Dir(db)); err != nil {
return
}
if err = perms.Chmod(filepath.Dir(db), !exists); err != nil {
return
}
if err = perms.Chown(db); err != nil {
return
}
if err = perms.Chmod(db, !exists); err != nil {
return
}
}
// TODO
if cache.db, err = sqlx.Connect("sqlite3", db); err != nil {
return
}
if !exists {
// New DB, so write the schema.
if _, err = cache.db.Exec(string(schemaBytes)); err != nil {
return
}
}
c = &cache
return
}

13
cachedb/funcs_cache.go Normal file
View File

@@ -0,0 +1,13 @@
package cachedb
// Close closes a Cache. This should be called when done using it.
func (c *Cache) Close() (err error) {
if c.db != nil {
if err = c.db.Close(); err != nil {
return
}
}
return
}

View File

@@ -1,27 +1,53 @@
package cachedb
import (
`net`
`time`
`github.com/jmoiron/sqlx`
`r00t2.io/gobroke/tunnelbroker`
)
/*
Cache is used to access a database holding historical tunnel configuration and update information.
This is primarily used to keep request load low and update load even lower to tunnelbroker.net servers.
*/
type Cache struct {
db *sqlx.DB
}
type TunnelDB struct {
/*
DbTunnel is a cached tunnelbroker.Tunnel paired with a conf.Tunnel checksum.
It is used to compare defined tunnels on restart, and can be used to fetch last-known usptream configuration.
If the CRC32 mismatches, an update will be forced.
Otherwise it is subject to normal frequency rates for checking.
*/
type DbTunnel struct {
*tunnelbroker.Tunnel
// CRC32 is a checksum of *the associated conf.Tunnel*, NOT the upstream tunnel configuration.
CRC32 uint32 `db:"cksum_crc32"`
// Created is when this *row* is created.
Created time.Time `db:"created"`
// Checked is when the client IP was last checked against the live configuration at tunnelbroker.net.
Checked time.Time `db:"checked"`
// Updated is when the client IP was last updated at tunnelbroker.net (by GoBroke), *or* the Tunnel changed.
Updated time.Time `db:"updated"`
}
type ClientIpDB struct {
ID uint64 `db:"id"`
TunID uint64 `db:"tun_id"`
*tunnelbroker.FetchedIP
Set time.Time `db:"when_set"`
/*
DbClientIP contains a row describing a result of a client IP fetch from a dynamic service (e.g. https://c4.r00t2.io/).
If an explicit address is provided for a conf.Tunnel, there will not be a corresponding row for it.
This is only a history of publicly fetched IPs.
*/
type DbClientIP struct {
// ID is a row identifier serving as primary key; it doesn't have any particular significance beyond that.
ID uint `db:"id"`
// TunID corresponds to a DbTunnel.Tunnel.ID. It is foreign keyed against it.
TunID uint `db:"tun_id"`
// ClientIPv4 is the client-end registered for tunnel TunID.
ClientIPv4 net.IP `db:"client_ip"`
// Set specifies when the IP was last set at tunnelbroker.net.
Set time.Time `db:"when_set"`
// Fetched specifies when the client IP was last *fetched* from a remote service.
Fetched time.Time `db:"when_fetched"`
}