/
glas.go
139 lines (117 loc) · 3.26 KB
/
glas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package glas // import "github.com/ingcr3at1on/glas"
import (
"context"
"fmt"
"io"
"strings"
"sync"
"github.com/ingcr3at1on/glas/internal"
pb "github.com/ingcr3at1on/glas/proto"
"github.com/pkg/errors"
telnet "github.com/reiver/go-telnet"
)
var (
welcome = strings.Join([]string{
`\ \ / /__| | ___ ___ _ __ ___ ___ | |_ ___ / ___| | __ _ ___`,
" \\ \\ /\\ / / _ \\ |/ __/ _ \\| '_ ` _ \\ / _ \\ | __/ _ \\ | | _| |/ _` / __|",
` \ V V / __/ | (_| (_) | | | | | | __/ | || (_) | | |_| | | (_| \__ \_`,
` \_/\_/ \___|_|\___\___/|_| |_| |_|\___| \__\___/ \____|_|\__,_|___( )`,
` |/`,
` _ _ _`,
` __ _ _ __ _____ ___ __ ___ _ __(_)_ __ ___ ___ _ __ | |_ __ _| |`,
" / _` | '_ \\ / _ \\ \\/ / '_ \\ / _ \\ '__| | '_ ` _ \\ / _ \\ '_ \\| __/ _` | |",
`| (_| | | | | | __/> <| |_) | __/ | | | | | | | | __/ | | | || (_| | |`,
` \__,_|_| |_| \___/_/\_\ .__/ \___|_| |_|_| |_| |_|\___|_| |_|\__\__,_|_|`,
` |_|`,
` __ __ _ _ ____ _ _ _`,
`| \/ | | | | _ \ ___| (_) ___ _ __ | |_`,
`| |\/| | | | | | | | / __| | |/ _ \ '_ \| __|`,
`| | | | |_| | |_| | | (__| | | __/ | | | |_`,
`|_| |_|\___/|____/ \___|_|_|\___|_| |_|\__|`,
}, "\n")
)
type (
// Glas is a mud client backend.
Glas struct {
config *Config
errCh chan error
terrCh chan error
pipeW *io.PipeWriter
pipeR *io.PipeReader
connected bool
}
)
// New returns a new instance of Glas.
func New(config *Config) (*Glas, error) {
if err := config.Validate(); err != nil {
return nil, errors.Wrap(err, "config.Validate")
}
g := Glas{
config: config,
errCh: make(chan error, 1),
terrCh: make(chan error),
}
g.pipeR, g.pipeW = io.Pipe()
return &g, nil
}
// Start the Glas client.
func (g *Glas) Start(ctx context.Context, cancel context.CancelFunc) error {
if ctx == nil {
return ErrNilContext
}
if cancel == nil {
return ErrNilCancelF
}
g.config.Output <- &pb.Output{Data: welcome}
// Add help and mention it here...
g.config.Output <- &pb.Output{Data: fmt.Sprintf("\n\nThe current command prefix is '%s'\n", g.config.CmdPrefix)}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
if err := g.routineHandleInput(ctx, cancel); err != nil {
g.errCh <- errors.Wrap(err, "routineHandleInput")
}
// fmt.Println("routineHandleInput finished")
}()
select {
case <-ctx.Done():
break
case err := <-g.errCh:
if err != nil {
return err
}
case err := <-g.terrCh:
if err != nil {
g.config.Output <- &pb.Output{Data: fmt.Sprintf("%s\n", err.Error())}
}
}
wg.Wait()
return nil
}
func (g *Glas) startTelnet(addr string) {
// FIXME: support tls
conn, err := telnet.DialTo(addr)
if err != nil {
g.terrCh <- err
return
}
defer conn.Close()
caller, err := internal.NewCaller(&internal.CallerConfig{
In: g.pipeR,
Out: g.config.Output,
}, g.terrCh)
if err != nil {
g.terrCh <- err
return
}
client := telnet.Client{
Caller: caller,
}
g.connected = true
err = client.Call(conn)
if err != nil {
g.terrCh <- errors.Wrapf(err, "client.Call : %s", conn.RemoteAddr().String())
return
}
}