-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
340 lines (298 loc) · 8.29 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package main
import "C"
import (
"bytes"
"encoding/base64"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"strconv"
"strings"
"time"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/google/uuid"
)
type Client struct {
Cli *oss.Client
Bucket *oss.Bucket
Endpoint string
AccessKeyId string
AccessKeySecret string
BucketName string
}
var Service *Client
// var c *oss.Bucket
func InitClient(endPoint, accessKeyId, accessKeySecret, bucketName string) error {
var ossClient *oss.Client
var err error
ossClient, err = oss.New(endPoint, accessKeyId, accessKeySecret)
if err != nil {
return err
}
var ossBucket *oss.Bucket
ossBucket, err = ossClient.Bucket(bucketName)
if err != nil {
return err
}
Service = &Client{
Cli: ossClient,
Bucket: ossBucket,
Endpoint: endPoint,
AccessKeyId: accessKeyId,
AccessKeySecret: accessKeySecret,
BucketName: bucketName,
}
return nil
}
var server_address string
var bind_address string
func main() {
var osskey = flag.String("osskey", "", "format: endpoint:accessKeyId:accessKeySecret:bucketName")
var mode = flag.String("mode", "", "client/server 二选一")
var address = flag.String("address", "", "监听地址或者目标地址,格式:127.0.0.1:8080")
// var proxy = flag.String("proxy", "", "代理服务器上网:http://127.0.0.1:8080,如果有密码:http://x.x.x.x,user,pass")
flag.Parse()
timeout = 30
server_address = *address
bind_address = *address
str1 := strings.Split(*osskey, ":")
// str2 := strings.Split(*proxy, ",")
// fmt.Println(str2)
if *mode == "" || *osskey == "" {
flag.PrintDefaults()
os.Exit(0)
}
InitClient(str1[0], str1[1], str1[2], str1[3])
if *mode == "client" {
startClient()
} else if *mode == "server" {
startServer()
}
}
func startServer() {
log.Println("[+]", "服务端启动成功")
for {
time.Sleep(1 * time.Second)
for _, c2 := range List(Service) {
if strings.Contains(c2.Key, "client.txt") {
go process_server(c2.Key)
}
}
}
}
func List(c *Client) []oss.ObjectProperties {
lsRes, err := c.Bucket.ListObjects(oss.MaxKeys(3), oss.Prefix(""))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// fmt.Println(lsRes)
return lsRes.Objects
}
var timeout int
func startClient() {
log.Println("[+]", "客户端启动成功")
server, err := net.Listen("tcp", bind_address)
if err != nil {
log.Fatalln("[x]", "listen address ["+bind_address+"] faild.")
}
for {
conn, err := server.Accept()
if err != nil {
log.Println("Accept() failed, err: ", err)
continue
}
log.Println("[+]", "有客户进入:", conn.RemoteAddr())
go process(conn)
}
}
func process(conn net.Conn) {
uuid := uuid.New()
key := uuid.String()
defer conn.Close() // 关闭连接
var buffer bytes.Buffer
_ = conn.SetReadDeadline(time.Now().Add(10 * time.Second))
for {
var buf [1]byte
n, err := conn.Read(buf[:])
if err != nil {
log.Println("[-]", uuid, "read from connect failed, err:", err)
break
}
buffer.Write(buf[:n])
if strings.Contains(buffer.String(), "\r\n\r\n") {
//fmt.Println("\n---------DEBUG CLIENT------\n", buffer.String(), "\n----------------------")
if strings.Contains(buffer.String(), "Content-Length") {
ContentLength := buffer.String()[strings.Index(buffer.String(), "Content-Length: ")+len("Content-Length: ") : strings.Index(buffer.String(), "Content-Length: ")+strings.Index(buffer.String()[strings.Index(buffer.String(), "Content-Length: "):], "\n")]
log.Println("[+]", uuid, "数据包长度为:", strings.TrimSpace(ContentLength))
if strings.TrimSpace(ContentLength) != "0" {
intContentLength, err := strconv.Atoi(strings.TrimSpace(ContentLength))
if err != nil {
log.Println("[-]", uuid, "Content-Length转换失败")
}
for i := 1; i <= intContentLength; i++ {
var b [1]byte
n, err = conn.Read(b[:])
if err != nil {
log.Println("[-]", uuid, "read from connect failed, err", err)
break
}
buffer.Write(b[:n])
}
}
}
if strings.Contains(buffer.String(), "Transfer-Encoding: chunked") {
for {
var b [1]byte
n, err = conn.Read(b[:])
if err != nil {
log.Println("[-]", uuid, "read from connect failed, err", err)
break
}
buffer.Write(b[:n])
if strings.Contains(buffer.String(), "0\r\n\r\n") {
break
}
}
}
log.Println("[+]", uuid, "从客户端接受HTTP头完毕")
break
}
}
b64 := base64.StdEncoding.EncodeToString(buffer.Bytes())
Send(Service, key+"/client.txt", b64)
i := 1
for {
i++
time.Sleep(1 * time.Second)
if i >= timeout {
log.Println("[x]", "超时,断开")
Del(Service, key+"/client.txt")
return
}
buff := Get(Service, key+"/server.txt")
if buff != nil {
log.Println("[x]", uuid, "收到服务器消息")
//fmt.Println(buff)
Del(Service, key+"/server.txt")
sDec, err := base64.StdEncoding.DecodeString(string(buff))
//fmt.Println(sDec)
if err != nil {
log.Println("[x]", uuid, "Base64解码错误")
return
}
conn.Write(sDec)
break
}
}
log.Println("[+]", "发送完成")
}
// // var name string = "1111.txt"
// // var content string = "测试数据"
func Send(c *Client, name string, content string) {
// 1.通过字符串上传对象
f := strings.NewReader(content)
// var err error
err := c.Bucket.PutObject(name, f)
if err != nil {
log.Println("[-]", "上传失败")
return
}
}
func Get(c *Client, name string) []byte {
println(name)
body, err := c.Bucket.GetObject(name)
if err != nil {
return nil
}
// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
defer body.Close()
// println(body)
data, err := ioutil.ReadAll(body)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// fmt.Println(data)
return data
}
func Del(c *Client, name string) {
err := c.Bucket.DeleteObject(name)
if err != nil {
panic(err)
}
}
func process_server(name string) {
uuid := name[:strings.Index(name, "/")]
log.Println("[+]", "发现客户端:"+uuid)
buff := Get(Service, name)
sDec, err := base64.StdEncoding.DecodeString(string(buff))
Del(Service, name)
conn, err := net.Dial("tcp", server_address)
if err != nil {
log.Println("[-]", uuid, "连接CS服务器失败")
return
}
defer conn.Close()
_, err = conn.Write(sDec)
if err != nil {
log.Println("[-]", uuid, "无法向CS服务器发送数据包")
return
}
_ = conn.SetReadDeadline(time.Now().Add(10 * time.Second))
var buffer bytes.Buffer
for {
var buf [1]byte
n, err := conn.Read(buf[:])
if err != nil {
log.Println("[-]", uuid, "read from connect failed, err", err)
break
}
buffer.Write(buf[:n])
if strings.Contains(buffer.String(), "\r\n\r\n") {
//fmt.Println("\n---------DEBUG SERVER------", buffer.String(), "\n----------------------")
if strings.Contains(buffer.String(), "Content-Length") {
ContentLength := buffer.String()[strings.Index(buffer.String(), "Content-Length: ")+len("Content-Length: ") : strings.Index(buffer.String(), "Content-Length: ")+strings.Index(buffer.String()[strings.Index(buffer.String(), "Content-Length: "):], "\n")]
log.Println("[+]", uuid, "数据包长度为:", strings.TrimSpace(ContentLength))
if strings.TrimSpace(ContentLength) != "0" {
intContentLength, err := strconv.Atoi(strings.TrimSpace(ContentLength))
if err != nil {
log.Println("[-]", uuid, "Content-Length转换失败")
}
for i := 1; i <= intContentLength; i++ {
var b [1]byte
n, err = conn.Read(b[:])
if err != nil {
log.Println("[-]", uuid, "read from connect failed, err", err)
break
}
buffer.Write(b[:n])
}
}
}
if strings.Contains(buffer.String(), "Transfer-Encoding: chunked") {
for {
var b [1]byte
n, err = conn.Read(b[:])
if err != nil {
log.Println("[-]", uuid, "read from connect failed, err", err)
break
}
buffer.Write(b[:n])
if strings.Contains(buffer.String(), "0\r\n\r\n") {
break
}
}
}
log.Println("[+]", uuid, "从CS服务器接受完毕")
break
}
}
b64 := base64.StdEncoding.EncodeToString(buffer.Bytes())
Send(Service, uuid+"/server.txt", b64)
log.Println("[+]", uuid, "服务器数据发送完毕")
return
}