2022-11-11 22:44:44 +08:00
|
|
|
package shadowsocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/adapter/inbound"
|
|
|
|
N "github.com/metacubex/mihomo/common/net"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
LC "github.com/metacubex/mihomo/listener/config"
|
|
|
|
"github.com/metacubex/mihomo/transport/shadowsocks/core"
|
|
|
|
"github.com/metacubex/mihomo/transport/socks5"
|
2022-11-11 22:44:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Listener struct {
|
|
|
|
closed bool
|
2022-12-05 10:12:53 +08:00
|
|
|
config LC.ShadowsocksServer
|
2022-11-11 22:44:44 +08:00
|
|
|
listeners []net.Listener
|
|
|
|
udpListeners []*UDPListener
|
|
|
|
pickCipher core.Cipher
|
|
|
|
}
|
|
|
|
|
|
|
|
var _listener *Listener
|
|
|
|
|
2024-08-23 21:05:43 +08:00
|
|
|
func New(config LC.ShadowsocksServer, tunnel C.Tunnel, additions ...inbound.Addition) (*Listener, error) {
|
2022-12-05 10:12:53 +08:00
|
|
|
pickCipher, err := core.PickCipher(config.Cipher, nil, config.Password)
|
2022-11-11 22:44:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sl := &Listener{false, config, nil, nil, pickCipher}
|
|
|
|
_listener = sl
|
|
|
|
|
2022-12-05 10:12:53 +08:00
|
|
|
for _, addr := range strings.Split(config.Listen, ",") {
|
2022-11-11 22:44:44 +08:00
|
|
|
addr := addr
|
|
|
|
|
2023-03-21 12:40:36 +08:00
|
|
|
if config.Udp {
|
|
|
|
//UDP
|
2024-08-23 21:05:43 +08:00
|
|
|
ul, err := NewUDP(addr, pickCipher, tunnel, additions...)
|
2023-03-21 12:40:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sl.udpListeners = append(sl.udpListeners, ul)
|
2022-11-11 22:44:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//TCP
|
2022-11-16 10:43:16 +08:00
|
|
|
l, err := inbound.Listen("tcp", addr)
|
2022-11-11 22:44:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sl.listeners = append(sl.listeners, l)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
c, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
if sl.closed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2024-08-23 21:05:43 +08:00
|
|
|
go sl.HandleConn(c, tunnel, additions...)
|
2022-11-11 22:44:44 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
return sl, nil
|
|
|
|
}
|
|
|
|
|
2022-12-05 00:20:50 +08:00
|
|
|
func (l *Listener) Close() error {
|
|
|
|
var retErr error
|
2022-11-11 22:44:44 +08:00
|
|
|
for _, lis := range l.listeners {
|
2022-12-05 00:20:50 +08:00
|
|
|
err := lis.Close()
|
|
|
|
if err != nil {
|
|
|
|
retErr = err
|
|
|
|
}
|
2022-11-11 22:44:44 +08:00
|
|
|
}
|
|
|
|
for _, lis := range l.udpListeners {
|
2022-12-05 00:20:50 +08:00
|
|
|
err := lis.Close()
|
|
|
|
if err != nil {
|
|
|
|
retErr = err
|
|
|
|
}
|
2022-11-11 22:44:44 +08:00
|
|
|
}
|
2022-12-05 00:20:50 +08:00
|
|
|
return retErr
|
2022-11-11 22:44:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Listener) Config() string {
|
2022-12-05 10:12:53 +08:00
|
|
|
return l.config.String()
|
2022-11-11 22:44:44 +08:00
|
|
|
}
|
|
|
|
|
2022-12-05 00:20:50 +08:00
|
|
|
func (l *Listener) AddrList() (addrList []net.Addr) {
|
|
|
|
for _, lis := range l.listeners {
|
|
|
|
addrList = append(addrList, lis.Addr())
|
|
|
|
}
|
|
|
|
for _, lis := range l.udpListeners {
|
|
|
|
addrList = append(addrList, lis.LocalAddr())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-28 18:59:31 +08:00
|
|
|
func (l *Listener) HandleConn(conn net.Conn, tunnel C.Tunnel, additions ...inbound.Addition) {
|
2023-04-17 19:29:07 +08:00
|
|
|
conn = l.pickCipher.StreamConn(conn)
|
|
|
|
conn = N.NewDeadlineConn(conn) // embed ss can't handle readDeadline correctly
|
2022-11-11 22:44:44 +08:00
|
|
|
|
2023-05-20 18:35:04 +08:00
|
|
|
target, err := socks5.ReadAddr0(conn)
|
2022-11-11 22:44:44 +08:00
|
|
|
if err != nil {
|
|
|
|
_ = conn.Close()
|
|
|
|
return
|
|
|
|
}
|
2023-09-28 18:59:31 +08:00
|
|
|
tunnel.HandleTCPConn(inbound.NewSocket(target, conn, C.SHADOWSOCKS, additions...))
|
2022-11-11 22:44:44 +08:00
|
|
|
}
|
|
|
|
|
2023-09-28 18:59:31 +08:00
|
|
|
func HandleShadowSocks(conn net.Conn, tunnel C.Tunnel, additions ...inbound.Addition) bool {
|
2022-11-11 22:44:44 +08:00
|
|
|
if _listener != nil && _listener.pickCipher != nil {
|
2023-09-28 18:59:31 +08:00
|
|
|
go _listener.HandleConn(conn, tunnel, additions...)
|
2022-11-11 22:44:44 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|