config.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package trojan
  2. import (
  3. "crypto/sha256"
  4. "encoding/hex"
  5. fmt "fmt"
  6. "github.com/xtls/xray-core/common"
  7. "github.com/xtls/xray-core/common/protocol"
  8. )
  9. // MemoryAccount is an account type converted from Account.
  10. type MemoryAccount struct {
  11. Password string
  12. Key []byte
  13. Flow string
  14. }
  15. // AsAccount implements protocol.AsAccount.
  16. func (a *Account) AsAccount() (protocol.Account, error) {
  17. password := a.GetPassword()
  18. key := hexSha224(password)
  19. return &MemoryAccount{
  20. Password: password,
  21. Key: key,
  22. Flow: a.Flow,
  23. }, nil
  24. }
  25. // Equals implements protocol.Account.Equals().
  26. func (a *MemoryAccount) Equals(another protocol.Account) bool {
  27. if account, ok := another.(*MemoryAccount); ok {
  28. return a.Password == account.Password
  29. }
  30. return false
  31. }
  32. func hexSha224(password string) []byte {
  33. buf := make([]byte, 56)
  34. hash := sha256.New224()
  35. common.Must2(hash.Write([]byte(password)))
  36. hex.Encode(buf, hash.Sum(nil))
  37. return buf
  38. }
  39. func hexString(data []byte) string {
  40. str := ""
  41. for _, v := range data {
  42. str += fmt.Sprintf("%02x", v)
  43. }
  44. return str
  45. }