-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
198 lines (173 loc) · 5.03 KB
/
main.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"log"
"time"
"bufio"
"fmt"
"os"
"os/exec"
"net/http"
_ "net/http/pprof"
"github.com/songgao/water"
)
var mCodec *fecCodec
var timeOutCount = 0
var mConfig Config
func main() {
go func() {
http.ListenAndServe(":8080", nil)
}()
fConfigPath := flag.String("c", "config.json", "config file path")
flag.Parse()
configPath := *fConfigPath
configFile, err := os.Open(configPath)
checkError(err)
defer configFile.Close()
configByte, _ := ioutil.ReadAll(configFile)
json.Unmarshal(configByte, &mConfig)
tun := createTUN("faketcp")
cmd := exec.Command("ip", "address", "add", "10.1.1.1/24", "dev", "faketcp")
cmd.Run()
cmd = exec.Command("ip", "link", "set", "up", "dev", "faketcp")
cmd.Run()
mCodec = newFecCodec(mConfig.SegCount, mConfig.FecCount, mConfig.FecCacheSize)
go func() {
if mConfig.Server {
//如果收到客户端要重连的请求,关闭其他操作之后执行握手
sh := newServerHandshak(tun)
sh.waitSyn() //这个不能在loop里,因为之所以重复循环是因为已经收到了syn包 不用再等了
for {
//握手
sh.sendSynAck()
sh.waitAck()
serverTuntoSocketChan := make(chan string)
serverSocketToQueueChan := make(chan string)
serverQueueToTunChan := make(chan string)
serverHeartBeatChan := make(chan string)
//心跳
shb := newServerHeartBeat()
go shb.start(serverHeartBeatChan)
//接收
if mConfig.EnableFEC {
go serverTunToSocketFEC(tun, serverTuntoSocketChan)
} else {
go serverTunToSocket(tun, serverTuntoSocketChan)
}
//发送
if mConfig.EnableFEC {
go serverSocketToQueueFEC(mConfig.ServerSocketTo, mConfig.ServerTunPort, serverSocketToQueueChan)
} else {
go serverSocketToQueueNoFEC(mConfig.ServerSocketTo, mConfig.ServerTunPort, serverSocketToQueueChan)
}
go serverQueueToTun(tun, serverQueueToTunChan)
<-serverTuntoSocketChan
serverStop()
<-serverSocketToQueueChan
<-serverQueueToTunChan
<-serverHeartBeatChan
}
} else {
mClt := newClient(tun)
//如果心跳出问题了,那么其他的go都要退出,重新开始进入握手状态
for {
//握手
chs := newClientHandshak(time.Duration(2)*time.Second, tun, mClt)
chs.startListen()
for {
chs.sendSYN()
time.Sleep(time.Duration(1) * time.Second)
if chs.checkConn() {
break
}
fmt.Println("client handshake retry")
}
chs.sendAck()
//发送和接收
chbRst := make(chan string)
clientTunToSocketChan := make(chan string)
clientSocketToQueueChan := make(chan string)
clientQueueToTunChan := make(chan string)
//接收
if mConfig.EnableFEC {
go mClt.tunToSocketFEC(clientTunToSocketChan)
} else {
go mClt.tunToSocketNoFEC(clientTunToSocketChan)
}
//发送
if mConfig.EnableFEC {
go mClt.socketToQueueFEC(clientSocketToQueueChan)
} else {
go mClt.socketToQueue(clientSocketToQueueChan)
}
go mClt.queueToTun(clientQueueToTunChan)
//心跳
chb := newClientHeartBeat()
go chb.start(chbRst)
//心跳出问题了
<-chbRst
//停掉所有的go
stopClient()
//等其他go都完事了,回到握手的状态
<-clientTunToSocketChan
<-clientSocketToQueueChan
<-clientQueueToTunChan
}
}
}()
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
if mConfig.Server {
fmt.Println("server drop ", serverDrop)
fmt.Println("server send ", serverSendCount)
fmt.Println("server recieve count ", serverReceiveCount)
} else {
// fmt.Println("client drop ", mClt.drop)
// fmt.Println("client send count ", mClt.sent)
// fmt.Println("client receive count ", mClt.recv)
}
fmt.Println("timeout count ", timeOutCount)
if mConfig.Report {
mCodec.dump()
}
}
func createTUN(name string) *water.Interface {
config := water.Config{
DeviceType: water.TUN,
}
config.Name = name
tunInterface, err := water.New(config)
if err != nil {
log.Fatal(err)
return nil
}
return tunInterface
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s\n", err.Error())
os.Exit(1)
}
}
//Config config
type Config struct {
Server bool `json:"server"`
TunInterfaceIP string `json:"tunInterfaceIP"`
TunSrcIP string `json:"TunSrcIP"`
TunSrcPort int `json:"tunSrcPort"`
QLen int `json:"qLen"`
EnableFEC bool `json:"enableFEC"`
SegCount int `json:"segCount"`
FecCount int `json:"fecCount"`
Gap int `json:"gap"`
FecCacheSize int `json:"fecCacheSize"`
StageTimeout int `json:"stageTimeout"`
Report bool `json:"report"`
ClientSocketListenPort string `json:"clientSocketListenPort"`
ClientTunToIP string `json:"clientTunToIP"`
ClientTunToPort int `json:"clientTunToPort"`
ServerTunPort int `json:"serverTunPort"`
ServerSocketTo string `json:"serverSocketTo"`
}