forked from flashmob/go-guerrilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
49 lines (44 loc) · 1.23 KB
/
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
package test
import (
"bufio"
"crypto/tls"
"errors"
"fmt"
"github.com/scrapbird/go-guerrilla"
"net"
"time"
)
func Connect(serverConfig guerrilla.ServerConfig, deadline time.Duration) (net.Conn, *bufio.Reader, error) {
var bufin *bufio.Reader
var conn net.Conn
var err error
if serverConfig.TLS.AlwaysOn {
// start tls automatically
conn, err = tls.Dial("tcp", serverConfig.ListenInterface, &tls.Config{
InsecureSkipVerify: true,
ServerName: "127.0.0.1",
})
} else {
conn, err = net.Dial("tcp", serverConfig.ListenInterface)
}
if err != nil {
// handle error
//t.Error("Cannot dial server", config.Servers[0].ListenInterface)
return conn, bufin, errors.New("Cannot dial server: " + serverConfig.ListenInterface + "," + err.Error())
}
bufin = bufio.NewReader(conn)
// should be ample time to complete the test
if err = conn.SetDeadline(time.Now().Add(time.Second * deadline)); err != nil {
return conn, bufin, err
}
// read greeting, ignore it
_, err = bufin.ReadString('\n')
return conn, bufin, err
}
func Command(conn net.Conn, bufin *bufio.Reader, command string) (reply string, err error) {
_, err = fmt.Fprintln(conn, command+"\r")
if err == nil {
return bufin.ReadString('\n')
}
return "", err
}