2018-06-13 17:00:58 +00:00
|
|
|
package socks
|
2018-06-10 14:50:03 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-25 09:47:39 +00:00
|
|
|
"io"
|
2018-06-10 14:50:03 +00:00
|
|
|
"net"
|
|
|
|
|
2021-06-10 06:05:56 +00:00
|
|
|
"github.com/Dreamacro/clash/adapter/inbound"
|
2021-07-18 08:09:09 +00:00
|
|
|
N "github.com/Dreamacro/clash/common/net"
|
2018-12-05 13:13:29 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-06-13 09:23:10 +00:00
|
|
|
authStore "github.com/Dreamacro/clash/listener/auth"
|
2021-07-18 08:09:09 +00:00
|
|
|
"github.com/Dreamacro/clash/transport/socks4"
|
2021-05-13 14:18:49 +00:00
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
2018-06-10 14:50:03 +00:00
|
|
|
)
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
type Listener struct {
|
2022-12-04 16:20:50 +00:00
|
|
|
listener net.Listener
|
|
|
|
addr string
|
|
|
|
closed bool
|
2018-11-22 03:54:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 16:35:37 +00:00
|
|
|
// RawAddress implements C.Listener
|
|
|
|
func (l *Listener) RawAddress() string {
|
|
|
|
return l.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address implements C.Listener
|
|
|
|
func (l *Listener) Address() string {
|
|
|
|
return l.listener.Addr().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements C.Listener
|
|
|
|
func (l *Listener) Close() error {
|
|
|
|
l.closed = true
|
|
|
|
return l.listener.Close()
|
|
|
|
}
|
|
|
|
|
2023-09-28 10:59:31 +00:00
|
|
|
func New(addr string, tunnel C.Tunnel, additions ...inbound.Addition) (*Listener, error) {
|
2022-12-04 16:20:50 +00:00
|
|
|
if len(additions) == 0 {
|
2022-12-05 02:12:53 +00:00
|
|
|
additions = []inbound.Addition{
|
|
|
|
inbound.WithInName("DEFAULT-SOCKS"),
|
|
|
|
inbound.WithSpecialRules(""),
|
|
|
|
}
|
2022-12-04 16:20:50 +00:00
|
|
|
}
|
2022-11-16 02:43:16 +00:00
|
|
|
l, err := inbound.Listen("tcp", addr)
|
2018-06-10 14:50:03 +00:00
|
|
|
if err != nil {
|
2018-11-22 03:54:01 +00:00
|
|
|
return nil, err
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
2018-07-15 14:23:20 +00:00
|
|
|
|
2021-07-19 06:07:51 +00:00
|
|
|
sl := &Listener{
|
2022-12-04 16:20:50 +00:00
|
|
|
listener: l,
|
|
|
|
addr: addr,
|
2021-07-19 06:07:51 +00:00
|
|
|
}
|
2018-07-15 14:23:20 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
c, err := l.Accept()
|
|
|
|
if err != nil {
|
2018-11-22 03:54:01 +00:00
|
|
|
if sl.closed {
|
2018-07-15 14:23:20 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2023-09-28 10:59:31 +00:00
|
|
|
go handleSocks(c, tunnel, additions...)
|
2018-07-15 14:23:20 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-11-22 03:54:01 +00:00
|
|
|
return sl, nil
|
|
|
|
}
|
|
|
|
|
2023-09-28 10:59:31 +00:00
|
|
|
func handleSocks(conn net.Conn, tunnel C.Tunnel, additions ...inbound.Addition) {
|
2023-09-02 08:25:55 +00:00
|
|
|
N.TCPKeepAlive(conn)
|
2021-07-18 08:09:09 +00:00
|
|
|
bufConn := N.NewBufferedConn(conn)
|
|
|
|
head, err := bufConn.Peek(1)
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch head[0] {
|
|
|
|
case socks4.Version:
|
2023-09-28 10:59:31 +00:00
|
|
|
HandleSocks4(bufConn, tunnel, additions...)
|
2021-07-18 08:09:09 +00:00
|
|
|
case socks5.Version:
|
2023-09-28 10:59:31 +00:00
|
|
|
HandleSocks5(bufConn, tunnel, additions...)
|
2021-07-18 08:09:09 +00:00
|
|
|
default:
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 10:59:31 +00:00
|
|
|
func HandleSocks4(conn net.Conn, tunnel C.Tunnel, additions ...inbound.Addition) {
|
2021-07-18 08:09:09 +00:00
|
|
|
addr, _, err := socks4.ServerHandshake(conn, authStore.Authenticator())
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
2023-09-28 10:59:31 +00:00
|
|
|
tunnel.HandleTCPConn(inbound.NewSocket(socks5.ParseAddr(addr), conn, C.SOCKS4, additions...))
|
2021-07-18 08:09:09 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 10:59:31 +00:00
|
|
|
func HandleSocks5(conn net.Conn, tunnel C.Tunnel, additions ...inbound.Addition) {
|
2019-06-27 09:04:25 +00:00
|
|
|
target, command, err := socks5.ServerHandshake(conn, authStore.Authenticator())
|
2018-06-10 14:50:03 +00:00
|
|
|
if err != nil {
|
2018-06-12 01:18:15 +00:00
|
|
|
conn.Close()
|
|
|
|
return
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
2019-04-25 05:48:47 +00:00
|
|
|
if command == socks5.CmdUDPAssociate {
|
2019-07-25 09:47:39 +00:00
|
|
|
defer conn.Close()
|
2021-10-09 12:35:06 +00:00
|
|
|
io.Copy(io.Discard, conn)
|
2019-04-23 15:29:36 +00:00
|
|
|
return
|
|
|
|
}
|
2023-09-28 10:59:31 +00:00
|
|
|
tunnel.HandleTCPConn(inbound.NewSocket(target, conn, C.SOCKS5, additions...))
|
2019-04-23 15:29:36 +00:00
|
|
|
}
|