2024-10-29 12:30:14 +00:00
|
|
|
package callog
|
2024-09-13 15:08:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2024-10-28 20:54:17 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/logutils"
|
2024-09-13 15:08:20 +00:00
|
|
|
)
|
|
|
|
|
2024-09-27 10:48:51 +00:00
|
|
|
var sensitiveKeys = []string{
|
|
|
|
"password",
|
|
|
|
"mnemonic",
|
|
|
|
"openseaAPIKey",
|
|
|
|
"poktToken",
|
|
|
|
"alchemyArbitrumMainnetToken",
|
|
|
|
"raribleTestnetAPIKey",
|
|
|
|
"alchemyOptimismMainnetToken",
|
|
|
|
"statusProxyBlockchainUser",
|
|
|
|
"alchemyEthereumSepoliaToken",
|
|
|
|
"alchemyArbitrumSepoliaToken",
|
|
|
|
"infuraToken",
|
|
|
|
"raribleMainnetAPIKey",
|
|
|
|
"alchemyEthereumMainnetToken",
|
|
|
|
"alchemyOptimismSepoliaToken",
|
|
|
|
"verifyENSURL",
|
|
|
|
"verifyTransactionURL",
|
|
|
|
}
|
|
|
|
|
|
|
|
var sensitiveRegexString = fmt.Sprintf(`(?i)(".*?(%s).*?")\s*:\s*("[^"]*")`, strings.Join(sensitiveKeys, "|"))
|
|
|
|
|
|
|
|
var sensitiveRegex = regexp.MustCompile(sensitiveRegexString)
|
2024-09-13 15:08:20 +00:00
|
|
|
|
|
|
|
func getFunctionName(fn any) string {
|
|
|
|
return runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
func getShortFunctionName(fn any) string {
|
|
|
|
fullName := getFunctionName(fn)
|
|
|
|
parts := strings.Split(fullName, ".")
|
|
|
|
return parts[len(parts)-1]
|
|
|
|
}
|
|
|
|
|
2024-10-29 12:30:14 +00:00
|
|
|
// Call executes the given function and logs request details if logging is enabled
|
2024-09-26 22:37:32 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - fn: The function to be executed
|
|
|
|
// - params: A variadic list of parameters to be passed to the function
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - The result of the function execution (if any)
|
|
|
|
//
|
|
|
|
// Functionality:
|
|
|
|
// 1. Sets up panic recovery to log and re-panic
|
|
|
|
// 2. Records start time if request logging is enabled
|
2024-10-29 12:30:14 +00:00
|
|
|
// 3. Uses reflection to Call the given function
|
2024-09-26 22:37:32 +00:00
|
|
|
// 4. If request logging is enabled, logs method name, parameters, response, and execution duration
|
|
|
|
// 5. Removes sensitive information before logging
|
2024-10-29 12:30:14 +00:00
|
|
|
func Call(logger *zap.Logger, fn any, params ...any) any {
|
2024-09-13 15:08:20 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("panic found in call", zap.Any("error", r), zap.Stack("stacktrace"))
|
2024-09-13 15:08:20 +00:00
|
|
|
panic(r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var startTime time.Time
|
|
|
|
|
2024-10-29 12:30:14 +00:00
|
|
|
requestLoggingEnabled := logger != nil
|
2024-10-24 21:46:23 +00:00
|
|
|
if requestLoggingEnabled {
|
2024-09-13 15:08:20 +00:00
|
|
|
startTime = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
fnValue := reflect.ValueOf(fn)
|
|
|
|
fnType := fnValue.Type()
|
|
|
|
if fnType.Kind() != reflect.Func {
|
|
|
|
panic("fn must be a function")
|
|
|
|
}
|
|
|
|
|
|
|
|
args := make([]reflect.Value, len(params))
|
|
|
|
for i, param := range params {
|
|
|
|
args[i] = reflect.ValueOf(param)
|
|
|
|
}
|
|
|
|
|
|
|
|
results := fnValue.Call(args)
|
|
|
|
|
|
|
|
var resp any
|
|
|
|
|
|
|
|
if len(results) > 0 {
|
|
|
|
resp = results[0].Interface()
|
|
|
|
}
|
|
|
|
|
2024-10-24 21:46:23 +00:00
|
|
|
if requestLoggingEnabled {
|
2024-09-13 15:08:20 +00:00
|
|
|
duration := time.Since(startTime)
|
|
|
|
methodName := getShortFunctionName(fn)
|
|
|
|
paramsString := removeSensitiveInfo(fmt.Sprintf("%+v", params))
|
|
|
|
respString := removeSensitiveInfo(fmt.Sprintf("%+v", resp))
|
2024-10-24 21:46:23 +00:00
|
|
|
|
2024-10-29 12:30:14 +00:00
|
|
|
logger.Debug("call",
|
2024-10-24 21:46:23 +00:00
|
|
|
zap.String("method", methodName),
|
|
|
|
zap.String("params", paramsString),
|
|
|
|
zap.String("resp", respString),
|
|
|
|
zap.Duration("duration", duration),
|
|
|
|
)
|
2024-09-13 15:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2024-10-29 12:30:14 +00:00
|
|
|
func CallWithResponse(logger *zap.Logger, fn any, params ...any) string {
|
|
|
|
resp := Call(logger, fn, params...)
|
2024-09-13 15:08:20 +00:00
|
|
|
if resp == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return resp.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeSensitiveInfo(jsonStr string) string {
|
|
|
|
// see related test for the usage of this function
|
|
|
|
return sensitiveRegex.ReplaceAllStringFunc(jsonStr, func(match string) string {
|
|
|
|
parts := sensitiveRegex.FindStringSubmatch(match)
|
|
|
|
return fmt.Sprintf(`%s:"***"`, parts[1])
|
|
|
|
})
|
|
|
|
}
|