mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 17:35:40 +08:00
chore: small optimizations
This commit is contained in:
parent
a814f8c545
commit
e12642b47a
@ -2,9 +2,9 @@ package dns
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"net/netip"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -287,6 +287,7 @@ func (c *Client) AddRecord(record string) error {
|
|||||||
domain, ip := r[0], r[1]
|
domain, ip := r[0], r[1]
|
||||||
m, err := MakeResponse(domain, ip, uint32(c.config.MaxTTL))
|
m, err := MakeResponse(domain, ip, uint32(c.config.MaxTTL))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.F("[dns] add custom record error: %s", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -306,26 +307,20 @@ func (c *Client) AddRecord(record string) error {
|
|||||||
// MakeResponse makes a dns response message for the given domain and ip address.
|
// MakeResponse makes a dns response message for the given domain and ip address.
|
||||||
// Note: you should make sure ttl > 0.
|
// Note: you should make sure ttl > 0.
|
||||||
func MakeResponse(domain, ip string, ttl uint32) (*Message, error) {
|
func MakeResponse(domain, ip string, ttl uint32) (*Message, error) {
|
||||||
ipb := net.ParseIP(ip)
|
addr, err := netip.ParseAddr(ip)
|
||||||
if ipb == nil {
|
if err != nil {
|
||||||
return nil, errors.New("MakeResponse: invalid ip format")
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var rdata []byte
|
var qtype, rdlen uint16 = QTypeA, net.IPv4len
|
||||||
var qtype, rdlen uint16
|
if addr.Is6() {
|
||||||
if rdata = ipb.To4(); rdata != nil {
|
qtype, rdlen = QTypeAAAA, net.IPv6len
|
||||||
qtype = QTypeA
|
|
||||||
rdlen = net.IPv4len
|
|
||||||
} else {
|
|
||||||
qtype = QTypeAAAA
|
|
||||||
rdlen = net.IPv6len
|
|
||||||
rdata = ipb
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m := NewMessage(0, ResponseMsg)
|
m := NewMessage(0, ResponseMsg)
|
||||||
m.SetQuestion(NewQuestion(qtype, domain))
|
m.SetQuestion(NewQuestion(qtype, domain))
|
||||||
rr := &RR{NAME: domain, TYPE: qtype, CLASS: ClassINET,
|
rr := &RR{NAME: domain, TYPE: qtype, CLASS: ClassINET,
|
||||||
TTL: ttl, RDLENGTH: rdlen, RDATA: rdata}
|
TTL: ttl, RDLENGTH: rdlen, RDATA: addr.AsSlice()}
|
||||||
m.AddAnswer(rr)
|
m.AddAnswer(rr)
|
||||||
|
|
||||||
return m, nil
|
return m, nil
|
||||||
|
2
go.mod
2
go.mod
@ -26,7 +26,7 @@ require (
|
|||||||
github.com/templexxx/xorsimd v0.4.1 // indirect
|
github.com/templexxx/xorsimd v0.4.1 // indirect
|
||||||
github.com/tjfoc/gmsm v1.4.1 // indirect
|
github.com/tjfoc/gmsm v1.4.1 // indirect
|
||||||
github.com/u-root/uio v0.0.0-20210528151154-e40b768296a7 // indirect
|
github.com/u-root/uio v0.0.0-20210528151154-e40b768296a7 // indirect
|
||||||
golang.org/x/net v0.0.0-20220127074510-2fabfed7e28f // indirect
|
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
|
||||||
)
|
)
|
||||||
|
|
||||||
// Replace dependency modules with local developing copy
|
// Replace dependency modules with local developing copy
|
||||||
|
4
go.sum
4
go.sum
@ -128,8 +128,8 @@ golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81R
|
|||||||
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||||
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||||
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||||
golang.org/x/net v0.0.0-20220127074510-2fabfed7e28f h1:o66Bv9+w/vuk7Krcig9jZqD01FP7BL8OliFqqw0xzPI=
|
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=
|
||||||
golang.org/x/net v0.0.0-20220127074510-2fabfed7e28f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
||||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
@ -2,6 +2,7 @@ package redir
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
"net/netip"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
@ -83,19 +84,20 @@ func (s *RedirProxy) Serve(cc net.Conn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.SetKeepAlive(true)
|
c.SetKeepAlive(true)
|
||||||
tgt, err := getOrigDst(c, s.ipv6)
|
tgtAddr, err := getOrigDst(c, s.ipv6)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.F("[redir] failed to get target address: %v", err)
|
log.F("[redir] failed to get target address: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
tgt := tgtAddr.String()
|
||||||
|
|
||||||
// loop request
|
// loop request
|
||||||
if c.LocalAddr().String() == tgt.String() {
|
if c.LocalAddr().String() == tgt {
|
||||||
log.F("[redir] %s <-> %s, unallowed request to redir port", c.RemoteAddr(), tgt)
|
log.F("[redir] %s <-> %s, unallowed request to redir port", c.RemoteAddr(), tgt)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rc, dialer, err := s.proxy.Dial("tcp", tgt.String())
|
rc, dialer, err := s.proxy.Dial("tcp", tgt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.F("[redir] %s <-> %s via %s, error in dial: %v", c.RemoteAddr(), tgt, dialer.Addr(), err)
|
log.F("[redir] %s <-> %s via %s, error in dial: %v", c.RemoteAddr(), tgt, dialer.Addr(), err)
|
||||||
return
|
return
|
||||||
@ -114,12 +116,12 @@ func (s *RedirProxy) Serve(cc net.Conn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the original destination of a TCP connection.
|
// Get the original destination of a TCP connection.
|
||||||
func getOrigDst(c *net.TCPConn, ipv6 bool) (*net.TCPAddr, error) {
|
func getOrigDst(c *net.TCPConn, ipv6 bool) (netip.AddrPort, error) {
|
||||||
rc, err := c.SyscallConn()
|
rc, err := c.SyscallConn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return netip.AddrPort{}, err
|
||||||
}
|
}
|
||||||
var addr *net.TCPAddr
|
var addr netip.AddrPort
|
||||||
rc.Control(func(fd uintptr) {
|
rc.Control(func(fd uintptr) {
|
||||||
if ipv6 {
|
if ipv6 {
|
||||||
addr, err = getorigdstIPv6(fd)
|
addr, err = getorigdstIPv6(fd)
|
||||||
@ -131,32 +133,25 @@ func getOrigDst(c *net.TCPConn, ipv6 bool) (*net.TCPAddr, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Call getorigdst() from linux/net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c
|
// Call getorigdst() from linux/net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c
|
||||||
func getorigdst(fd uintptr) (*net.TCPAddr, error) {
|
func getorigdst(fd uintptr) (netip.AddrPort, error) {
|
||||||
const _SO_ORIGINAL_DST = 80 // from linux/include/uapi/linux/netfilter_ipv4.h
|
const _SO_ORIGINAL_DST = 80 // from linux/include/uapi/linux/netfilter_ipv4.h
|
||||||
var raw syscall.RawSockaddrInet4
|
var raw syscall.RawSockaddrInet4
|
||||||
siz := unsafe.Sizeof(raw)
|
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 {
|
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
|
return netip.AddrPort{}, err
|
||||||
}
|
}
|
||||||
var addr net.TCPAddr
|
port := raw.Port<<8 | raw.Port>>8 // raw.Port is big-endian
|
||||||
addr.IP = raw.Addr[:]
|
return netip.AddrPortFrom(netip.AddrFrom4(raw.Addr), port), nil
|
||||||
port := (*[2]byte)(unsafe.Pointer(&raw.Port)) // raw.Port is big-endian
|
|
||||||
addr.Port = int(port[0])<<8 | int(port[1])
|
|
||||||
return &addr, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call ipv6_getorigdst() from linux/net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c
|
// Call ipv6_getorigdst() from linux/net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c
|
||||||
// NOTE: I haven't tried yet but it should work since Linux 3.8.
|
func getorigdstIPv6(fd uintptr) (netip.AddrPort, error) {
|
||||||
func getorigdstIPv6(fd uintptr) (*net.TCPAddr, error) {
|
|
||||||
const _IP6T_SO_ORIGINAL_DST = 80 // from linux/include/uapi/linux/netfilter_ipv6/ip6_tables.h
|
const _IP6T_SO_ORIGINAL_DST = 80 // from linux/include/uapi/linux/netfilter_ipv6/ip6_tables.h
|
||||||
var raw syscall.RawSockaddrInet6
|
var raw syscall.RawSockaddrInet6
|
||||||
siz := unsafe.Sizeof(raw)
|
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 {
|
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
|
return netip.AddrPort{}, err
|
||||||
}
|
}
|
||||||
var addr net.TCPAddr
|
port := raw.Port<<8 | raw.Port>>8 // raw.Port is big-endian
|
||||||
addr.IP = raw.Addr[:]
|
return netip.AddrPortFrom(netip.AddrFrom16(raw.Addr), port), nil
|
||||||
port := (*[2]byte)(unsafe.Pointer(&raw.Port)) // raw.Port is big-endian
|
|
||||||
addr.Port = int(port[0])<<8 | int(port[1])
|
|
||||||
return &addr, nil
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"net/netip"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/nadoo/glider/pkg/log"
|
"github.com/nadoo/glider/pkg/log"
|
||||||
@ -74,15 +75,13 @@ func (s *Socks5) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.A
|
|||||||
buf := pool.GetBuffer(socks.MaxAddrLen)
|
buf := pool.GetBuffer(socks.MaxAddrLen)
|
||||||
defer pool.PutBuffer(buf)
|
defer pool.PutBuffer(buf)
|
||||||
|
|
||||||
var uAddress string
|
uAddress := uAddr.String()
|
||||||
h, p, _ := net.SplitHostPort(uAddr.String())
|
h, p, _ := net.SplitHostPort(uAddress)
|
||||||
// if returned bind ip is unspecified
|
// if returned bind ip is unspecified
|
||||||
if ip := net.ParseIP(h); ip != nil && ip.IsUnspecified() {
|
if ip, err := netip.ParseAddr(h); err == nil && ip.IsUnspecified() {
|
||||||
// indicate using conventional addr
|
// indicate using conventional addr
|
||||||
h, _, _ = net.SplitHostPort(s.addr)
|
h, _, _ = net.SplitHostPort(s.addr)
|
||||||
uAddress = net.JoinHostPort(h, p)
|
uAddress = net.JoinHostPort(h, p)
|
||||||
} else {
|
|
||||||
uAddress = uAddr.String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pc, nextHop, err := s.dialer.DialUDP(network, uAddress)
|
pc, nextHop, err := s.dialer.DialUDP(network, uAddress)
|
||||||
|
Loading…
Reference in New Issue
Block a user