2021-02-05 01:37:12 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-11-14 05:33:43 -05:00
|
|
|
"crypto/rand"
|
2021-02-05 01:37:12 -05:00
|
|
|
"database/sql"
|
2022-11-14 05:33:43 -05:00
|
|
|
"encoding/base32"
|
2021-02-05 01:37:12 -05:00
|
|
|
"fmt"
|
2022-11-14 05:33:43 -05:00
|
|
|
"github.com/dgryski/dgoogauth"
|
2021-02-05 01:37:12 -05:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
2022-11-14 05:33:43 -05:00
|
|
|
"log"
|
2021-02-05 01:37:12 -05:00
|
|
|
"os"
|
2022-11-14 05:33:43 -05:00
|
|
|
"strings"
|
2021-02-05 01:37:12 -05:00
|
|
|
"text/tabwriter"
|
|
|
|
)
|
|
|
|
|
2021-02-08 04:53:09 -05:00
|
|
|
const (
|
2022-11-14 05:33:43 -05:00
|
|
|
version = "1.0.5"
|
2021-02-08 04:53:09 -05:00
|
|
|
)
|
|
|
|
|
2021-02-05 01:37:12 -05:00
|
|
|
var (
|
|
|
|
dbPath = kingpin.Flag("db.path", "path do openvpn-user db").Default("./openvpn-user.db").String()
|
|
|
|
|
2021-02-08 04:53:09 -05:00
|
|
|
dbInitCommand = kingpin.Command("db-init", "Init db.")
|
|
|
|
dbMigrateCommand = kingpin.Command("db-migrate", "STUB: Migrate db.")
|
2021-02-05 01:37:12 -05:00
|
|
|
|
|
|
|
createCommand = kingpin.Command("create", "Create user.")
|
|
|
|
createCommandUserFlag = createCommand.Flag("user", "Username.").Required().String()
|
|
|
|
createCommandPasswordFlag = createCommand.Flag("password", "Password.").Required().String()
|
|
|
|
|
2022-05-23 09:21:54 -04:00
|
|
|
deleteCommand = kingpin.Command("delete", "Delete user.")
|
|
|
|
deleteCommandUserForceFlag = deleteCommand.Flag("force", "delete from db.").Default("false").Bool()
|
|
|
|
deleteCommandUserFlag = deleteCommand.Flag("user", "Username.").Required().String()
|
2021-02-05 01:37:12 -05:00
|
|
|
|
|
|
|
revokeCommand = kingpin.Command("revoke", "Revoke user.")
|
|
|
|
revokeCommandUserFlag = revokeCommand.Flag("user", "Username.").Required().String()
|
|
|
|
|
|
|
|
restoreCommand = kingpin.Command("restore", "Restore user.")
|
|
|
|
restoreCommandUserFlag = restoreCommand.Flag("user", "Username.").Required().String()
|
|
|
|
|
|
|
|
listCommand = kingpin.Command("list", "List active users.")
|
|
|
|
listAll = listCommand.Flag("all", "Show all users include revoked and deleted.").Default("false").Bool()
|
|
|
|
|
2021-02-08 04:53:09 -05:00
|
|
|
checkCommand = kingpin.Command("check", "check user existent.")
|
|
|
|
checkCommandUserFlag = checkCommand.Flag("user", "Username.").Required().String()
|
2021-02-05 13:19:54 -05:00
|
|
|
|
2021-02-05 01:37:12 -05:00
|
|
|
authCommand = kingpin.Command("auth", "Auth user.")
|
|
|
|
authCommandUserFlag = authCommand.Flag("user", "Username.").Required().String()
|
2022-11-14 05:33:43 -05:00
|
|
|
authCommandPasswordFlag = authCommand.Flag("password", "Password.").String()
|
|
|
|
authCommandTotpFlag = authCommand.Flag("totp", "TOTP code.").String()
|
|
|
|
//authCommandHotpFlag = authCommand.Flag("hotp", "HOTP code.").String()
|
2021-02-05 01:37:12 -05:00
|
|
|
|
|
|
|
changePasswordCommand = kingpin.Command("change-password", "Change password")
|
|
|
|
changePasswordCommandUserFlag = changePasswordCommand.Flag("user", "Username.").Required().String()
|
|
|
|
changePasswordCommandPasswordFlag = changePasswordCommand.Flag("password", "Password.").Required().String()
|
|
|
|
|
2022-11-14 05:33:43 -05:00
|
|
|
updateSecretCommand = kingpin.Command("update-secret", "update OTP secret")
|
|
|
|
updateSecretCommandUserFlag = updateSecretCommand.Flag("user", "Username.").Required().String()
|
|
|
|
updateSecretCommandSecretFlag = updateSecretCommand.Flag("secret", "Secret.").Default("generate").String()
|
|
|
|
|
|
|
|
registerAppCommand = kingpin.Command("register-app", "update OTP secret")
|
|
|
|
registerAppCommandUserFlag = registerAppCommand.Flag("user", "Username.").Required().String()
|
|
|
|
|
|
|
|
getSecretCommand = kingpin.Command("get-secret", "gwt OTP secret")
|
|
|
|
getSecretCommandUserFlag = getSecretCommand.Flag("user", "Username.").Required().String()
|
|
|
|
|
2021-02-08 04:53:09 -05:00
|
|
|
debug = kingpin.Flag("debug", "Enable debug mode.").Default("false").Bool()
|
2021-02-05 01:37:12 -05:00
|
|
|
)
|
|
|
|
|
2022-11-14 05:33:43 -05:00
|
|
|
type Migration struct {
|
|
|
|
id int64
|
|
|
|
name string
|
|
|
|
sql string
|
|
|
|
}
|
|
|
|
|
2021-02-05 01:37:12 -05:00
|
|
|
type User struct {
|
2022-11-14 05:33:43 -05:00
|
|
|
id int64
|
|
|
|
name string
|
|
|
|
password string
|
|
|
|
revoked bool
|
|
|
|
deleted bool
|
|
|
|
secret string
|
|
|
|
appConfigured bool
|
2021-02-05 01:37:12 -05:00
|
|
|
}
|
|
|
|
|
2022-11-14 05:33:43 -05:00
|
|
|
var (
|
|
|
|
migrations []Migration
|
|
|
|
)
|
|
|
|
|
2021-02-05 01:37:12 -05:00
|
|
|
func main() {
|
2022-11-14 05:33:43 -05:00
|
|
|
|
|
|
|
migrations = append(migrations, Migration{name: "users_add_secret_column_2022_11_10", sql: "ALTER TABLE users ADD COLUMN secret string"})
|
|
|
|
migrations = append(migrations, Migration{name: "users_add_2fa_column_2022_11_11", sql: "ALTER TABLE users ADD COLUMN app_configured integer default 0"})
|
|
|
|
|
2021-02-19 01:45:26 -05:00
|
|
|
kingpin.Version(version)
|
2021-02-05 01:37:12 -05:00
|
|
|
switch kingpin.Parse() {
|
|
|
|
case createCommand.FullCommand():
|
|
|
|
createUser(*createCommandUserFlag, *createCommandPasswordFlag)
|
|
|
|
case deleteCommand.FullCommand():
|
|
|
|
deleteUser(*deleteCommandUserFlag)
|
|
|
|
case revokeCommand.FullCommand():
|
|
|
|
revokedUser(*revokeCommandUserFlag)
|
|
|
|
case restoreCommand.FullCommand():
|
|
|
|
restoreUser(*restoreCommandUserFlag)
|
|
|
|
case listCommand.FullCommand():
|
|
|
|
printUsers()
|
2021-02-05 13:19:54 -05:00
|
|
|
case checkCommand.FullCommand():
|
|
|
|
_ = checkUserExistent(*checkCommandUserFlag)
|
2021-02-05 01:37:12 -05:00
|
|
|
case authCommand.FullCommand():
|
2022-11-14 05:33:43 -05:00
|
|
|
provideAuthType := 0
|
|
|
|
if *authCommandPasswordFlag != "" {
|
|
|
|
provideAuthType += 1
|
|
|
|
}
|
|
|
|
if *authCommandTotpFlag != "" {
|
|
|
|
provideAuthType += 1
|
|
|
|
}
|
|
|
|
//if *authCommandHotpFlag != "" {
|
|
|
|
// provideAuthType += 1
|
|
|
|
//}
|
|
|
|
if provideAuthType == 1 {
|
|
|
|
authUser(*authCommandUserFlag, *authCommandPasswordFlag, *authCommandTotpFlag)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Please provide only one type of auth paswword")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-02-05 01:37:12 -05:00
|
|
|
case changePasswordCommand.FullCommand():
|
|
|
|
changeUserPassword(*changePasswordCommandUserFlag, *changePasswordCommandPasswordFlag)
|
2022-11-14 05:33:43 -05:00
|
|
|
case updateSecretCommand.FullCommand():
|
|
|
|
registerOtpSecret(*updateSecretCommandUserFlag, *updateSecretCommandSecretFlag)
|
|
|
|
case registerAppCommand.FullCommand():
|
|
|
|
registerOtpApplication(*registerAppCommandUserFlag)
|
|
|
|
case getSecretCommand.FullCommand():
|
|
|
|
getUserOtpSecret(*getSecretCommandUserFlag)
|
2021-02-05 01:37:12 -05:00
|
|
|
case dbInitCommand.FullCommand():
|
|
|
|
initDb()
|
|
|
|
case dbMigrateCommand.FullCommand():
|
|
|
|
migrateDb()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDb() *sql.DB {
|
|
|
|
db, err := sql.Open("sqlite3", *dbPath)
|
|
|
|
checkErr(err)
|
|
|
|
if db == nil {
|
|
|
|
panic("db is nil")
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func initDb() {
|
|
|
|
// boolean fields are integer because of sqlite does not support boolean: 1 = true, 0 = false
|
|
|
|
_, err := getDb().Exec("CREATE TABLE IF NOT EXISTS users(id integer not null primary key autoincrement, username string UNIQUE, password string, revoked integer default 0, deleted integer default 0)")
|
|
|
|
checkErr(err)
|
2022-11-14 05:33:43 -05:00
|
|
|
_, err = getDb().Exec("CREATE TABLE IF NOT EXISTS migrations(id integer not null primary key autoincrement, name string)")
|
|
|
|
checkErr(err)
|
2021-02-05 01:37:12 -05:00
|
|
|
fmt.Printf("Database initialized at %s\n", *dbPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func migrateDb() {
|
2022-11-14 05:33:43 -05:00
|
|
|
var c int
|
|
|
|
for _, migration := range migrations {
|
|
|
|
c = -1
|
|
|
|
err := getDb().QueryRow("SELECT count(*) FROM migrations WHERE name = $1", migration.name).Scan(&c)
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if c == 0 {
|
|
|
|
fmt.Printf("Migrating database with new migration %s\n", migration.name)
|
|
|
|
_, err := getDb().Exec(migration.sql)
|
|
|
|
checkErr(err)
|
|
|
|
_, err = getDb().Exec("INSERT INTO migrations(name) VALUES ($1)", migration.name)
|
|
|
|
checkErr(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println("Migrations are up to date")
|
2021-02-05 01:37:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func createUser(username, password string) {
|
|
|
|
if !checkUserExistent(username) {
|
|
|
|
hash, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost)
|
|
|
|
_, err := getDb().Exec("INSERT INTO users(username, password) VALUES ($1, $2)", username, string(hash))
|
|
|
|
checkErr(err)
|
|
|
|
fmt.Printf("User %s created\n", username)
|
2021-02-08 04:53:09 -05:00
|
|
|
} else {
|
|
|
|
fmt.Printf("ERROR: User %s already registered\n", username)
|
|
|
|
os.Exit(1)
|
2021-02-05 01:37:12 -05:00
|
|
|
}
|
2021-02-08 04:53:09 -05:00
|
|
|
|
2021-02-05 01:37:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteUser(username string) {
|
2022-05-23 09:21:54 -04:00
|
|
|
deleteQuery := "UPDATE users SET deleted = 1 WHERE username = $1"
|
|
|
|
if *deleteCommandUserForceFlag {
|
|
|
|
deleteQuery = "DELETE FROM users WHERE username = $1"
|
|
|
|
}
|
|
|
|
res, err := getDb().Exec(deleteQuery, username)
|
2021-02-05 01:37:12 -05:00
|
|
|
checkErr(err)
|
2021-02-08 04:53:09 -05:00
|
|
|
if rowsAffected, rowsErr := res.RowsAffected(); rowsErr != nil {
|
|
|
|
if rowsAffected == 1 {
|
|
|
|
fmt.Printf("User %s deleted\n", username)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if *debug {
|
|
|
|
fmt.Printf("ERROR: due deleting user %s: %s\n", username, rowsErr)
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 01:37:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func revokedUser(username string) {
|
2021-02-08 04:53:09 -05:00
|
|
|
if !userDeleted(username) {
|
|
|
|
res, err := getDb().Exec("UPDATE users SET revoked = 1 WHERE username = $1", username)
|
|
|
|
checkErr(err)
|
|
|
|
if rowsAffected, rowsErr := res.RowsAffected(); rowsErr != nil {
|
|
|
|
if rowsAffected == 1 {
|
|
|
|
fmt.Printf("User %s revoked\n", username)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if *debug {
|
|
|
|
fmt.Printf("ERROR: due reoking user %s: %s\n", username, rowsErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 01:37:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func restoreUser(username string) {
|
2021-02-08 04:53:09 -05:00
|
|
|
if !userDeleted(username) {
|
|
|
|
res, err := getDb().Exec("UPDATE users SET revoked = 0 WHERE username = $1", username)
|
|
|
|
checkErr(err)
|
|
|
|
if rowsAffected, rowsErr := res.RowsAffected(); rowsErr != nil {
|
|
|
|
if rowsAffected == 1 {
|
|
|
|
fmt.Printf("User %s restored\n", username)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if *debug {
|
|
|
|
fmt.Printf("ERROR: due restoring user %s: %s\n", username, rowsErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 01:37:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkUserExistent(username string) bool {
|
|
|
|
// we need to check if there is already such a user
|
2021-02-08 04:53:09 -05:00
|
|
|
// return true if user exist
|
2021-02-05 01:37:12 -05:00
|
|
|
var c int
|
|
|
|
_ = getDb().QueryRow("SELECT count(*) FROM users WHERE username = $1", username).Scan(&c)
|
2021-02-08 04:53:09 -05:00
|
|
|
if c == 1 {
|
|
|
|
fmt.Printf("User %s exist\n", username)
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func userDeleted(username string) bool {
|
|
|
|
// return true if user marked as deleted
|
|
|
|
u := User{}
|
2022-11-14 05:33:43 -05:00
|
|
|
_ = getDb().QueryRow("SELECT deleted FROM users WHERE username = $1", username).Scan(&u.deleted)
|
2022-05-23 09:21:54 -04:00
|
|
|
if u.deleted {
|
2021-02-08 04:53:09 -05:00
|
|
|
fmt.Printf("User %s marked as deleted\n", username)
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func userIsActive(username string) bool {
|
|
|
|
// return true if user exist and not deleted and revoked
|
|
|
|
u := User{}
|
2022-11-14 05:33:43 -05:00
|
|
|
err := getDb().QueryRow("SELECT revoked,deleted FROM users WHERE username = $1", username).Scan(&u.revoked, &u.deleted)
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
fmt.Println("User not found")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-05-23 09:21:54 -04:00
|
|
|
if !u.revoked && !u.deleted {
|
2022-11-14 05:33:43 -05:00
|
|
|
if *debug {
|
|
|
|
fmt.Printf("User %s is active\n", username)
|
|
|
|
}
|
2021-02-05 01:37:12 -05:00
|
|
|
return true
|
|
|
|
} else {
|
2021-02-08 04:53:09 -05:00
|
|
|
fmt.Println("User may be deleted or revoked")
|
2021-02-05 01:37:12 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func listUsers() []User {
|
|
|
|
condition := "WHERE deleted = 0 AND revoked = 0"
|
|
|
|
var users []User
|
|
|
|
if *listAll {
|
|
|
|
condition = ""
|
|
|
|
}
|
2022-11-14 05:33:43 -05:00
|
|
|
query := "SELECT id, username, password, revoked, deleted FROM users " + condition
|
2021-02-05 01:37:12 -05:00
|
|
|
rows, err := getDb().Query(query)
|
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
u := User{}
|
|
|
|
err := rows.Scan(&u.id, &u.name, &u.password, &u.revoked, &u.deleted)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
users = append(users, u)
|
|
|
|
}
|
|
|
|
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
|
|
|
func printUsers() {
|
|
|
|
ul := listUsers()
|
|
|
|
if len(ul) > 0 {
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent|tabwriter.Debug)
|
|
|
|
_, _ = fmt.Fprintln(w, "id\t username\t revoked\t deleted")
|
|
|
|
for _, u := range ul {
|
|
|
|
fmt.Fprintf(w, "%d\t %s\t %v\t %v\n", u.id, u.name, u.revoked, u.deleted)
|
|
|
|
}
|
|
|
|
_ = w.Flush()
|
|
|
|
} else {
|
|
|
|
fmt.Println("No users created yet")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func changeUserPassword(username, password string) {
|
|
|
|
hash, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost)
|
|
|
|
_, err := getDb().Exec("UPDATE users SET password = $1 WHERE username = $2", hash, username)
|
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
fmt.Println("Password changed")
|
|
|
|
}
|
|
|
|
|
2022-11-14 05:33:43 -05:00
|
|
|
func registerOtpSecret(username, secret string) {
|
|
|
|
if userIsActive(username) {
|
|
|
|
if secret == "generate" {
|
|
|
|
randomStr := randStr(6, "alphanum")
|
|
|
|
|
|
|
|
secret = base32.StdEncoding.EncodeToString([]byte(randomStr))
|
|
|
|
if *debug {
|
|
|
|
fmt.Printf("new generated secret for user %s: %s\n", username, secret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := getDb().Exec("UPDATE users SET secret = $1 WHERE username = $2", secret, username)
|
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
fmt.Println("Secret updated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerOtpApplication(username string) {
|
|
|
|
if userIsActive(username) {
|
|
|
|
|
|
|
|
_, err := getDb().Exec("UPDATE users SET app_configured = 1 WHERE username = $2")
|
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
fmt.Printf("OTP application for user %s configured\n", username)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUserOtpSecret(username string) {
|
|
|
|
if userIsActive(username) {
|
|
|
|
u := User{}
|
|
|
|
_ = getDb().QueryRow("SELECT secret FROM users WHERE username = $1", username).Scan(&u.secret)
|
|
|
|
|
|
|
|
fmt.Println(u.secret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func authUser(username, password, totp string) {
|
2021-02-05 01:37:12 -05:00
|
|
|
|
2022-11-14 10:29:12 -05:00
|
|
|
row := getDb().QueryRow("SELECT id, username, password, revoked, deleted, secret, app_configured FROM users WHERE username = $1", username)
|
2021-02-05 01:37:12 -05:00
|
|
|
u := User{}
|
2022-11-14 09:45:12 -05:00
|
|
|
err := row.Scan(&u.id, &u.name, &u.password, &u.revoked, &u.deleted, &u.secret, &u.appConfigured)
|
2021-02-05 01:37:12 -05:00
|
|
|
checkErr(err)
|
|
|
|
|
2021-02-08 04:53:09 -05:00
|
|
|
if userIsActive(username) {
|
2022-11-14 05:33:43 -05:00
|
|
|
if password == "" && len(totp) > 0 {
|
|
|
|
otpConfig := &dgoogauth.OTPConfig{
|
|
|
|
Secret: strings.TrimSpace(u.secret),
|
|
|
|
WindowSize: 3,
|
|
|
|
HotpCounter: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
// get rid of the extra \n from the token string
|
|
|
|
// otherwise the validation will fail
|
|
|
|
trimmedToken := strings.TrimSpace(totp)
|
|
|
|
|
|
|
|
// Validate token
|
|
|
|
_, err := otpConfig.Authenticate(trimmedToken)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
} else {
|
|
|
|
fmt.Println("Authorization successful")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
} else if len(password) > 0 && totp == "" {
|
|
|
|
|
|
|
|
err = bcrypt.CompareHashAndPassword([]byte(u.password), []byte(password))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Authorization failed")
|
|
|
|
if *debug {
|
|
|
|
fmt.Println("Passwords mismatched")
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
} else {
|
|
|
|
fmt.Println("Authorization successful")
|
|
|
|
os.Exit(0)
|
2021-02-08 04:53:09 -05:00
|
|
|
}
|
2021-02-05 01:37:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println("Authorization failed")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2022-11-14 05:33:43 -05:00
|
|
|
func randStr(strSize int, randType string) string {
|
|
|
|
|
|
|
|
var dictionary string
|
|
|
|
|
|
|
|
if randType == "alphanum" {
|
|
|
|
dictionary = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
}
|
|
|
|
|
|
|
|
if randType == "alpha" {
|
|
|
|
dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
}
|
|
|
|
|
|
|
|
if randType == "number" {
|
|
|
|
dictionary = "0123456789"
|
|
|
|
}
|
|
|
|
|
|
|
|
var bytes = make([]byte, strSize)
|
|
|
|
rand.Read(bytes)
|
|
|
|
for k, v := range bytes {
|
|
|
|
bytes[k] = dictionary[v%byte(len(dictionary))]
|
|
|
|
}
|
|
|
|
return string(bytes)
|
|
|
|
}
|
|
|
|
|
2021-02-05 01:37:12 -05:00
|
|
|
func checkErr(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|