99 lines
2.7 KiB
Go
99 lines
2.7 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/go-acme/lego/v4/lego"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
"local-ip.sh/utils"
|
|
)
|
|
|
|
var command = &cobra.Command{
|
|
Use: "local-ip.sh",
|
|
PreRun: func(cmd *cobra.Command, args []string) {
|
|
nameservers, err := cmd.Flags().GetString("nameservers")
|
|
if err != nil {
|
|
utils.Logger.Fatal().Err(err).Msg("Unexpected error")
|
|
}
|
|
viper.Set("NameServers", strings.Split(nameservers, ","))
|
|
|
|
staging, err := cmd.Flags().GetBool("staging")
|
|
if err != nil {
|
|
utils.Logger.Fatal().Err(err).Msg("Unexpected error")
|
|
}
|
|
|
|
var caDir string
|
|
if staging {
|
|
caDir = lego.LEDirectoryStaging
|
|
} else {
|
|
caDir = lego.LEDirectoryProduction
|
|
}
|
|
viper.Set("CADirURL", caDir)
|
|
|
|
parsedCaDirUrl, _ := url.Parse(caDir)
|
|
caDirHostname := parsedCaDirUrl.Hostname()
|
|
email := viper.GetString("Email")
|
|
|
|
viper.Set("AccountFilePath", fmt.Sprintf("./.lego/accounts/%s/%s/account.json", caDirHostname, email))
|
|
viper.Set("KeyFilePath", fmt.Sprintf("./.lego/accounts/%s/%s/keys/%s.key", caDirHostname, email, email))
|
|
|
|
utils.InitConfig()
|
|
},
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
c := utils.GetConfig()
|
|
fmt.Printf("%#v\n", c)
|
|
fmt.Println(c)
|
|
// n := xip.NewXip()
|
|
|
|
// go func() {
|
|
// account := certs.LoadAccount()
|
|
// certsClient := certs.NewCertsClient(n, account)
|
|
|
|
// time.Sleep(5 * time.Second)
|
|
// certsClient.RequestCertificates()
|
|
|
|
// for {
|
|
// // try to renew certificate every day
|
|
// time.Sleep(24 * time.Hour)
|
|
// certsClient.RequestCertificates()
|
|
// }
|
|
// }()
|
|
|
|
// go http.ServeHttp()
|
|
|
|
// n.StartServer()
|
|
},
|
|
}
|
|
|
|
func Execute() {
|
|
command.Flags().Uint("dns-port", 53, "Port for the DNS server")
|
|
viper.BindPFlag("dns-port", command.Flags().Lookup("dns-port"))
|
|
|
|
command.Flags().Uint("http-port", 80, "Port for the HTTP server")
|
|
viper.BindPFlag("http-port", command.Flags().Lookup("http-port"))
|
|
|
|
command.Flags().Uint("https-port", 443, "Port for the HTTPS server")
|
|
viper.BindPFlag("https-port", command.Flags().Lookup("https-port"))
|
|
|
|
command.Flags().Bool("staging", false, "Enable to use the Let's Encrypt staging environment to obtain certificates")
|
|
viper.BindPFlag("staging", command.Flags().Lookup("staging"))
|
|
|
|
command.Flags().String("domain", "", "Root domain (required)")
|
|
command.MarkFlagRequired("domain")
|
|
viper.BindPFlag("domain", command.Flags().Lookup("domain"))
|
|
|
|
command.Flags().String("email", "", "ACME account email address (required)")
|
|
command.MarkFlagRequired("email")
|
|
viper.BindPFlag("email", command.Flags().Lookup("email"))
|
|
|
|
command.Flags().String("nameservers", "", "List of nameservers separated by commas")
|
|
command.MarkFlagRequired("nameservers")
|
|
|
|
if err := command.Execute(); err != nil {
|
|
utils.Logger.Fatal().Err(err).Msg("")
|
|
}
|
|
}
|