-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcommon.go
49 lines (43 loc) · 888 Bytes
/
common.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
package common
import (
"crypto/sha256"
"fmt"
"os"
"path/filepath"
"github.com/p4gefau1t/trojan-go/log"
)
type Runnable interface {
Run() error
Close() error
}
func SHA224String(password string) string {
hash := sha256.New224()
hash.Write([]byte(password))
val := hash.Sum(nil)
str := ""
for _, v := range val {
str += fmt.Sprintf("%02x", v)
}
return str
}
func GetProgramDir() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
return dir
}
func GetAssetLocation(file string) string {
if filepath.IsAbs(file) {
return file
}
if loc := os.Getenv("TROJAN_GO_LOCATION_ASSET"); loc != "" {
absPath, err := filepath.Abs(loc)
if err != nil {
log.Fatal(err)
}
log.Debugf("env set: TROJAN_GO_LOCATION_ASSET=%s", absPath)
return filepath.Join(absPath, file)
}
return filepath.Join(GetProgramDir(), file)
}