add IP to sessions, IP+hostname to clients
All checks were successful
check / check (push) Successful in 1m5s
All checks were successful
check / check (push) Successful in 1m5s
- Add ip column to sessions table (real client IP of session creator) - Add ip and hostname columns to clients table (per-connection tracking) - Update CreateSession, RegisterUser, LoginUser to store new fields - Add GetClientHostInfo query method - Update SessionHostInfo to include IP - Extract executeCreateSession to fix funlen lint - Add tests for session IP, client IP/hostname, login client tracking - Update README with new field documentation
This commit is contained in:
@@ -102,7 +102,7 @@ func FormatHostmask(nick, username, hostname string) string {
|
||||
// CreateSession registers a new session and its first client.
|
||||
func (database *Database) CreateSession(
|
||||
ctx context.Context,
|
||||
nick, username, hostname string,
|
||||
nick, username, hostname, remoteIP string,
|
||||
) (int64, int64, string, error) {
|
||||
if username == "" {
|
||||
username = nick
|
||||
@@ -127,10 +127,11 @@ func (database *Database) CreateSession(
|
||||
|
||||
res, err := transaction.ExecContext(ctx,
|
||||
`INSERT INTO sessions
|
||||
(uuid, nick, username, hostname,
|
||||
(uuid, nick, username, hostname, ip,
|
||||
created_at, last_seen)
|
||||
VALUES (?, ?, ?, ?, ?, ?)`,
|
||||
sessionUUID, nick, username, hostname, now, now)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
sessionUUID, nick, username, hostname,
|
||||
remoteIP, now, now)
|
||||
if err != nil {
|
||||
_ = transaction.Rollback()
|
||||
|
||||
@@ -145,10 +146,11 @@ func (database *Database) CreateSession(
|
||||
|
||||
clientRes, err := transaction.ExecContext(ctx,
|
||||
`INSERT INTO clients
|
||||
(uuid, session_id, token,
|
||||
(uuid, session_id, token, ip, hostname,
|
||||
created_at, last_seen)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
clientUUID, sessionID, tokenHash, now, now)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
clientUUID, sessionID, tokenHash,
|
||||
remoteIP, hostname, now, now)
|
||||
if err != nil {
|
||||
_ = transaction.Rollback()
|
||||
|
||||
@@ -236,14 +238,16 @@ func (database *Database) GetSessionByNick(
|
||||
return sessionID, nil
|
||||
}
|
||||
|
||||
// SessionHostInfo holds the username and hostname for a session.
|
||||
// SessionHostInfo holds the username, hostname, and IP
|
||||
// for a session.
|
||||
type SessionHostInfo struct {
|
||||
Username string
|
||||
Hostname string
|
||||
IP string
|
||||
}
|
||||
|
||||
// GetSessionHostInfo returns the username and hostname
|
||||
// for a session.
|
||||
// GetSessionHostInfo returns the username, hostname,
|
||||
// and IP for a session.
|
||||
func (database *Database) GetSessionHostInfo(
|
||||
ctx context.Context,
|
||||
sessionID int64,
|
||||
@@ -252,10 +256,10 @@ func (database *Database) GetSessionHostInfo(
|
||||
|
||||
err := database.conn.QueryRowContext(
|
||||
ctx,
|
||||
`SELECT username, hostname
|
||||
`SELECT username, hostname, ip
|
||||
FROM sessions WHERE id = ?`,
|
||||
sessionID,
|
||||
).Scan(&info.Username, &info.Hostname)
|
||||
).Scan(&info.Username, &info.Hostname, &info.IP)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"get session host info: %w", err,
|
||||
@@ -265,6 +269,35 @@ func (database *Database) GetSessionHostInfo(
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
// ClientHostInfo holds the IP and hostname for a client.
|
||||
type ClientHostInfo struct {
|
||||
IP string
|
||||
Hostname string
|
||||
}
|
||||
|
||||
// GetClientHostInfo returns the IP and hostname for a
|
||||
// client.
|
||||
func (database *Database) GetClientHostInfo(
|
||||
ctx context.Context,
|
||||
clientID int64,
|
||||
) (*ClientHostInfo, error) {
|
||||
var info ClientHostInfo
|
||||
|
||||
err := database.conn.QueryRowContext(
|
||||
ctx,
|
||||
`SELECT ip, hostname
|
||||
FROM clients WHERE id = ?`,
|
||||
clientID,
|
||||
).Scan(&info.IP, &info.Hostname)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"get client host info: %w", err,
|
||||
)
|
||||
}
|
||||
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
// GetChannelByName returns the channel ID for a name.
|
||||
func (database *Database) GetChannelByName(
|
||||
ctx context.Context,
|
||||
|
||||
Reference in New Issue
Block a user