Skip to content

Commit

Permalink
issue #23 peer client preshared key, update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
vx3r committed Mar 18, 2020
1 parent 38a284c commit 200e47b
Show file tree
Hide file tree
Showing 12 changed files with 484 additions and 348 deletions.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func configClient(c *gin.Context) {
return
}
// return config as png qrcode
png, err := qrcode.Encode(string(configData), qrcode.Medium, 220)
png, err := qrcode.Encode(string(configData), qrcode.Medium, 250)
if err != nil {
log.WithFields(log.Fields{
"err": err,
Expand Down
6 changes: 6 additions & 0 deletions core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ func CreateClient(client *model.Client) (*model.Client, error) {
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()
if err != nil {
return nil, err
Expand Down
120 changes: 118 additions & 2 deletions core/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// Migrate all changes, current struct fields change
func Migrate() error {
func MigrateInitialStructChange() error {
clients, err := readClients()
if err != nil {
return err
Expand Down Expand Up @@ -98,7 +98,7 @@ func Migrate() error {
server.ListenPort = int(s["listenPort"].(float64))
server.PrivateKey = s["privateKey"].(string)
server.PublicKey = s["publicKey"].(string)
server.PresharedKey = s["presharedKey"].(string)
//server.PresharedKey = s["presharedKey"].(string)
server.Endpoint = s["endpoint"].(string)
server.PersistentKeepalive = int(s["persistentKeepalive"].(float64))
server.Dns = make([]string, 0)
Expand Down Expand Up @@ -144,6 +144,122 @@ func Migrate() error {
return nil
}

// Migrate presharedKey issue #23
func MigratePresharedKey() error {
clients, err := readClients()
if err != nil {
return err
}

s, err := deserialize("server.json")
if err != nil {
return err
}

for _, client := range clients {
if _, ok := client["presharedKey"]; ok {
log.Infof("client %s has been already migrated for preshared key", client["id"])
continue
}

c := &model.Client{}
c.Id = client["id"].(string)
c.Name = client["name"].(string)
c.Email = client["email"].(string)
c.Enable = client["enable"].(bool)
c.IgnorePersistentKeepalive = client["ignorePersistentKeepalive"].(bool)
c.PresharedKey = s["presharedKey"].(string)
c.AllowedIPs = make([]string, 0)
for _, address := range client["allowedIPs"].([]interface{}) {
c.AllowedIPs = append(c.AllowedIPs, address.(string))
}
c.Address = make([]string, 0)
for _, address := range client["address"].([]interface{}) {
c.Address = append(c.Address, address.(string))
}
c.PrivateKey = client["privateKey"].(string)
c.PublicKey = client["publicKey"].(string)
created, err := time.Parse(time.RFC3339, client["created"].(string))
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Errorf("failed to parse time")
continue
}
c.Created = created
updated, err := time.Parse(time.RFC3339, client["updated"].(string))
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Errorf("failed to parse time")
continue
}
c.Updated = updated

err = storage.Serialize(c.Id, c)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Errorf("failed to Serialize client")
}
}

if _, ok := s["presharedKey"]; ok {
server := &model.Server{}

server.Address = make([]string, 0)
server.Address = make([]string, 0)
for _, address := range s["address"].([]interface{}) {
server.Address = append(server.Address, address.(string))
}
server.ListenPort = int(s["listenPort"].(float64))
server.PrivateKey = s["privateKey"].(string)
server.PublicKey = s["publicKey"].(string)
server.Endpoint = s["endpoint"].(string)
server.PersistentKeepalive = int(s["persistentKeepalive"].(float64))
server.Dns = make([]string, 0)
for _, address := range s["dns"].([]interface{}) {
server.Dns = append(server.Dns, address.(string))
}
if val, ok := s["preUp"]; ok {
server.PreUp = val.(string)
}
if val, ok := s["postUp"]; ok {
server.PostUp = val.(string)
}
if val, ok := s["preDown"]; ok {
server.PreDown = val.(string)
}
if val, ok := s["postDown"]; ok {
server.PostDown = val.(string)
}
created, err := time.Parse(time.RFC3339, s["created"].(string))
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Errorf("failed to parse time")
}
server.Created = created
updated, err := time.Parse(time.RFC3339, s["updated"].(string))
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Errorf("failed to parse time")
}
server.Updated = updated

err = storage.Serialize("server.json", server)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Errorf("failed to Serialize server")
}

}

return nil
}

func readClients() ([]map[string]interface{}, error) {
clients := make([]map[string]interface{}, 0)

Expand Down
8 changes: 1 addition & 7 deletions core/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ func ReadServer() (*model.Server, error) {
server.PrivateKey = key.String()
server.PublicKey = key.PublicKey().String()

presharedKey, err := wgtypes.GenerateKey()
if err != nil {
return nil, err
}
server.PresharedKey = presharedKey.String()

server.Endpoint = "wireguard.example.com:123"
server.ListenPort = 51820

Expand Down Expand Up @@ -91,7 +85,7 @@ func UpdateServer(server *model.Server) (*model.Server, error) {

server.PrivateKey = current.(*model.Server).PrivateKey
server.PublicKey = current.(*model.Server).PublicKey
server.PresharedKey = current.(*model.Server).PresharedKey
//server.PresharedKey = current.(*model.Server).PresharedKey
server.Updated = time.Now().UTC()

err = storage.Serialize("server.json", server)
Expand Down
15 changes: 14 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,27 @@ go 1.13

require (
github.com/danielkov/gin-helmet v0.0.0-20171108135313-1387e224435e
github.com/gin-contrib/cors v1.3.0
github.com/gin-contrib/cors v1.3.1
github.com/gin-contrib/static v0.0.0-20191128031702-f81c604d8ac2
github.com/gin-gonic/gin v1.5.0
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/golang/protobuf v1.3.5 // indirect
github.com/joho/godotenv v1.3.0
github.com/json-iterator/go v1.1.9 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/satori/go.uuid v1.2.0
github.com/sirupsen/logrus v1.4.2
github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086
golang.org/x/crypto v0.0.0-20200317142112-1b76d66859c6 // indirect
golang.org/x/sys v0.0.0-20200317113312-5766fd39f98d // indirect
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20200205215550-e35592f146e4
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
gopkg.in/go-playground/validator.v9 v9.31.0 // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/yaml.v2 v2.2.8 // indirect
)
29 changes: 21 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Fatal("failed to initialize env")
}).Fatal("failed to load .env file")
}

// check directories or create it
Expand All @@ -39,11 +39,11 @@ func main() {
log.WithFields(log.Fields{
"err": err,
"dir": filepath.Join(os.Getenv("WG_CONF_DIR")),
}).Fatal("failed to mkdir")
}).Fatal("failed to create directory")
}
}

// check if server.json exists otherwise create it
// check if server.json exists otherwise create it with default values
if !util.FileExists(filepath.Join(os.Getenv("WG_CONF_DIR"), "server.json")) {
_, err = core.ReadServer()
if err != nil {
Expand All @@ -66,23 +66,36 @@ func main() {
}

// migrate
err = core.Migrate()
err = core.MigrateInitialStructChange()
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Fatal("failed to migrate")
}).Fatal("failed to migrate initial struct changes")
}
err = core.MigratePresharedKey()
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Fatal("failed to migrate preshared key struct changes")
}

// dump wg config file
err = core.UpdateServerConfigWg()
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Fatal("failed to dump wg config file")
}

// creates a gin router with default middleware: logger and recovery (crash-free) middleware
app := gin.Default()

// same as
// cors middleware
config := cors.DefaultConfig()
config.AllowAllOrigins = true
app.Use(cors.New(config))
//app.Use(cors.Default())

// protection
// protection middleware
app.Use(helmet.Default())

// no route redirect to frontend app
Expand Down
1 change: 1 addition & 0 deletions model/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Client struct {
Email string `json:"email"`
Enable bool `json:"enable"`
IgnorePersistentKeepalive bool `json:"ignorePersistentKeepalive"`
PresharedKey string `json:"presharedKey"`
AllowedIPs []string `json:"allowedIPs"`
Address []string `json:"address"`
PrivateKey string `json:"privateKey"`
Expand Down
1 change: 0 additions & 1 deletion model/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type Server struct {
Mtu int `json:"mtu"`
PrivateKey string `json:"privateKey"`
PublicKey string `json:"publicKey"`
PresharedKey string `json:"presharedKey"`
Endpoint string `json:"endpoint"`
PersistentKeepalive int `json:"persistentKeepalive"`
Dns []string `json:"dns"`
Expand Down
5 changes: 2 additions & 3 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ MTU = {{.Server.Mtu}}
{{- end}}
[Peer]
PublicKey = {{ .Server.PublicKey }}
PresharedKey = {{ .Server.PresharedKey }}
PresharedKey = {{ .Client.PresharedKey }}
AllowedIPs = {{ StringsJoin .Client.AllowedIPs ", " }}
Endpoint = {{ .Server.Endpoint }}
{{ if and (ne .Server.PersistentKeepalive 0) (not .Client.IgnorePersistentKeepalive) -}}
Expand All @@ -230,13 +230,12 @@ PreUp = {{ .Server.PreUp }}
PostUp = {{ .Server.PostUp }}
PreDown = {{ .Server.PreDown }}
PostDown = {{ .Server.PostDown }}
{{ $server := .Server }}
{{- range .Clients }}
{{ if .Enable -}}
# {{.Name}} / {{.Email}} / Updated: {{.Updated}} / Created: {{.Created}}
[Peer]
PublicKey = {{ .PublicKey }}
PresharedKey = {{ $server.PresharedKey }}
PresharedKey = {{ .PresharedKey }}
AllowedIPs = {{ StringsJoin .Address ", " }}
{{- end }}
{{ end }}`
Expand Down
Loading

0 comments on commit 200e47b

Please sign in to comment.