|
@@ -37,49 +37,46 @@ func (e ProtocolError) Error() string {
|
37
|
37
|
}
|
38
|
38
|
|
39
|
39
|
// Query makes an Ident query, if timeout is >0 the query is timed out after that many seconds.
|
40
|
|
-func Query(ip string, portOnServer, portOnClient int, timeout float64) (Response, error) {
|
41
|
|
- var (
|
42
|
|
- conn net.Conn
|
43
|
|
- err error
|
44
|
|
- fields []string
|
45
|
|
- r *bufio.Reader
|
46
|
|
- resp string
|
47
|
|
- )
|
48
|
|
-
|
|
40
|
+func Query(ip string, portOnServer, portOnClient int, timeout time.Duration) (response Response, err error) {
|
|
41
|
+ var conn net.Conn
|
49
|
42
|
if timeout > 0 {
|
50
|
|
- conn, err = net.DialTimeout("tcp", net.JoinHostPort(ip, "113"), time.Duration(timeout)*time.Second)
|
|
43
|
+ conn, err = net.DialTimeout("tcp", net.JoinHostPort(ip, "113"), timeout)
|
51
|
44
|
} else {
|
52
|
45
|
conn, err = net.Dial("tcp", net.JoinHostPort(ip, "113"))
|
53
|
46
|
}
|
54
|
47
|
if err != nil {
|
55
|
|
- return Response{}, err
|
|
48
|
+ return
|
56
|
49
|
}
|
57
|
50
|
|
58
|
51
|
// stop the ident read after <timeout> seconds
|
59
|
52
|
if timeout > 0 {
|
60
|
|
- conn.SetDeadline(time.Now().Add(time.Second * time.Duration(timeout)))
|
|
53
|
+ conn.SetDeadline(time.Now().Add(timeout))
|
61
|
54
|
}
|
62
|
55
|
|
63
|
56
|
_, err = conn.Write([]byte(fmt.Sprintf("%d, %d", portOnClient, portOnServer) + "\r\n"))
|
64
|
57
|
if err != nil {
|
65
|
|
- return Response{}, err
|
|
58
|
+ return
|
66
|
59
|
}
|
67
|
60
|
|
68
|
|
- r = bufio.NewReader(conn)
|
69
|
|
- resp, err = r.ReadString('\n')
|
|
61
|
+ r := bufio.NewReaderSize(conn, 1024)
|
|
62
|
+ respBytes, err := r.ReadSlice('\n')
|
70
|
63
|
if err != nil {
|
71
|
|
- return Response{}, err
|
|
64
|
+ return
|
72
|
65
|
}
|
|
66
|
+ resp := string(respBytes)
|
73
|
67
|
|
74
|
|
- fields = strings.SplitN(strings.TrimSpace(resp), " : ", 4)
|
|
68
|
+ fields := strings.SplitN(resp, ":", 4)
|
75
|
69
|
if len(fields) < 3 {
|
76
|
|
- return Response{}, ProtocolError{resp}
|
|
70
|
+ return response, ProtocolError{resp}
|
|
71
|
+ }
|
|
72
|
+ for i, field := range fields {
|
|
73
|
+ fields[i] = strings.TrimSpace(field)
|
77
|
74
|
}
|
78
|
75
|
|
79
|
76
|
switch fields[1] {
|
80
|
77
|
case "USERID":
|
81
|
78
|
if len(fields) != 4 {
|
82
|
|
- return Response{}, ProtocolError{resp}
|
|
79
|
+ return response, ProtocolError{resp}
|
83
|
80
|
}
|
84
|
81
|
|
85
|
82
|
var os, charset string
|
|
@@ -99,10 +96,12 @@ func Query(ip string, portOnServer, portOnClient int, timeout float64) (Response
|
99
|
96
|
}, nil
|
100
|
97
|
case "ERROR":
|
101
|
98
|
if len(fields) != 3 {
|
102
|
|
- return Response{}, ProtocolError{resp}
|
|
99
|
+ return response, ProtocolError{resp}
|
103
|
100
|
}
|
104
|
101
|
|
105
|
|
- return Response{}, ResponseError{fields[2]}
|
|
102
|
+ return response, ResponseError{fields[2]}
|
|
103
|
+ default:
|
|
104
|
+ err = ProtocolError{resp}
|
106
|
105
|
}
|
107
|
|
- return Response{}, err
|
|
106
|
+ return
|
108
|
107
|
}
|