-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Copy http2_util, logger_conf and logger_util to util project (#21
) * 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
1 parent
a84da0d
commit fdfe321
Showing
10 changed files
with
664 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.