headscale/utils.go

128 lines
3.2 KiB
Go
Raw Normal View History

2020-06-21 04:32:08 -06:00
// Codehere is mostly taken from github.com/tailscale/tailscale
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package headscale
import (
"crypto/rand"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
"net"
2021-02-21 14:11:27 -07:00
"time"
2020-06-21 04:32:08 -06:00
mathrand "math/rand"
"golang.org/x/crypto/nacl/box"
2021-06-24 07:44:19 -06:00
"gorm.io/gorm"
2021-06-25 10:57:08 -06:00
"tailscale.com/types/wgkey"
2020-06-21 04:32:08 -06:00
)
2021-05-05 17:01:45 -06:00
// Error is used to compare errors as per https://dave.cheney.net/2016/04/07/constant-errors
2021-05-05 15:00:04 -06:00
type Error string
func (e Error) Error() string { return string(e) }
2021-06-25 10:57:08 -06:00
func decode(msg []byte, v interface{}, pubKey *wgkey.Key, privKey *wgkey.Private) error {
2020-06-21 04:32:08 -06:00
return decodeMsg(msg, v, pubKey, privKey)
}
2021-06-25 10:57:08 -06:00
func decodeMsg(msg []byte, v interface{}, pubKey *wgkey.Key, privKey *wgkey.Private) error {
2020-06-21 04:32:08 -06:00
decrypted, err := decryptMsg(msg, pubKey, privKey)
if err != nil {
return err
}
// fmt.Println(string(decrypted))
if err := json.Unmarshal(decrypted, v); err != nil {
return fmt.Errorf("response: %v", err)
}
return nil
}
2021-06-25 10:57:08 -06:00
func decryptMsg(msg []byte, pubKey *wgkey.Key, privKey *wgkey.Private) ([]byte, error) {
2020-06-21 04:32:08 -06:00
var nonce [24]byte
if len(msg) < len(nonce)+1 {
return nil, fmt.Errorf("response missing nonce, len=%d", len(msg))
}
copy(nonce[:], msg)
msg = msg[len(nonce):]
pub, pri := (*[32]byte)(pubKey), (*[32]byte)(privKey)
decrypted, ok := box.Open(nil, msg, &nonce, pub, pri)
if !ok {
return nil, fmt.Errorf("cannot decrypt response")
}
return decrypted, nil
}
2021-06-25 10:57:08 -06:00
func encode(v interface{}, pubKey *wgkey.Key, privKey *wgkey.Private) ([]byte, error) {
2020-06-21 04:32:08 -06:00
b, err := json.Marshal(v)
if err != nil {
return nil, err
}
return encodeMsg(b, pubKey, privKey)
}
2021-06-25 10:57:08 -06:00
func encodeMsg(b []byte, pubKey *wgkey.Key, privKey *wgkey.Private) ([]byte, error) {
2020-06-21 04:32:08 -06:00
var nonce [24]byte
if _, err := io.ReadFull(rand.Reader, nonce[:]); err != nil {
panic(err)
}
pub, pri := (*[32]byte)(pubKey), (*[32]byte)(privKey)
msg := box.Seal(nonce[:], b, &nonce, pub, pri)
return msg, nil
}
func (h *Headscale) getAvailableIP() (*net.IP, error) {
db, err := h.db()
if err != nil {
return nil, err
}
i := 0
for {
ip, err := getRandomIP()
if err != nil {
return nil, err
}
m := Machine{}
2021-06-24 07:44:19 -06:00
if result := db.First(&m, "ip_address = ?", ip.String()); errors.Is(result.Error, gorm.ErrRecordNotFound) {
2020-06-21 04:32:08 -06:00
return ip, nil
}
i++
if i == 100 { // really random number
break
}
}
return nil, errors.New("Could not find an available IP address in 100.64.0.0/10")
}
func getRandomIP() (*net.IP, error) {
2021-02-21 14:11:27 -07:00
mathrand.Seed(time.Now().Unix())
2020-06-21 04:32:08 -06:00
ipo, ipnet, err := net.ParseCIDR("100.64.0.0/10")
if err == nil {
ip := ipo.To4()
2021-05-08 05:27:40 -06:00
// fmt.Println("In Randomize IPAddr: IP ", ip, " IPNET: ", ipnet)
// fmt.Println("Final address is ", ip)
2020-06-21 04:32:08 -06:00
// fmt.Println("Broadcast address is ", ipb)
// fmt.Println("Network address is ", ipn)
2021-02-21 14:11:27 -07:00
r := mathrand.Uint32()
2020-06-21 04:32:08 -06:00
ipRaw := make([]byte, 4)
2021-02-21 14:11:27 -07:00
binary.LittleEndian.PutUint32(ipRaw, r)
2020-06-21 04:32:08 -06:00
// ipRaw[3] = 254
// fmt.Println("ipRaw is ", ipRaw)
for i, v := range ipRaw {
// fmt.Println("IP Before: ", ip[i], " v is ", v, " Mask is: ", ipnet.Mask[i])
ip[i] = ip[i] + (v &^ ipnet.Mask[i])
// fmt.Println("IP After: ", ip[i])
}
2021-05-08 05:27:40 -06:00
// fmt.Println("FINAL IP: ", ip.String())
2020-06-21 04:32:08 -06:00
return &ip, nil
}
2021-02-21 14:11:27 -07:00
return nil, err
2020-06-21 04:32:08 -06:00
}