-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
echoclient.go
69 lines (65 loc) · 2.32 KB
/
echoclient.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
// Copyright (c) 2016-present Cloud <cloud@txthinking.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package brook
import (
"fmt"
)
func EchoClient(server string, times int) error {
c, err := DialTCP("tcp", "", server)
if err != nil {
return err
}
defer c.Close()
var b [1024 * 2]byte
for i := 0; i < times; i++ {
if _, err := c.Write([]byte(c.LocalAddr().String())); err != nil {
return err
}
i, err := c.Read(b[:])
if err != nil {
return err
}
if c.LocalAddr().String() == string(b[:i]) {
fmt.Printf("TCP: src:%s -> dst:%s\n", c.LocalAddr().String(), c.RemoteAddr().String())
fmt.Printf("TCP: dst:%s <- src:%s\n", c.LocalAddr().String(), c.RemoteAddr().String())
}
if c.LocalAddr().String() != string(b[:i]) {
fmt.Printf("TCP: src:%s -> dst:proxy -> src:proxy -> dst:%s\n", c.LocalAddr().String(), c.RemoteAddr().String())
fmt.Printf("TCP: dst:%s <- src:proxy <- dst:%s <- src:%s\n", c.LocalAddr().String(), string(b[:i]), c.RemoteAddr().String())
}
}
c1, err := DialUDP("udp", "", server)
if err != nil {
return err
}
defer c1.Close()
for i := 0; i < times; i++ {
if _, err := c1.Write([]byte(c1.LocalAddr().String())); err != nil {
return err
}
i, err := c1.Read(b[:])
if err != nil {
return err
}
if c1.LocalAddr().String() == string(b[:i]) {
fmt.Printf("UDP: src:%s -> dst:%s\n", c1.LocalAddr().String(), c1.RemoteAddr().String())
fmt.Printf("UDP: dst:%s <- src:%s\n", c1.LocalAddr().String(), c1.RemoteAddr().String())
}
if c1.LocalAddr().String() != string(b[:i]) {
fmt.Printf("UDP: src:%s -> dst:proxy -> src:proxy -> dst:%s\n", c1.LocalAddr().String(), c1.RemoteAddr().String())
fmt.Printf("UDP: dst:%s <- src:proxy <- dst:%s <- src:%s\n", c1.LocalAddr().String(), string(b[:i]), c1.RemoteAddr().String())
}
}
return nil
}