Compare commits
5 Commits
v0.5.2
...
4aee69385d
| Author | SHA1 | Date | |
|---|---|---|---|
| 4aee69385d | |||
| 6b9ad95c36 | |||
| a38aee1219 | |||
| 24f657224c | |||
| d813175125 |
@@ -0,0 +1,27 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/zones"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Command
|
||||||
|
var envCmd = &cobra.Command{
|
||||||
|
Use: "env",
|
||||||
|
Short: "generates environment variables for shell scripts",
|
||||||
|
RunE: func(_ *cobra.Command, _ []string) error {
|
||||||
|
m, err := zones.New(cfg.Base, cfg.Domain)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.WriteEnv(os.Stdout)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(envCmd)
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package wireguard
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
)
|
||||||
|
|
||||||
|
// BinaryKey is a binary blob
|
||||||
|
type BinaryKey []byte
|
||||||
|
|
||||||
|
func (k BinaryKey) String() string {
|
||||||
|
return base64.StdEncoding.EncodeToString(k)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsZero tells if the key hasn't been set
|
||||||
|
func (k BinaryKey) IsZero() bool {
|
||||||
|
return len(k) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// BinaryKeyFromBase64 decodes a base64-based string into
|
||||||
|
// a [BinaryKey]
|
||||||
|
func BinaryKeyFromBase64(data string) (BinaryKey, error) {
|
||||||
|
b, err := base64.StdEncoding.DecodeString(data)
|
||||||
|
return BinaryKey(b), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// KeyPair holds a Key pair
|
||||||
|
type KeyPair struct {
|
||||||
|
PrivateKey BinaryKey
|
||||||
|
PublicKey BinaryKey
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// Package zones abstracts the cluster zones
|
||||||
|
package zones
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
|
// WriteEnv generates environment variables for shell scripts
|
||||||
|
func (*Zones) WriteEnv(io.Writer) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,11 +1,86 @@
|
|||||||
package zones
|
package zones
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/wireguard"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GetWireguardConfig reads a wgN.conf file
|
||||||
|
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
|
||||||
|
data, err := m.ReadFile("wg%v.conf", ring)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
r := bytes.NewReader(data)
|
||||||
|
return wireguard.NewConfigFromReader(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) tryWireguardZeroConfig() error {
|
||||||
|
wg, err := m.GetWireguardConfig(0)
|
||||||
|
switch {
|
||||||
|
case os.IsNotExist(err):
|
||||||
|
return nil
|
||||||
|
case err != nil:
|
||||||
|
return err
|
||||||
|
default:
|
||||||
|
return m.applyWireguardZeroConfig(wg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) applyWireguardZeroConfig(wg *wireguard.Config) error {
|
||||||
|
addr := wg.GetAddress()
|
||||||
|
zoneID, nodeID, ok := ParseRingZeroAddress(addr)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("%s: invalid %s address: %s", m.Name, "wg1", addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println(m.Name, "wg0", addr, zoneID, nodeID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) tryWireguardOneConfig() error {
|
||||||
|
wg, err := m.GetWireguardConfig(1)
|
||||||
|
switch {
|
||||||
|
case os.IsNotExist(err):
|
||||||
|
return nil
|
||||||
|
case err != nil:
|
||||||
|
return err
|
||||||
|
default:
|
||||||
|
return m.applyWireguardOneConfig(wg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) applyWireguardOneConfig(wg *wireguard.Config) error {
|
||||||
|
addr := wg.GetAddress()
|
||||||
|
zoneID, nodeID, ok := ParseRingOneAddress(addr)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("%s: invalid %s address: %s", m.Name, "wg1", addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println(m.Name, "wg1", addr, zoneID, nodeID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Machine) applyZoneNodeID(_, _ int) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Machine) lookupNetIP() ([]netip.Addr, error) {
|
func (m *Machine) lookupNetIP() ([]netip.Addr, error) {
|
||||||
timeout := 2 * time.Second
|
timeout := 2 * time.Second
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||||
@@ -26,5 +101,13 @@ func (m *Machine) updatePublicAddresses() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machine) scan() error {
|
func (m *Machine) scan() error {
|
||||||
|
if err := m.tryWireguardZeroConfig(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := m.tryWireguardOneConfig(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return m.updatePublicAddresses()
|
return m.updatePublicAddresses()
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-1
@@ -1,6 +1,10 @@
|
|||||||
package zones
|
package zones
|
||||||
|
|
||||||
import "net/netip"
|
import (
|
||||||
|
"net/netip"
|
||||||
|
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/wireguard"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// MaxZoneID indicates the highest ID allowed for a Zone
|
// MaxZoneID indicates the highest ID allowed for a Zone
|
||||||
@@ -11,6 +15,15 @@ const (
|
|||||||
RingsCount = 2
|
RingsCount = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// RingInfo contains represents the Wireguard endpoint details
|
||||||
|
// for a Machine on a particular ring
|
||||||
|
type RingInfo struct {
|
||||||
|
Ring int
|
||||||
|
Enabled bool
|
||||||
|
Keys *wireguard.KeyPair
|
||||||
|
Address netip.Addr
|
||||||
|
}
|
||||||
|
|
||||||
// RingAddressEncoder provides encoder/decoder access for a particular
|
// RingAddressEncoder provides encoder/decoder access for a particular
|
||||||
// Wireguard ring
|
// Wireguard ring
|
||||||
type RingAddressEncoder struct {
|
type RingAddressEncoder struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user