Removing admin module and functions

The administration of the server is done using cli tools. So I don't
think that admin account and module is required. At least for now.

Note that this also remove `tbl_admin` creation in database migrate.
Since no release were made until this commit, the database migration
still in version 1. Manual removal database table for `tbl_admin`
is required on the existing running server.
This commit is contained in:
ditatompel 2024-05-23 03:40:06 +07:00
parent 40b9a6e1d6
commit 253230dc5a
No known key found for this signature in database
GPG key ID: 31D3D06D77950979
9 changed files with 34 additions and 360 deletions

View file

@ -12,13 +12,13 @@ Source code of [https://xmr.ditatompel.com](https://xmr.ditatompel.com), a websi
### Server requirements
- MySQL/MariaDB
- [GeoIP Database](https://dev.maxmind.com/geoip/geoip2/geolite2/) (optional). Place it to `./assets/geoip`, see [./internal/repo/geoip.go](./internal/repo/geoip.go).
- [GeoIP Database](https://dev.maxmind.com/geoip/geoip2/geolite2/) (optional). Place it to `./assets/geoip`, see [./internal/geo/ip.go](./internal/geo/ip.go).
## Installation
### For initial server setup:
1. Download [GeoIP Database](https://dev.maxmind.com/geoip/geoip2/geolite2/) and place it to `./assets/geoip`. (see [./internal/repo/geoip.go](./internal/geo/ip.go)).
1. Download [GeoIP Database](https://dev.maxmind.com/geoip/geoip2/geolite2/) and place it to `./assets/geoip`. (see [./internal/geo/ip.go](./internal/geo/ip.go)).
2. Copy `.env.example` to `.env` and edit it to match with server environment.
3. Build the binary with `make build`.
4. Run the service with `./bin/xmr-nodes-server-linux-<YOUR_CPU_ARCH> serve`.

View file

@ -1,89 +0,0 @@
package server
import (
"bufio"
"fmt"
"os"
"strings"
"syscall"
"xmr-remote-nodes/cmd"
"xmr-remote-nodes/internal/database"
"xmr-remote-nodes/internal/repo"
"github.com/spf13/cobra"
"golang.org/x/term"
)
var AdminCmd = &cobra.Command{
Use: "admin",
Short: "Create Admin",
Long: `Create an admin account for WebUI access.`,
Run: func(_ *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println("Usage: xmr-nodes admin create")
os.Exit(1)
}
if args[0] == "create" {
if err := database.ConnectDB(); err != nil {
panic(err)
}
if err := createAdmin(); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Admin account created")
os.Exit(0)
}
},
}
func init() {
cmd.Root.AddCommand(serveCmd)
cmd.Root.AddCommand(importCmd)
cmd.Root.AddCommand(cronCmd)
cmd.Root.AddCommand(probersCmd)
probersCmd.AddCommand(listProbersCmd)
probersCmd.AddCommand(addProbersCmd)
probersCmd.AddCommand(editProbersCmd)
probersCmd.AddCommand(deleteProbersCmd)
listProbersCmd.Flags().StringP("sort-by", "s", "last_submit_ts", "Sort by column name, can be id or last_submit_ts")
listProbersCmd.Flags().StringP("sort-dir", "d", "desc", "Sort direction, can be asc or desc")
}
func createAdmin() error {
admin := repo.NewAdminRepo(database.GetDB())
a := repo.Admin{
Username: stringPrompt("Username:"),
Password: passPrompt("Password:"),
}
_, err := admin.CreateAdmin(&a)
return err
}
func stringPrompt(label string) string {
var s string
r := bufio.NewReader(os.Stdin)
for {
fmt.Fprint(os.Stderr, label+" ")
s, _ = r.ReadString('\n')
if s != "" {
break
}
}
return strings.TrimSpace(s)
}
func passPrompt(label string) string {
var s string
for {
fmt.Fprint(os.Stderr, label+" ")
b, _ := term.ReadPassword(int(syscall.Stdin))
s = string(b)
if s != "" {
break
}
}
fmt.Println()
return s
}

16
cmd/server/init.go Normal file
View file

@ -0,0 +1,16 @@
package server
import "xmr-remote-nodes/cmd"
func init() {
cmd.Root.AddCommand(serveCmd)
cmd.Root.AddCommand(importCmd)
cmd.Root.AddCommand(cronCmd)
cmd.Root.AddCommand(probersCmd)
probersCmd.AddCommand(listProbersCmd)
probersCmd.AddCommand(addProbersCmd)
probersCmd.AddCommand(editProbersCmd)
probersCmd.AddCommand(deleteProbersCmd)
listProbersCmd.Flags().StringP("sort-by", "s", "last_submit_ts", "Sort by column name, can be id or last_submit_ts")
listProbersCmd.Flags().StringP("sort-dir", "d", "desc", "Sort direction, can be asc or desc")
}

View file

@ -1,6 +1,7 @@
package server
import (
"bufio"
"fmt"
"os"
"strconv"
@ -71,6 +72,19 @@ xmr-nodes probers list -s last_submit_ts -d asc sin1`,
},
}
func stringPrompt(label string) string {
var s string
r := bufio.NewReader(os.Stdin)
for {
fmt.Fprint(os.Stderr, label+" ")
s, _ = r.ReadString('\n')
if s != "" {
break
}
}
return strings.TrimSpace(s)
}
var addProbersCmd = &cobra.Command{
Use: "add [name]",
Short: "Add new prober",

3
go.mod
View file

@ -3,7 +3,6 @@ module xmr-remote-nodes
go 1.22.2
require (
github.com/alexedwards/argon2id v1.0.0
github.com/go-sql-driver/mysql v1.8.1
github.com/gofiber/fiber/v2 v2.52.4
github.com/google/uuid v1.6.0
@ -12,7 +11,6 @@ require (
github.com/oschwald/geoip2-golang v1.9.0
github.com/spf13/cobra v1.8.0
golang.org/x/net v0.21.0
golang.org/x/term v0.19.0
)
require (
@ -29,6 +27,5 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.51.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/sys v0.19.0 // indirect
)

44
go.sum
View file

@ -1,7 +1,5 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/alexedwards/argon2id v1.0.0 h1:wJzDx66hqWX7siL/SRUmgz3F8YMrd/nfX/xHHcQQP0w=
github.com/alexedwards/argon2id v1.0.0/go.mod h1:tYKkqIjzXvZdzPvADMWOEZ+l6+BD6CtBXMj5fnJppiw=
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
@ -53,54 +51,12 @@ github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1S
github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q=
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View file

@ -67,29 +67,10 @@ func setSchemaVersion(db *DB, version int) error {
func v1(db *DB) error {
slog.Debug("[DB] Migrating database schema version 1")
// table: tbl_admin
slog.Debug("[DB] Creating table: tbl_admin")
_, err := db.Exec(`
CREATE TABLE tbl_admin (
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
lastactive_ts INT(11) UNSIGNED NOT NULL DEFAULT 0,
created_ts INT(11) UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (id)
)`)
if err != nil {
return err
}
slog.Debug("[DB] Adding unique key to table: tbl_admin")
_, err = db.Exec(`ALTER TABLE tbl_admin ADD UNIQUE KEY (username)`)
if err != nil {
return err
}
// table: tbl_cron
slog.Debug("[DB] Creating table: tbl_cron")
_, err = db.Exec(`
_, err := db.Exec(`
CREATE TABLE tbl_cron (
id INT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NOT NULL DEFAULT '',

View file

@ -1,194 +0,0 @@
package repo
import (
"errors"
"fmt"
"strings"
"time"
"xmr-remote-nodes/internal/database"
"github.com/alexedwards/argon2id"
)
type Admin struct {
Id int `db:"id"`
Username string `db:"username"`
Password string `db:"password"`
LastactiveTs int64 `db:"lastactive_ts"`
CreatedTs int64 `db:"created_ts"`
}
type AdminRepo struct {
db *database.DB
}
type AdminRepository interface {
CreateAdmin(*Admin) (*Admin, error)
Login(username string, password string) (*Admin, error)
}
func NewAdminRepo(db *database.DB) AdminRepository {
return &AdminRepo{db}
}
func (repo *AdminRepo) CreateAdmin(admin *Admin) (*Admin, error) {
if !validUsername(admin.Username) {
return nil, errors.New("username is not valid, must be at least 4 characters long and contain only lowercase letters and numbers")
}
if !strongPassword(admin.Password) {
return nil, errors.New("password is not strong enough, must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, one number and one special character")
}
hash, err := setPassword(admin.Password)
if err != nil {
return nil, err
}
admin.Password = hash
admin.CreatedTs = time.Now().Unix()
if repo.isUsernameExists(admin.Username) {
return nil, errors.New("username already exists")
}
_, err = repo.db.Exec(`
INSERT INTO tbl_admin (
username,
password,
created_ts
) VALUES (
?,
?,
?
)`,
admin.Username,
admin.Password,
admin.CreatedTs)
if err != nil {
return nil, err
}
return admin, nil
}
func (repo *AdminRepo) Login(username, password string) (*Admin, error) {
row, err := repo.db.Query(`
SELECT
id,
username,
password
FROM
tbl_admin
WHERE
username = ?
LIMIT 1`, username)
if err != nil {
fmt.Println(err)
return nil, err
}
defer row.Close()
admin := Admin{}
if row.Next() {
err = row.Scan(&admin.Id, &admin.Username, &admin.Password)
if err != nil {
fmt.Println(err)
return nil, err
}
} else {
return nil, errors.New("Invalid username or password")
}
match, err := checkPassword(admin.Password, password)
if err != nil {
fmt.Println(err)
return nil, err
}
if !match {
return nil, errors.New("Invalid username or password")
}
_, err = repo.db.Exec(`
UPDATE tbl_admin
SET lastactive_ts = ?
WHERE id = ?`, time.Now().Unix(), admin.Id)
if err != nil {
fmt.Println(err)
return nil, err
}
return &admin, nil
}
func (repo *AdminRepo) isUsernameExists(username string) bool {
row, err := repo.db.Query(`
SELECT
id
FROM
tbl_admin
WHERE
username = ?
LIMIT 1`, username)
if err != nil {
return false
}
defer row.Close()
return row.Next()
}
func setPassword(password string) (string, error) {
hash, err := argon2id.CreateHash(password, argon2id.DefaultParams)
if err != nil {
return "", err
}
return hash, nil
}
func checkPassword(hash, password string) (bool, error) {
match, err := argon2id.ComparePasswordAndHash(password, hash)
if err != nil {
return false, err
}
return match, nil
}
func strongPassword(password string) bool {
if len(password) < 8 {
return false
}
if !strings.ContainsAny(password, "0123456789") {
return false
}
if !strings.ContainsAny(password, "ABCDEFGHIJKLMNOPQRSTUVWXYZ") {
return false
}
if !strings.ContainsAny(password, "abcdefghijklmnopqrstuvwxyz") {
return false
}
if !strings.ContainsAny(password, "!@#$%^&*()_+|~-=`{}[]:;<>?,./") {
return false
}
return true
}
// No special character and unicode for username
func validUsername(username string) bool {
if len(username) < 5 || len(username) > 20 {
return false
}
// reject witespace, tabs, newlines, and other special characters
if strings.ContainsAny(username, " \t\n") {
return false
}
// reject unicode
if strings.ContainsAny(username, "^\x00-\x7F") {
return false
}
// reject special characters
if strings.ContainsAny(username, "!@#$%^&*()_+|~-=`{}[]:;<>?,./ ") { // note last blank space
return false
}
if !strings.ContainsAny(username, "abcdefghijklmnopqrstuvwxyz0123456789") {
return false
}
return true
}

View file

@ -2,11 +2,4 @@
package main
import (
"xmr-remote-nodes/cmd"
"xmr-remote-nodes/cmd/server"
)
func init() {
cmd.Root.AddCommand(server.AdminCmd)
}
import _ "xmr-remote-nodes/cmd/server"