mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-09-12 11:52:33 +00:00
65 lines
1.1 KiB
Go
65 lines
1.1 KiB
Go
package socks
|
|
|
|
import (
|
|
"net"
|
|
|
|
"github.com/Dreamacro/clash/adapters/inbound"
|
|
C "github.com/Dreamacro/clash/constant"
|
|
"github.com/Dreamacro/clash/log"
|
|
"github.com/Dreamacro/clash/tunnel"
|
|
|
|
"github.com/Dreamacro/go-shadowsocks2/socks"
|
|
)
|
|
|
|
var (
|
|
tun = tunnel.Instance()
|
|
)
|
|
|
|
type SockListener struct {
|
|
net.Listener
|
|
address string
|
|
closed bool
|
|
}
|
|
|
|
func NewSocksProxy(addr string) (*SockListener, error) {
|
|
l, err := net.Listen("tcp", addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
sl := &SockListener{l, addr, false}
|
|
go func() {
|
|
log.Infoln("SOCKS proxy listening at: %s", addr)
|
|
for {
|
|
c, err := l.Accept()
|
|
if err != nil {
|
|
if sl.closed {
|
|
break
|
|
}
|
|
continue
|
|
}
|
|
go handleSocks(c)
|
|
}
|
|
}()
|
|
|
|
return sl, nil
|
|
}
|
|
|
|
func (l *SockListener) Close() {
|
|
l.closed = true
|
|
l.Listener.Close()
|
|
}
|
|
|
|
func (l *SockListener) Address() string {
|
|
return l.address
|
|
}
|
|
|
|
func handleSocks(conn net.Conn) {
|
|
target, err := socks.Handshake(conn)
|
|
if err != nil {
|
|
conn.Close()
|
|
return
|
|
}
|
|
conn.(*net.TCPConn).SetKeepAlive(true)
|
|
tun.Add(adapters.NewSocket(target, conn, C.SOCKS))
|
|
}
|