这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go console is a go lib to implement consoles, it includes:
- Std output console
- Telnet console
- SSH console
- MQTT console (with lib mqtt-shell of freedeamer82)
- Other Consoles can be added easily(BLE,Serial etc..)


Expand Down Expand Up @@ -54,6 +55,20 @@ sshc, _ := console.NewSSHConsoleWithCertificates(sshPrivateKeyPath, sshAuthorize
go sshc.Start("localhost", sshPort, 2)
```


### Mqtt console example
```sh
func onNewConsole(console *console.Console) {
console.EnableLogin("root")
}

opt1 := console.WithOptionMqttConsoleMaxConnections(3)
opt2 := console.WithOptionMqttConsoleTimeout(timeoutSec * time.Second)
mqttConsole := console.NewMqttConsole(clientOps, "test", opt1, opt2)
mqttConsole.AddCallbackOnNewConsole(onNewConsole)
go mqttConsole.Start()
```

### go console api
handle operation
- func (c *Console) Start()
Expand Down Expand Up @@ -155,4 +170,7 @@ go build

//console ssh (pub keys)
./server 3

//console mqtt
./server 4
```
33 changes: 29 additions & 4 deletions examples/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package main

import (
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/freedreamer82/go-console/examples/utils"
"github.com/freedreamer82/go-console/pkg/console"
log "github.com/sirupsen/logrus"
"os"
"strconv"
"time"
Expand All @@ -19,7 +22,7 @@ var users = map[string]string{

const sshPrivateKeyPath = "examples/utils/server_rsa"
const sshPrivateKeyPassPhrase = "test"
const timeoutSec = 10
const timeoutSec = 20

//client1 and client2 are authorized
const sshAuthorizedKeysPath = "examples/utils/authorized_keys"
Expand All @@ -29,10 +32,11 @@ const (
Telnet
SSHPassword
SSHPublicKey
Mqtt
)

//change this const to start another console
const example = StdOutput
const example = Mqtt

func main() {

Expand All @@ -51,11 +55,13 @@ func main() {
startSSHPasswordConsole()
case SSHPublicKey:
startSSHPublicKeyConsole()
case Mqtt:
startMqttConsole()
}

}

func onNewTelnetConsole(console *console.Console) {
func onNewConsole(console *console.Console) {
console.EnableLogin("root")
}

Expand All @@ -73,7 +79,7 @@ func startTelnetConsole() {
fmt.Printf("opening Telnet console on localhost %d", telnetPort)
fmt.Println()
ct := console.NewTelnetConsole(telnetPort, 2)
ct.AddCallbackOnNewConsole(onNewTelnetConsole)
ct.AddCallbackOnNewConsole(onNewConsole)
ct.SetTimeout(timeoutSec * time.Second)
select {}
}
Expand All @@ -97,3 +103,22 @@ func startSSHPublicKeyConsole() {
go sshc.Start("localhost", sshPort, 2)
select {}
}

func startMqttConsole() {
fmt.Printf("opening Mqtt console")
fmt.Println()
clientOps := mqtt.NewClientOptions()
addr := fmt.Sprintf("tcp://%s:%d", utils.BrokerHost, utils.BrokerPort)
log.Info("Connecting to : " + addr)
clientOps.AddBroker(addr)
if utils.BrokerUser != "" && utils.BrokerPassword != "" {
clientOps.SetUsername(utils.BrokerUser)
clientOps.SetPassword(utils.BrokerPassword)
}
opt1 := console.WithOptionMqttConsoleMaxConnections(3)
opt2 := console.WithOptionMqttConsoleTimeout(timeoutSec * time.Second)
mqttConsole := console.NewMqttConsole(clientOps, "test", opt1, opt2)
mqttConsole.AddCallbackOnNewConsole(onNewConsole)
go mqttConsole.Start()
select {}
}
6 changes: 6 additions & 0 deletions examples/utils/brokerconf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package utils

const BrokerHost = ""
const BrokerPort = 1883
const BrokerUser = ""
const BrokerPassword = ""
11 changes: 8 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
module github.com/freedreamer82/go-console
module github.com/freedreamer82/go-console

go 1.18

require (
github.com/eclipse/paho.mqtt.golang v1.4.1
github.com/freedreamer82/mqtt-shell v0.0.0-20220624081242-fdcac9e75f9d
github.com/lithammer/shortuuid/v3 v3.0.7
github.com/sirupsen/logrus v1.8.1
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9
golang.org/x/term v0.0.0-20220411215600-e5f449aeb171
)

require (
github.com/google/uuid v1.2.0 // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
golang.org/x/net v0.0.0-20220607020251-c690dde0001d // indirect
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d // indirect
)
15 changes: 14 additions & 1 deletion pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,15 @@ type Console struct {
uuid string
}

func NewConsole(iorw ConsoleI) *Console {
type ConsoleOption func(console *Console)

func WithOptionCustomUUID(uuid string) ConsoleOption {
return func(console *Console) {
console.uuid = uuid
}
}

func NewConsole(iorw ConsoleI, opts ...ConsoleOption) *Console {

c := Console{term: terminal.NewTerminal(iorw, prompt), eol: eol, mask: 0,
welcome: defaultWelcome, userLevel: Root, iorw: iorw, onclose: nil}
Expand All @@ -86,6 +94,10 @@ func NewConsole(iorw ConsoleI) *Console {
c.timeout = 0
c.lastActivitytime = time.Now()

for _, opt := range opts {
opt(&c)
}

c.AddCallbackOnClose(c.dummyCb)
log.Printf("Open Console %s", c.uuid)
return &c
Expand Down Expand Up @@ -184,6 +196,7 @@ func (c *Console) handleLogin(cmd string) bool {
if cmd == c.password {
c.mask.ToggleFlag(USER_LOGGED)
c.enablePrompt(true)
c.Print("Authenticated")
return true
}
return false
Expand Down
Loading