filter

package
v0.0.0-...-9d1dd97 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 13, 2024 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

LuaPool is a pool of Lua state instances.

Functions

func PreCompileLuaFilters

func PreCompileLuaFilters() (err error)

PreCompileLuaFilters is a function that pre-compiles Lua filters. It iterates over the filters available in the configuration. For each filter, it creates a new LuaFilter instance passing the filter name and script path, and then adds it to the LuaFilters. Note: If LuaFilters is nil, a new instance of PreCompiledLuaFilters is created. If LuaFilters already exists, it's reset before the new filters are added. If an error occurs when creating a new LuaFilter, it returns immediately with that error. It returns nil if no error occurs.

Returns:

error if any error occurs while initializing the Lua filters

Types

type LuaFilter

type LuaFilter struct {
	// Name is a string that represents the name of the Lua filter.
	Name string

	// CompiledScript is a pointer to a FunctionProto struct from the go-lua package.
	// It represents a compiled Lua function that can be executed by a Lua VM.
	CompiledScript *lua.FunctionProto
}

LuaFilter represents a struct for managing Lua filters. It contains fields for filter name and a compiled Lua script.

func NewLuaFilter

func NewLuaFilter(name string, scriptPath string) (*LuaFilter, error)

NewLuaFilter creates a new instance of LuaFilter. It requires two parameters: name and scriptPath. The name parameter is a string representing the name of the LuaFilter. If it is empty, an error is returned. The scriptPath parameter is a string representing the path to a Lua script file. If the scriptPath is empty, an error is returned. If the scriptPath is valid, the Lua script at the given path is compiled. If script compilation fails, it returns the related error. If both parameters are valid and the script compilation is successful, a pointer to the LuaFilter instance is returned. The returned LuaFilter instance includes the provided name and the compiled script.

type PreCompiledLuaFilters

type PreCompiledLuaFilters struct {
	// LuaScripts is a slice of pointers to LuaFilter,
	// each of which represents a precompiled Lua script.
	LuaScripts []*LuaFilter

	// Mu is a read/write mutex used to allow safe concurrent access to the LuaScripts.
	Mu sync.RWMutex
}

PreCompiledLuaFilters represents a collection of precompiled Lua scripts along with a mutex for handling concurrent access to the script data.

var LuaFilters *PreCompiledLuaFilters

LuaFilters holds pre-compiled Lua scripts for use across the application. It allows faster access and execution of frequently used scripts.

func (*PreCompiledLuaFilters) Add

func (a *PreCompiledLuaFilters) Add(luaFilter *LuaFilter)

Add appends a LuaFilter to the LuaScripts in the PreCompiledLuaFilters. It ensures thread-safety by obtaining a lock before performing the operation, and then unlocking once the operation is complete.

Parameters:

luaFilter: The LuaFilter instance that should be added.

Usage:

luaFilters := &PreCompiledLuaFilters{}
filter := &LuaFilter{}
luaFilters.Add(filter)

func (*PreCompiledLuaFilters) Reset

func (a *PreCompiledLuaFilters) Reset()

Reset clears the LuaScripts slice of a PreCompiledLuaFilters object.The method also prevents race conditions by Locking the Mutex before executing, and Unlocking once it has finished. Existing entries in the slice are discarded.

type Request

type Request struct {
	// Debug is a flag that is set if running in debug more.
	Debug bool

	// UserFound indicates whether a user has been found on the system.
	UserFound bool

	// Authenticated indicates whether the user has authenticated.
	Authenticated bool

	// NoAuth indicates if any authentication method has no effect.
	NoAuth bool

	// Session is a GUID representing the user's session.
	Session string

	// ClientIP is the IP address of the client making the request.
	ClientIP string

	// ClientPort is the port being used by the client making the request.
	ClientPort string

	// ClientHost is the hostname of the client making the request.
	ClientHost string

	// ClientID is a unique ID representing the client making the request.
	ClientID string

	// LocalIP is the local IP address the request is made to.
	LocalIP string

	// LocalPort is the local port the request is made to.
	LocalPort string

	// Username is the username of the authenticated user.
	Username string

	// Account is the account name of the authenticated user.
	Account string

	// UniqueUserID is the unique user identifier of the authenticated user.
	UniqueUserID string

	// DisplayName is the display name of the authenticated user.
	DisplayName string

	// Password is the password of the authenticated user. Please ensure this is handled securely.
	Password string

	// Protocol is the protocol used by the client making the request.
	Protocol string

	// XSSL contains SSL information.
	XSSL string

	// XSSLSessionID is the SSL session identifier.
	XSSLSessionID string

	// XSSLClientVerify indicates whether SSL client is verified.
	XSSLClientVerify string

	// XSSLClientDN is the client's Distinguished Name in the SSL certificate.
	XSSLClientDN string

	// XSSLClientCN is the client's Common Name in the SSL certificate.
	XSSLClientCN string

	// XSSLIssuer is the issuer of the SSL certificate.
	XSSLIssuer string

	// XSSLClientNotBefore is the date before which the SSL certificate is not valid.
	XSSLClientNotBefore string

	// XSSLClientNotAfter is the date after which the SSL certificate is not valid.
	XSSLClientNotAfter string

	// XSSLSubjectDN is the Subject's Distinguished Name in the SSL certificate.
	XSSLSubjectDN string

	// XSSLIssuerDN is the Issuer's Distinguished Name in the SSL certificate.
	XSSLIssuerDN string

	// XSSLClientSubjectDN is the client's Subject Distinguished Name in the SSL certificate.
	XSSLClientSubjectDN string

	// XSSLClientIssuerDN is the client's Issuer Distinguished Name in the SSL certificate.
	XSSLClientIssuerDN string

	// XSSLProtocol is the SSL protocol used.
	XSSLProtocol string

	// XSSLCipher is the encryption cipher used in the SSL protocol.
	XSSLCipher string

	NginxBackendServers []map[string]int

	UsedNginxBackendAddress *string

	UsedNginxBackendPort *int

	// Log is used to capture logging information.
	Logs *lualib.CustomLogKeyValue

	// Context includes context data from the caller.
	*lualib.Context
}

Request represents a request made to the system. It contains various properties related to the request. The properties include debug mode, user information, authentication status, client information, local information, logging, and context data. The type also contains methods for executing Lua scripts within the context of the request. Debug is a flag that is set if running in debug mode. UserFound indicates whether a user has been found on the system. Authenticated indicates whether the user has authenticated. NoAuth indicates if any authentication method has no effect. Session is a GUID representing the user's session. ClientIP is the IP address of the client making the request. ClientPort is the port being used by the client making the request. ClientHost is the hostname of the client making the request. ClientID is a unique ID representing the client making the request. LocalIP is the local IP address the request is made to. LocalPort is the local port the request is made to. Username is the username of the authenticated user. Account is the account name of the authenticated user. UniqueUserID is the unique user identifier of the authenticated user. DisplayName is the display name of the authenticated user. Password is the password of the authenticated user. Please ensure this is handled securely. Protocol is the protocol used by the client making the request. NginxBackendServers is a list of Nginx backend servers used for monitoring. UsedNginxBackendAddress is the address of the Nginx backend server currently being used. UsedNginxBackendPort is the port of the Nginx backend server currently being used. Logs is used to capture logging information. Context includes context data from the caller. CallFilterLua executes Lua scripts within the context of the request. It returns the action flag indicating whether a script took action, and any errors encountered during script execution. logError logs the error encountered during Lua script execution. logResult logs the result of a Lua script execution. setGlobals sets the global variables for the Lua state. setRequest creates a Lua table representing the request properties. executeScriptWithinContext executes a Lua script within the context of the request.

func (*Request) CallFilterLua

func (r *Request) CallFilterLua(ctx *gin.Context) (action bool, err error)

CallFilterLua attempts to execute Lua scripts defined in LuaFilters. It returns true if at least one of the scripts executed successfully, otherwise it returns false. The error return value is used to indicate any issues with the Lua filters.

It initially checks if any LuaFilters are defined. If none are found, it returns false with an ErrNoFiltersDefined error. It then creates a new Lua state and sets up the necessary global variables and request context. Scripts from the LuaFilters are executed in sequence within the provided context until a script executes successfully or all scripts have been attempted. If the context has been cancelled, the function returns without executing any more scripts. If a script returns an error, it is skipped and the next script is tried.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL