hysteria2.go 2.1 KB

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