0
0
mirror of https://github.com/vx3r/wg-gen-web.git synced 2024-12-22 00:53:22 +00:00
wg-gen-web/core/client.go

281 lines
5.8 KiB
Go
Raw Normal View History

package core
2020-01-30 06:45:49 +00:00
import (
"errors"
uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
2020-02-03 07:19:24 +00:00
"github.com/skip2/go-qrcode"
2020-01-30 06:45:49 +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"
2020-01-30 06:45:49 +00:00
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/util"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
2020-02-03 07:19:24 +00:00
"gopkg.in/gomail.v2"
2020-01-30 06:45:49 +00:00
"io/ioutil"
"os"
"path/filepath"
"sort"
2020-02-03 07:19:24 +00:00
"strconv"
2020-01-30 06:45:49 +00:00
"time"
)
2020-01-30 07:21:45 +00:00
// CreateClient client with all necessary data
2020-01-30 06:45:49 +00:00
func CreateClient(client *model.Client) (*model.Client, error) {
// check if client is valid
errs := client.IsValid()
if len(errs) != 0 {
for _, err := range errs {
log.WithFields(log.Fields{
"err": err,
}).Error("client validation error")
}
return nil, errors.New("failed to validate client")
}
2020-01-30 06:45:49 +00:00
u := uuid.NewV4()
client.Id = u.String()
key, err := wgtypes.GeneratePrivateKey()
if err != nil {
return nil, err
}
client.PrivateKey = key.String()
client.PublicKey = key.PublicKey().String()
presharedKey, err := wgtypes.GenerateKey()
if err != nil {
return nil, err
}
client.PresharedKey = presharedKey.String()
reserverIps, err := GetAllReservedIps()
2020-01-30 06:45:49 +00:00
if err != nil {
return nil, err
}
ips := make([]string, 0)
for _, network := range client.Address {
ip, err := util.GetAvailableIp(network, reserverIps)
2020-01-30 06:45:49 +00:00
if err != nil {
return nil, err
}
2020-01-30 07:12:26 +00:00
if util.IsIPv6(ip) {
2020-01-30 06:45:49 +00:00
ip = ip + "/128"
} else {
ip = ip + "/32"
}
ips = append(ips, ip)
}
client.Address = ips
2020-01-30 06:45:49 +00:00
client.Created = time.Now().UTC()
client.Updated = client.Created
err = storage.Serialize(client.Id, client)
2020-01-30 06:45:49 +00:00
if err != nil {
return nil, err
}
v, err := storage.Deserialize(client.Id)
2020-01-30 06:45:49 +00:00
if err != nil {
return nil, err
}
client = v.(*model.Client)
// data modified, dump new config
return client, UpdateServerConfigWg()
2020-01-30 06:45:49 +00:00
}
2020-01-30 07:21:45 +00:00
// ReadClient client by id
2020-01-30 06:45:49 +00:00
func ReadClient(id string) (*model.Client, error) {
v, err := storage.Deserialize(id)
2020-01-30 06:45:49 +00:00
if err != nil {
return nil, err
}
client := v.(*model.Client)
return client, nil
}
2020-01-30 07:21:45 +00:00
// UpdateClient preserve keys
2020-01-30 06:45:49 +00:00
func UpdateClient(Id string, client *model.Client) (*model.Client, error) {
v, err := storage.Deserialize(Id)
2020-01-30 06:45:49 +00:00
if err != nil {
return nil, err
}
current := v.(*model.Client)
if current.Id != client.Id {
return nil, errors.New("records Id mismatch")
}
// check if client is valid
errs := client.IsValid()
if len(errs) != 0 {
for _, err := range errs {
log.WithFields(log.Fields{
"err": err,
}).Error("client validation error")
}
return nil, errors.New("failed to validate client")
}
2020-01-30 06:45:49 +00:00
// keep keys
client.PrivateKey = current.PrivateKey
client.PublicKey = current.PublicKey
client.Updated = time.Now().UTC()
err = storage.Serialize(client.Id, client)
2020-01-30 06:45:49 +00:00
if err != nil {
return nil, err
}
v, err = storage.Deserialize(Id)
2020-01-30 06:45:49 +00:00
if err != nil {
return nil, err
}
client = v.(*model.Client)
// data modified, dump new config
return client, UpdateServerConfigWg()
2020-01-30 06:45:49 +00:00
}
2020-01-30 07:21:45 +00:00
// DeleteClient from disk
2020-01-30 06:45:49 +00:00
func DeleteClient(id string) error {
path := filepath.Join(os.Getenv("WG_CONF_DIR"), id)
err := os.Remove(path)
if err != nil {
return err
}
// data modified, dump new config
return UpdateServerConfigWg()
2020-02-03 07:19:24 +00:00
}
2020-01-30 07:21:45 +00:00
// ReadClients all clients
2020-01-30 06:45:49 +00:00
func ReadClients() ([]*model.Client, error) {
clients := make([]*model.Client, 0)
files, err := ioutil.ReadDir(filepath.Join(os.Getenv("WG_CONF_DIR")))
if err != nil {
return nil, err
}
for _, f := range files {
// clients file name is an uuid
_, err := uuid.FromString(f.Name())
if err == nil {
c, err := storage.Deserialize(f.Name())
2020-01-30 06:45:49 +00:00
if err != nil {
log.WithFields(log.Fields{
"err": err,
"path": f.Name(),
}).Error("failed to deserialize client")
2020-01-30 06:45:49 +00:00
} else {
clients = append(clients, c.(*model.Client))
}
}
}
sort.Slice(clients, func(i, j int) bool {
return clients[i].Created.After(clients[j].Created)
})
return clients, nil
}
// ReadClientConfig in wg format
func ReadClientConfig(id string) ([]byte, error) {
client, err := ReadClient(id)
2020-01-30 06:45:49 +00:00
if err != nil {
return nil, err
}
server, err := ReadServer()
2020-01-30 06:45:49 +00:00
if err != nil {
return nil, err
}
configDataWg, err := template.DumpClientWg(client, server)
2020-01-30 06:45:49 +00:00
if err != nil {
return nil, err
}
return configDataWg, nil
2020-01-30 06:45:49 +00:00
}
2020-01-30 07:12:26 +00:00
// EmailClient send email to client
func EmailClient(id string) error {
client, err := ReadClient(id)
2020-01-30 06:45:49 +00:00
if err != nil {
return err
}
configData, err := ReadClientConfig(id)
2020-01-30 06:45:49 +00:00
if err != nil {
return err
}
// conf as .conf file
tmpfileCfg, err := ioutil.TempFile("", "wireguard-vpn-*.conf")
2020-01-30 06:45:49 +00:00
if err != nil {
return err
2020-01-30 06:45:49 +00:00
}
if _, err := tmpfileCfg.Write(configData); err != nil {
return err
}
if err := tmpfileCfg.Close(); err != nil {
return err
}
defer os.Remove(tmpfileCfg.Name()) // clean up
2020-01-30 06:45:49 +00:00
// conf as png image
png, err := qrcode.Encode(string(configData), qrcode.Medium, 280)
2020-01-30 06:45:49 +00:00
if err != nil {
return err
2020-01-30 06:45:49 +00:00
}
tmpfilePng, err := ioutil.TempFile("", "qrcode-*.png")
2020-01-30 06:45:49 +00:00
if err != nil {
return err
}
if _, err := tmpfilePng.Write(png); err != nil {
return err
2020-01-30 06:45:49 +00:00
}
if err := tmpfilePng.Close(); err != nil {
return err
2020-01-30 06:45:49 +00:00
}
defer os.Remove(tmpfilePng.Name()) // clean up
2020-01-30 06:45:49 +00:00
// get email body
emailBody, err := template.DumpEmail(client, filepath.Base(tmpfilePng.Name()))
2020-01-30 06:45:49 +00:00
if err != nil {
return err
}
// port to int
port, err := strconv.Atoi(os.Getenv("SMTP_PORT"))
2020-01-30 06:45:49 +00:00
if err != nil {
return err
}
d := gomail.NewDialer(os.Getenv("SMTP_HOST"), port, os.Getenv("SMTP_USERNAME"), os.Getenv("SMTP_PASSWORD"))
s, err := d.Dial()
2020-01-30 06:45:49 +00:00
if err != nil {
return err
}
m := gomail.NewMessage()
2020-01-30 06:45:49 +00:00
m.SetHeader("From", os.Getenv("SMTP_FROM"))
m.SetAddressHeader("To", client.Email, client.Name)
m.SetHeader("Subject", "WireGuard VPN Configuration")
m.SetBody("text/html", string(emailBody))
m.Attach(tmpfileCfg.Name())
m.Embed(tmpfilePng.Name())
err = gomail.Send(s, m)
2020-01-30 06:45:49 +00:00
if err != nil {
return err
}
return nil
2020-01-30 07:12:26 +00:00
}