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