rcebot

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2023 License: AGPL-3.0 Imports: 17 Imported by: 0

README

Cubic RCE Bot

Test Release AUR version

Execute commands on a remote host via a Telegram bot.

Overview

Configuration examples and systemd unit files can be found in the docs directory.

  • Only authorized users can execute allowed commands.
  • Configuration can be reloaded by sending a SIGUSR1 signal to the process.

License

AGPLv3

Documentation

Index

Constants

View Source
const (
	// DefaultExecTimeout is the default command execution timeout.
	DefaultExecTimeout = 15 * time.Second

	// DefaultExitTimeout is the default command exit timeout.
	DefaultExitTimeout = 5 * time.Second
)

Variables

View Source
var Commands = []tele.Command{
	{
		Text:        "start",
		Description: "Get started with the bot",
	},
	{
		Text:        "list",
		Description: "List commands authorized for you to request execution",
	},
	{
		Text:        "exec",
		Description: "Execute an authorized command at the specified index",
	},
	{
		Text:        "cancel",
		Description: "Cancel a running command at the specified index",
	},
}

Functions

func EscapeMarkdownV2CodeBlock

func EscapeMarkdownV2CodeBlock(s string) string

EscapeMarkdownV2CodeBlock escapes the given string for use in a MarkdownV2 code block.

func EscapeMarkdownV2Plaintext

func EscapeMarkdownV2Plaintext(s string) string

EscapeMarkdownV2Plaintext escapes the given string for use in a MarkdownV2 plaintext.

func HandleStart

func HandleStart(c tele.Context) error

HandleStart handles the `/start` command.

Types

type Command added in v1.1.0

type Command struct {
	// Name is the command name.
	Name string `json:"name"`

	// Args is the list of command arguments.
	Args []string `json:"args"`

	// ExecTimeoutSec is the command execution timeout in seconds.
	// When command execution exceeds this timeout, an interrupt signal is sent to the process.
	// If the process does not exit within [ExitTimeoutSec], it is terminated.
	//
	// If zero, [DefaultExecTimeout] is used.
	ExecTimeoutSec int `json:"execTimeoutSec"`

	// ExitTimeoutSec is the command exit timeout in seconds.
	// When command execution exceeds [ExecTimeoutSec], an interrupt signal is sent to the process.
	// If the process does not exit within this timeout, it is terminated.
	//
	// If zero, [DefaultExitTimeout] is used.
	ExitTimeoutSec int `json:"exitTimeoutSec"`
	// contains filtered or unexported fields
}

Command is an authorized command.

type CommandOutputResponseBuilder

type CommandOutputResponseBuilder struct {
	// contains filtered or unexported fields
}

CommandOutputResponseBuilder is a reusable builder for command output responses.

func (*CommandOutputResponseBuilder) Build

func (rb *CommandOutputResponseBuilder) Build(output []byte, err error) string

Build uses the builder's internal buffer to build a response for the given command output. The returned string is only valid until the next call to Build.

type Config

type Config struct {
	// Token is the bot token.
	Token string `json:"token"`

	// URL is the custom bot API URL.
	// Leave empty to use the official bot API.
	URL string `json:"url"`

	// Users is the list of authorized users.
	Users []User `json:"users"`
}

Config is the configuration for the bot.

func (Config) UserCommandsByID

func (c Config) UserCommandsByID() map[int64][]Command

UserCommandsByID returns a map of user ID to list of commands.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler handles bot commands.

func (*Handler) HandleCancel added in v1.1.0

func (h *Handler) HandleCancel(c tele.Context) error

HandleCancel handles the `/cancel` command.

func (*Handler) HandleExec

func (h *Handler) HandleExec(c tele.Context) error

HandleExec handles the `/exec` command.

func (*Handler) HandleList

func (h *Handler) HandleList(c tele.Context) error

HandleList handles the `/list` command.

func (*Handler) ReplaceUserCommandsByID

func (h *Handler) ReplaceUserCommandsByID(m map[int64][]Command)

ReplaceUserCommandsByID replaces the user commands map.

func (*Handler) SetCommand added in v1.1.0

func (h *Handler) SetCommand(next tele.HandlerFunc) tele.HandlerFunc

SetCommand is a middleware that parses the specified command index and adds the command and its index to the context. It short-circuits the command handler if the index is invalid.

func (*Handler) SetContext added in v1.1.0

func (h *Handler) SetContext(ctx context.Context)

SetContext sets the context.

func (*Handler) SetUserCommands added in v1.1.0

func (h *Handler) SetUserCommands(next tele.HandlerFunc) tele.HandlerFunc

SetUserCommands is a middleware that adds the user's list of authorized commands to the context. It short-circuits the command handler if the user is not authorized to execute any commands.

func (*Handler) Wait added in v1.1.0

func (h *Handler) Wait()

Wait waits for all running commands to finish.

type Runner

type Runner struct {
	// Config is the bot configuration.
	Config Config

	// Handler is the bot handler.
	Handler Handler
	// contains filtered or unexported fields
}

Runner loads the configuration and creates a handler.

func NewRunner

func NewRunner(configPath string, logger *zap.Logger) (*Runner, error)

NewRunner creates a new runner.

func (*Runner) Start

func (r *Runner) Start(ctx context.Context) error

Start starts the runner.

func (*Runner) Wait added in v1.1.0

func (r *Runner) Wait()

Wait waits for the runner to finish.

type User

type User struct {
	// ID is the Telegram user ID.
	ID int64 `json:"id"`

	// Commands is the list of commands the user is allowed to execute.
	Commands []Command `json:"commands"`
}

User is an authorized user.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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