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 {
case "init":
dsnet.Init()
case "init":
dsnet.Init()
case "up":
case "up":
case "add":
case "add":
case "report":
case "report":
case "down":
case "down":
default:
help();
default:
help()
}
}

View File

@ -6,14 +6,12 @@ const (
// these end up in the config file
DEFAULT_INTERFACE_NAME = "dsnet"
DEFAULT_REPORT_FILE = "/var/lib/dsnet-report.json"
DEFAULT_LISTEN_PORT = 51820;
DEFAULT_REPORT_FILE = "/var/lib/dsnet-report.json"
DEFAULT_LISTEN_PORT = 51820
// keepalive always configured for everything
KEEPALIVE_SECONDS = 21;
KEEPALIVE_SECONDS = 21
// 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
import (
"net"
"math/rand"
"fmt"
"time"
"encoding/json"
"fmt"
"math/rand"
"net"
"time"
//"github.com/mikioh/ipaddr"
)
@ -15,12 +14,12 @@ func Init() {
presharedKey := GenerateJSONKey()
conf := DsnetConfig{
PrivateKey: privateKey,
PrivateKey: privateKey,
PresharedKey: presharedKey,
ListenPort: DEFAULT_LISTEN_PORT,
Network: getRandomNetwork(),
Peers: make([]PeerConfig,0),
Domain: "dsnet",
ListenPort: DEFAULT_LISTEN_PORT,
Network: getRandomNetwork(),
Peers: make([]PeerConfig, 0),
Domain: "dsnet",
}
//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?)
// TODO also the 20 bit block and 16 bit block?
func getRandomNetwork() JSONIPNet {
rbs := make([]byte, 2)
rand.Seed(time.Now().UTC().UnixNano())
rand.Read(rbs)
return JSONIPNet{
net.IPNet {
net.IP{10,rbs[0],rbs[1]<<2,0},
net.IPMask{255,255,252,0},
net.IPNet{
net.IP{10, rbs[0], rbs[1] << 2, 0},
net.IPMask{255, 255, 252, 0},
},
}
}

View File

@ -1,24 +1,24 @@
package dsnet
import (
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"net"
"time"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
// see https://github.com/WireGuard/wgctrl-go/blob/master/wgtypes/types.go for definitions
type PeerConfig struct {
// 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
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 string `validate:"required,gte=1,lte=255"`
Description string `validate:"required,gte=1,lte=255"`
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"`
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"`
}
type Peer struct {
@ -33,35 +33,35 @@ type Peer struct {
// if no data for x days, consider revoking access
Expired bool
PublicKey wgtypes.Key
PresharedKey wgtypes.Key
Endpoint *net.UDPAddr
PublicKey wgtypes.Key
PresharedKey wgtypes.Key
Endpoint *net.UDPAddr
LastHandshakeTime time.Time
ReceiveBytes int64
TransmitBytes int64
AllowedIPs []net.IPNet
ReceiveBytes int64
TransmitBytes int64
AllowedIPs []net.IPNet
}
type DsnetConfig struct {
PrivateKey JSONKey `validate:"required,len=44"`
PresharedKey JSONKey `validate:"required,len=44"`
ListenPort int `validate:"gte=1024,lte=65535"`
Peers []PeerConfig
PrivateKey JSONKey `validate:"required,len=44"`
PresharedKey JSONKey `validate:"required,len=44"`
ListenPort int `validate:"gte=1024,lte=65535"`
Peers []PeerConfig
// IP network from which to allocate automatic sequential addresses
// 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
// 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
}
type Dsnet struct {
Name string
Name string
PrivateKey wgtypes.Key
PublicKey wgtypes.Key
PublicKey wgtypes.Key
ListenPort int
Peers []Peer
Peers []Peer
}
type JSONIPNet struct {
@ -87,7 +87,7 @@ func (k JSONKey) MarshalJSON() ([]byte, error) {
func GenerateJSONPrivateKey() JSONKey {
privateKey, err := wgtypes.GeneratePrivateKey()
if (err != nil) {
if err != nil {
panic(err)
}
@ -99,7 +99,7 @@ func GenerateJSONPrivateKey() JSONKey {
func GenerateJSONKey() JSONKey {
privateKey, err := wgtypes.GenerateKey()
if (err != nil) {
if err != nil {
panic(err)
}