2025-09-05 18:35:46 +08:00
|
|
|
package config
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
import (
|
|
|
|
|
"crypto/tls"
|
|
|
|
|
"crypto/x509"
|
|
|
|
|
"encoding/pem"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
"github.com/youmark/pkcs8"
|
|
|
|
|
)
|
2025-09-05 18:35:46 +08:00
|
|
|
|
|
|
|
|
type tlsConfig struct {
|
|
|
|
|
CAPath string `json:"capath" yaml:"capath"`
|
|
|
|
|
KeyPath string `json:"keypath" yaml:"keypath"`
|
|
|
|
|
CertPath string `json:"certpath" yaml:"certpath"`
|
|
|
|
|
Password string `json:"password" yaml:"password"`
|
|
|
|
|
SkipVerify bool `json:"skipverify" yaml:"skipverify"`
|
|
|
|
|
ServerName string `json:"servername" yaml:"servername"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type rabbitConfig struct {
|
2025-09-19 16:17:46 +08:00
|
|
|
Broker string `json:"broker" yaml:"broker"`
|
2025-09-05 18:35:46 +08:00
|
|
|
Username string `json:"username" yaml:"username"`
|
|
|
|
|
Password string `json:"password" yaml:"password"`
|
|
|
|
|
TLS *tlsConfig `json:"tls" yaml:"tls"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewRabbitConfig() *rabbitConfig {
|
|
|
|
|
return new(rabbitConfig)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
func (conf *rabbitConfig) GenAddress(tls bool) string {
|
2025-09-05 18:35:46 +08:00
|
|
|
if conf == nil {
|
|
|
|
|
panic("rabbit config is nil")
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
address := "amqp://"
|
|
|
|
|
if tls {
|
|
|
|
|
address = "amqps://"
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
2025-09-19 16:17:46 +08:00
|
|
|
if conf.GetUsername() != "" && conf.GetPassword() != "" {
|
|
|
|
|
address += conf.GetUsername() + ":" + conf.GetPassword() + "@"
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
2025-09-19 16:17:46 +08:00
|
|
|
address += conf.GetBroker() + "/"
|
2025-09-05 18:35:46 +08:00
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
return address
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
func (conf *rabbitConfig) GetBroker() string {
|
2025-09-05 18:35:46 +08:00
|
|
|
if conf == nil {
|
|
|
|
|
panic("rabbit config is nil")
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
return conf.Broker
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
func (conf *rabbitConfig) SetBroker(broker string) *rabbitConfig {
|
2025-09-05 18:35:46 +08:00
|
|
|
if conf == nil {
|
|
|
|
|
panic("rabbit config is nil")
|
|
|
|
|
}
|
2025-09-19 16:17:46 +08:00
|
|
|
conf.Broker = broker
|
2025-09-05 18:35:46 +08:00
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
return conf
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
func (conf *rabbitConfig) GetUsername() string {
|
2025-09-05 18:35:46 +08:00
|
|
|
if conf == nil {
|
|
|
|
|
panic("rabbit config is nil")
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
return conf.Username
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
func (conf *rabbitConfig) SetUsername(username string) *rabbitConfig {
|
2025-09-05 18:35:46 +08:00
|
|
|
if conf == nil {
|
2025-09-19 16:17:46 +08:00
|
|
|
panic("rabbit config is nil")
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
2025-09-19 16:17:46 +08:00
|
|
|
conf.Username = username
|
2025-09-05 18:35:46 +08:00
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
return conf
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
func (conf *rabbitConfig) GetPassword() string {
|
2025-09-05 18:35:46 +08:00
|
|
|
if conf == nil {
|
2025-09-19 16:17:46 +08:00
|
|
|
panic("rabbit config is nil")
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
return conf.Password
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
func (conf *rabbitConfig) SetPassword(password string) *rabbitConfig {
|
2025-09-05 18:35:46 +08:00
|
|
|
if conf == nil {
|
2025-09-19 16:17:46 +08:00
|
|
|
panic("rabbit config is nil")
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
2025-09-19 16:17:46 +08:00
|
|
|
conf.Password = password
|
2025-09-05 18:35:46 +08:00
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
return conf
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
func (conf *rabbitConfig) InitTLS() *rabbitConfig {
|
2025-09-05 18:35:46 +08:00
|
|
|
if conf == nil {
|
|
|
|
|
panic("rabbit config is nil")
|
|
|
|
|
}
|
|
|
|
|
conf.TLS = new(tlsConfig)
|
2025-09-19 16:17:46 +08:00
|
|
|
|
|
|
|
|
return conf
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (conf *rabbitConfig) GetTLS() *tlsConfig {
|
|
|
|
|
if conf == nil {
|
|
|
|
|
panic("rabbit config is nil")
|
|
|
|
|
}
|
2025-09-19 16:17:46 +08:00
|
|
|
|
2025-09-05 18:35:46 +08:00
|
|
|
return conf.TLS
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (conf *tlsConfig) GetCAPath() string {
|
|
|
|
|
if conf == nil {
|
|
|
|
|
panic("rabbit tls is nil")
|
|
|
|
|
}
|
2025-09-19 16:17:46 +08:00
|
|
|
|
2025-09-05 18:35:46 +08:00
|
|
|
return conf.CAPath
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
func (conf *tlsConfig) SetCAPath(caPath string) *tlsConfig {
|
2025-09-05 18:35:46 +08:00
|
|
|
if conf == nil {
|
|
|
|
|
panic("rabbit tls is nil")
|
|
|
|
|
}
|
|
|
|
|
conf.CAPath = caPath
|
2025-09-19 16:17:46 +08:00
|
|
|
|
|
|
|
|
return conf
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (conf *tlsConfig) GetKeyPath() string {
|
|
|
|
|
if conf == nil {
|
|
|
|
|
panic("rabbit tls is nil")
|
|
|
|
|
}
|
2025-09-19 16:17:46 +08:00
|
|
|
|
2025-09-05 18:35:46 +08:00
|
|
|
return conf.KeyPath
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
func (conf *tlsConfig) SetKeyPath(keyPath string) *tlsConfig {
|
2025-09-05 18:35:46 +08:00
|
|
|
if conf == nil {
|
|
|
|
|
panic("rabbit tls is nil")
|
|
|
|
|
}
|
|
|
|
|
conf.KeyPath = keyPath
|
2025-09-19 16:17:46 +08:00
|
|
|
|
|
|
|
|
return conf
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (conf *tlsConfig) GetCertPath() string {
|
|
|
|
|
if conf == nil {
|
|
|
|
|
panic("rabbit tls is nil")
|
|
|
|
|
}
|
2025-09-19 16:17:46 +08:00
|
|
|
|
2025-09-05 18:35:46 +08:00
|
|
|
return conf.CertPath
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
func (conf *tlsConfig) SetCertPath(certPath string) *tlsConfig {
|
2025-09-05 18:35:46 +08:00
|
|
|
if conf == nil {
|
|
|
|
|
panic("rabbit tls is nil")
|
|
|
|
|
}
|
2025-09-19 16:17:46 +08:00
|
|
|
|
2025-09-05 18:35:46 +08:00
|
|
|
conf.CertPath = certPath
|
2025-09-19 16:17:46 +08:00
|
|
|
|
|
|
|
|
return conf
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (conf *tlsConfig) GetPassword() string {
|
|
|
|
|
if conf == nil {
|
|
|
|
|
panic("rabbit tls is nil")
|
|
|
|
|
}
|
2025-09-19 16:17:46 +08:00
|
|
|
|
2025-09-05 18:35:46 +08:00
|
|
|
return conf.Password
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
func (conf *tlsConfig) SetPassword(password string) *tlsConfig {
|
2025-09-05 18:35:46 +08:00
|
|
|
if conf == nil {
|
|
|
|
|
panic("rabbit tls is nil")
|
|
|
|
|
}
|
|
|
|
|
conf.Password = password
|
2025-09-19 16:17:46 +08:00
|
|
|
|
|
|
|
|
return conf
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (conf *tlsConfig) GetSkipVerify() bool {
|
|
|
|
|
if conf == nil {
|
|
|
|
|
panic("rabbit tls is nil")
|
|
|
|
|
}
|
2025-09-19 16:17:46 +08:00
|
|
|
|
2025-09-05 18:35:46 +08:00
|
|
|
return conf.SkipVerify
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
func (conf *tlsConfig) SetSkipVerify(skipVerify bool) *tlsConfig {
|
2025-09-05 18:35:46 +08:00
|
|
|
if conf == nil {
|
|
|
|
|
panic("rabbit tls is nil")
|
|
|
|
|
}
|
|
|
|
|
conf.SkipVerify = skipVerify
|
2025-09-19 16:17:46 +08:00
|
|
|
|
|
|
|
|
return conf
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (conf *tlsConfig) GetServerName() string {
|
|
|
|
|
if conf == nil {
|
|
|
|
|
panic("rabbit tls is nil")
|
|
|
|
|
}
|
2025-09-19 16:17:46 +08:00
|
|
|
|
2025-09-05 18:35:46 +08:00
|
|
|
return conf.ServerName
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
func (conf *tlsConfig) SetServerName(serverName string) *tlsConfig {
|
2025-09-05 18:35:46 +08:00
|
|
|
if conf == nil {
|
|
|
|
|
panic("rabbit tls is nil")
|
|
|
|
|
}
|
|
|
|
|
conf.ServerName = serverName
|
2025-09-19 16:17:46 +08:00
|
|
|
|
|
|
|
|
return conf
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (conf *tlsConfig) GenTLSConfig(tag string) (*tls.Config, error) {
|
|
|
|
|
if conf == nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if conf.GetCAPath() == "" || conf.GetCertPath() == "" ||
|
|
|
|
|
conf.GetKeyPath() == "" {
|
|
|
|
|
return nil, errors.New("rabbit tls not valid")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
caPem, err := os.ReadFile(conf.GetCAPath())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
certPool := x509.NewCertPool()
|
|
|
|
|
certPool.AppendCertsFromPEM(caPem)
|
|
|
|
|
|
|
|
|
|
keyPem, err := os.ReadFile(conf.GetKeyPath())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
certPem, err := os.ReadFile(conf.GetCertPath())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pemBlock, err := parsePrivateKey(keyPem, []byte(conf.GetPassword()))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cliCert, err := tls.X509KeyPair(certPem, pem.EncodeToMemory(pemBlock))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &tls.Config{
|
|
|
|
|
Certificates: []tls.Certificate{cliCert},
|
|
|
|
|
RootCAs: certPool,
|
|
|
|
|
ServerName: conf.GetServerName(),
|
|
|
|
|
InsecureSkipVerify: conf.GetSkipVerify(),
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parsePrivateKey(key, password []byte) (*pem.Block, error) {
|
|
|
|
|
block, _ := pem.Decode(key)
|
|
|
|
|
if block == nil {
|
|
|
|
|
return nil, errors.New("no valid pem")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var privateKey any
|
|
|
|
|
var err error
|
|
|
|
|
switch block.Type {
|
|
|
|
|
case "RSA PRIVATE KEY":
|
|
|
|
|
privateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
|
|
|
|
|
case "PRIVATE KEY":
|
|
|
|
|
privateKey, err = x509.ParsePKCS8PrivateKey(block.Bytes)
|
|
|
|
|
case "ENCRYPTED PRIVATE KEY":
|
|
|
|
|
privateKey, err = pkcs8.ParsePKCS8PrivateKey(block.Bytes, password)
|
|
|
|
|
default:
|
|
|
|
|
return nil, fmt.Errorf("unsupported key type: %s", block.Type)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pemBytes, err := x509.MarshalPKCS8PrivateKey(privateKey)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &pem.Block{
|
|
|
|
|
Type: "PRIVATE KEY",
|
|
|
|
|
Bytes: pemBytes,
|
|
|
|
|
}, nil
|
2025-09-05 18:35:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func rabbitConfigName() string {
|
|
|
|
|
return "rabbit.json"
|
|
|
|
|
}
|