2018-06-14 01:00:58 +08:00
|
|
|
package socks
|
2018-06-10 22:50:03 +08:00
|
|
|
|
|
|
|
import (
|
2019-07-25 17:47:39 +08:00
|
|
|
"io"
|
2018-06-10 22:50:03 +08:00
|
|
|
"net"
|
|
|
|
|
2021-06-10 14:05:56 +08:00
|
|
|
"github.com/Dreamacro/clash/adapter/inbound"
|
2021-07-18 16:09:09 +08:00
|
|
|
N "github.com/Dreamacro/clash/common/net"
|
2018-12-05 21:13:29 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-06-13 17:23:10 +08:00
|
|
|
authStore "github.com/Dreamacro/clash/listener/auth"
|
2021-07-18 16:09:09 +08:00
|
|
|
"github.com/Dreamacro/clash/transport/socks4"
|
2021-05-13 22:18:49 +08:00
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
2018-06-10 22:50:03 +08:00
|
|
|
)
|
|
|
|
|
2021-06-13 17:23:10 +08:00
|
|
|
type Listener struct {
|
2021-06-13 23:05:22 +08:00
|
|
|
listener net.Listener
|
2021-08-01 00:35:37 +08:00
|
|
|
addr string
|
2021-06-13 23:05:22 +08:00
|
|
|
closed bool
|
2022-12-04 13:37:14 +08:00
|
|
|
preferRulesName string
|
|
|
|
name string
|
2018-11-22 11:54:01 +08:00
|
|
|
}
|
|
|
|
|
2021-08-01 00:35:37 +08: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()
|
|
|
|
}
|
|
|
|
|
2022-11-16 10:43:16 +08:00
|
|
|
func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
|
2022-12-04 13:37:14 +08:00
|
|
|
return NewWithInfos(addr,"DEFAULT-SOCKS","",in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWithInfos(addr,name,preferRulesName string, in chan<- C.ConnContext) (*Listener, error) {
|
2022-11-16 10:43:16 +08:00
|
|
|
l, err := inbound.Listen("tcp", addr)
|
2018-06-10 22:50:03 +08:00
|
|
|
if err != nil {
|
2018-11-22 11:54:01 +08:00
|
|
|
return nil, err
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
2018-07-15 22:23:20 +08:00
|
|
|
|
2021-07-19 14:07:51 +08:00
|
|
|
sl := &Listener{
|
|
|
|
listener: l,
|
2021-08-01 00:35:37 +08:00
|
|
|
addr: addr,
|
2022-12-04 13:37:14 +08:00
|
|
|
name: name,
|
|
|
|
preferRulesName: preferRulesName,
|
2021-07-19 14:07:51 +08:00
|
|
|
}
|
2018-07-15 22:23:20 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
c, err := l.Accept()
|
|
|
|
if err != nil {
|
2018-11-22 11:54:01 +08:00
|
|
|
if sl.closed {
|
2018-07-15 22:23:20 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2022-12-04 13:37:14 +08:00
|
|
|
go handleSocks(sl.name,sl.preferRulesName,c, in)
|
2018-07-15 22:23:20 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-11-22 11:54:01 +08:00
|
|
|
return sl, nil
|
|
|
|
}
|
|
|
|
|
2022-12-04 13:37:14 +08:00
|
|
|
func handleSocks(name,preferRulesName string,conn net.Conn, in chan<- C.ConnContext) {
|
2022-02-23 11:22:46 +08:00
|
|
|
conn.(*net.TCPConn).SetKeepAlive(true)
|
2021-07-18 16:09:09 +08:00
|
|
|
bufConn := N.NewBufferedConn(conn)
|
|
|
|
head, err := bufConn.Peek(1)
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch head[0] {
|
|
|
|
case socks4.Version:
|
2022-12-04 13:37:14 +08:00
|
|
|
HandleSocks4(name,preferRulesName,bufConn, in)
|
2021-07-18 16:09:09 +08:00
|
|
|
case socks5.Version:
|
2022-12-04 13:37:14 +08:00
|
|
|
HandleSocks5(name,preferRulesName,bufConn, in)
|
2021-07-18 16:09:09 +08:00
|
|
|
default:
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 13:37:14 +08:00
|
|
|
func HandleSocks4(name,preferRulesName string, conn net.Conn, in chan<- C.ConnContext) {
|
2021-07-18 16:09:09 +08:00
|
|
|
addr, _, err := socks4.ServerHandshake(conn, authStore.Authenticator())
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
2022-12-04 13:37:14 +08:00
|
|
|
in <- inbound.NewSocketWithInfos(socks5.ParseAddr(addr), conn, C.SOCKS4,name,preferRulesName)
|
2021-07-18 16:09:09 +08:00
|
|
|
}
|
|
|
|
|
2022-12-04 13:37:14 +08:00
|
|
|
func HandleSocks5(name,preferRulesName string,conn net.Conn, in chan<- C.ConnContext) {
|
2019-06-27 17:04:25 +08:00
|
|
|
target, command, err := socks5.ServerHandshake(conn, authStore.Authenticator())
|
2018-06-10 22:50:03 +08:00
|
|
|
if err != nil {
|
2018-06-12 09:18:15 +08:00
|
|
|
conn.Close()
|
|
|
|
return
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
2019-04-25 13:48:47 +08:00
|
|
|
if command == socks5.CmdUDPAssociate {
|
2019-07-25 17:47:39 +08:00
|
|
|
defer conn.Close()
|
2021-10-09 20:35:06 +08:00
|
|
|
io.Copy(io.Discard, conn)
|
2019-04-23 23:29:36 +08:00
|
|
|
return
|
|
|
|
}
|
2022-12-04 13:37:14 +08:00
|
|
|
in <- inbound.NewSocketWithInfos(target, conn, C.SOCKS5,name,preferRulesName)
|
2019-04-23 23:29:36 +08:00
|
|
|
}
|