Documentation
¶
Overview ¶
Package database provides data models and database operations for TunneLab.
This package defines the database schema and models for clients, tunnels, and connection logs. It uses SQLite as the storage backend.
Models:
- Client: Represents a client with authentication tokens
- Tunnel: Represents a tunnel configuration
- ConnectionLog: Logs tunnel connections and requests
Usage:
repo, err := NewRepository("tunnelab.db")
if err != nil {
log.Fatal(err)
}
client, err := repo.GetClientByToken("token")
Package database provides database operations for TunneLab.
This package implements the Repository pattern for SQLite database operations including client management, tunnel configuration, and connection logging.
Example:
repo, err := NewRepository("tunnelab.db")
if err != nil {
log.Fatal(err)
}
defer repo.Close()
client, err := repo.GetClientByToken("token")
if err != nil {
log.Fatal(err)
}
Index ¶
- type Client
- type ConnectionLog
- type Repository
- func (r *Repository) Close() error
- func (r *Repository) CloseTunnel(tunnelID string) error
- func (r *Repository) CreateClient(client *Client) error
- func (r *Repository) CreateTunnel(tunnel *Tunnel) error
- func (r *Repository) GetActiveTunnelsByClient(clientID string) ([]*Tunnel, error)
- func (r *Repository) GetClientByToken(token string) (*Client, error)
- func (r *Repository) GetTunnelBySubdomain(subdomain string) (*Tunnel, error)
- type Tunnel
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
ID string `db:"id"` // Unique client identifier
Name string `db:"name"` // Human-readable client name
APIToken string `db:"api_token"` // Authentication token
MaxTunnels int `db:"max_tunnels"` // Maximum tunnels allowed
AllowedSubdomains string `db:"allowed_subdomains"` // Comma-separated allowed subdomains
CreatedAt time.Time `db:"created_at"` // Creation timestamp
UpdatedAt time.Time `db:"updated_at"` // Last update timestamp
Status string `db:"status"` // Client status (active, inactive, etc.)
}
Client represents a client that can create tunnels.
type ConnectionLog ¶
type ConnectionLog struct {
ID int64 `db:"id"` // Unique log entry identifier
TunnelID string `db:"tunnel_id"` // ID of the tunnel
ClientIP string `db:"client_ip"` // Client IP address
RequestMethod string `db:"request_method"` // HTTP method
RequestPath string `db:"request_path"` // Request path
ResponseStatus int `db:"response_status"` // HTTP response status code
BytesSent int64 `db:"bytes_sent"` // Bytes sent
BytesReceived int64 `db:"bytes_received"` // Bytes received
DurationMs int `db:"duration_ms"` // Request duration in milliseconds
CreatedAt time.Time `db:"created_at"` // Timestamp of the request
}
ConnectionLog represents a log entry for tunnel connections and requests.
type Repository ¶
type Repository struct {
// contains filtered or unexported fields
}
Repository provides database operations for TunneLab data.
func NewRepository ¶
func NewRepository(dbPath string) (*Repository, error)
NewRepository creates a new Repository instance with the specified database path.
It opens the database, verifies connectivity, and runs migrations if needed.
Parameters:
- dbPath: Path to the SQLite database file
Returns:
- *Repository: Repository instance
- error: Error if database cannot be opened or migrated
func (*Repository) Close ¶
func (r *Repository) Close() error
func (*Repository) CloseTunnel ¶
func (r *Repository) CloseTunnel(tunnelID string) error
func (*Repository) CreateClient ¶
func (r *Repository) CreateClient(client *Client) error
CreateClient creates a new client in the database.
Parameters:
- client: The client to create
Returns:
- error: Database error if any
func (*Repository) CreateTunnel ¶
func (r *Repository) CreateTunnel(tunnel *Tunnel) error
func (*Repository) GetActiveTunnelsByClient ¶
func (r *Repository) GetActiveTunnelsByClient(clientID string) ([]*Tunnel, error)
func (*Repository) GetClientByToken ¶
func (r *Repository) GetClientByToken(token string) (*Client, error)
GetClientByToken retrieves a client by their API token.
Parameters:
- token: The API token to look up
Returns:
- *Client: The client if found and active
- error: Database error if any
- nil, nil: If token not found (not an error)
func (*Repository) GetTunnelBySubdomain ¶
func (r *Repository) GetTunnelBySubdomain(subdomain string) (*Tunnel, error)
type Tunnel ¶
type Tunnel struct {
ID string `db:"id"` // Unique tunnel identifier
ClientID string `db:"client_id"` // ID of the owning client
Subdomain string `db:"subdomain"` // Subdomain for public access
Protocol string `db:"protocol"` // Protocol type (http, tcp, etc.)
LocalPort int `db:"local_port"` // Local port to forward traffic to
PublicPort int `db:"public_port"` // Remote port for TCP tunnels
PublicURL string `db:"public_url"` // Public URL for accessing the tunnel
CreatedAt time.Time `db:"created_at"` // Creation timestamp
ClosedAt *time.Time `db:"closed_at"` // Timestamp of tunnel closure
Status string `db:"status"` // Tunnel status (active, inactive, etc.)
}
Tunnel represents a tunnel configuration created by a client.