chore: shadowsocks listener support the "udp" setting

This commit is contained in:
wwqgtxx 2023-03-21 12:40:36 +08:00
parent 154fbb34ea
commit 0336435ebc
6 changed files with 44 additions and 36 deletions

View File

@ -9,6 +9,7 @@ type ShadowsocksServer struct {
Listen string
Password string
Cipher string
Udp bool
}
func (t ShadowsocksServer) String() string {

View File

@ -11,6 +11,7 @@ type ShadowSocksOption struct {
BaseOption
Password string `inbound:"password"`
Cipher string `inbound:"cipher"`
UDP bool `inbound:"udp,omitempty"`
}
func (o ShadowSocksOption) Equal(config C.InboundConfig) bool {
@ -37,6 +38,7 @@ func NewShadowSocks(options *ShadowSocksOption) (*ShadowSocks, error) {
Listen: base.RawAddress(),
Password: options.Password,
Cipher: options.Cipher,
Udp: options.UDP,
},
}, nil
}

View File

@ -271,6 +271,7 @@ func ReCreateShadowSocks(shadowSocksConfig string, tcpIn chan<- C.ConnContext, u
Listen: addr,
Password: password,
Cipher: cipher,
Udp: true,
}
}

View File

@ -73,7 +73,7 @@ func ParseListener(mapping map[string]any) (C.InboundListener, error) {
}
listener, err = IN.NewTun(tunOption)
case "shadowsocks":
shadowsocksOption := &IN.ShadowSocksOption{}
shadowsocksOption := &IN.ShadowSocksOption{UDP: true}
err = decoder.Decode(mapping, shadowsocksOption)
if err != nil {
return nil, err

View File

@ -33,12 +33,14 @@ func New(config LC.ShadowsocksServer, tcpIn chan<- C.ConnContext, udpIn chan<- C
for _, addr := range strings.Split(config.Listen, ",") {
addr := addr
//UDP
ul, err := NewUDP(addr, pickCipher, udpIn)
if err != nil {
return nil, err
if config.Udp {
//UDP
ul, err := NewUDP(addr, pickCipher, udpIn)
if err != nil {
return nil, err
}
sl.udpListeners = append(sl.udpListeners, ul)
}
sl.udpListeners = append(sl.udpListeners, ul)
//TCP
l, err := inbound.Listen("tcp", addr)

View File

@ -76,37 +76,39 @@ func New(config LC.ShadowsocksServer, tcpIn chan<- C.ConnContext, udpIn chan<- C
for _, addr := range strings.Split(config.Listen, ",") {
addr := addr
//UDP
ul, err := net.ListenPacket("udp", addr)
if err != nil {
return nil, err
}
err = sockopt.UDPReuseaddr(ul.(*net.UDPConn))
if err != nil {
log.Warnln("Failed to Reuse UDP Address: %s", err)
}
sl.udpListeners = append(sl.udpListeners, ul)
go func() {
conn := bufio.NewPacketConn(ul)
for {
buff := buf.NewPacket()
remoteAddr, err := conn.ReadPacket(buff)
if err != nil {
buff.Release()
if sl.closed {
break
}
continue
}
_ = sl.service.NewPacket(context.TODO(), conn, buff, metadata.Metadata{
Protocol: "shadowsocks",
Source: remoteAddr,
})
if config.Udp {
//UDP
ul, err := net.ListenPacket("udp", addr)
if err != nil {
return nil, err
}
}()
err = sockopt.UDPReuseaddr(ul.(*net.UDPConn))
if err != nil {
log.Warnln("Failed to Reuse UDP Address: %s", err)
}
sl.udpListeners = append(sl.udpListeners, ul)
go func() {
conn := bufio.NewPacketConn(ul)
for {
buff := buf.NewPacket()
remoteAddr, err := conn.ReadPacket(buff)
if err != nil {
buff.Release()
if sl.closed {
break
}
continue
}
_ = sl.service.NewPacket(context.TODO(), conn, buff, metadata.Metadata{
Protocol: "shadowsocks",
Source: remoteAddr,
})
}
}()
}
//TCP
l, err := inbound.Listen("tcp", addr)