gonsole

package module
v0.0.0-...-3d0ed7e Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2023 License: Apache-2.0 Imports: 34 Imported by: 3

README

gonsole

基于websocket的远程控制台系统


0x1 简述

历数各类软件系统,你会发现每一个牛B的系统都会自带一个控制台,用于观察系统状态和调整系统参数,比如Linux, MySQL等。

  1. 支持自定义command:server.RegisterCommand(cmd)
  2. 支持自定义topic,订阅后可周期性推送数据到控制台:server.RegisterTopic(topic)
  3. 安全验证:对于关键的系统命令,可以设置cmd.IsPublic=false,这一类命令只能使用auth验证后才能使用
  4. 历史命令:输入history查看历史命令,输入 !98 执行历史命令列表中的第98命令
  5. Tab键命令补全

0x2 基本命令图示
01 帮助中心 help
02 日志列表 log.list
03 命令输入框

0x3 Demo
  1. 直接运行examples/demo/main.go
  2. 在浏览器中输入 http://127.0.0.1:8888/console
  3. 按提示在文件框中输入help命令,查看帮助信息
  4. 可以通过查看main.go的源代码,学习如何注册command和topic

部分代码如下:

func main() {
	var webPort = 8888
	var mux = http.NewServeMux()
	var server = gonsole.NewServer(mux,
		gonsole.WithPort(webPort),                                      // webserver端口
		gonsole.WithPageTemplate("console.html"),                       // 页面文件模板
		gonsole.WithUserPasswords(map[string]string{"xmli": "123456"}), // 认证使用的用户名密码
		gonsole.WithEnablePProf(true),                                  // 开启pprof
	)

	server.RegisterCommand(&gonsole.Command{
		Name:     "hi",
		Note:     "打印 hi console",
		IsPublic: false,
		Handler: func(client *gonsole.Client, texts [] string) {
			var bean struct {
				Text string
			}

			bean.Text = "hello world"
			client.SendBean(bean)
		},
	})
}

0x4 Road Map
  1. 引入完整的登录验证方式
  2. 将项目中的js逐步过渡为Vue框架, 目标是梳理代码框架, 通过import减小代码单元的大小
  3. 逐步使用typescript代替javascript, 引入编译机制
  4. 升级golang以引入泛型机制. 但这件事情在centos的yum默认支持到1.18+之前不能考虑. 目前(2022-09-03) 最新版本是golang 1.19。(centos目前官方不再更新)
  5. 逐步移除gaio这个库, 在golang 1.17+的centos上编译会报错, 它升级太慢了. 相关issue
  6. 引入对https的支持, 或者至少设计出完整的支持方案. 在golang库中直接支持可能比在nginx上支持要更下简单一些, 毕竟会减少对nginx的依赖. 另外, gaio这个库似乎不支持https

0x5 感谢JetBrains的免费License支持

Documentation

Index

Constants

View Source
const (
	FlagPublic    = 0x0002 // 不需要登录就可以使用的命令
	FlagInvisible = 0x0004 // 在inputBox中无法看到和使用的命令
)

Variables

View Source
var AppBuildTime string // 应用构建时间: date +%Y-%m-%dT%H:%M:%S
View Source
var GitBranchName string // git分支名: git rev-parse --abbrev-ref HEAD

GitBranchName 参考:《编译时向 go 程序写入 git 版本信息》 http://mengqi.info/html/2015/201502171941-build-go-program-with-git-version.html

IMPORT_PATH=github.com/lixianmin/gonsole FLAGS="-w -s -X $IMPORT_PATH.GitBranchName=`git rev-parse --abbrev-ref HEAD` -X $IMPORT_PATH.GitCommitId=`git log --pretty=format:\"%h\" -1` -X '$IMPORT_PATH.GitCommitMessage=`git show -s --format=%s`' -X $IMPORT_PATH.GitCommitTime=`git log --date=format:'%Y-%m-%dT%H:%M:%S' --pretty=format:%ad -1` -X $IMPORT_PATH.AppBuildTime=`date +%Y-%m-%dT%H:%M:%S`" go build -ldflags "$FLAGS" -mod vendor -gcflags "-N -l"

View Source
var GitCommitId string // git提交id: git log --pretty=format:\"%h\" -1
View Source
var GitCommitMessage string // git提交的message: git show -s --format=%s
View Source
var GitCommitTime string // git提交的时间: git log --date=format:'%Y-%m-%dT%H:%M:%S' --pretty=format:%ad -1

Functions

func ToHtmlTable

func ToHtmlTable(data interface{}) string

func ToSnakeName

func ToSnakeName(name string) string

Types

type Client

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

func (*Client) Attachment

func (client *Client) Attachment() road.Attachment

func (*Client) OnClosed

func (client *Client) OnClosed(callback func())

func (*Client) PushDefault

func (client *Client) PushDefault(v interface{})

func (*Client) Session

func (client *Client) Session() road.Session

type Command

type Command struct {
	loom.Flag                                                        // command的flag
	Name      string                                                 // 名称
	Note      string                                                 // 描述
	Handler   func(client *Client, args []string) (*Response, error) // 处理方法
}

func (*Command) GetName

func (cmd *Command) GetName() string

func (*Command) GetNote

func (cmd *Command) GetNote() string

func (*Command) IsBuiltin

func (cmd *Command) IsBuiltin() bool

func (*Command) IsInvisible

func (cmd *Command) IsInvisible() bool

func (*Command) IsPublic

func (cmd *Command) IsPublic() bool

func (*Command) Run

func (cmd *Command) Run(client *Client, args []string) (*Response, error)

type ConsoleService

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

func (*ConsoleService) Command

func (my *ConsoleService) Command(ctx context.Context, request *commandRqt) (*Response, error)

func (*ConsoleService) Hint

func (my *ConsoleService) Hint(ctx context.Context, request *hintRqt) ([]byte, error)

func (*ConsoleService) Sub

func (my *ConsoleService) Sub(ctx context.Context, request *subRqt) (*Response, error)

func (*ConsoleService) Unsub

func (my *ConsoleService) Unsub(ctx context.Context, request *subRqt) (*Response, error)

type IServeMux

type IServeMux interface {
	HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
}

type Response

type Response struct {
	Operation string      `json:"op"`
	Data      interface{} `json:"data"`
}

func NewDefaultResponse

func NewDefaultResponse(data interface{}) *Response

func NewEmptyResponse

func NewEmptyResponse() *Response

func NewHtmlResponse

func NewHtmlResponse(data string) *Response

func NewTableResponse

func NewTableResponse(table interface{}) *Response

type Server

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

func NewServer

func NewServer(mux IServeMux, opts ...ServerOption) *Server

func (*Server) App

func (server *Server) App() *epoll.App

func (*Server) ConsoleUrl

func (server *Server) ConsoleUrl() string

func (*Server) GPID

func (server *Server) GPID() string

func (*Server) RegisterCommand

func (server *Server) RegisterCommand(cmd *Command)

func (*Server) RegisterService

func (server *Server) RegisterService(name string, service component.Component)

func (*Server) RegisterTopic

func (server *Server) RegisterTopic(topic *Topic)

type ServerOption

type ServerOption func(*serverOptions)

func WithAutoLoginTime

func WithAutoLoginTime(d time.Duration) ServerOption

WithAutoLoginTime 如果手动登录过,则在时限内自动登录

func WithDeadlockIgnores

func WithDeadlockIgnores(ignores []string) ServerOption

WithDeadlockIgnores 死锁检查时可以忽略的调用字符串

func WithDirectory

func WithDirectory(path string) ServerOption

WithDirectory 项目根目录,表现在url中

func WithEnablePProf

func WithEnablePProf(enable bool) ServerOption

WithEnablePProf 激活pprof

func WithLogListRoot

func WithLogListRoot(path string) ServerOption

WithLogListRoot log.list命令显示的日志文件根目录

func WithPageBody

func WithPageBody(body string) ServerOption

WithPageBody 主页(console.html)主体

func WithPageTemplate

func WithPageTemplate(path string) ServerOption

WithPageTemplate 主页(console.html)模板文件的路径名

func WithPageTitle

func WithPageTitle(title string) ServerOption

WithPageTitle 主页(console.html)标题

func WithPort

func WithPort(port int) ServerOption

WithPort 服务器端口

func WithUserPasswords

func WithUserPasswords(passwords map[string]string) ServerOption

WithUserPasswords 可以登陆的用户名与密码

func WithWebSocketPath

func WithWebSocketPath(path string) ServerOption

WithWebSocketPath websocket监听的路径

type Topic

type Topic struct {
	loom.Flag
	Name          string           // 名称
	Note          string           // 描述
	Interval      time.Duration    // 推送周期
	BuildResponse func() *Response // 创建数据
	// contains filtered or unexported fields
}

func (*Topic) GetName

func (topic *Topic) GetName() string

func (*Topic) GetNote

func (topic *Topic) GetNote() string

func (*Topic) IsBuiltin

func (topic *Topic) IsBuiltin() bool

func (*Topic) IsInvisible

func (topic *Topic) IsInvisible() bool

func (*Topic) IsPublic

func (topic *Topic) IsPublic() bool

Directories

Path Synopsis
web
web.vue

Jump to

Keyboard shortcuts

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