glider/proxy/redir/redir_linux.go

184 lines
4.6 KiB
Go
Raw Normal View History

// getOrigDst:
// https://github.com/shadowsocks/go-shadowsocks2/blob/master/tcp_linux.go#L30
package redir
import (
2017-07-30 12:03:19 +08:00
"errors"
"net"
"net/url"
"strings"
"syscall"
"unsafe"
"github.com/nadoo/glider/common/log"
"github.com/nadoo/glider/common/socks"
"github.com/nadoo/glider/proxy"
)
const (
2017-10-02 19:39:57 +08:00
// SO_ORIGINAL_DST from linux/include/uapi/linux/netfilter_ipv4.h
2017-09-21 23:13:44 +08:00
SO_ORIGINAL_DST = 80
2017-10-02 19:39:57 +08:00
// IP6T_SO_ORIGINAL_DST from linux/include/uapi/linux/netfilter_ipv6/ip6_tables.h
2017-09-21 23:13:44 +08:00
IP6T_SO_ORIGINAL_DST = 80
)
// RedirProxy struct.
type RedirProxy struct {
proxy proxy.Proxy
addr string
ipv6 bool
}
func init() {
proxy.RegisterServer("redir", NewRedirServer)
2018-09-17 19:22:36 +08:00
proxy.RegisterServer("redir6", NewRedir6Server)
}
// NewRedirProxy returns a redirect proxy.
func NewRedirProxy(s string, p proxy.Proxy, ipv6 bool) (*RedirProxy, error) {
u, err := url.Parse(s)
if err != nil {
log.F("parse err: %s", err)
return nil, err
}
addr := u.Host
r := &RedirProxy{
proxy: p,
addr: addr,
ipv6: ipv6,
}
return r, nil
}
// NewRedirServer returns a redir server.
func NewRedirServer(s string, p proxy.Proxy) (proxy.Server, error) {
return NewRedirProxy(s, p, false)
2018-09-04 20:26:40 +08:00
}
// NewRedir6Server returns a redir server for ipv6.
func NewRedir6Server(s string, p proxy.Proxy) (proxy.Server, error) {
return NewRedirProxy(s, p, true)
}
// ListenAndServe listens on server's addr and serves connections.
func (s *RedirProxy) ListenAndServe() {
l, err := net.Listen("tcp", s.addr)
if err != nil {
2018-06-28 23:20:04 +08:00
log.F("[redir] failed to listen on %s: %v", s.addr, err)
return
}
2018-06-28 23:20:04 +08:00
log.F("[redir] listening TCP on %s", s.addr)
for {
c, err := l.Accept()
if err != nil {
2018-06-28 23:20:04 +08:00
log.F("[redir] failed to accept: %v", err)
continue
}
2018-11-28 23:28:32 +08:00
go s.Serve(c)
}
}
// Serve serves connections.
func (s *RedirProxy) Serve(c net.Conn) {
2018-11-28 23:28:32 +08:00
defer c.Close()
if c, ok := c.(*net.TCPConn); ok {
c.SetKeepAlive(true)
}
tgt, err := getOrigDst(c, s.ipv6)
if err != nil {
log.F("[redir] failed to get target address: %v", err)
return
}
2019-03-04 19:19:00 +08:00
// loop request
if c.LocalAddr().String() == tgt.String() {
2019-03-05 22:30:22 +08:00
log.F("[redir] %s <-> %s, unallowed request to redir port", c.RemoteAddr(), tgt)
2019-03-04 19:19:00 +08:00
return
}
rc, dialer, err := s.proxy.Dial("tcp", tgt.String())
2018-11-28 23:28:32 +08:00
if err != nil {
log.F("[redir] %s <-> %s via %s, error in dial: %v", c.RemoteAddr(), tgt, dialer.Addr(), err)
2018-11-28 23:28:32 +08:00
return
}
defer rc.Close()
log.F("[redir] %s <-> %s via %s", c.RemoteAddr(), tgt, dialer.Addr())
2018-11-28 23:28:32 +08:00
2020-10-01 22:38:34 +08:00
if err = proxy.Relay(c, rc); err != nil {
log.F("[redir] %s <-> %s via %s, relay error: %v", c.RemoteAddr(), tgt, dialer.Addr(), err)
// record remote conn failure only
if !strings.Contains(err.Error(), s.addr) {
s.proxy.Record(dialer, false)
}
2018-11-28 23:28:32 +08:00
}
}
2017-07-30 12:03:19 +08:00
// Get the original destination of a TCP connection.
func getOrigDst(conn net.Conn, ipv6 bool) (socks.Addr, error) {
2017-07-30 12:03:19 +08:00
c, ok := conn.(*net.TCPConn)
if !ok {
return nil, errors.New("only work with TCP connection")
}
f, err := c.File()
if err != nil {
return nil, err
}
defer f.Close()
fd := f.Fd()
// The File() call above puts both the original socket fd and the file fd in blocking mode.
// Set the file fd back to non-blocking mode and the original socket fd will become non-blocking as well.
// Otherwise blocking I/O will waste OS threads.
if err := syscall.SetNonblock(int(fd), true); err != nil {
return nil, err
}
if ipv6 {
2017-09-21 23:13:44 +08:00
return getorigdstIPv6(fd)
2017-07-30 12:03:19 +08:00
}
return getorigdst(fd)
}
// Call getorigdst() from linux/net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c
func getorigdst(fd uintptr) (socks.Addr, error) {
raw := syscall.RawSockaddrInet4{}
siz := unsafe.Sizeof(raw)
if err := socketcall(GETSOCKOPT, fd, syscall.IPPROTO_IP, SO_ORIGINAL_DST, uintptr(unsafe.Pointer(&raw)), uintptr(unsafe.Pointer(&siz)), 0); err != nil {
return nil, err
}
addr := make([]byte, 1+net.IPv4len+2)
2018-07-03 00:31:43 +08:00
addr[0] = socks.ATypIP4
copy(addr[1:1+net.IPv4len], raw.Addr[:])
port := (*[2]byte)(unsafe.Pointer(&raw.Port)) // big-endian
addr[1+net.IPv4len], addr[1+net.IPv4len+1] = port[0], port[1]
return addr, nil
}
// Call ipv6_getorigdst() from linux/net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c
func getorigdstIPv6(fd uintptr) (socks.Addr, error) {
raw := syscall.RawSockaddrInet6{}
siz := unsafe.Sizeof(raw)
if err := socketcall(GETSOCKOPT, fd, syscall.IPPROTO_IPV6, IP6T_SO_ORIGINAL_DST, uintptr(unsafe.Pointer(&raw)), uintptr(unsafe.Pointer(&siz)), 0); err != nil {
return nil, err
}
addr := make([]byte, 1+net.IPv6len+2)
2018-07-03 00:31:43 +08:00
addr[0] = socks.ATypIP6
copy(addr[1:1+net.IPv6len], raw.Addr[:])
port := (*[2]byte)(unsafe.Pointer(&raw.Port)) // big-endian
addr[1+net.IPv6len], addr[1+net.IPv6len+1] = port[0], port[1]
return addr, nil
}