config.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package trojan
  2. import (
  3. "crypto/sha256"
  4. "encoding/hex"
  5. "fmt"
  6. "google.golang.org/protobuf/proto"
  7. "github.com/xtls/xray-core/common"
  8. "github.com/xtls/xray-core/common/protocol"
  9. )
  10. // MemoryAccount is an account type converted from Account.
  11. type MemoryAccount struct {
  12. Password string
  13. Key []byte
  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. }, nil
  23. }
  24. // Equals implements protocol.Account.Equals().
  25. func (a *MemoryAccount) Equals(another protocol.Account) bool {
  26. if account, ok := another.(*MemoryAccount); ok {
  27. return a.Password == account.Password
  28. }
  29. return false
  30. }
  31. func (a *MemoryAccount) ToProto() proto.Message {
  32. return &Account{
  33. Password: a.Password,
  34. }
  35. }
  36. func hexSha224(password string) []byte {
  37. buf := make([]byte, 56)
  38. hash := sha256.New224()
  39. common.Must2(hash.Write([]byte(password)))
  40. hex.Encode(buf, hash.Sum(nil))
  41. return buf
  42. }
  43. func hexString(data []byte) string {
  44. str := ""
  45. for _, v := range data {
  46. str += fmt.Sprintf("%02x", v)
  47. }
  48. return str
  49. }