Skip to content

Commit

Permalink
chore: Copy http2_util, logger_conf and logger_util to util project (#21
Browse files Browse the repository at this point in the history
)

* chore: Copy http2_util, logger_conf and logger_util to util project

* Standardize on logger instead of logger_util

* Copy util_3gpp to the util project
  • Loading branch information
ghislainbourgeois authored May 29, 2024
1 parent a84da0d commit fdfe321
Show file tree
Hide file tree
Showing 10 changed files with 664 additions and 6 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ require (
github.com/evanphx/json-patch v4.11.0+incompatible
github.com/gin-gonic/gin v1.7.3
github.com/mitchellh/mapstructure v1.4.1
github.com/omec-project/openapi v1.2.0
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.0
github.com/smartystreets/goconvey v1.6.4
github.com/stretchr/testify v1.8.0
github.com/thakurajayL/go-ipam v0.0.5-dev
go.mongodb.org/mongo-driver v1.10.1
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b
)

Expand All @@ -30,6 +32,7 @@ require (
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt v3.2.1+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
Expand Down Expand Up @@ -58,7 +61,6 @@ require (
go.uber.org/zap v1.23.0 // indirect
go4.org/intern v0.0.0-20220617035311-6925f38cc365 // indirect
go4.org/unsafe/assume-no-moving-gc v0.0.0-20220617031537-928513b29760 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde // indirect
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
golang.org/x/text v0.3.7 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5x
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt v3.2.1+incompatible h1:73Z+4BJcrTC+KczS6WvTPvRGOp1WmfEP4Q1lOd9Z/+c=
github.com/golang-jwt/jwt v3.2.1+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down Expand Up @@ -199,6 +201,8 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/omec-project/openapi v1.2.0 h1:7Wvi0HLvhvxMyQtqGcqtMCPC/0QCGAFP5htrXCfWxRc=
github.com/omec-project/openapi v1.2.0/go.mod h1:hjU13MB1m9MHTko87JfsUNCdeD6/m6VkNZDD8Vq5U9M=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
Expand Down
50 changes: 50 additions & 0 deletions http2_util/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2019 Communication Service/Software Laboratory, National Chiao Tung University (free5gc.org)
//
// SPDX-License-Identifier: Apache-2.0

//go:build !debug
// +build !debug

package http2_util

import (
"crypto/tls"
"fmt"
"net/http"
"os"
"time"

"github.com/pkg/errors"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)

// NewServer returns a server instance with HTTP/2.0 and HTTP/2.0 cleartext support
// If this function cannot open or create the secret log file,
// **it still returns server instance** but without the secret log and error indication
func NewServer(bindAddr string, preMasterSecretLogPath string, handler http.Handler) (server *http.Server, err error) {
if handler == nil {
return nil, errors.New("server needs handler to handle request")
}

h2Server := &http2.Server{
// TODO: extends the idle time after re-use openapi client
IdleTimeout: 1 * time.Millisecond,
}
server = &http.Server{
Addr: bindAddr,
Handler: h2c.NewHandler(handler, h2Server),
}

if preMasterSecretLogPath != "" {
preMasterSecretFile, err := os.OpenFile(preMasterSecretLogPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return server, fmt.Errorf("create pre-master-secret log [%s] fail: %s", preMasterSecretLogPath, err)
}
server.TLSConfig = &tls.Config{
KeyLogWriter: preMasterSecretFile,
}
}

return
}
42 changes: 42 additions & 0 deletions http2_util/server_debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2019 Communication Service/Software Laboratory, National Chiao Tung University (free5gc.org)
//
// SPDX-License-Identifier: Apache-2.0

//+build debug

package http2_util

import (
"crypto/tls"
"github.com/pkg/errors"
"net/http"
"os"
)

type ZeroSource struct{}

func (ZeroSource) Read(b []byte) (n int, err error) {
for i := range b {
b[i] = 0
}
return len(b), nil
}

func NewServer(bindAddr string, tlskeylog string, handler http.Handler) (server *http.Server, err error) {
keylogFile, err := os.OpenFile(tlskeylog, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return nil, err
}
if handler == nil {
return nil, errors.New("server need handler")
}
server = &http.Server{
Addr: bindAddr,
TLSConfig: &tls.Config{
KeyLogWriter: keylogFile,
Rand: ZeroSource{},
},
Handler: handler,
}
return
}
26 changes: 21 additions & 5 deletions logger/logger_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,27 @@ type Logger struct {
NWDAF *LogSetting `yaml:"NWDAF" valid:"optional"`
WEBUI *LogSetting `yaml:"WEBUI" valid:"optional"`

Aper *LogSetting `yaml:"Aper" valid:"optional"`
FSM *LogSetting `yaml:"FSM" valid:"optional"`
NAS *LogSetting `yaml:"NAS" valid:"optional"`
NGAP *LogSetting `yaml:"NGAP" valid:"optional"`
PFCP *LogSetting `yaml:"PFCP" valid:"optional"`
Aper *LogSetting `yaml:"Aper" valid:"optional"`
CommonConsumerTest *LogSetting `yaml:"CommonConsumerTest" valid:"optional"`
FSM *LogSetting `yaml:"FSM" valid:"optional"`
MongoDBLibrary *LogSetting `yaml:"MongoDBLibrary" valid:"optional"`
NAS *LogSetting `yaml:"NAS" valid:"optional"`
NGAP *LogSetting `yaml:"NGAP" valid:"optional"`
OpenApi *LogSetting `yaml:"OpenApi" valid:"optional"`
NamfCommunication *LogSetting `yaml:"NamfCommunication" valid:"optional"`
NamfEventExposure *LogSetting `yaml:"NamfEventExposure" valid:"optional"`
NnssfNSSAIAvailability *LogSetting `yaml:"NnssfNSSAIAvailability" valid:"optional"`
NnssfNSSelection *LogSetting `yaml:"NnssfNSSelection" valid:"optional"`
NsmfEventExposure *LogSetting `yaml:"NsmfEventExposure" valid:"optional"`
NsmfPDUSession *LogSetting `yaml:"NsmfPDUSession" valid:"optional"`
NudmEventExposure *LogSetting `yaml:"NudmEventExposure" valid:"optional"`
NudmParameterProvision *LogSetting `yaml:"NudmParameterProvision" valid:"optional"`
NudmSubscriberDataManagement *LogSetting `yaml:"NudmSubscriberDataManagement" valid:"optional"`
NudmUEAuthentication *LogSetting `yaml:"NudmUEAuthentication" valid:"optional"`
NudmUEContextManagement *LogSetting `yaml:"NudmUEContextManagement" valid:"optional"`
NudrDataRepository *LogSetting `yaml:"NudrDataRepository" valid:"optional"`
PathUtil *LogSetting `yaml:"PathUtil" valid:"optional"`
PFCP *LogSetting `yaml:"PFCP" valid:"optional"`
}

func (l *Logger) Validate() (bool, error) {
Expand Down
65 changes: 65 additions & 0 deletions logger_conf/conf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2019 Communication Service/Software Laboratory, National Chiao Tung University (free5gc.org)
//
// SPDX-License-Identifier: Apache-2.0

package logger_conf

import (
"log"
"os"
"strconv"

"github.com/omec-project/util/path_util"
)

var Free5gcLogDir string = path_util.Free5gcPath("free5gc/log") + "/"
var LibLogDir string = Free5gcLogDir + "lib/"
var NfLogDir string = Free5gcLogDir + "nf/"

var Free5gcLogFile string = Free5gcLogDir + "free5gc.log"

func init() {
if err := os.MkdirAll(LibLogDir, 0775); err != nil {
log.Printf("Mkdir %s failed: %+v", LibLogDir, err)
}
if err := os.MkdirAll(NfLogDir, 0775); err != nil {
log.Printf("Mkdir %s failed: %+v", NfLogDir, err)
}

// Create log file or if it already exist, check if user can access it
f, fileOpenErr := os.OpenFile(Free5gcLogFile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if fileOpenErr != nil {
// user cannot access it.
log.Printf("Cannot Open %s\n", Free5gcLogFile)
} else {
// user can access it
if err := f.Close(); err != nil {
log.Printf("File %s cannot been closed\n", Free5gcLogFile)
}
}

sudoUID, errUID := strconv.Atoi(os.Getenv("SUDO_UID"))
sudoGID, errGID := strconv.Atoi(os.Getenv("SUDO_GID"))

if errUID == nil && errGID == nil {
// if using sudo to run the program, errUID will be nil and sudoUID will get the uid who run sudo
// else errUID will not be nil and sudoUID will be nil
// If user using sudo to run the program and create log file, log will own by root,
// here we change own to user so user can view and reuse the file
if err := os.Chown(Free5gcLogDir, sudoUID, sudoGID); err != nil {
log.Printf("Dir %s chown to %d:%d error: %v\n", Free5gcLogDir, sudoUID, sudoGID, err)
}
if err := os.Chown(LibLogDir, sudoUID, sudoGID); err != nil {
log.Printf("Dir %s chown to %d:%d error: %v\n", LibLogDir, sudoUID, sudoGID, err)
}
if err := os.Chown(NfLogDir, sudoUID, sudoGID); err != nil {
log.Printf("Dir %s chown to %d:%d error: %v\n", NfLogDir, sudoUID, sudoGID, err)
}

if fileOpenErr == nil {
if err := os.Chown(Free5gcLogFile, sudoUID, sudoGID); err != nil {
log.Printf("File %s chown to %d:%d error: %v\n", Free5gcLogFile, sudoUID, sudoGID, err)
}
}
}
}
21 changes: 21 additions & 0 deletions util_3gpp/3gpp_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2019 Communication Service/Software Laboratory, National Chiao Tung University (free5gc.org)
//
// SPDX-License-Identifier: Apache-2.0

package util_3gpp

type Dnn []uint8

func (d *Dnn) MarshalBinary() (data []byte, err error) {

data = append(data, uint8(len(*d)))
data = append(data, (*d)...)

return data, nil
}

func (d *Dnn) UnmarshalBinary(data []byte) error {

(*d) = data[1:]
return nil
}
59 changes: 59 additions & 0 deletions util_3gpp/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2019 Communication Service/Software Laboratory, National Chiao Tung University (free5gc.org)
//
// SPDX-License-Identifier: Apache-2.0

package logger

import (
"os"
"time"

formatter "github.com/antonfisher/nested-logrus-formatter"
"github.com/sirupsen/logrus"

"github.com/omec-project/util/logger_conf"
"github.com/omec-project/util/logger"
)

var log *logrus.Logger

// Util3GPPLog : Log entry of util_3gpp
var Util3GPPLog *logrus.Entry

func init() {
log = logrus.New()
log.SetReportCaller(false)

log.Formatter = &formatter.Formatter{
TimestampFormat: time.RFC3339,
TrimMessages: true,
NoFieldsSpace: true,
HideKeys: true,
FieldsOrder: []string{"component", "category"},
}

free5gcLogHook, err := logger.NewFileHook(logger_conf.Free5gcLogFile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err == nil {
log.Hooks.Add(free5gcLogHook)
}

selfLogHook, err := logger.NewFileHook(logger_conf.LibLogDir+"util_3gpp.log",
os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err == nil {
log.Hooks.Add(selfLogHook)
}

Util3GPPLog = log.WithFields(logrus.Fields{"component": "LIB", "category": "3GPP"})
}

// SetLogLevel : set the log level (panic|fatal|error|warn|info|debug|trace)
func SetLogLevel(level logrus.Level) {
Util3GPPLog.Infoln("set log level :", level)
log.SetLevel(level)
}

// SetReportCaller : Set whether shows the filePath and functionName on loggers
func SetReportCaller(bool bool) {
Util3GPPLog.Infoln("set report call :", bool)
log.SetReportCaller(bool)
}
18 changes: 18 additions & 0 deletions util_3gpp/str_conv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2019 Communication Service/Software Laboratory, National Chiao Tung University (free5gc.org)
//
// SPDX-License-Identifier: Apache-2.0

package util_3gpp

import (
"fmt"

"github.com/omec-project/openapi/models"
)

func SNssaiToString(snssai *models.Snssai) (str string) {
if snssai.Sd == "" {
return fmt.Sprintf("%d-%s", snssai.Sst, snssai.Sd)
}
return fmt.Sprintf("%d", snssai.Sst)
}
Loading

0 comments on commit fdfe321

Please sign in to comment.