github.com/metacubex/mihomo@v1.18.5/listener/inbound/hysteria2.go (about)

     1  package inbound
     2  
     3  import (
     4  	C "github.com/metacubex/mihomo/constant"
     5  	LC "github.com/metacubex/mihomo/listener/config"
     6  	"github.com/metacubex/mihomo/listener/sing_hysteria2"
     7  	"github.com/metacubex/mihomo/log"
     8  )
     9  
    10  type Hysteria2Option struct {
    11  	BaseOption
    12  	Users                 map[string]string `inbound:"users,omitempty"`
    13  	Obfs                  string            `inbound:"obfs,omitempty"`
    14  	ObfsPassword          string            `inbound:"obfs-password,omitempty"`
    15  	Certificate           string            `inbound:"certificate"`
    16  	PrivateKey            string            `inbound:"private-key"`
    17  	MaxIdleTime           int               `inbound:"max-idle-time,omitempty"`
    18  	ALPN                  []string          `inbound:"alpn,omitempty"`
    19  	Up                    string            `inbound:"up,omitempty"`
    20  	Down                  string            `inbound:"down,omitempty"`
    21  	IgnoreClientBandwidth bool              `inbound:"ignore-client-bandwidth,omitempty"`
    22  	Masquerade            string            `inbound:"masquerade,omitempty"`
    23  	CWND                  int               `inbound:"cwnd,omitempty"`
    24  	UdpMTU                int               `inbound:"udp-mtu,omitempty"`
    25  	MuxOption             MuxOption         `inbound:"mux-option,omitempty"`
    26  }
    27  
    28  func (o Hysteria2Option) Equal(config C.InboundConfig) bool {
    29  	return optionToString(o) == optionToString(config)
    30  }
    31  
    32  type Hysteria2 struct {
    33  	*Base
    34  	config *Hysteria2Option
    35  	l      *sing_hysteria2.Listener
    36  	ts     LC.Hysteria2Server
    37  }
    38  
    39  func NewHysteria2(options *Hysteria2Option) (*Hysteria2, error) {
    40  	base, err := NewBase(&options.BaseOption)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	return &Hysteria2{
    45  		Base:   base,
    46  		config: options,
    47  		ts: LC.Hysteria2Server{
    48  			Enable:                true,
    49  			Listen:                base.RawAddress(),
    50  			Users:                 options.Users,
    51  			Obfs:                  options.Obfs,
    52  			ObfsPassword:          options.ObfsPassword,
    53  			Certificate:           options.Certificate,
    54  			PrivateKey:            options.PrivateKey,
    55  			MaxIdleTime:           options.MaxIdleTime,
    56  			ALPN:                  options.ALPN,
    57  			Up:                    options.Up,
    58  			Down:                  options.Down,
    59  			IgnoreClientBandwidth: options.IgnoreClientBandwidth,
    60  			Masquerade:            options.Masquerade,
    61  			CWND:                  options.CWND,
    62  			UdpMTU:                options.UdpMTU,
    63  			MuxOption:             options.MuxOption.Build(),
    64  		},
    65  	}, nil
    66  }
    67  
    68  // Config implements constant.InboundListener
    69  func (t *Hysteria2) Config() C.InboundConfig {
    70  	return t.config
    71  }
    72  
    73  // Address implements constant.InboundListener
    74  func (t *Hysteria2) Address() string {
    75  	if t.l != nil {
    76  		for _, addr := range t.l.AddrList() {
    77  			return addr.String()
    78  		}
    79  	}
    80  	return ""
    81  }
    82  
    83  // Listen implements constant.InboundListener
    84  func (t *Hysteria2) Listen(tunnel C.Tunnel) error {
    85  	var err error
    86  	t.l, err = sing_hysteria2.New(t.ts, tunnel, t.Additions()...)
    87  	if err != nil {
    88  		return err
    89  	}
    90  	log.Infoln("Hysteria2[%s] proxy listening at: %s", t.Name(), t.Address())
    91  	return nil
    92  }
    93  
    94  // Close implements constant.InboundListener
    95  func (t *Hysteria2) Close() error {
    96  	return t.l.Close()
    97  }
    98  
    99  var _ C.InboundListener = (*Hysteria2)(nil)