2020-02-05 01:53:53 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2020-02-06 04:30:36 +00:00
|
|
|
"errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2020-02-05 01:53:53 +00:00
|
|
|
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/model"
|
|
|
|
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/storage"
|
|
|
|
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/template"
|
|
|
|
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/util"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ReadServer object, create default one
|
|
|
|
func ReadServer() (*model.Server, error) {
|
|
|
|
if !util.FileExists(filepath.Join(os.Getenv("WG_CONF_DIR"), "server.json")) {
|
|
|
|
server := &model.Server{}
|
|
|
|
|
|
|
|
key, err := wgtypes.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
server.PrivateKey = key.String()
|
|
|
|
server.PublicKey = key.PublicKey().String()
|
|
|
|
|
|
|
|
server.Endpoint = "wireguard.example.com:123"
|
|
|
|
server.ListenPort = 51820
|
2020-02-19 07:19:16 +00:00
|
|
|
|
|
|
|
server.Address = make([]string, 0)
|
|
|
|
server.Address = append(server.Address, "fd9f:6666::10:6:6:1/64")
|
|
|
|
server.Address = append(server.Address, "10.6.6.1/24")
|
|
|
|
|
|
|
|
server.Dns = make([]string, 0)
|
|
|
|
server.Dns = append(server.Dns, "fd9f::10:0:0:2")
|
|
|
|
server.Dns = append(server.Dns, "10.0.0.2")
|
|
|
|
|
2020-04-14 16:47:56 +00:00
|
|
|
server.AllowedIPs = make([]string, 0)
|
|
|
|
server.AllowedIPs = append(server.AllowedIPs, "0.0.0.0/0")
|
|
|
|
server.AllowedIPs = append(server.AllowedIPs, "::/0")
|
|
|
|
|
2020-02-05 01:53:53 +00:00
|
|
|
server.PersistentKeepalive = 16
|
2020-02-19 07:19:16 +00:00
|
|
|
server.Mtu = 0
|
2020-02-05 02:57:43 +00:00
|
|
|
server.PreUp = "echo WireGuard PreUp"
|
|
|
|
server.PostUp = "echo WireGuard PostUp"
|
|
|
|
server.PreDown = "echo WireGuard PreDown"
|
|
|
|
server.PostDown = "echo WireGuard PostDown"
|
2020-02-05 01:53:53 +00:00
|
|
|
server.Created = time.Now().UTC()
|
|
|
|
server.Updated = server.Created
|
|
|
|
|
|
|
|
err = storage.Serialize("server.json", server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-17 01:31:44 +00:00
|
|
|
|
|
|
|
// server.json was missing, dump wg config after creation
|
|
|
|
err = UpdateServerConfigWg()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-05 01:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c, err := storage.Deserialize("server.json")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.(*model.Server), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateServer keep private values from existing one
|
|
|
|
func UpdateServer(server *model.Server) (*model.Server, error) {
|
|
|
|
current, err := storage.Deserialize("server.json")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-06 04:30:36 +00:00
|
|
|
|
|
|
|
// check if server is valid
|
|
|
|
errs := server.IsValid()
|
|
|
|
if len(errs) != 0 {
|
|
|
|
for _, err := range errs {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"err": err,
|
|
|
|
}).Error("server validation error")
|
|
|
|
}
|
|
|
|
return nil, errors.New("failed to validate server")
|
|
|
|
}
|
|
|
|
|
2020-02-05 01:53:53 +00:00
|
|
|
server.PrivateKey = current.(*model.Server).PrivateKey
|
|
|
|
server.PublicKey = current.(*model.Server).PublicKey
|
2020-03-18 06:57:15 +00:00
|
|
|
//server.PresharedKey = current.(*model.Server).PresharedKey
|
2020-02-05 01:53:53 +00:00
|
|
|
server.Updated = time.Now().UTC()
|
|
|
|
|
|
|
|
err = storage.Serialize("server.json", server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := storage.Deserialize("server.json")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
server = v.(*model.Server)
|
|
|
|
|
|
|
|
return server, UpdateServerConfigWg()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateServerConfigWg in wg format
|
|
|
|
func UpdateServerConfigWg() error {
|
|
|
|
clients, err := ReadClients()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
server, err := ReadServer()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = template.DumpServerWg(clients, server)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-06 04:30:36 +00:00
|
|
|
|
|
|
|
// GetAllReservedIps the list of all reserved IPs, client and server
|
|
|
|
func GetAllReservedIps() ([]string, error) {
|
|
|
|
clients, err := ReadClients()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
server, err := ReadServer()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reserverIps := make([]string, 0)
|
|
|
|
|
|
|
|
for _, client := range clients {
|
2020-02-19 07:19:16 +00:00
|
|
|
for _, cidr := range client.Address {
|
|
|
|
ip, err := util.GetIpFromCidr(cidr)
|
2020-02-06 04:30:36 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"err": err,
|
2020-02-19 07:19:16 +00:00
|
|
|
"cidr": cidr,
|
2020-02-06 04:30:36 +00:00
|
|
|
}).Error("failed to ip from cidr")
|
|
|
|
} else {
|
|
|
|
reserverIps = append(reserverIps, ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 07:19:16 +00:00
|
|
|
for _, cidr := range server.Address {
|
|
|
|
ip, err := util.GetIpFromCidr(cidr)
|
2020-02-06 04:30:36 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"err": err,
|
|
|
|
"cidr": err,
|
|
|
|
}).Error("failed to ip from cidr")
|
|
|
|
} else {
|
|
|
|
reserverIps = append(reserverIps, ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return reserverIps, nil
|
|
|
|
}
|
2020-07-15 06:52:16 +00:00
|
|
|
|
|
|
|
// ReadWgConfigFile return content of wireguard config file
|
|
|
|
func ReadWgConfigFile() ([]byte, error) {
|
|
|
|
return util.ReadFile(filepath.Join(os.Getenv("WG_CONF_DIR"), os.Getenv("WG_INTERFACE_NAME")))
|
|
|
|
}
|