2022-12-05 10:12:53 +08:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
2023-11-03 21:01:45 +08:00
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
LC "github.com/metacubex/mihomo/listener/config"
|
|
|
|
"github.com/metacubex/mihomo/listener/sing_shadowsocks"
|
|
|
|
"github.com/metacubex/mihomo/log"
|
2022-12-05 10:12:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type ShadowSocksOption struct {
|
|
|
|
BaseOption
|
2023-11-23 08:20:26 +08:00
|
|
|
Password string `inbound:"password"`
|
|
|
|
Cipher string `inbound:"cipher"`
|
|
|
|
UDP bool `inbound:"udp,omitempty"`
|
|
|
|
MuxOption MuxOption `inbound:"mux-option,omitempty"`
|
2022-12-05 10:12:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o ShadowSocksOption) Equal(config C.InboundConfig) bool {
|
|
|
|
return optionToString(o) == optionToString(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ShadowSocks struct {
|
|
|
|
*Base
|
|
|
|
config *ShadowSocksOption
|
|
|
|
l C.MultiAddrListener
|
2022-12-05 17:43:50 +08:00
|
|
|
ss LC.ShadowsocksServer
|
2022-12-05 10:12:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewShadowSocks(options *ShadowSocksOption) (*ShadowSocks, error) {
|
|
|
|
base, err := NewBase(&options.BaseOption)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ShadowSocks{
|
|
|
|
Base: base,
|
|
|
|
config: options,
|
2022-12-05 17:43:50 +08:00
|
|
|
ss: LC.ShadowsocksServer{
|
2023-11-23 08:20:26 +08:00
|
|
|
Enable: true,
|
|
|
|
Listen: base.RawAddress(),
|
|
|
|
Password: options.Password,
|
|
|
|
Cipher: options.Cipher,
|
|
|
|
Udp: options.UDP,
|
|
|
|
MuxOption: options.MuxOption.Build(),
|
2022-12-05 17:43:50 +08:00
|
|
|
},
|
2022-12-05 10:12:53 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config implements constant.InboundListener
|
|
|
|
func (s *ShadowSocks) Config() C.InboundConfig {
|
|
|
|
return s.config
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address implements constant.InboundListener
|
|
|
|
func (s *ShadowSocks) Address() string {
|
|
|
|
if s.l != nil {
|
|
|
|
for _, addr := range s.l.AddrList() {
|
|
|
|
return addr.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen implements constant.InboundListener
|
2023-09-28 18:59:31 +08:00
|
|
|
func (s *ShadowSocks) Listen(tunnel C.Tunnel) error {
|
2022-12-05 10:12:53 +08:00
|
|
|
var err error
|
2023-09-28 18:59:31 +08:00
|
|
|
s.l, err = sing_shadowsocks.New(s.ss, tunnel, s.Additions()...)
|
2022-12-05 10:12:53 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Infoln("ShadowSocks[%s] proxy listening at: %s", s.Name(), s.Address())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements constant.InboundListener
|
|
|
|
func (s *ShadowSocks) Close() error {
|
|
|
|
return s.l.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ C.InboundListener = (*ShadowSocks)(nil)
|