headscale/utils.go

124 lines
3.2 KiB
Go
Raw Permalink Normal View History

2020-06-21 10:32:08 +00: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 21:11:27 +00:00
"time"
2020-06-21 10:32:08 +00:00
mathrand "math/rand"
"golang.org/x/crypto/nacl/box"
2021-06-24 13:44:19 +00:00
"gorm.io/gorm"
2021-06-25 16:57:08 +00:00
"tailscale.com/types/wgkey"
2020-06-21 10:32:08 +00:00
)
2021-05-05 23:01:45 +00:00
// Error is used to compare errors as per https://dave.cheney.net/2016/04/07/constant-errors
2021-05-05 21:00:04 +00:00
type Error string
func (e Error) Error() string { return string(e) }
2021-06-25 16:57:08 +00:00
func decode(msg []byte, v interface{}, pubKey *wgkey.Key, privKey *wgkey.Private) error {
2020-06-21 10:32:08 +00:00
return decodeMsg(msg, v, pubKey, privKey)
}
2021-06-25 16:57:08 +00:00
func decodeMsg(msg []byte, v interface{}, pubKey *wgkey.Key, privKey *wgkey.Private) error {
2020-06-21 10:32:08 +00: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 16:57:08 +00:00
func decryptMsg(msg []byte, pubKey *wgkey.Key, privKey *wgkey.Private) ([]byte, error) {
2020-06-21 10:32:08 +00: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 16:57:08 +00:00
func encode(v interface{}, pubKey *wgkey.Key, privKey *wgkey.Private) ([]byte, error) {
2020-06-21 10:32:08 +00:00
b, err := json.Marshal(v)
if err != nil {
return nil, err
}
return encodeMsg(b, pubKey, privKey)
}
2021-06-25 16:57:08 +00:00
func encodeMsg(b []byte, pubKey *wgkey.Key, privKey *wgkey.Private) ([]byte, error) {
2020-06-21 10:32:08 +00: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) {
i := 0
for {
ip, err := getRandomIP()
if err != nil {
return nil, err
}
m := Machine{}
2021-07-04 19:40:46 +00:00
if result := h.db.First(&m, "ip_address = ?", ip.String()); errors.Is(result.Error, gorm.ErrRecordNotFound) {
2020-06-21 10:32:08 +00: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 21:11:27 +00:00
mathrand.Seed(time.Now().Unix())
2020-06-21 10:32:08 +00:00
ipo, ipnet, err := net.ParseCIDR("100.64.0.0/10")
if err == nil {
ip := ipo.To4()
2021-05-08 11:27:40 +00:00
// fmt.Println("In Randomize IPAddr: IP ", ip, " IPNET: ", ipnet)
// fmt.Println("Final address is ", ip)
2020-06-21 10:32:08 +00:00
// fmt.Println("Broadcast address is ", ipb)
// fmt.Println("Network address is ", ipn)
2021-02-21 21:11:27 +00:00
r := mathrand.Uint32()
2020-06-21 10:32:08 +00:00
ipRaw := make([]byte, 4)
2021-02-21 21:11:27 +00:00
binary.LittleEndian.PutUint32(ipRaw, r)
2020-06-21 10:32:08 +00: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 11:27:40 +00:00
// fmt.Println("FINAL IP: ", ip.String())
2020-06-21 10:32:08 +00:00
return &ip, nil
}
2021-02-21 21:11:27 +00:00
return nil, err
2020-06-21 10:32:08 +00:00
}