mirror of
https://github.com/jsiebens/ionscale.git
synced 2026-03-31 15:07:49 +01:00
initial working version
Signed-off-by: Johan Siebens <johan.siebens@gmail.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/sony/sonyflake"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var sf *sonyflake.Sonyflake
|
||||
|
||||
func init() {
|
||||
sf = sonyflake.NewSonyflake(sonyflake.Settings{
|
||||
MachineID: machineID(),
|
||||
StartTime: time.Date(2022, 05, 01, 00, 0, 0, 0, time.UTC),
|
||||
})
|
||||
if sf == nil {
|
||||
panic("unable to initialize sonyflake")
|
||||
}
|
||||
}
|
||||
|
||||
func machineID() func() (uint16, error) {
|
||||
envMachineID := os.Getenv("IONSCALE_MACHINE_ID")
|
||||
if len(envMachineID) != 0 {
|
||||
return func() (uint16, error) {
|
||||
id, err := strconv.ParseInt(envMachineID, 0, 16)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return uint16(id), nil
|
||||
}
|
||||
}
|
||||
|
||||
envMachineIP := os.Getenv("IONSCALE_MACHINE_IP")
|
||||
if len(envMachineIP) != 0 {
|
||||
return func() (uint16, error) {
|
||||
ip := net.ParseIP(envMachineIP).To4()
|
||||
if len(ip) < 4 {
|
||||
return 0, fmt.Errorf("invalid IP")
|
||||
}
|
||||
return uint16(ip[2])<<8 + uint16(ip[3]), nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NextID() uint64 {
|
||||
id, _ := sf.NextID()
|
||||
return id
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"tailscale.com/types/key"
|
||||
)
|
||||
|
||||
const (
|
||||
discoPublicHexPrefix = "discokey:"
|
||||
nodePublicHexPrefix = "nodekey:"
|
||||
machinePublicHexPrefix = "mkey:"
|
||||
privateHexPrefix = "privkey:"
|
||||
)
|
||||
|
||||
func ParseMachinePrivateKey(machineKey string) (*key.MachinePrivate, error) {
|
||||
if !strings.HasPrefix(machineKey, privateHexPrefix) {
|
||||
machineKey = privateHexPrefix + machineKey
|
||||
}
|
||||
|
||||
var mp key.MachinePrivate
|
||||
|
||||
if err := mp.UnmarshalText([]byte(machineKey)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &mp, nil
|
||||
}
|
||||
|
||||
func ParseMachinePublicKey(machineKey string) (*key.MachinePublic, error) {
|
||||
if !strings.HasPrefix(machineKey, machinePublicHexPrefix) {
|
||||
machineKey = machinePublicHexPrefix + machineKey
|
||||
}
|
||||
|
||||
var mp key.MachinePublic
|
||||
|
||||
if err := mp.UnmarshalText([]byte(machineKey)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &mp, nil
|
||||
}
|
||||
|
||||
func ParseNodePublicKey(machineKey string) (*key.NodePublic, error) {
|
||||
if !strings.HasPrefix(machineKey, nodePublicHexPrefix) {
|
||||
machineKey = nodePublicHexPrefix + machineKey
|
||||
}
|
||||
|
||||
var mp key.NodePublic
|
||||
|
||||
if err := mp.UnmarshalText([]byte(machineKey)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &mp, nil
|
||||
}
|
||||
|
||||
func ParseDiscoPublicKey(machineKey string) (*key.DiscoPublic, error) {
|
||||
if !strings.HasPrefix(machineKey, discoPublicHexPrefix) {
|
||||
machineKey = discoPublicHexPrefix + machineKey
|
||||
}
|
||||
|
||||
var mp key.DiscoPublic
|
||||
|
||||
if err := mp.UnmarshalText([]byte(machineKey)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &mp, nil
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
var entropy *rand.Rand
|
||||
|
||||
func init() {
|
||||
seed := time.Now().UnixNano()
|
||||
source := rand.NewSource(seed)
|
||||
entropy = rand.New(source)
|
||||
}
|
||||
|
||||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
func RandStringBytes(n int) string {
|
||||
b := make([]byte, n)
|
||||
for i := range b {
|
||||
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func RandUint64(n uint64) uint64 {
|
||||
return entropy.Uint64() % n
|
||||
}
|
||||
|
||||
func RandomBytes(size int) ([]byte, error) {
|
||||
buf := make([]byte, size)
|
||||
if _, err := entropy.Read(buf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf, nil
|
||||
}
|
||||
Reference in New Issue
Block a user