Merge from remote branch

This commit is contained in:
yaling888
2021-07-28 22:13:21 +08:00
33 changed files with 1549 additions and 142 deletions

View File

@ -45,6 +45,7 @@ func HandleConn(c net.Conn, in chan<- C.ConnContext, cache *cache.Cache) {
if request.Method == http.MethodConnect {
resp = responseWith(200)
resp.Status = "Connection established"
resp.ContentLength = -1
if resp.Write(conn) != nil {
break // close connection

View File

@ -10,7 +10,6 @@ import (
type Listener struct {
listener net.Listener
address string
closed bool
}
@ -31,7 +30,6 @@ func NewWithAuthenticate(addr string, in chan<- C.ConnContext, authenticate bool
hl := &Listener{
listener: l,
address: addr,
}
go func() {
for {
@ -55,5 +53,5 @@ func (l *Listener) Close() {
}
func (l *Listener) Address() string {
return l.address
return l.listener.Addr().String()
}

View File

@ -157,7 +157,7 @@ func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
socksListener = tcpListener
socksUDPListener = udpListener
log.Infoln("SOCKS5 proxy listening at: %s", socksListener.Address())
log.Infoln("SOCKS proxy listening at: %s", socksListener.Address())
return nil
}
@ -289,7 +289,7 @@ func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
return err
}
log.Infoln("Mixed(http+socks5) proxy listening at: %s", mixedListener.Address())
log.Infoln("Mixed(http+socks) proxy listening at: %s", mixedListener.Address())
return nil
}

View File

@ -9,12 +9,12 @@ import (
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/listener/http"
"github.com/Dreamacro/clash/listener/socks"
"github.com/Dreamacro/clash/transport/socks4"
"github.com/Dreamacro/clash/transport/socks5"
)
type Listener struct {
listener net.Listener
address string
closed bool
cache *cache.Cache
}
@ -25,7 +25,10 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
return nil, err
}
ml := &Listener{l, addr, false, cache.New(30 * time.Second)}
ml := &Listener{
listener: l,
cache: cache.New(30 * time.Second),
}
go func() {
for {
c, err := ml.listener.Accept()
@ -48,7 +51,7 @@ func (l *Listener) Close() {
}
func (l *Listener) Address() string {
return l.address
return l.listener.Addr().String()
}
func handleConn(conn net.Conn, in chan<- C.ConnContext, cache *cache.Cache) {
@ -58,10 +61,12 @@ func handleConn(conn net.Conn, in chan<- C.ConnContext, cache *cache.Cache) {
return
}
if head[0] == socks5.Version {
socks.HandleSocks(bufConn, in)
return
switch head[0] {
case socks4.Version:
socks.HandleSocks4(bufConn, in)
case socks5.Version:
socks.HandleSocks5(bufConn, in)
default:
http.HandleConn(bufConn, in, cache)
}
http.HandleConn(bufConn, in, cache)
}

View File

@ -9,7 +9,6 @@ import (
type Listener struct {
listener net.Listener
address string
closed bool
}
@ -18,7 +17,9 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
if err != nil {
return nil, err
}
rl := &Listener{l, addr, false}
rl := &Listener{
listener: l,
}
go func() {
for {
@ -42,7 +43,7 @@ func (l *Listener) Close() {
}
func (l *Listener) Address() string {
return l.address
return l.listener.Addr().String()
}
func handleRedir(conn net.Conn, in chan<- C.ConnContext) {

View File

@ -6,14 +6,15 @@ import (
"net"
"github.com/Dreamacro/clash/adapter/inbound"
N "github.com/Dreamacro/clash/common/net"
C "github.com/Dreamacro/clash/constant"
authStore "github.com/Dreamacro/clash/listener/auth"
"github.com/Dreamacro/clash/transport/socks4"
"github.com/Dreamacro/clash/transport/socks5"
)
type Listener struct {
listener net.Listener
address string
closed bool
}
@ -23,7 +24,9 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
return nil, err
}
sl := &Listener{l, addr, false}
sl := &Listener{
listener: l,
}
go func() {
for {
c, err := l.Accept()
@ -33,7 +36,7 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
}
continue
}
go HandleSocks(c, in)
go handleSocks(c, in)
}
}()
@ -46,10 +49,40 @@ func (l *Listener) Close() {
}
func (l *Listener) Address() string {
return l.address
return l.listener.Addr().String()
}
func HandleSocks(conn net.Conn, in chan<- C.ConnContext) {
func handleSocks(conn net.Conn, in chan<- C.ConnContext) {
bufConn := N.NewBufferedConn(conn)
head, err := bufConn.Peek(1)
if err != nil {
conn.Close()
return
}
switch head[0] {
case socks4.Version:
HandleSocks4(bufConn, in)
case socks5.Version:
HandleSocks5(bufConn, in)
default:
conn.Close()
}
}
func HandleSocks4(conn net.Conn, in chan<- C.ConnContext) {
addr, _, err := socks4.ServerHandshake(conn, authStore.Authenticator())
if err != nil {
conn.Close()
return
}
if c, ok := conn.(*net.TCPConn); ok {
c.SetKeepAlive(true)
}
in <- inbound.NewSocket(socks5.ParseAddr(addr), conn, C.SOCKS4)
}
func HandleSocks5(conn net.Conn, in chan<- C.ConnContext) {
target, command, err := socks5.ServerHandshake(conn, authStore.Authenticator())
if err != nil {
conn.Close()
@ -63,5 +96,5 @@ func HandleSocks(conn net.Conn, in chan<- C.ConnContext) {
io.Copy(ioutil.Discard, conn)
return
}
in <- inbound.NewSocket(target, conn, C.SOCKS)
in <- inbound.NewSocket(target, conn, C.SOCKS5)
}

View File

@ -13,7 +13,6 @@ import (
type UDPListener struct {
packetConn net.PacketConn
address string
closed bool
}
@ -27,7 +26,9 @@ func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error)
log.Warnln("Failed to Reuse UDP Address: %s", err)
}
sl := &UDPListener{l, addr, false}
sl := &UDPListener{
packetConn: l,
}
go func() {
for {
buf := pool.Get(pool.RelayBufferSize)
@ -52,7 +53,7 @@ func (l *UDPListener) Close() error {
}
func (l *UDPListener) Address() string {
return l.address
return l.packetConn.LocalAddr().String()
}
func handleSocksUDP(pc net.PacketConn, in chan<- *inbound.PacketAdapter, buf []byte, addr net.Addr) {
@ -69,7 +70,7 @@ func handleSocksUDP(pc net.PacketConn, in chan<- *inbound.PacketAdapter, buf []b
bufRef: buf,
}
select {
case in <- inbound.NewPacket(target, packet, C.TPROXY):
case in <- inbound.NewPacket(target, packet, C.SOCKS5):
default:
}
}

View File

@ -10,7 +10,6 @@ import (
type Listener struct {
listener net.Listener
address string
closed bool
}
@ -33,7 +32,6 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
rl := &Listener{
listener: l,
address: addr,
}
go func() {
@ -58,7 +56,7 @@ func (l *Listener) Close() {
}
func (l *Listener) Address() string {
return l.address
return l.listener.Addr().String()
}
func (l *Listener) handleTProxy(conn net.Conn, in chan<- C.ConnContext) {

View File

@ -11,7 +11,6 @@ import (
type UDPListener struct {
packetConn net.PacketConn
address string
closed bool
}
@ -21,7 +20,9 @@ func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error)
return nil, err
}
rl := &UDPListener{l, addr, false}
rl := &UDPListener{
packetConn: l,
}
c := l.(*net.UDPConn)
@ -65,7 +66,7 @@ func (l *UDPListener) Close() error {
}
func (l *UDPListener) Address() string {
return l.address
return l.packetConn.LocalAddr().String()
}
func handlePacketConn(pc net.PacketConn, in chan<- *inbound.PacketAdapter, buf []byte, lAddr *net.UDPAddr, rAddr *net.UDPAddr) {