-
Notifications
You must be signed in to change notification settings - Fork 0
/
netcat.go
57 lines (53 loc) · 1.01 KB
/
netcat.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
package main
import (
"bufio"
"fmt"
"io"
"log"
"net"
"os"
)
func main() {
fmt.Println("Please input server:")
var address string
fmt.Scan(&address)
if address == "" {
address = "localhost:9700"
}
conn, err := net.Dial("tcp", address)
defer func() {
_ = conn.Close()
}()
if err != nil {
log.Fatal(err)
}
tcpPacket := make(chan []byte)
go listenConn(conn, tcpPacket)
go sendMessage(conn, bufio.NewScanner(os.Stdin))
for bytes := range tcpPacket {
decryptedBytes, err := decrypt(bytes)
if err != nil {
log.Println(err)
}
os.Stdout.Write(append(decryptedBytes, '\n'))
}
log.Println("服务器关闭")
}
// 消息入口,所有入口消息都加密了
func sendMessage(conn io.Writer, scanner *bufio.Scanner) {
for scanner.Scan() {
var message string
message = scanner.Text()
if len(message) == 0 {
continue
}
encryptedMsg, err := encrypt([]byte(message))
if err != nil {
log.Fatal(err)
}
_, err = conn.Write(packet(encryptedMsg))
if err != nil {
log.Fatal(err)
}
}
}