github.com/xraypb/xray-core@v1.6.6/infra/conf/mtproto.go (about) 1 package conf 2 3 import ( 4 "encoding/hex" 5 "encoding/json" 6 7 "github.com/golang/protobuf/proto" 8 "github.com/xraypb/xray-core/common/protocol" 9 "github.com/xraypb/xray-core/common/serial" 10 "github.com/xraypb/xray-core/proxy/mtproto" 11 ) 12 13 type MTProtoAccount struct { 14 Secret string `json:"secret"` 15 } 16 17 // Build implements Buildable 18 func (a *MTProtoAccount) Build() (*mtproto.Account, error) { 19 if len(a.Secret) != 32 { 20 return nil, newError("MTProto secret must have 32 chars") 21 } 22 secret, err := hex.DecodeString(a.Secret) 23 if err != nil { 24 return nil, newError("failed to decode secret: ", a.Secret).Base(err) 25 } 26 return &mtproto.Account{ 27 Secret: secret, 28 }, nil 29 } 30 31 type MTProtoServerConfig struct { 32 Users []json.RawMessage `json:"users"` 33 } 34 35 func (c *MTProtoServerConfig) Build() (proto.Message, error) { 36 config := &mtproto.ServerConfig{} 37 38 if len(c.Users) == 0 { 39 return nil, newError("zero MTProto users configured.") 40 } 41 config.User = make([]*protocol.User, len(c.Users)) 42 for idx, rawData := range c.Users { 43 user := new(protocol.User) 44 if err := json.Unmarshal(rawData, user); err != nil { 45 return nil, newError("invalid MTProto user").Base(err) 46 } 47 account := new(MTProtoAccount) 48 if err := json.Unmarshal(rawData, account); err != nil { 49 return nil, newError("invalid MTProto user").Base(err) 50 } 51 accountProto, err := account.Build() 52 if err != nil { 53 return nil, newError("failed to parse MTProto user").Base(err) 54 } 55 user.Account = serial.ToTypedMessage(accountProto) 56 config.User[idx] = user 57 } 58 59 return config, nil 60 } 61 62 type MTProtoClientConfig struct{} 63 64 func (c *MTProtoClientConfig) Build() (proto.Message, error) { 65 config := new(mtproto.ClientConfig) 66 return config, nil 67 }