..
This commit is contained in:
50
database.go
50
database.go
@@ -1,23 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"crypto/sha512"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"github.com/miekg/dns"
|
||||
"github.com/patrickmn/go-cache"
|
||||
password "github.com/sethvargo/go-password/password"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/syndtr/goleveldb/leveldb"
|
||||
"github.com/syndtr/goleveldb/leveldb/util"
|
||||
"math"
|
||||
"math/big"
|
||||
bcrypt "golang.org/x/crypto/bcrypt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
"github.com/patrickmn/go-cache"
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
@@ -57,11 +53,11 @@ func (d *Database) Authorize(host string, token_user string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
if len(token) < sha512.Size {
|
||||
if len(token) < sha256.Size {
|
||||
return false
|
||||
}
|
||||
|
||||
if len(token_user) < sha512.Size {
|
||||
if len(token_user) < sha256.Size {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -74,24 +70,21 @@ func (d *Database) Authorize(host string, token_user string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func generateToken() string {
|
||||
maxInt := big.NewInt(math.MaxInt64)
|
||||
randInt, err := rand.Int(rand.Reader, maxInt)
|
||||
func generateToken() (string, []byte, error) {
|
||||
pw, err := password.Generate(64, 10, 10, false, false)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
h_plain := []byte(randInt.String() + time.Now().String())
|
||||
|
||||
h:= sha256.Sum256(h_plain)
|
||||
hash, errHash := bcrypt.GenerateFromPassword([]byte(pw), bcrypt.DefaultCost)
|
||||
|
||||
for i:=0;i<10000; i++ {
|
||||
h = sha256.Sum256(h[:])
|
||||
if errHash != nil {
|
||||
return "", nil , errHash
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
base64.NewEncoder(base64.URLEncoding, &buf).Write(h[:])
|
||||
return buf.String()
|
||||
|
||||
return pw, hash, nil
|
||||
}
|
||||
|
||||
func (d *Database) IsBannedHost(host string) bool {
|
||||
@@ -119,24 +112,27 @@ func (d *Database) IncrementBanHost(host string) {
|
||||
}
|
||||
|
||||
|
||||
func (d *Database) CreateHost(host string) (string, error) {
|
||||
func (d *Database) CreateHost(host string) (string, []byte, error) {
|
||||
if d.ExistHost(host) {
|
||||
return "", errors.New("host already existent")
|
||||
return "", nil, errors.New("host already existent")
|
||||
}
|
||||
|
||||
if host == "" {
|
||||
return "", errors.New("given hostname is empty")
|
||||
return "", nil, errors.New("given hostname is empty")
|
||||
}
|
||||
|
||||
if _, ok := dns.IsDomainName(host + "." + d._zone); !ok {
|
||||
return "", errors.New("given hostname is invalid")
|
||||
return "", nil, errors.New("given hostname is invalid")
|
||||
}
|
||||
|
||||
|
||||
token := generateToken()
|
||||
d._db.Put([]byte("hosts/" + host), []byte(token), nil)
|
||||
pw, hash, errToken := generateToken()
|
||||
if errToken != nil {
|
||||
return "", nil, errToken
|
||||
}
|
||||
|
||||
return token, nil
|
||||
d._db.Put([]byte("hosts/" + host), hash, nil)
|
||||
return pw, hash, nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user