providers.json

This commit is contained in:
ZhuoQinghui 2025-02-17 17:39:24 +08:00
parent ad99da70fb
commit 8a09c75e47
7 changed files with 185 additions and 72 deletions

15
main.go
View File

@ -1,8 +1,21 @@
package main package main
import "acme-mana/src" import (
"acme-mana/src"
"acme-mana/src/cmd/cmd_handle"
"acme-mana/src/conf"
)
func main() { func main() {
//src.Start() //src.Start()
src.StartProgram() src.StartProgram()
select {}
//runServer()
}
func runServer() {
conf.LoadAppConfig()
cmd_handle.RunStart(nil, nil)
select {}
} }

View File

@ -46,20 +46,6 @@ func Apply(cert *conf.CertConf) {
provider := getProvider(p) provider := getProvider(p)
//var provider challenge.Provider
//
//switch p.Type {
//case "ali":
// conf := alidns.NewDefaultConfig()
// conf.RegionID = p.Conf["RegionID"]
// conf.APIKey = p.Conf["APIKey"]
// conf.SecretKey = p.Conf["SecretKey"]
// provider, err = alidns.NewDNSProviderConfig(conf)
// if err != nil {
// log.Fatal(err)
// }
//}
chall := client.Challenge chall := client.Challenge
err = chall.SetDNS01Provider(provider) err = chall.SetDNS01Provider(provider)
if err != nil { if err != nil {
@ -87,61 +73,6 @@ func Apply(cert *conf.CertConf) {
} }
//func Apply1(domain Domain) {
// email, hosts, name := domain.Email, domain.Host, domain.Name
// privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
// if err != nil {
// log.Fatal(err)
// }
//
// acmeUser := &AcmeUser{
// Email: email,
// key: privateKey,
// }
//
// config := lego.NewConfig(acmeUser)
//
// client, err := lego.NewClient(config)
// if err != nil {
// log.Fatal(err)
// }
//
// ali := appConfig.Provider.Ali
// conf := alidns.NewDefaultConfig()
// conf.RegionID = ali.RegionID
// conf.APIKey = ali.APIKey
// conf.SecretKey = ali.SecretKey
// provider, err := alidns.NewDNSProviderConfig(conf)
// if err != nil {
// log.Fatal(err)
// }
//
// challenge := client.Challenge
// err = challenge.SetDNS01Provider(provider)
// if err != nil {
// return
// }
//
// registrar := client.Registration
// reg, err := registrar.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
// if err != nil {
// log.Fatal(err)
// }
// acmeUser.Registration = reg
//
// request := certificate.ObtainRequest{
// Domains: hosts,
// Bundle: true,
// }
//
// cert, err := client.Certificate.Obtain(request)
// if err != nil {
// log.Fatal(err)
// }
//
// saveCertFile(cert, name)
//}
func getProvider(p *conf.ProviderConf) challenge.Provider { func getProvider(p *conf.ProviderConf) challenge.Provider {
switch p.Type { switch p.Type {

75
src/acme/provider.go Normal file
View File

@ -0,0 +1,75 @@
package acme
import (
"embed"
"encoding/json"
"log"
)
//go:embed providers.json
var providerFile embed.FS
type ProviderInfo struct {
Type string `json:"type"`
Remarks string `json:"remarks"`
Doc *ProviderDoc `json:"doc"`
Variables *ConfVariables `json:"variables"`
}
type ProviderDoc struct {
Show string `json:"show"`
Home string `json:"home"`
Api string `json:"api"`
Sdk string `json:"sdk"`
}
type ConfVariables struct {
Info string `json:"info"`
Docs *[]string `json:"docs"`
Items *[]ConfItem `json:"items"`
}
type ConfItem struct {
Name string `json:"name"`
Type string `json:"type"`
Require bool `json:"require"`
Info string `json:"info"`
}
var ProviderInfoMap map[string]ProviderInfo = nil
// ProviderInfos 数组
var ProviderInfos = make([]ProviderInfo, 0)
func ListProvider() *[]ProviderInfo {
if ProviderInfoMap == nil {
initProvider()
}
return &ProviderInfos
}
func MapProvider() *map[string]ProviderInfo {
if ProviderInfoMap == nil {
initProvider()
}
return &ProviderInfoMap
}
func GetProvider(name string) *ProviderInfo {
info := ProviderInfoMap[name]
return &info
}
func initProvider() {
data, err := providerFile.ReadFile("providers.json")
if err != nil {
log.Fatalf("Failed to read config file: %v", err)
}
var providers []ProviderInfo
if err := json.Unmarshal(data, &providers); err != nil {
log.Fatalf("Failed to parse config file: %v", err)
}
ProviderInfoMap = make(map[string]ProviderInfo)
for _, provider := range providers {
ProviderInfos = append(ProviderInfos, provider)
ProviderInfoMap[provider.Type] = provider
}
}

66
src/acme/providers.json Normal file
View File

@ -0,0 +1,66 @@
[
{
"type": "alidns",
"remarks": "ALIBABA CLOUD DNS",
"doc": {
"show": "https://go-acme.github.io/lego/dns/alidns/index.html",
"home": "https://www.alibabacloud.com/product/dns",
"api": "https://www.alibabacloud.com/help/en/alibaba-cloud-dns/latest/api-alidns-2015-01-09-dir-parsing-records",
"sdk": "https://github.com/aliyun/alibaba-cloud-sdk-go"
},
"variables": {
"info": "",
"docs": [
""
],
"items": [
{
"name": "RegionID",
"type": "string",
"required": true,
"info": "RegionID"
},{
"name": "APIKey",
"type": "string",
"required": true,
"info": "APIKey"
},{
"name": "SecretKey",
"type": "string",
"required": true,
"info": "SecretKey"
}
]
}
},
{
"type": "tencentcloud",
"remarks": "TENCENT CLOUD DNS",
"doc": {
"show": "https://go-acme.github.io/lego/dns/tencentcloud/index.html",
"home": "https://cloud.tencent.com/product/cns",
"api": "https://cloud.tencent.com/document/product/1427/56153",
"sdk": "https://github.com/tencentcloud/tencentcloud-sdk-go"
},
"variables": {
"info": "",
"docs": [
""
],
"items": [
{
"name": "SecretID",
"type": "string",
"required": true,
"info": "SecretID"
},
{
"name": "SecretKey",
"type": "string",
"required": true,
"info": "SecretKey"
}
]
}
}
]

View File

@ -87,6 +87,7 @@ func ServerStop(cmd *cobra.Command, args []string) {
func daemonStart() { func daemonStart() {
// 启动 HttpServer // 启动 HttpServer
server.HttpInstance = &server.HttpServer{}
server.HttpInstance.Init() server.HttpInstance.Init()
server.HttpInstance.Start() server.HttpInstance.Start()

View File

@ -30,6 +30,7 @@ func (s *HttpServer) Init() {
// initServer 初始化 // initServer 初始化
func (s *HttpServer) initServer(host string, port int) { func (s *HttpServer) initServer(host string, port int) {
//gin.SetMode(gin.ReleaseMode)
s.engine = gin.Default() s.engine = gin.Default()
s.register() s.register()
s.status = false s.status = false
@ -73,14 +74,20 @@ func (s *HttpServer) register() {
service.Use(gin.Logger()) service.Use(gin.Logger())
service.Use(http_handler.GlobalErrorHandler()) service.Use(http_handler.GlobalErrorHandler())
fs := http.FileServer(http.FS(staticFiles)) fs := http.FS(staticFiles)
http.Handle("/static/", http.StripPrefix("/static/", fs)) service.StaticFS("/s", fs)
certHandler := http_handler.CertHandlerInstance certHandler := http_handler.CertHandlerInstance
certGroup := service.Group("/api/v1/cert", http_handler.AuthMiddleware()) certGroup := service.Group("/api/v1/cert", http_handler.AuthMiddleware())
certGroup.GET("/", certHandler.Get) certGroup.GET("/", certHandler.Get)
providerHandler := http_handler.ProviderHandlerInstance
providerGroup := service.Group("/api/v1/provider", http_handler.AuthMiddleware())
providerGroup.GET("/list", providerHandler.List)
providerGroup.GET("/map", providerHandler.Map)
confHandler := http_handler.ConfHandlerInstance confHandler := http_handler.ConfHandlerInstance
confGroup := service.Group("/api/v1", http_handler.AuthMiddleware()) confGroup := service.Group("/api/v1", http_handler.AuthMiddleware())
confGroup.GET("/conf", confHandler.Get) confGroup.GET("/conf", confHandler.Get)
} }

View File

@ -0,0 +1,20 @@
package http_handler
import (
"acme-mana/src/acme"
"acme-mana/src/server/model"
"github.com/gin-gonic/gin"
)
type ProviderHandler struct {
}
var ProviderHandlerInstance = &ProviderHandler{}
func (h *ProviderHandler) List(c *gin.Context) {
c.JSON(200, model.SuccessD(acme.ListProvider()))
}
func (h *ProviderHandler) Map(c *gin.Context) {
c.JSON(200, model.SuccessD(acme.MapProvider()))
}