mirror of https://github.com/dirtbags/moth.git
Better token generation
This commit is contained in:
parent
e68a041f33
commit
45c0cad5d4
|
@ -11,6 +11,7 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"math/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Instance struct {
|
type Instance struct {
|
||||||
|
@ -52,6 +53,18 @@ func NewInstance(base, mothballDir, stateDir, resourcesDir, password string) (*I
|
||||||
return ctx, nil
|
return ctx, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Stuff people with mediocre handwriting could write down unambiguously, and can be entered without holding down shift
|
||||||
|
const distinguishableChars = "234678abcdefhijkmnpqrtuvwxyz="
|
||||||
|
|
||||||
|
func mktoken() string {
|
||||||
|
a := make([]byte, 8)
|
||||||
|
for i := range(a) {
|
||||||
|
char := rand.Intn(len(distinguishableChars))
|
||||||
|
a[i] = distinguishableChars[char]
|
||||||
|
}
|
||||||
|
return string(a)
|
||||||
|
}
|
||||||
|
|
||||||
func (ctx *Instance) MaybeInitialize() {
|
func (ctx *Instance) MaybeInitialize() {
|
||||||
// Only do this if it hasn't already been done
|
// Only do this if it hasn't already been done
|
||||||
if _, err := os.Stat(ctx.StatePath("initialized")); err == nil {
|
if _, err := os.Stat(ctx.StatePath("initialized")); err == nil {
|
||||||
|
@ -75,8 +88,8 @@ func (ctx *Instance) MaybeInitialize() {
|
||||||
// Preseed available team ids if file doesn't exist
|
// Preseed available team ids if file doesn't exist
|
||||||
if f, err := os.OpenFile(ctx.StatePath("teamids.txt"), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644); err == nil {
|
if f, err := os.OpenFile(ctx.StatePath("teamids.txt"), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644); err == nil {
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
for i := 0; i <= 9999; i += 1 {
|
for i := 0; i <= 100; i += 1 {
|
||||||
fmt.Fprintf(f, "%04d\n", i)
|
fmt.Fprintln(f, mktoken())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,10 +6,12 @@ import (
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
"math/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func setup() error {
|
func setup() error {
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue