rsh

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2025 License: MIT Imports: 37 Imported by: 0

README

Remote shell over gRPC

Like ssh or kubectl exec, but over gRPC.

This library contains code to run remote commands using the gRPC framework. Apart from the library, client and server CLIs are included.

Features:

  • Execute shell commands or spawn an interactive shell on the server.
  • Interactive PTY sessions are used to run the commands.
  • Client is able to exit using the exit code of the remote command.

Usage

You'll need to have go > 1.18 installed.

  1. Create the server

    go run ./cmd/rsh/server
    
  2. Run the client

    # Spawn interactive shell
    go run ./cmd/rsh/client
    
    # Run command
    go run ./cmd/rsh/client -- ping 1.1.1.1 -c 3
    

Server and client use 127.0.0.1:22222 for the connections by default.

Building

To compile server and client binaries, run:

make

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeDuplicateSlice

func DeDuplicateSlice[T any](array []T) []T

数组去重

func GetAllGetParams

func GetAllGetParams(c *gin.Context) string

得到所有get参数

func GetAllPostParams

func GetAllPostParams(c *gin.Context) string

得到所有post参数

func GetNodeID

func GetNodeID() string

获取唯一ID

func HandleNotFound

func HandleNotFound(c *gin.Context)

404,找不到路径时的处理

func ReadStream

func ReadStream(stream pb.RemoteShell_SessionClient, stdout, stderr io.WriteCloser) (*int, error)

func Recover

func Recover(c *gin.Context)

500,内部发生异常时的处理

func WriteStream

func WriteStream(stream pb.RemoteShell_SessionClient, inc <-chan rune, sigc <-chan os.Signal)

Types

type CallFn

type CallFn func(context.Context, *Connection) error

Types

type Client

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

Client is the remote shell client.

func NewClientInsecure

func NewClientInsecure(server string) *Client

NewClientInsecure creates an insecure client.

func (*Client) Exec

func (c *Client) Exec(opts *ExecOptions) (*int, error)

Exec executes a command in the server.

func (*Client) ExecContext

func (c *Client) ExecContext(ctx context.Context, opts *ExecOptions) (*int, error)

ExecContext is like Exec, but with context.

type Connection

type Connection struct {
	*grpc.ClientConn
	// contains filtered or unexported fields
}

func (*Connection) Close

func (c *Connection) Close() error

type ConnectionManager

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

func NewConnectionManager

func NewConnectionManager(tlscfg *tls.Config) *ConnectionManager

func (*ConnectionManager) Call

func (m *ConnectionManager) Call(ctx context.Context, address string, fn CallFn) error

func (*ConnectionManager) Close

func (m *ConnectionManager) Close()

func (*ConnectionManager) CloseConnection

func (m *ConnectionManager) CloseConnection(address string) error

func (*ConnectionManager) Connect

func (m *ConnectionManager) Connect(ctx context.Context, address string) (*Connection, error)

type ExecOptions

type ExecOptions struct {
	Terminal       bool
	Command        string
	Args           []string
	CombinedOutput bool
}

ExecOptions are the options for Exec.

type Result

type Result struct {
	Ctx *gin.Context
}

放回结果

func NewResult

func NewResult(ctx *gin.Context) *Result

生成result

func (*Result) ErrorCode

func (r *Result) ErrorCode(code int, msg string, data interface{})

出错,接受code和msg

func (*Result) Success

func (r *Result) Success(data interface{})

成功

type ResultCont

type ResultCont struct {
	Status string      `json:"status"` //提示状态
	Code   int         `json:"code"`   //提示代码
	Msg    string      `json:"msg"`    //提示信息
	Data   interface{} `json:"data"`   //出错
}

返回的结果的内容:

type ReverseClient

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

ReverseClient is the local shell server.

func NewReverseClient

func NewReverseClient(address string, shell string, tlcfg *tls.Config, channelServer *grpctunnel.ReverseTunnelServer) *ReverseClient

NewReverseClient creates a new local shell client.

func (*ReverseClient) Dialer

func (s *ReverseClient) Dialer() func(context.Context, string) (net.Conn, error)

func (*ReverseClient) Serve

func (s *ReverseClient) Serve() error

Serve starts the server.

type ReverseServer

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

func NewReverseServer

func NewReverseServer(router *gin.Engine, tlscfg *tls.Config, allowClients []string) *ReverseServer

Reverse client. 集成在客户端的反向 shell(用于 grpc server 端调用 agent 侧 shell)

func (*ReverseServer) GetClient

func (s *ReverseServer) GetClient(id string) grpc.ClientConnInterface

func (*ReverseServer) RegisterHandlers

func (s *ReverseServer) RegisterHandlers()

type Server

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

Server is the remote shell server.

func NewServer

func NewServer(address string, shell string) *Server

NewServer creates a new remote shell server.

func (*Server) Serve

func (s *Server) Serve() error

Serve starts the server.

Directories

Path Synopsis
cmd
rsh/client command
rsh/server command

Jump to

Keyboard shortcuts

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