From 21055d60617331e37ef872c9c81bb3d55b903e7e Mon Sep 17 00:00:00 2001 From: "CTFang@WireLab" <ctfang.cs12@nycu.edu.tw> Date: Wed, 22 May 2024 06:16:37 +0000 Subject: [PATCH] feat: use config file in billing server --- backend/billing/server.go | 63 ++++++++++++++++----------------------- backend/factory/config.go | 21 ++++++++----- config/webuicfg.yaml | 11 +++++-- 3 files changed, 47 insertions(+), 48 deletions(-) diff --git a/backend/billing/server.go b/backend/billing/server.go index 3b36b78..0517333 100644 --- a/backend/billing/server.go +++ b/backend/billing/server.go @@ -4,10 +4,12 @@ package billing import ( "encoding/json" "os" + "path" "strconv" "sync" "github.com/fclairamb/ftpserver/config" + ftpconf "github.com/fclairamb/ftpserver/config/confpar" "github.com/fclairamb/ftpserver/server" ftpserver "github.com/fclairamb/ftpserverlib" @@ -21,55 +23,40 @@ type BillingDomain struct { wg *sync.WaitGroup } -type Access struct { - User string `json:"user"` - Pass string `json:"pass"` - Fs string `json:"fs"` - Params map[string]string `json:"params"` -} - -type PortRange struct { - Start int `json:"start"` - End int `json:"end"` -} - -type FtpConfig struct { - Version int `json:"version"` - Accesses []Access `json:"accesses"` - Listen_address string `json:"listen_address"` - - Passive_transfer_port_range PortRange `json:"passive_transfer_port_range"` -} - // The ftp server is for CDR Push method, that is the CHF will send the CDR file to the FTP server func OpenServer(wg *sync.WaitGroup) *BillingDomain { - // Arguments vars - confFile := "/tmp/webconsole/ftpserver.json" - b := &BillingDomain{ wg: wg, } - if _, err := os.Stat("/tmp/webconsole"); err != nil { - if err_mk := os.Mkdir("/tmp/webconsole", os.ModePerm); err_mk != nil { + + billingConfig := factory.WebuiConfig.Configuration.BillingServer + + basePath := billingConfig.BastPath + confFile := path.Join(basePath, "ftpserver.json") + + if _, err := os.Stat(basePath); err != nil { + if err_mk := os.Mkdir(basePath, os.ModePerm); err_mk != nil { logger.BillingLog.Error(err_mk) } } - billingConfig := factory.WebuiConfig.Configuration.BillingServer addr := billingConfig.HostIPv4 + ":" + strconv.Itoa(billingConfig.ListenPort) params := map[string]string{ - "basePath": "/tmp/webconsole", + "basePath": basePath, } - if billingConfig.Tls != nil { - params["cert"] = billingConfig.Tls.Pem - params["key"] = billingConfig.Tls.Key + logger.BillingLog.Infof("Open BillingServer on %+v", basePath) + + if billingConfig.Cert != nil { + params["cert"] = billingConfig.Cert.Pem + params["key"] = billingConfig.Cert.Key + logger.BillingLog.Infof("Use tls: %+v, %+v", params["cert"], params["key"]) } - ftpConfig := FtpConfig{ + ftpConfig := ftpconf.Content{ Version: 1, - Accesses: []Access{ + Accesses: []*ftpconf.Access{ { User: "admin", Pass: "free5gc", @@ -77,11 +64,11 @@ func OpenServer(wg *sync.WaitGroup) *BillingDomain { Params: params, }, }, - Passive_transfer_port_range: PortRange{ - Start: 2123, - End: 2130, + PassiveTransferPortRange: &ftpconf.PortRange{ + Start: billingConfig.PortRange.Start, + End: billingConfig.PortRange.End, }, - Listen_address: addr, + ListenAddress: addr, } file, err := json.MarshalIndent(ftpConfig, "", " ") @@ -100,7 +87,7 @@ func OpenServer(wg *sync.WaitGroup) *BillingDomain { logger.BillingLog.Error("Can't load conf", "Err", errConfig) return nil } - logger.BillingLog.Warnf("conf %+v", conf.Content.Accesses[0].Params) + // Loading the driver var errNewServer error b.driver, errNewServer = server.NewServer(conf, logger.FtpServerLog) @@ -118,7 +105,7 @@ func OpenServer(wg *sync.WaitGroup) *BillingDomain { b.ftpServer.Logger = logger.FtpServerLog go b.Serve() - logger.BillingLog.Info("Billing server Start") + logger.BillingLog.Info("Billing server started") return b } diff --git a/backend/factory/config.go b/backend/factory/config.go index a4dd3c9..f4db6bb 100644 --- a/backend/factory/config.go +++ b/backend/factory/config.go @@ -35,7 +35,7 @@ func (c *Config) Validate() (bool, error) { } type Info struct { - Version string `yaml:"version,omitempty" valid:"required,in(1.0.2)"` + Version string `yaml:"version,omitempty" valid:"required,in(1.0.3)"` Description string `yaml:"description,omitempty" valid:"type(string)"` } @@ -58,17 +58,24 @@ type WebServer struct { PORT string `yaml:"port" valid:"required"` } -type Tls struct { +type Cert struct { Pem string `yaml:"pem,omitempty" valid:"type(string),minstringlength(1),required"` Key string `yaml:"key,omitempty" valid:"type(string),minstringlength(1),required"` } +type PortRange struct { + Start int `yaml:"start,omitempty" valid:"required" json:"start"` + End int `yaml:"end,omitempty" valid:"required" json:"end"` +} + type BillingServer struct { - Enable bool `yaml:"enable,omitempty" valid:"required,type(bool)"` - HostIPv4 string `yaml:"hostIPv4,omitempty" valid:"required,host"` - Port int `yaml:"port,omitempty" valid:"optional,port"` - ListenPort int `yaml:"listenPort,omitempty" valid:"required,port"` - Tls *Tls `yaml:"tls,omitempty" valid:"optional"` + Enable bool `yaml:"enable,omitempty" valid:"required,type(bool)"` + HostIPv4 string `yaml:"hostIPv4,omitempty" valid:"required,host"` + ListenPort int `yaml:"listenPort,omitempty" valid:"required,port"` + PortRange PortRange `yaml:"portRange,omitempty" valid:"required"` + BastPath string `yaml:"basePath,omitempty" valid:"type(string),required"` + Cert *Cert `yaml:"cert,omitempty" valid:"optional"` + Port int `yaml:"port,omitempty" valid:"optional,port"` } type Mongodb struct { diff --git a/config/webuicfg.yaml b/config/webuicfg.yaml index 232a56f..3b3e316 100644 --- a/config/webuicfg.yaml +++ b/config/webuicfg.yaml @@ -1,5 +1,5 @@ info: - version: 1.0.2 + version: 1.0.3 description: WebUI initial local configuration configuration: @@ -15,10 +15,15 @@ configuration: enable: true hostIPv4: 127.0.0.1 listenPort: 2121 - port: 2122 - tls: + portRange: # passive port range + start: 2123 + end: 2130 + basePath: /tmp/webconsole + port: 2122 # CGF's FTP server port (not used for now) + cert: pem: cert/chf.pem key: cert/chf.key + logger: # log output setting enable: true # true or false -- GitLab