initial commit
This commit is contained in:
31
cachedb/_static/cache.schema.sql
Normal file
31
cachedb/_static/cache.schema.sql
Normal file
@@ -0,0 +1,31 @@
|
||||
PRAGMA foreign_keys=OFF;
|
||||
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 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
|
||||
);
|
||||
INSERT INTO sqlite_sequence VALUES('client_ips',0);
|
||||
COMMIT;
|
||||
PRAGMA foreign_keys=ON;
|
||||
15
cachedb/consts.go
Normal file
15
cachedb/consts.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package cachedb
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
var (
|
||||
//go:embed "_static/cache.schema.sql"
|
||||
schemaBytes []byte
|
||||
)
|
||||
|
||||
const (
|
||||
SelectTunnels string = ""
|
||||
SelectTunnelById string = ""
|
||||
)
|
||||
35
cachedb/funcs.go
Normal file
35
cachedb/funcs.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package cachedb
|
||||
|
||||
import (
|
||||
`os`
|
||||
`path/filepath`
|
||||
|
||||
`r00t2.io/sysutils/paths`
|
||||
)
|
||||
|
||||
/*
|
||||
NewCache returns a Cache from path to SQLite file `db` (or ":memory:" for an in-memory one).
|
||||
|
||||
It will be created if it doesn't exist for persistent caches.
|
||||
*/
|
||||
func NewCache(db string) (c *Cache, err error) {
|
||||
|
||||
var exists bool
|
||||
|
||||
switch db {
|
||||
case ":memory:":
|
||||
default:
|
||||
if exists, err = paths.RealPathExists(&db); err != nil {
|
||||
return
|
||||
}
|
||||
if !exists {
|
||||
if err = os.MkdirAll(filepath.Dir(db), 0700); err != nil {
|
||||
return
|
||||
}
|
||||
if err = os.WriteFile()
|
||||
}
|
||||
}
|
||||
// TODO
|
||||
|
||||
return
|
||||
}
|
||||
27
cachedb/types.go
Normal file
27
cachedb/types.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package cachedb
|
||||
|
||||
import (
|
||||
`time`
|
||||
|
||||
`github.com/jmoiron/sqlx`
|
||||
`r00t2.io/gobroke/tunnelbroker`
|
||||
)
|
||||
|
||||
type Cache struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
type TunnelDB struct {
|
||||
*tunnelbroker.Tunnel
|
||||
Created time.Time `db:"created"`
|
||||
Checked time.Time `db:"checked"`
|
||||
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"`
|
||||
Fetched time.Time `db:"when_fetched"`
|
||||
}
|
||||
Reference in New Issue
Block a user