hysteria2.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package v4
  2. import (
  3. "github.com/golang/protobuf/proto"
  4. "github.com/v2fly/v2ray-core/v5/common/protocol"
  5. "github.com/v2fly/v2ray-core/v5/common/serial"
  6. "github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon"
  7. "github.com/v2fly/v2ray-core/v5/proxy/hysteria2"
  8. )
  9. // Hysteria2ServerTarget is configuration of a single hysteria2 server
  10. type Hysteria2ServerTarget struct {
  11. Address *cfgcommon.Address `json:"address"`
  12. Port uint16 `json:"port"`
  13. Email string `json:"email"`
  14. Level byte `json:"level"`
  15. }
  16. // Hysteria2ClientConfig is configuration of hysteria2 servers
  17. type Hysteria2ClientConfig struct {
  18. Servers []*Hysteria2ServerTarget `json:"servers"`
  19. }
  20. // Build implements Buildable
  21. func (c *Hysteria2ClientConfig) Build() (proto.Message, error) {
  22. config := new(hysteria2.ClientConfig)
  23. if len(c.Servers) == 0 {
  24. return nil, newError("0 Hysteria2 server configured.")
  25. }
  26. serverSpecs := make([]*protocol.ServerEndpoint, len(c.Servers))
  27. for idx, rec := range c.Servers {
  28. if rec.Address == nil {
  29. return nil, newError("Hysteria2 server address is not set.")
  30. }
  31. if rec.Port == 0 {
  32. return nil, newError("Invalid Hysteria2 port.")
  33. }
  34. account := &hysteria2.Account{}
  35. hysteria2 := &protocol.ServerEndpoint{
  36. Address: rec.Address.Build(),
  37. Port: uint32(rec.Port),
  38. User: []*protocol.User{
  39. {
  40. Level: uint32(rec.Level),
  41. Email: rec.Email,
  42. Account: serial.ToTypedMessage(account),
  43. },
  44. },
  45. }
  46. serverSpecs[idx] = hysteria2
  47. }
  48. config.Server = serverSpecs
  49. return config, nil
  50. }
  51. // Hysteria2ServerConfig is Inbound configuration
  52. type Hysteria2ServerConfig struct{}
  53. // Build implements Buildable
  54. func (c *Hysteria2ServerConfig) Build() (proto.Message, error) {
  55. config := new(hysteria2.ServerConfig)
  56. return config, nil
  57. }