Skip to content

Commit

Permalink
feat: use config file in billing server
Browse files Browse the repository at this point in the history
  • Loading branch information
andy89923 committed May 22, 2024
1 parent 8553d72 commit 21055d6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 48 deletions.
63 changes: 25 additions & 38 deletions backend/billing/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -21,67 +23,52 @@ 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",
Fs: "os",
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, "", " ")
Expand All @@ -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)
Expand All @@ -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
}
Expand Down
21 changes: 14 additions & 7 deletions backend/factory/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)"`
}

Expand All @@ -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 {
Expand Down
11 changes: 8 additions & 3 deletions config/webuicfg.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
info:
version: 1.0.2
version: 1.0.3
description: WebUI initial local configuration

configuration:
Expand All @@ -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
Expand Down

0 comments on commit 21055d6

Please sign in to comment.