1
0
Fork 0
mirror of https://github.com/SamTherapy/dnscrypt.git synced 2024-12-22 08:50:43 +00:00
dnscrypt/cmd/generate.go
Andrey Meshkov 5ddb58f703
Graceful shutdown of the DNSCrypt server (#6)
Graceful shutdown of the DNSCrypt server

This PR implements Server.Shutdown(ctx context.Context) method that allows
to shut down the DNSCrypt server gracefully.

Some additional changes that were inadvertently made while doing that:
1. Added benchmark tests
2. Started using dns.ReadFromSessionUDP / dns.WriteToSessionUDP instead of implementing it by ourselves
3. Generally improved tests
4. Added depguard 
5. Improved comments overall in the code
2021-03-19 15:42:48 +03:00

50 lines
1.5 KiB
Go

package main
import (
"io/ioutil"
"github.com/AdguardTeam/golibs/log"
"github.com/ameshkov/dnscrypt/v2"
"gopkg.in/yaml.v3"
)
// GenerateArgs is the "generate" command arguments structure
type GenerateArgs struct {
ProviderName string `short:"p" long:"provider-name" description:"DNSCrypt provider name. Param is required." required:"true"`
Out string `short:"o" long:"out" description:"Path to the resulting config file. Param is required." required:"true"`
PrivateKey string `short:"k" long:"private-key" description:"Private key (hex-encoded)"`
CertificateTTL int `short:"t" long:"ttl" description:"Certificate time-to-live (seconds)"`
}
// generate generates a DNSCrypt server configuration
func generate(args GenerateArgs) {
log.Info("Generating configuration for %s", args.ProviderName)
var privateKey []byte
var err error
if args.PrivateKey != "" {
privateKey, err = dnscrypt.HexDecodeKey(args.PrivateKey)
if err != nil {
log.Fatalf("failed to generate private key: %v", err)
}
}
rc, err := dnscrypt.GenerateResolverConfig(args.ProviderName, privateKey)
if err != nil {
log.Fatalf("failed to generate resolver config: %v", err)
}
b, err := yaml.Marshal(rc)
if err != nil {
log.Fatalf("failed to serialize to yaml: %v", err)
}
// nolint
err = ioutil.WriteFile(args.Out, b, 0600)
if err != nil {
log.Fatalf("failed to save %s: %v", args.Out, err)
}
log.Info("Configuration has been written to %s", args.Out)
log.Info("Go to https://dnscrypt.info/stamps to generate an SDNS stamp")
}