Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding DNS header #587

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var clientPacketConnFuncFactoryMap = map[string]pktconns.ClientPacketConnFuncFac
"wechat": pktconns.NewClientWeChatConnFunc,
"wechat-video": pktconns.NewClientWeChatConnFunc,
"faketcp": pktconns.NewClientFakeTCPConnFunc,
"dns": pktconns.NewClientDnsConnFunc,
}

func client(config *clientConfig) {
Expand Down
1 change: 1 addition & 0 deletions app/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var serverPacketConnFuncFactoryMap = map[string]pktconns.ServerPacketConnFuncFac
"wechat": pktconns.NewServerWeChatConnFunc,
"wechat-video": pktconns.NewServerWeChatConnFunc,
"faketcp": pktconns.NewServerFakeTCPConnFunc,
"dns": pktconns.NewServerDnsConnFunc,
}

func server(config *serverConfig) {
Expand Down
131 changes: 131 additions & 0 deletions core/pktconns/dns/obfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package dns

import (
"encoding/binary"
"math/rand"
"net"
"os"
"sync"
"syscall"
"time"

"github.com/miekg/dns"
)

const udpBufferSize = 4096

type DnsUDPPacketConn struct {
orig *net.UDPConn

readBuf []byte
readMutex sync.Mutex
writeBuf []byte
writeMutex sync.Mutex
header []byte
}

func NewDnsUDPConn(orig *net.UDPConn, domain string) *DnsUDPPacketConn {
var header []byte

header = binary.BigEndian.AppendUint16(header, 0x0000) // Transaction ID
header = binary.BigEndian.AppendUint16(header, 0x0100) // Flags: Standard query
header = binary.BigEndian.AppendUint16(header, 0x0001) // Questions
header = binary.BigEndian.AppendUint16(header, 0x0000) // Answer RRs
header = binary.BigEndian.AppendUint16(header, 0x0000) // Authority RRs
header = binary.BigEndian.AppendUint16(header, 0x0000) // Additional RRs

buf := make([]byte, 0x100)

off1, err := dns.PackDomainName(dns.Fqdn(domain), buf, 0, nil, false)

if err != nil {
return nil
}

header = append(header, buf[:off1]...)

header = binary.BigEndian.AppendUint16(header, 0x0001) // Type: A
header = binary.BigEndian.AppendUint16(header, 0x0001) // Class: IN

return &DnsUDPPacketConn{
orig: orig,
readBuf: make([]byte, udpBufferSize),
writeBuf: make([]byte, udpBufferSize),
header: header,
}
}

func (c *DnsUDPPacketConn) ReadFrom(p []byte) (int, net.Addr, error) {
for {
c.readMutex.Lock()
n, addr, err := c.orig.ReadFrom(c.readBuf)
if n <= len(c.header) {
c.readMutex.Unlock()
return 0, addr, err
}

var newN = copy(p, c.readBuf[len(c.header):n])

c.readMutex.Unlock()
if newN > 0 {
// Valid packet
return newN, addr, err
} else if err != nil {
// Not valid and orig.ReadFrom had some error
return 0, addr, err
}
}
}

func (c *DnsUDPPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
c.writeMutex.Lock()

copy(c.writeBuf, c.header)
binary.BigEndian.PutUint16(c.writeBuf[0:], uint16(rand.Uint32()))

var bn = copy(c.writeBuf[len(c.header):], p)

_, err = c.orig.WriteTo(c.writeBuf[:len(c.header)+bn], addr)
c.writeMutex.Unlock()
if err != nil {
return 0, err
} else {
return len(p), nil
}
}

func (c *DnsUDPPacketConn) Close() error {
return c.orig.Close()
}

func (c *DnsUDPPacketConn) LocalAddr() net.Addr {
return c.orig.LocalAddr()
}

func (c *DnsUDPPacketConn) SetDeadline(t time.Time) error {
return c.orig.SetDeadline(t)
}

func (c *DnsUDPPacketConn) SetReadDeadline(t time.Time) error {
return c.orig.SetReadDeadline(t)
}

func (c *DnsUDPPacketConn) SetWriteDeadline(t time.Time) error {
return c.orig.SetWriteDeadline(t)
}

func (c *DnsUDPPacketConn) SetReadBuffer(bytes int) error {
return c.orig.SetReadBuffer(bytes)
}

func (c *DnsUDPPacketConn) SetWriteBuffer(bytes int) error {
return c.orig.SetWriteBuffer(bytes)
}

func (c *DnsUDPPacketConn) SyscallConn() (syscall.RawConn, error) {
return c.orig.SyscallConn()
}

func (c *DnsUDPPacketConn) File() (f *os.File, err error) {
return c.orig.File()
}
29 changes: 29 additions & 0 deletions core/pktconns/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"time"

"github.com/apernet/hysteria/core/pktconns/dns"
"github.com/apernet/hysteria/core/pktconns/faketcp"
"github.com/apernet/hysteria/core/pktconns/obfs"
"github.com/apernet/hysteria/core/pktconns/udp"
Expand Down Expand Up @@ -109,6 +110,20 @@ func NewClientFakeTCPConnFunc(obfsPassword string, hopInterval time.Duration) Cl
}
}

func NewClientDnsConnFunc(domain string, hopInterval time.Duration) ClientPacketConnFunc {
return func(server string) (net.PacketConn, net.Addr, error) {
sAddr, err := net.ResolveUDPAddr("udp", server)
if err != nil {
return nil, nil, err
}
udpConn, err := net.ListenUDP("udp", nil)
if err != nil {
return nil, nil, err
}
return dns.NewDnsUDPConn(udpConn, domain), sAddr, nil
}
}

func NewServerUDPConnFunc(obfsPassword string) ServerPacketConnFunc {
if obfsPassword == "" {
return func(listen string) (net.PacketConn, error) {
Expand Down Expand Up @@ -180,6 +195,20 @@ func NewServerFakeTCPConnFunc(obfsPassword string) ServerPacketConnFunc {
}
}

func NewServerDnsConnFunc(domain string) ServerPacketConnFunc {
return func(listen string) (net.PacketConn, error) {
laddrU, err := net.ResolveUDPAddr("udp", listen)
if err != nil {
return nil, err
}
udpConn, err := net.ListenUDP("udp", laddrU)
if err != nil {
return nil, err
}
return dns.NewDnsUDPConn(udpConn, domain), nil
}
}

func isMultiPortAddr(addr string) bool {
_, portStr, err := net.SplitHostPort(addr)
if err == nil && (strings.Contains(portStr, ",") || strings.Contains(portStr, "-")) {
Expand Down