working commit
This commit is contained in:
@@ -7,6 +7,8 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"mstore/pkg/auxx509"
|
||||
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
@@ -31,6 +33,10 @@ type Config struct {
|
||||
Logpath string `json:"logpath" yaml:"logpath"`
|
||||
Runpath string `json:"runpath" yaml:"runpath"`
|
||||
Version string `json:"version" yaml:"version"`
|
||||
Certpath string `json:"certpath" yaml:"certath"`
|
||||
Keypath string `json:"keypath" yaml:"keypath"`
|
||||
X509Cert string `json:"X509cert" yaml:"X509cert"`
|
||||
X509Key string `json:"X509key" yaml:"X509key"`
|
||||
}
|
||||
|
||||
func NewConfig() *Config {
|
||||
@@ -40,6 +46,12 @@ func NewConfig() *Config {
|
||||
runfile := fmt.Sprintf("%s.run", srvname)
|
||||
runpath := filepath.Join(rundir, runfile)
|
||||
|
||||
certpath := fmt.Sprintf("%s.crt", srvname)
|
||||
certpath = filepath.Join(confdir, certpath)
|
||||
|
||||
keypath := fmt.Sprintf("%s.crt", srvname)
|
||||
keypath = filepath.Join(confdir, keypath)
|
||||
|
||||
return &Config{
|
||||
Service: Service{
|
||||
Address: "0.0.0.0",
|
||||
@@ -55,6 +67,8 @@ func NewConfig() *Config {
|
||||
Logpath: logpath,
|
||||
Runpath: runpath,
|
||||
Version: version,
|
||||
Certpath: certpath,
|
||||
Keypath: keypath,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,3 +112,52 @@ func (conf *Config) ReadOptions() error {
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (conf *Config) ReadX509Cert() error {
|
||||
var err error
|
||||
if conf.Certpath != "" && conf.Keypath != "" {
|
||||
if !filepath.IsAbs(conf.Certpath) {
|
||||
conf.Certpath = filepath.Join(confdir, conf.Certpath)
|
||||
}
|
||||
certBytes, err := os.ReadFile(conf.Certpath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !filepath.IsAbs(conf.Keypath) {
|
||||
conf.Keypath = filepath.Join(confdir, conf.Keypath)
|
||||
}
|
||||
keyBytes, err := os.ReadFile(conf.Keypath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conf.X509Cert = string(certBytes)
|
||||
conf.X509Key = string(keyBytes)
|
||||
return err
|
||||
}
|
||||
/*
|
||||
if conf.X509Cert != "" && conf.X509Key != "" {
|
||||
x509Cert, err := base64.StdEncoding.DecodeString(conf.X509Cert)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conf.X509Cert = string(x509Cert)
|
||||
x509Key, err := base64.StdEncoding.DecodeString(conf.X509Key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conf.X509Key = string(x509Key)
|
||||
}
|
||||
*/
|
||||
if conf.X509Cert == "" || conf.X509Key == "" {
|
||||
certBytes, keyBytes, err := auxx509.CreateSelfSignedCert("localhost")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conf.X509Cert = string(certBytes)
|
||||
conf.X509Key = string(keyBytes)
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
+49
-8
@@ -15,7 +15,11 @@ import (
|
||||
const protocol = "tcp"
|
||||
|
||||
type ServiceParams struct {
|
||||
Handler *handler.Handler
|
||||
Handler *handler.Handler
|
||||
X509Cert []byte
|
||||
X509Key []byte
|
||||
Portnum int64
|
||||
Address string
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
@@ -23,16 +27,23 @@ type Service struct {
|
||||
rout *router.Router
|
||||
logg *logger.Logger
|
||||
|
||||
address string
|
||||
portnum int64
|
||||
listen net.Listener
|
||||
hsrv *http.Server
|
||||
address string
|
||||
portnum int64
|
||||
x509cert []byte
|
||||
x509key []byte
|
||||
|
||||
listen net.Listener
|
||||
hsrv *http.Server
|
||||
}
|
||||
|
||||
func NewService(params *ServiceParams) (*Service, error) {
|
||||
var err error
|
||||
svc := &Service{
|
||||
hand: params.Handler,
|
||||
hand: params.Handler,
|
||||
x509cert: conf.X509Cert,
|
||||
x509key: conf.X509Key,
|
||||
portnum: conf.Portnum,
|
||||
address: conf.Address,
|
||||
}
|
||||
svc.logg = logger.NewLogger("service")
|
||||
return svc, err
|
||||
@@ -55,8 +66,38 @@ func (svc *Service) Build() error {
|
||||
svc.logg.Infof("%s\t%s", item.Method, item.RawPath)
|
||||
}
|
||||
|
||||
listenAddress := fmt.Sprintf("%s:%d", svc.address, svc.portnum)
|
||||
svc.listen, err = net.Listen(protocol, listenAddress)
|
||||
const useTLS = true
|
||||
if useTLS {
|
||||
tlsCert, err := tls.X509KeyPair(svc.x509cert, svc.x509key)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tlsConfig := tls.Config{
|
||||
Certificates: []tls.Certificate{tlsCert},
|
||||
ClientAuth: tls.NoClientCert,
|
||||
InsecureSkipVerify: true,
|
||||
}
|
||||
|
||||
listenAddress := fmt.Sprintf("%s:%d", svc.address, svc.portnum)
|
||||
svc.listen, err = tls.Listen(svc.protocol, listenAddress, &tlsConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
} else {
|
||||
listenAddress := fmt.Sprintf("%s:%d", svc.address, svc.portnum)
|
||||
svc.listen, err = net.Listen(svc.protocol, listenAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
//listenAddress := fmt.Sprintf("%s:%d", svc.address, svc.portnum)
|
||||
//svc.listen, err = net.Listen(protocol, listenAddress)
|
||||
|
||||
svc.log.Infof("Service listening at %v", svc.listen.Addr())
|
||||
|
||||
svc.hsrv = &http.Server{
|
||||
Handler: svc.rout,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user