chore: listeners support shadowsocks/vmess
This commit is contained in:
@ -87,10 +87,10 @@ func (o BaseOption) Equal(config C.InboundConfig) bool {
|
||||
}
|
||||
|
||||
func (o BaseOption) Additions() []inbound.Addition {
|
||||
return []inbound.Addition{{
|
||||
InName: o.NameStr,
|
||||
SpecialRules: o.SpecialRules,
|
||||
}}
|
||||
return []inbound.Addition{
|
||||
inbound.WithInName(o.NameStr),
|
||||
inbound.WithSpecialRules(o.SpecialRules),
|
||||
}
|
||||
}
|
||||
|
||||
var _ C.InboundConfig = (*BaseOption)(nil)
|
||||
|
79
listener/inbound/shadowsocks.go
Normal file
79
listener/inbound/shadowsocks.go
Normal file
@ -0,0 +1,79 @@
|
||||
package inbound
|
||||
|
||||
import (
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
LC "github.com/Dreamacro/clash/listener/config"
|
||||
"github.com/Dreamacro/clash/listener/sing_shadowsocks"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
)
|
||||
|
||||
type ShadowSocksOption struct {
|
||||
BaseOption
|
||||
Password string `inbound:"password"`
|
||||
Cipher string `inbound:"cipher"`
|
||||
}
|
||||
|
||||
func (o ShadowSocksOption) Equal(config C.InboundConfig) bool {
|
||||
return optionToString(o) == optionToString(config)
|
||||
}
|
||||
|
||||
type ShadowSocks struct {
|
||||
*Base
|
||||
config *ShadowSocksOption
|
||||
l C.MultiAddrListener
|
||||
}
|
||||
|
||||
func NewShadowSocks(options *ShadowSocksOption) (*ShadowSocks, error) {
|
||||
base, err := NewBase(&options.BaseOption)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ShadowSocks{
|
||||
Base: base,
|
||||
config: options,
|
||||
}, 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
|
||||
func (s *ShadowSocks) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
|
||||
var err error
|
||||
s.l, err = sing_shadowsocks.New(
|
||||
LC.ShadowsocksServer{
|
||||
Enable: true,
|
||||
Listen: s.RawAddress(),
|
||||
Password: s.config.Password,
|
||||
Cipher: s.config.Cipher,
|
||||
},
|
||||
tcpIn,
|
||||
udpIn,
|
||||
s.Additions()...,
|
||||
)
|
||||
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)
|
91
listener/inbound/vmess.go
Normal file
91
listener/inbound/vmess.go
Normal file
@ -0,0 +1,91 @@
|
||||
package inbound
|
||||
|
||||
import (
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
LC "github.com/Dreamacro/clash/listener/config"
|
||||
"github.com/Dreamacro/clash/listener/sing_vmess"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
)
|
||||
|
||||
type VmessOption struct {
|
||||
BaseOption
|
||||
Users []VmessUser `inbound:"users"`
|
||||
}
|
||||
|
||||
type VmessUser struct {
|
||||
Username string `inbound:"username,omitempty"`
|
||||
UUID string `inbound:"uuid"`
|
||||
AlterID int `inbound:"alterId"`
|
||||
}
|
||||
|
||||
func (o VmessOption) Equal(config C.InboundConfig) bool {
|
||||
return optionToString(o) == optionToString(config)
|
||||
}
|
||||
|
||||
type Vmess struct {
|
||||
*Base
|
||||
config *VmessOption
|
||||
l C.MultiAddrListener
|
||||
}
|
||||
|
||||
func NewVmess(options *VmessOption) (*Vmess, error) {
|
||||
base, err := NewBase(&options.BaseOption)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Vmess{
|
||||
Base: base,
|
||||
config: options,
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
// Config implements constant.InboundListener
|
||||
func (v *Vmess) Config() C.InboundConfig {
|
||||
return v.config
|
||||
}
|
||||
|
||||
// Address implements constant.InboundListener
|
||||
func (v *Vmess) Address() string {
|
||||
if v.l != nil {
|
||||
for _, addr := range v.l.AddrList() {
|
||||
return addr.String()
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Listen implements constant.InboundListener
|
||||
func (v *Vmess) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
|
||||
var err error
|
||||
users := make([]LC.VmessUser, len(v.config.Users))
|
||||
for i, v := range v.config.Users {
|
||||
users[i] = LC.VmessUser{
|
||||
Username: v.Username,
|
||||
UUID: v.UUID,
|
||||
AlterID: v.AlterID,
|
||||
}
|
||||
}
|
||||
v.l, err = sing_vmess.New(
|
||||
LC.VmessServer{
|
||||
Enable: true,
|
||||
Listen: v.RawAddress(),
|
||||
Users: users,
|
||||
},
|
||||
tcpIn,
|
||||
udpIn,
|
||||
v.Additions()...,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Infoln("Vmess[%s] proxy listening at: %s", v.Name(), v.Address())
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close implements constant.InboundListener
|
||||
func (v *Vmess) Close() error {
|
||||
return v.l.Close()
|
||||
}
|
||||
|
||||
var _ C.InboundListener = (*Vmess)(nil)
|
Reference in New Issue
Block a user