-
Notifications
You must be signed in to change notification settings - Fork 0
/
cipher.go
56 lines (49 loc) · 1.11 KB
/
cipher.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
package ssr
import (
"net"
"strconv"
"github.com/josexy/mini-ss/cipher"
"github.com/josexy/mini-ss/ssr/obfs"
"github.com/josexy/mini-ss/ssr/protocol"
)
// SSRClientStreamCipher shadowsocks StreamCipher wrapper key
type SSRClientStreamCipher struct {
cipher.StreamCipher
Obfs obfs.Obfs
Proto protocol.Protocol
}
func NewSSRClientStreamCipher(ciph cipher.StreamCipher, addr string,
protocolName, protocolParam,
obfsName, obfsParam string) (*SSRClientStreamCipher, error) {
var key []byte
var ivSize int
if ciph != nil {
key = ciph.Key()
ivSize = ciph.IVSize()
}
host, p, _ := net.SplitHostPort(addr)
port, _ := strconv.ParseUint(p, 10, 16)
obfs, overHead, err := obfs.GetObfs(obfsName, &obfs.Base{
Host: host,
Port: int(port),
Key: key,
IVSize: ivSize,
Param: obfsParam,
})
if err != nil {
return nil, err
}
proto, err := protocol.GetProtocol(protocolName, &protocol.Base{
Key: key,
Overhead: overHead,
Param: protocolParam,
})
if err != nil {
return nil, err
}
return &SSRClientStreamCipher{
StreamCipher: ciph,
Obfs: obfs,
Proto: proto,
}, nil
}