dsnet/types.go

107 lines
2.6 KiB
Go
Raw Normal View History

2020-02-10 20:58:13 +01:00
package dsnet
import (
2020-03-02 00:08:10 +01:00
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
2020-02-10 20:58:13 +01:00
"net"
2020-02-28 00:22:32 +01:00
"time"
2020-02-10 20:58:13 +01:00
)
// see https://github.com/WireGuard/wgctrl-go/blob/master/wgtypes/types.go for definitions
2020-02-20 20:08:07 +01:00
type PeerConfig struct {
// username of person running this host/router
2020-03-02 00:08:10 +01:00
Owner string `validate:"required,gte=1,lte=255"`
2020-02-20 20:08:07 +01:00
// Used to update DNS
2020-03-02 00:08:10 +01:00
Hostname string `validate:"required,gte=1,lte=255"`
2020-02-20 20:08:07 +01:00
// Description of what the host is and/or does
2020-03-02 00:08:10 +01:00
Description string `validate:"required,gte=1,lte=255"`
2020-02-20 20:08:07 +01:00
2020-03-02 00:08:10 +01:00
PublicKey JSONKey `validate:"required,len=44"`
PresharedKey JSONKey `validate:"required,len=44"`
Endpoint net.UDPAddr `validate:"required,udp4_addr"`
AllowedIPs []net.IPNet `validate:"dive,required,cidr"`
2020-02-20 20:08:07 +01:00
}
2020-02-10 20:58:13 +01:00
type Peer struct {
2020-02-20 20:08:07 +01:00
// username of person running this host/router
Owner string
// Used to update DNS
Hostname string
// Description of what the host is and/or does
2020-02-10 20:58:13 +01:00
Description string
2020-02-20 20:08:07 +01:00
// whether last heartbeat/rxdata was received (50% margin)
Online bool
// if no data for x days, consider revoking access
Expired bool
2020-03-02 00:08:10 +01:00
PublicKey wgtypes.Key
PresharedKey wgtypes.Key
Endpoint *net.UDPAddr
2020-02-10 20:58:13 +01:00
LastHandshakeTime time.Time
2020-03-02 00:08:10 +01:00
ReceiveBytes int64
TransmitBytes int64
AllowedIPs []net.IPNet
2020-02-10 20:58:13 +01:00
}
2020-02-20 20:08:07 +01:00
type DsnetConfig struct {
2020-03-02 00:08:10 +01:00
PrivateKey JSONKey `validate:"required,len=44"`
PresharedKey JSONKey `validate:"required,len=44"`
ListenPort int `validate:"gte=1024,lte=65535"`
Peers []PeerConfig
2020-02-27 23:19:48 +01:00
// IP network from which to allocate automatic sequential addresses
2020-02-27 23:31:44 +01:00
// Network is chosen randomly when not specified
2020-03-02 00:08:10 +01:00
Network JSONIPNet `validate:"required"`
2020-02-27 23:19:48 +01:00
// domain to append to hostnames. Relies on separate DNS server for
// resolution. Informational only.
2020-03-02 00:08:10 +01:00
Domain string `validate:"required,gte=1,lte=255"`
2020-03-01 23:03:31 +01:00
// TODO Default subnets to route via VPN
2020-03-02 00:17:01 +01:00
ReportFile string `validate:"required"`
2020-02-20 20:08:07 +01:00
}
type Dsnet struct {
2020-03-02 00:08:10 +01:00
Name string
2020-02-28 00:22:32 +01:00
PrivateKey wgtypes.Key
2020-03-02 00:08:10 +01:00
PublicKey wgtypes.Key
2020-02-20 20:08:07 +01:00
ListenPort int
2020-03-02 00:08:10 +01:00
Peers []Peer
2020-02-20 20:08:07 +01:00
}
2020-03-01 23:03:31 +01:00
2020-03-01 23:29:11 +01:00
type JSONIPNet struct {
2020-03-01 23:58:21 +01:00
IPNet net.IPNet
2020-03-01 23:03:31 +01:00
}
2020-03-01 23:29:11 +01:00
func (n JSONIPNet) MarshalJSON() ([]byte, error) {
2020-03-01 23:58:21 +01:00
return []byte("\"" + n.IPNet.String() + "\""), nil
2020-03-01 23:03:31 +01:00
}
2020-03-01 23:29:11 +01:00
func (n *JSONIPNet) String() string {
2020-03-01 23:58:21 +01:00
return n.IPNet.String()
}
type JSONKey struct {
Key wgtypes.Key
}
func (k JSONKey) MarshalJSON() ([]byte, error) {
return []byte("\"" + k.Key.String() + "\""), nil
}
func GenerateJSONPrivateKey() JSONKey {
privateKey, err := wgtypes.GeneratePrivateKey()
2020-03-02 00:48:02 +01:00
check(err)
2020-03-01 23:58:21 +01:00
return JSONKey{
Key: privateKey,
}
}
func GenerateJSONKey() JSONKey {
privateKey, err := wgtypes.GenerateKey()
2020-03-02 00:48:02 +01:00
check(err)
2020-03-01 23:58:21 +01:00
return JSONKey{
Key: privateKey,
}
2020-03-01 23:03:31 +01:00
}