-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo.go.bak
103 lines (83 loc) · 2.87 KB
/
echo.go.bak
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
package main
import (
"crypto/rand"
"fmt"
"io"
"net"
"github.com/Howard0o0/shadowsocks-mini/encrypt"
"github.com/Howard0o0/shadowsocks-mini/tinylog"
"github.com/Howard0o0/shadowsocks-mini/util"
)
var codec encrypt.MetaCipher
func cypherTest() {
passwd := "2pKR1Kl2jayPXtbwT2GKd129GSOU64Cdv7ejfUZJ8+Ar/wgYc1hjla4fCsBrVB2gkKjf5tFQMbPjZ3tmTp+aZBZLLoS5hafGFzrb7c1F0P18QcriAmqcuyoe6cNlN38Q/iek9ldDOfLXR0BKAMjMsPWYjPHeBfccPHAT4e4t07Ul1aUzMIZTKAmOPRVVx3hCupv4iwf0D5ZfzkSq+cIEElrlO4hpEWKeA5myLGi4rzhR6ughogY1VuxxP4mDNNK82Xr7C8mmdA1tPkh+JimhGiB1xExcxS+rgVl5y02+Npdb725ggmxvG1LBItjntuQB/HIkDNyHrc/6FLEOk90ytA=="
codec, _ = encrypt.NewChacha20Codec([]byte(passwd))
nonce := make([]byte, codec.NonceSize())
rand.Read(nonce)
for i := 0; i < 10; i++ {
cypherText := codec.Seal(util.IntToBytes(i))
tinylog.LogInfo("plainText : %d, cypherTextLen : %d \n", i, len(cypherText))
decipher, _ := codec.Open(cypherText)
j := util.BytesToInt(decipher)
tinylog.LogInfo("decypherText : %d \n", j)
}
}
func ssserverTest() {
listenAddr := ":1998"
listener, err := net.Listen("tcp", listenAddr)
if err != nil {
tinylog.LogFatal("Listen error : %v \n", err)
}
passwd := "2pKR1Kl2jayPXtbwT2GKd129GSOU64Cdv7ejfUZJ8+Ar/wgYc1hjla4fCsBrVB2gkKjf5tFQMbPjZ3tmTp+aZBZLLoS5hafGFzrb7c1F0P18QcriAmqcuyoe6cNlN38Q/iek9ldDOfLXR0BKAMjMsPWYjPHeBfccPHAT4e4t07Ul1aUzMIZTKAmOPRVVx3hCupv4iwf0D5ZfzkSq+cIEElrlO4hpEWKeA5myLGi4rzhR6ughogY1VuxxP4mDNNK82Xr7C8mmdA1tPkh+JimhGiB1xExcxS+rgVl5y02+Npdb725ggmxvG1LBItjntuQB/HIkDNyHrc/6FLEOk90ytA=="
// codec, err = encrypt.NewCypherBookCodec(passwd)
// passwd := "howard5279"
codec, err = encrypt.NewChacha20Codec([]byte(passwd))
if err != nil {
tinylog.LogFatal("new codec error : %v \n", err)
}
for {
conn, err := listener.Accept()
if err != nil {
tinylog.LogError("accept error : %v \n", err)
conn.Close()
continue
}
cs, err := encrypt.NewCypherStream("chacha20", passwd, conn, false)
if err != nil {
tinylog.LogError("new cypherstream err : %s \n", err.Error())
conn.Close()
}
go HandleConn(cs, true)
}
}
func HandleConn(conn encrypt.CipherStreamer, needNonce bool) {
// plainText := []byte("hello!")
// cypherText := conn.codec.Encode(plainText)
// tinylog.LogDebug("cypherText : %v\n", cypherText)
// decypherText := conn.codec.Decode(cypherText)
// tinylog.LogDebug("cypherText : %s\n", string(decypherText))
defer conn.Close()
// readbuf := make([]byte, 4096)
var readbuf []byte
var err error
for {
readbuf, err = util.ReadAll(conn)
if err != nil {
if err != io.EOF {
fmt.Println("read error:", err)
}
break
}
if len(readbuf) == 0 {
continue
}
tinylog.LogInfo("receive : %v \n", string(readbuf))
resp := []byte(fmt.Sprintf("receive : %v", string(readbuf)))
tinylog.LogDebug("resp : %v\n", string(resp))
_, err = conn.Write(resp)
if err != nil {
tinylog.LogError("write error:%v", err)
break
}
}
}