-
Notifications
You must be signed in to change notification settings - Fork 5
/
connection.go
176 lines (151 loc) · 4.97 KB
/
connection.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package tcpip
import (
"bytes"
"encoding/binary"
"fmt"
"log"
"syscall"
"time"
)
func calcSequenceNumber(packet []byte, add uint32) []byte {
var sum uint32
sum = binary.BigEndian.Uint32(packet) + add
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, sum)
return b
}
func SetSockAddrInet4(destIp []byte, destPort int) syscall.SockaddrInet4 {
return syscall.SockaddrInet4{
Addr: [4]byte{destIp[0], destIp[1], destIp[2], destIp[3]},
Port: destPort,
}
}
func startConnectionFromEth(sendfd int, tcpip TCPIP) (TCPIP, error) {
localif, _ := getLocalIpAddr("lo")
synPacket := NewPacket(tcpip)
destIp := localif.LocalIpAddr
destPort := UintTo2byte(tcpip.DestPort)
addr := syscall.SockaddrLinklayer{
Protocol: syscall.ETH_P_IP,
Ifindex: localif.Index,
}
err := SendRaw(sendfd, synPacket, addr)
if err != nil {
return TCPIP{}, fmt.Errorf("Send SYN packet err : %v\n", err)
}
fmt.Println("Send SYN packet")
synack := SocketRecvfromEth(sendfd, destIp, destPort)
var ack TCPIP
// 0x12 = SYNACK, 0x11 = FINACK, 0x10 = ACK
if synack.ControlFlags[0] == SYNACK || synack.ControlFlags[0] == FINACK || synack.ControlFlags[0] == ACK {
// SYNACKに対してACKを送り返す
ack = TCPIP{
DestIP: tcpip.DestIP,
DestPort: tcpip.DestPort,
TcpFlag: "ACK",
SeqNumber: synack.AcknowlegeNumber,
AckNumber: calcSequenceNumber(synack.SequenceNumber, 1),
}
ackPacket := NewPacket(ack)
err = SendRaw(sendfd, ackPacket, addr)
if err != nil {
return TCPIP{}, fmt.Errorf("Send ACK packet err : %v\n", err)
}
}
return ack, nil
}
func StartTCPConnection(sendfd int, tcpip TCPIP) (TCPIP, error) {
synPacket := NewTCPIP(tcpip)
destIp := Iptobyte(tcpip.DestIP)
destPort := UintTo2byte(tcpip.DestPort)
addr := SetSockAddrInet4(destIp, int(tcpip.DestPort))
// SYNを送る
err := SendIPv4Socket(sendfd, synPacket, addr)
if err != nil {
return TCPIP{}, fmt.Errorf("Send SYN packet err : %v\n", err)
}
// SYNACKを受け取る
synack := RecvIPSocket(sendfd, destIp, destPort)
var ack TCPIP
// 0x12 = SYNACK, 0x11 = FINACK, 0x10 = ACK
if synack.ControlFlags[0] == SYNACK || synack.ControlFlags[0] == FINACK { //|| synack.ControlFlags[0] == ACK {
fmt.Printf("Recv SYNACK|FINACK %x from : %s\n", synack.ControlFlags, tcpip.DestIP)
// SYNACKに対してACKを送り返す
ack = TCPIP{
DestIP: tcpip.DestIP,
DestPort: tcpip.DestPort,
TcpFlag: "ACK",
SeqNumber: synack.AcknowlegeNumber,
AckNumber: calcSequenceNumber(synack.SequenceNumber, 1),
}
ackPacket := NewTCPIP(ack)
err = SendIPv4Socket(sendfd, ackPacket, addr)
fmt.Printf("Send ACK to : %s\n", tcpip.DestIP)
if err != nil {
return TCPIP{}, fmt.Errorf("Send ACK packet err : %v\n", err)
}
}
return ack, nil
}
func SendToNginx(sendfd int, tcpip TCPIP) {
pshPacket := NewTCPIP(tcpip)
destIp := Iptobyte(tcpip.DestIP)
destPort := UintTo2byte(tcpip.DestPort)
addr := SetSockAddrInet4(destIp, int(tcpip.DestPort))
// httpリクエストを送る
err := SendIPv4Socket(sendfd, pshPacket, addr)
if err != nil {
log.Fatalf("Send PSH packet err : %v\n", err)
}
var serverPshack TCPHeader
var tolalLength uint32
for {
recvBuf := make([]byte, 1500)
_, _, err := syscall.Recvfrom(sendfd, recvBuf, 0)
if err != nil {
log.Fatalf("read err : %v", err)
}
// IPヘッダのProtocolがTCPであることをチェック
if recvBuf[9] == 0x06 && bytes.Equal(recvBuf[16:20], destIp) && bytes.Equal(recvBuf[20:22], destPort) {
tolalLength = uint32(sumByteArr(recvBuf[2:4]))
// IPヘッダを省いて20byte目からのTCPパケットをパースする
serverPshack = parseTCP(recvBuf[20:])
if serverPshack.ControlFlags[0] == PSHACK {
fmt.Println("recv PSHACK from server")
fmt.Printf("----- print HTTP Renponse -----\n")
fmt.Printf("%s\n\n", string(serverPshack.TCPData))
time.Sleep(100 * time.Millisecond)
tcpLength := tolalLength - 20
tcpLength = tcpLength - uint32(serverPshack.HeaderLength[0]>>4<<2)
ack := TCPIP{
DestIP: tcpip.DestIP,
DestPort: tcpip.DestPort,
TcpFlag: "ACK",
SeqNumber: serverPshack.AcknowlegeNumber,
AckNumber: calcSequenceNumber(serverPshack.SequenceNumber, tcpLength),
}
ackPacket := NewTCPIP(ack)
// HTTPを受信したことに対してACKを送る
SendIPv4Socket(sendfd, ackPacket, addr)
//time.Sleep(100 * time.Millisecond)
fmt.Println("Send ACK to server")
} else if serverPshack.ControlFlags[0] == FINACK { //FIN ACKであれば
fmt.Println("recv FINACK from server")
finack := TCPIP{
DestIP: tcpip.DestIP,
DestPort: tcpip.DestPort,
TcpFlag: "FINACK",
SeqNumber: serverPshack.AcknowlegeNumber,
AckNumber: calcSequenceNumber(serverPshack.SequenceNumber, 1),
}
send_finackPacket := NewTCPIP(finack)
SendIPv4Socket(sendfd, send_finackPacket, addr)
fmt.Println("Send FINACK to server")
time.Sleep(100 * time.Millisecond)
// FINACKを送ったら終了なのでbreakスルー
break
}
}
}
syscall.Close(sendfd)
}