-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility.go
92 lines (85 loc) · 2.4 KB
/
utility.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
package main
import (
"crypto/tls"
"fmt"
"github.com/brucespang/go-tcpinfo"
"github.com/gorilla/websocket"
"log"
"net"
"net/url"
"reflect"
"strconv"
"strings"
"time"
"unsafe"
)
type TimedTCPInfo struct {
MsgId int32
Timestamp time.Time
TcpInfo *tcpinfo.TCPInfo
}
func connect() *websocket.Conn {
addrParts := strings.Split(address, "/")
pathString := ""
for _, part := range addrParts[1:] {
pathString += "/" + part
}
var conn *websocket.Conn
if *https {
conf := &tls.Config{InsecureSkipVerify: true}
dialer := websocket.Dialer{
TLSClientConfig: conf,
HandshakeTimeout: 10 * time.Second,
}
if *srcPort != 0 {
dialer.NetDialContext = (&net.Dialer{LocalAddr: &net.TCPAddr{Port: *srcPort}}).DialContext
}
u := url.URL{Scheme: "wss", Host: addrParts[0], Path: pathString + "/echo"}
c, _, err := dialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("dial: ", err)
}
conn = c
} else {
dialer := websocket.Dialer{HandshakeTimeout: 10 * time.Second}
if *srcPort != 0 {
dialer.NetDialContext = (&net.Dialer{LocalAddr: &net.TCPAddr{Port: *srcPort}}).DialContext
}
u := url.URL{Scheme: "ws", Host: addrParts[0], Path: "/echo"}
c, _, err := dialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("dial: ", err)
}
conn = c
}
_ = conn.WriteMessage(websocket.TextMessage, []byte(strconv.FormatUint(*responseBytes, 10)))
return conn
}
func getTimestamp() time.Time {
return time.Now()
}
func printLogs() {
fmt.Println("Repetitions:\t\t", *reps)
fmt.Println("Request Bytes:\t\t", *requestBytes)
fmt.Println("Response Bytes:\t\t", *responseBytes)
fmt.Println("Send Interval:\t\t", *interval)
fmt.Println("TLS enabled:\t\t", *https)
fmt.Println("Traceroute IP:\t", *tracerouteIp)
fmt.Println("TCP Stats enabled:\t", *sockOpt)
fmt.Println("Address:\t\t", address)
fmt.Println()
}
func getTCPConnFromWebsocketConn(conn *websocket.Conn) *net.TCPConn {
if *https {
return getConnFromTLSConn(conn.UnderlyingConn().(*tls.Conn)).(*net.TCPConn)
} else {
return conn.UnderlyingConn().(*net.TCPConn)
}
}
// to get the internal wrapped connection from the tls.Conn
func getConnFromTLSConn(tlsConn *tls.Conn) net.Conn {
// awful workaround until https://github.com/golang/go/issues/29257 is solved.
conn := reflect.ValueOf(tlsConn).Elem().FieldByName("conn")
conn = reflect.NewAt(conn.Type(), unsafe.Pointer(conn.UnsafeAddr())).Elem()
return conn.Interface().(net.Conn)
}