forked from shadowsocks/shadowsocks-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
60 lines (51 loc) · 1.22 KB
/
util.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
package shadowsocks
import (
"crypto/hmac"
"crypto/sha1"
"encoding/binary"
"errors"
"fmt"
"os"
)
func PrintVersion() {
const version = "1.2.1"
fmt.Println("shadowsocks-go version", version)
}
func IsFileExists(path string) (bool, error) {
stat, err := os.Stat(path)
if err == nil {
if stat.Mode()&os.ModeType == 0 {
return true, nil
}
return false, errors.New(path + " exists but is not regular file")
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func HmacSha1(key []byte, data []byte) []byte {
hmacSha1 := hmac.New(sha1.New, key)
hmacSha1.Write(data)
return hmacSha1.Sum(nil)[:10]
}
func otaConnectAuth(iv, key, data []byte) []byte {
return append(data, HmacSha1(append(iv, key...), data)...)
}
func otaReqChunkAuth(iv []byte, chunkId uint32, data []byte) []byte {
nb := make([]byte, 2)
binary.BigEndian.PutUint16(nb, uint16(len(data)))
chunkIdBytes := make([]byte, 4)
binary.BigEndian.PutUint32(chunkIdBytes, chunkId)
header := append(nb, HmacSha1(append(iv, chunkIdBytes...), data)...)
return append(header, data...)
}
type ClosedFlag struct {
flag bool
}
func (flag *ClosedFlag) SetClosed() {
flag.flag = true
}
func (flag *ClosedFlag) IsClosed() bool {
return flag.flag
}