-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli_client.go
executable file
·81 lines (63 loc) · 1.14 KB
/
cli_client.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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"net"
"os"
"strconv"
)
type CliClient struct {
Host string
Port int
Conn net.Conn
Reader *bufio.Reader
}
func NewCliClient(host string, port int) (*CliClient, error) {
conn, err := net.Dial("tcp4", host+":"+strconv.Itoa(port))
if err != nil {
return nil, err
}
reader := bufio.NewReader(os.Stdin)
cli := &CliClient{
Host: host,
Port: port,
Conn: conn,
Reader: reader,
}
return cli, nil
}
func (client *CliClient) listen() {
for {
fmt.Printf("%s:%d> ", client.Host, client.Port)
cmd, _ := client.Reader.ReadString('\n')
if cmd == "" {
continue
}
result := client.sendCommand(cmd)
fmt.Println(result)
}
}
func (client *CliClient) sendCommand(cmd string) string {
// Send Command
client.Conn.Write([]byte(cmd + "\n"))
// Receive command
reader := bufio.NewReader(client.Conn)
var buffer bytes.Buffer
for {
data, err := reader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
break
}
fmt.Print(err)
break
}
if string(data) == TUNNEL_RADAR_EOF {
break
}
buffer.Write(data)
}
return buffer.String() + "\r\n"
}