Browse Source

Update DB APIs

master
HimbeerserverDE 1 year ago
parent
commit
2b7abb3e63
No known key found for this signature in database
GPG Key ID: 1A651504791E6A8B
  1. 116
      ban.go
  2. 2
      igutils.go
  3. 56
      privs.go
  4. 2
      rpc.go
  5. 47
      storage.go

116
ban.go

@ -1,7 +1,6 @@
package main
import (
"database/sql"
"errors"
"fmt"
"net"
@ -11,38 +10,9 @@ import (
var ErrInvalidAddress = errors.New("invalid ip address format")
// addBanItem inserts a ban DB entry
func addBanItem(db *sql.DB, addr, name string) error {
_, err := db.Exec(`INSERT INTO ban (
addr,
name
) VALUES (
?,
?
);`, name)
return err
}
// readBanItem selects and reads a ban DB entry
func readBanItem(db *sql.DB, addr string) (string, error) {
var r string
err := db.QueryRow(`SELECT name FROM ban WHERE addr = ?;`, addr).Scan(&r)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return "", err
}
return r, nil
}
// deleteBanItem deletes a ban DB entry
func deleteBanItem(db *sql.DB, name string) error {
_, err := db.Exec(`DELETE FROM ban WHERE name = ? OR addr = ?;`, name)
return err
}
// BanList returns the list of banned players and IP addresses
func BanList() (map[string]string, error) {
db, err := initAuthDB()
db, err := authDB()
if err != nil {
return nil, err
}
@ -68,85 +38,81 @@ func BanList() (map[string]string, error) {
return r, nil
}
// IsBanned reports whether a Conn is banned
func (c *Conn) IsBanned() (bool, string, error) {
db, err := initAuthDB()
// IsBanned reports whether an IP address is banned
func IsBanned(addr string) (bool, string, error) {
db, err := authDB()
if err != nil {
return true, "", err
}
defer db.Close()
var name string
err = db.QueryRow(`SELECT name FROM ban WHERE addr = ?;`, addr).Scan(&name)
return name != "", name, err
}
// IsBanned reports whether a Conn is banned
func (c *Conn) IsBanned() (bool, string, error) {
addr := c.Addr().(*net.UDPAddr).IP.String()
name, err := readBanItem(db, addr)
banned, name, err := IsBanned(addr)
if err != nil {
return true, "", err
}
return name != "", name, nil
return banned, name, nil
}
// Ban adds a Conn to the ban list
func (c *Conn) Ban() error {
banned, _, err := c.IsBanned()
if err != nil {
return err
}
if banned {
return fmt.Errorf("ip address %s is already banned", c.Addr().String())
}
db, err := initAuthDB()
// Ban adds an IP address to the ban list
func Ban(addr, name string) error {
db, err := authDB()
if err != nil {
return err
}
defer db.Close()
name := c.Username()
addr := c.Addr().(*net.UDPAddr).IP.String()
err = addBanItem(db, addr, name)
if err != nil {
return err
if net.ParseIP(addr) == nil {
return ErrInvalidAddress
}
c.CloseWith(AccessDeniedCustomString, "Banned.", false)
return nil
_, err = db.Exec(`INSERT INTO ban (
addr,
name
) VALUES (
?,
?
);`, addr, name)
return err
}
func Ban(addr string) error {
db, err := initAuthDB()
// Ban adds a Conn to the ban list
func (c *Conn) Ban() error {
banned, _, err := c.IsBanned()
if err != nil {
return err
}
defer db.Close()
name := "not known"
if net.ParseIP(addr) == nil {
return ErrInvalidAddress
if banned {
return fmt.Errorf("ip address %s is already banned", c.Addr().String())
}
err = addBanItem(db, addr, name)
if err != nil {
return err
}
name := c.Username()
addr := c.Addr().(*net.UDPAddr).IP.String()
Ban(addr, name)
c.CloseWith(AccessDeniedCustomString, "Banned.", false)
return nil
}
func Unban(name string) error {
db, err := initAuthDB()
// Unban removes a player from the ban list
func Unban(id string) error {
db, err := authDB()
if err != nil {
return err
}
defer db.Close()
err = deleteBanItem(db, name)
if err != nil {
return err
}
return nil
_, err = db.Exec(`DELETE FROM ban WHERE name = ? OR addr = ?;`, id)
return err
}

2
igutils.go

@ -462,7 +462,7 @@ func init() {
return
}
err := Ban(param)
err := Ban(param, "not known")
if err != nil {
c2 := ConnByUsername(param)
if c2 == nil {

56
privs.go

@ -9,7 +9,6 @@ import (
_ "github.com/mattn/go-sqlite3"
)
// encodePrivs encodes priv map into DB-ready string
func encodePrivs(privs map[string]bool) string {
lenP := 0
for priv := range privs {
@ -34,7 +33,6 @@ func encodePrivs(privs map[string]bool) string {
return r
}
// decodePrivs decodes DB-ready string into priv map
func decodePrivs(s string) map[string]bool {
ps := strings.Split(s, "|")
@ -49,46 +47,18 @@ func decodePrivs(s string) map[string]bool {
return r
}
// addPrivItem inserts a priv DB entry
func addPrivItem(db *sql.DB, name string) error {
_, err := db.Exec(`INSERT INTO privileges (
name,
privileges
) VALUES (
?,
""
);`, name)
return err
}
// modPrivItem updates a priv DB entry
func modPrivItem(db *sql.DB, name, privs string) error {
_, err := db.Exec(`UPDATE privileges SET privileges = ? WHERE name = ?;`, privs, name)
return err
}
// readPrivItem selects and reads a priv DB entry
func readPrivItem(db *sql.DB, name string) (string, error) {
var r string
err := db.QueryRow(`SELECT privileges FROM privileges WHERE name = ?;`, name).Scan(&r)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return "", err
}
return r, err
}
// Privs returns the privileges of a player
func Privs(name string) (map[string]bool, error) {
db, err := initAuthDB()
db, err := authDB()
if err != nil {
return nil, err
}
defer db.Close()
eprivs, err := readPrivItem(db, name)
if err != nil {
return nil, err
var eprivs string
err = db.QueryRow(`SELECT privileges FROM privileges WHERE name = ?;`, name).Scan(&eprivs)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return make(map[string]bool), err
}
return decodePrivs(eprivs), nil
@ -101,18 +71,20 @@ func (c *Conn) Privs() (map[string]bool, error) {
// SetPrivs sets the privileges of a player
func SetPrivs(name string, privs map[string]bool) error {
db, err := initAuthDB()
db, err := authDB()
if err != nil {
return err
}
defer db.Close()
err = modPrivItem(db, name, encodePrivs(privs))
if err != nil {
return err
}
return nil
_, err = db.Exec(`REPLACE INTO privileges (
name,
priviliges
) VALUES (
?,
?
);`, name, encodePrivs(privs))
return err
}
// SetPrivs sets the privileges of a Conn

2
rpc.go

@ -164,7 +164,7 @@ func processRpc(c *Conn, r *bytes.Reader) bool {
go c.doRpc("->ISBANNED "+r, rq)
case "<-BAN":
target := strings.Split(msg, " ")[2]
err := Ban(target)
err := Ban(target, "not known")
if err != nil {
c2 := ConnByUsername(target)
if c2 == nil {

47
storage.go

@ -3,33 +3,37 @@ package main
import (
"database/sql"
"errors"
"os"
_ "github.com/mattn/go-sqlite3"
)
func initStorageDB() (*sql.DB, error) {
os.Mkdir("storage", 0777)
func storageDB() (*DB, error) {
return OpenSQLite3("storage.sqlite", `CREATE TABLE IF NOT EXISTS storage (
key VARCHAR(512) PRIMARY KEY NOT NULL,
value VARCHAR(512) NOT NULL
);`)
}
db, err := sql.Open("sqlite3", "storage/storage.sqlite")
// StorageKey returns an entry from the storage database
func StorageKey(key string) (string, error) {
db, err := storageDB()
if err != nil {
return nil, err
return "", err
}
defer db.Close()
if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS storage (
key VARCHAR(512) PRIMARY KEY NOT NULL,
value VARCHAR(512) NOT NULL
);`); err != nil {
db.Close()
return nil, err
var r string
err = db.QueryRow(`SELECT value FROM storage WHERE key = ?;`, key).Scan(&r)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return "", err
}
return db, nil
return r, nil
}
// SetStorageKey sets an entry in the storage database
func SetStorageKey(key, value string) error {
db, err := initStorageDB()
db, err := storageDB()
if err != nil {
return err
}
@ -48,20 +52,3 @@ func SetStorageKey(key, value string) error {
}
return err
}
// StorageKey returns an entry from the storage database
func StorageKey(key string) (string, error) {
db, err := initStorageDB()
if err != nil {
return "", err
}
defer db.Close()
var r string
err = db.QueryRow(`SELECT value FROM storage WHERE key = ?;`, key).Scan(&r)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return "", err
}
return r, nil
}

Loading…
Cancel
Save