github.com/ipfans/trojan-go@v0.11.0/common/common.go (about)

     1  package common
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/ipfans/trojan-go/log"
    10  )
    11  
    12  type Runnable interface {
    13  	Run() error
    14  	Close() error
    15  }
    16  
    17  func SHA224String(password string) string {
    18  	hash := sha256.New224()
    19  	hash.Write([]byte(password))
    20  	val := hash.Sum(nil)
    21  	str := ""
    22  	for _, v := range val {
    23  		str += fmt.Sprintf("%02x", v)
    24  	}
    25  	return str
    26  }
    27  
    28  func GetProgramDir() string {
    29  	dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
    30  	if err != nil {
    31  		log.Fatal(err)
    32  	}
    33  	return dir
    34  }
    35  
    36  func GetAssetLocation(file string) string {
    37  	if filepath.IsAbs(file) {
    38  		return file
    39  	}
    40  	if loc := os.Getenv("TROJAN_GO_LOCATION_ASSET"); loc != "" {
    41  		absPath, err := filepath.Abs(loc)
    42  		if err != nil {
    43  			log.Fatal(err)
    44  		}
    45  		log.Debugf("env set: TROJAN_GO_LOCATION_ASSET=%s", absPath)
    46  		return filepath.Join(absPath, file)
    47  	}
    48  	return filepath.Join(GetProgramDir(), file)
    49  }