This commit is contained in:
Callan Bryant 2020-03-01 23:08:10 +00:00
parent 87317e374a
commit a99449e960
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA
4 changed files with 49 additions and 53 deletions

View File

@ -18,19 +18,19 @@ func main() {
} }
switch cmd { switch cmd {
case "init": case "init":
dsnet.Init() dsnet.Init()
case "up": case "up":
case "add": case "add":
case "report": case "report":
case "down": case "down":
default: default:
help(); help()
} }
} }

View File

@ -6,14 +6,12 @@ const (
// these end up in the config file // these end up in the config file
DEFAULT_INTERFACE_NAME = "dsnet" DEFAULT_INTERFACE_NAME = "dsnet"
DEFAULT_REPORT_FILE = "/var/lib/dsnet-report.json" DEFAULT_REPORT_FILE = "/var/lib/dsnet-report.json"
DEFAULT_LISTEN_PORT = 51820; DEFAULT_LISTEN_PORT = 51820
// keepalive always configured for everything // keepalive always configured for everything
KEEPALIVE_SECONDS = 21; KEEPALIVE_SECONDS = 21
// when is a peer considered gone forever? (could remove) // when is a peer considered gone forever? (could remove)
EXPIRY_DAYS = 28; EXPIRY_DAYS = 28
) )

26
init.go
View File

@ -1,12 +1,11 @@
package dsnet package dsnet
import ( import (
"net"
"math/rand"
"fmt"
"time"
"encoding/json" "encoding/json"
"fmt"
"math/rand"
"net"
"time"
//"github.com/mikioh/ipaddr" //"github.com/mikioh/ipaddr"
) )
@ -15,12 +14,12 @@ func Init() {
presharedKey := GenerateJSONKey() presharedKey := GenerateJSONKey()
conf := DsnetConfig{ conf := DsnetConfig{
PrivateKey: privateKey, PrivateKey: privateKey,
PresharedKey: presharedKey, PresharedKey: presharedKey,
ListenPort: DEFAULT_LISTEN_PORT, ListenPort: DEFAULT_LISTEN_PORT,
Network: getRandomNetwork(), Network: getRandomNetwork(),
Peers: make([]PeerConfig,0), Peers: make([]PeerConfig, 0),
Domain: "dsnet", Domain: "dsnet",
} }
//fmt.Println(conf.Network.String()) //fmt.Println(conf.Network.String())
@ -32,16 +31,15 @@ func Init() {
} }
// get a random /22 subnet on 10.0.0.0 (1023 hosts) (or /24?) // get a random /22 subnet on 10.0.0.0 (1023 hosts) (or /24?)
// TODO also the 20 bit block and 16 bit block?
func getRandomNetwork() JSONIPNet { func getRandomNetwork() JSONIPNet {
rbs := make([]byte, 2) rbs := make([]byte, 2)
rand.Seed(time.Now().UTC().UnixNano()) rand.Seed(time.Now().UTC().UnixNano())
rand.Read(rbs) rand.Read(rbs)
return JSONIPNet{ return JSONIPNet{
net.IPNet { net.IPNet{
net.IP{10,rbs[0],rbs[1]<<2,0}, net.IP{10, rbs[0], rbs[1] << 2, 0},
net.IPMask{255,255,252,0}, net.IPMask{255, 255, 252, 0},
}, },
} }
} }

View File

@ -1,24 +1,24 @@
package dsnet package dsnet
import ( import (
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"net" "net"
"time" "time"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
) )
// see https://github.com/WireGuard/wgctrl-go/blob/master/wgtypes/types.go for definitions // see https://github.com/WireGuard/wgctrl-go/blob/master/wgtypes/types.go for definitions
type PeerConfig struct { type PeerConfig struct {
// username of person running this host/router // username of person running this host/router
Owner string `validate:"required,gte=1,lte=255"` Owner string `validate:"required,gte=1,lte=255"`
// Used to update DNS // Used to update DNS
Hostname string `validate:"required,gte=1,lte=255"` Hostname string `validate:"required,gte=1,lte=255"`
// Description of what the host is and/or does // Description of what the host is and/or does
Description string `validate:"required,gte=1,lte=255"` Description string `validate:"required,gte=1,lte=255"`
PublicKey JSONKey `validate:"required,len=44"` PublicKey JSONKey `validate:"required,len=44"`
PresharedKey JSONKey `validate:"required,len=44"` PresharedKey JSONKey `validate:"required,len=44"`
Endpoint net.UDPAddr `validate:"required,udp4_addr"` Endpoint net.UDPAddr `validate:"required,udp4_addr"`
AllowedIPs []net.IPNet `validate:"dive,required,cidr"` AllowedIPs []net.IPNet `validate:"dive,required,cidr"`
} }
type Peer struct { type Peer struct {
@ -33,35 +33,35 @@ type Peer struct {
// if no data for x days, consider revoking access // if no data for x days, consider revoking access
Expired bool Expired bool
PublicKey wgtypes.Key PublicKey wgtypes.Key
PresharedKey wgtypes.Key PresharedKey wgtypes.Key
Endpoint *net.UDPAddr Endpoint *net.UDPAddr
LastHandshakeTime time.Time LastHandshakeTime time.Time
ReceiveBytes int64 ReceiveBytes int64
TransmitBytes int64 TransmitBytes int64
AllowedIPs []net.IPNet AllowedIPs []net.IPNet
} }
type DsnetConfig struct { type DsnetConfig struct {
PrivateKey JSONKey `validate:"required,len=44"` PrivateKey JSONKey `validate:"required,len=44"`
PresharedKey JSONKey `validate:"required,len=44"` PresharedKey JSONKey `validate:"required,len=44"`
ListenPort int `validate:"gte=1024,lte=65535"` ListenPort int `validate:"gte=1024,lte=65535"`
Peers []PeerConfig Peers []PeerConfig
// IP network from which to allocate automatic sequential addresses // IP network from which to allocate automatic sequential addresses
// Network is chosen randomly when not specified // Network is chosen randomly when not specified
Network JSONIPNet `validate:"required"` Network JSONIPNet `validate:"required"`
// domain to append to hostnames. Relies on separate DNS server for // domain to append to hostnames. Relies on separate DNS server for
// resolution. Informational only. // resolution. Informational only.
Domain string `validate:"required,gte=1,lte=255"` Domain string `validate:"required,gte=1,lte=255"`
// TODO Default subnets to route via VPN // TODO Default subnets to route via VPN
} }
type Dsnet struct { type Dsnet struct {
Name string Name string
PrivateKey wgtypes.Key PrivateKey wgtypes.Key
PublicKey wgtypes.Key PublicKey wgtypes.Key
ListenPort int ListenPort int
Peers []Peer Peers []Peer
} }
type JSONIPNet struct { type JSONIPNet struct {
@ -87,7 +87,7 @@ func (k JSONKey) MarshalJSON() ([]byte, error) {
func GenerateJSONPrivateKey() JSONKey { func GenerateJSONPrivateKey() JSONKey {
privateKey, err := wgtypes.GeneratePrivateKey() privateKey, err := wgtypes.GeneratePrivateKey()
if (err != nil) { if err != nil {
panic(err) panic(err)
} }
@ -99,7 +99,7 @@ func GenerateJSONPrivateKey() JSONKey {
func GenerateJSONKey() JSONKey { func GenerateJSONKey() JSONKey {
privateKey, err := wgtypes.GenerateKey() privateKey, err := wgtypes.GenerateKey()
if (err != nil) { if err != nil {
panic(err) panic(err)
} }