mirror of
https://github.com/DNSCrypt/dnscrypt-proxy.git
synced 2025-03-04 02:14:40 +01:00
Allow arbitrary addresses to be set in listen_addresses
Only works on OpenBSD/FreeBSD/Linux (including Android) Fixes #1362
This commit is contained in:
parent
02a6ca1098
commit
f9c11f0897
7 changed files with 207 additions and 15 deletions
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
crypto_rand "crypto/rand"
|
crypto_rand "crypto/rand"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"net"
|
"net"
|
||||||
|
@ -288,16 +289,6 @@ func (proxy *Proxy) udpListener(clientPc *net.UDPConn) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (proxy *Proxy) udpListenerFromAddr(listenAddr *net.UDPAddr) error {
|
|
||||||
clientPc, err := net.ListenUDP("udp", listenAddr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
proxy.registerUDPListener(clientPc)
|
|
||||||
dlog.Noticef("Now listening to %v [UDP]", listenAddr)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (proxy *Proxy) tcpListener(acceptPc *net.TCPListener) {
|
func (proxy *Proxy) tcpListener(acceptPc *net.TCPListener) {
|
||||||
defer acceptPc.Close()
|
defer acceptPc.Close()
|
||||||
for {
|
for {
|
||||||
|
@ -326,22 +317,44 @@ func (proxy *Proxy) tcpListener(acceptPc *net.TCPListener) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (proxy *Proxy) tcpListenerFromAddr(listenAddr *net.TCPAddr) error {
|
func (proxy *Proxy) udpListenerFromAddr(listenAddr *net.UDPAddr) error {
|
||||||
acceptPc, err := net.ListenTCP("tcp", listenAddr)
|
listenConfig, err := proxy.udpListenerConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
proxy.registerTCPListener(acceptPc)
|
clientPc, err := listenConfig.ListenPacket(context.Background(), "udp", listenAddr.String())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
proxy.registerUDPListener(clientPc.(*net.UDPConn))
|
||||||
|
dlog.Noticef("Now listening to %v [UDP]", listenAddr)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (proxy *Proxy) tcpListenerFromAddr(listenAddr *net.TCPAddr) error {
|
||||||
|
listenConfig, err := proxy.tcpListenerConfig()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
acceptPc, err := listenConfig.Listen(context.Background(), "tcp", listenAddr.String())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
proxy.registerTCPListener(acceptPc.(*net.TCPListener))
|
||||||
dlog.Noticef("Now listening to %v [TCP]", listenAddr)
|
dlog.Noticef("Now listening to %v [TCP]", listenAddr)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (proxy *Proxy) localDoHListenerFromAddr(listenAddr *net.TCPAddr) error {
|
func (proxy *Proxy) localDoHListenerFromAddr(listenAddr *net.TCPAddr) error {
|
||||||
acceptPc, err := net.ListenTCP("tcp", listenAddr)
|
listenConfig, err := proxy.tcpListenerConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
proxy.registerLocalDoHListener(acceptPc)
|
acceptPc, err := listenConfig.Listen(context.Background(), "tcp", listenAddr.String())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
proxy.registerLocalDoHListener(acceptPc.(*net.TCPListener))
|
||||||
dlog.Noticef("Now listening to https://%v%v [DoH]", listenAddr, proxy.localDoHPath)
|
dlog.Noticef("Now listening to https://%v%v [DoH]", listenAddr, proxy.localDoHPath)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
31
dnscrypt-proxy/setsockopts_darwin.go
Normal file
31
dnscrypt-proxy/setsockopts_darwin.go
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (proxy *Proxy) udpListenerConfig() (*net.ListenConfig, error) {
|
||||||
|
return &net.ListenConfig{
|
||||||
|
Control: func(network, address string, c syscall.RawConn) error {
|
||||||
|
_ = c.Control(func(fd uintptr) {
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_DF, 0)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_TOS, 0x70)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_RCVBUF, 4096)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_SNDBUF, 4096)
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (proxy *Proxy) tcpListenerConfig() (*net.ListenConfig, error) {
|
||||||
|
return &net.ListenConfig{
|
||||||
|
Control: func(network, address string, c syscall.RawConn) error {
|
||||||
|
_ = c.Control(func(fd uintptr) {
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_TOS, 0x70)
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
35
dnscrypt-proxy/setsockopts_freebsd.go
Normal file
35
dnscrypt-proxy/setsockopts_freebsd.go
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (proxy *Proxy) udpListenerConfig() (*net.ListenConfig, error) {
|
||||||
|
return &net.ListenConfig{
|
||||||
|
Control: func(network, address string, c syscall.RawConn) error {
|
||||||
|
_ = c.Control(func(fd uintptr) {
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_BINDANY, 1)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_BINDANY, 1)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_DF, 0)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_TOS, 0x70)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_RCVBUF, 4096)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_SNDBUF, 4096)
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (proxy *Proxy) tcpListenerConfig() (*net.ListenConfig, error) {
|
||||||
|
return &net.ListenConfig{
|
||||||
|
Control: func(network, address string, c syscall.RawConn) error {
|
||||||
|
_ = c.Control(func(fd uintptr) {
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_BINDANY, 1)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_BINDANY, 1)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_TOS, 0x70)
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
35
dnscrypt-proxy/setsockopts_linux.go
Normal file
35
dnscrypt-proxy/setsockopts_linux.go
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (proxy *Proxy) udpListenerConfig() (*net.ListenConfig, error) {
|
||||||
|
return &net.ListenConfig{
|
||||||
|
Control: func(network, address string, c syscall.RawConn) error {
|
||||||
|
_ = c.Control(func(fd uintptr) {
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_FREEBIND, 1)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_DF, 0)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_TOS, 0x70)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_MTU_DISCOVER, syscall.IP_PMTUDISC_DONT)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_RCVBUFFORCE, 4096)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_SNDBUFFORCE, 4096)
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (proxy *Proxy) tcpListenerConfig() (*net.ListenConfig, error) {
|
||||||
|
return &net.ListenConfig{
|
||||||
|
Control: func(network, address string, c syscall.RawConn) error {
|
||||||
|
_ = c.Control(func(fd uintptr) {
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_FREEBIND, 1)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_TOS, 0x70)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_QUICKACK, 1)
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
33
dnscrypt-proxy/setsockopts_openbsd.go
Normal file
33
dnscrypt-proxy/setsockopts_openbsd.go
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (proxy *Proxy) udpListenerConfig() (*net.ListenConfig, error) {
|
||||||
|
return &net.ListenConfig{
|
||||||
|
Control: func(network, address string, c syscall.RawConn) error {
|
||||||
|
_ = c.Control(func(fd uintptr) {
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_BINDANY, 1)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_DF, 0)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_TOS, 0x70)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_RCVBUF, 4096)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_SNDBUF, 4096)
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (proxy *Proxy) tcpListenerConfig() (*net.ListenConfig, error) {
|
||||||
|
return &net.ListenConfig{
|
||||||
|
Control: func(network, address string, c syscall.RawConn) error {
|
||||||
|
_ = c.Control(func(fd uintptr) {
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_BINDANY, 1)
|
||||||
|
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_TOS, 0x70)
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
15
dnscrypt-proxy/setsockopts_others.go
Normal file
15
dnscrypt-proxy/setsockopts_others.go
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
// +build !freebsd,!openbsd,!windows,!darwin,!linux
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (proxy *Proxy) udpListenerConfig() (*net.ListenConfig, error) {
|
||||||
|
return &net.ListenConfig{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (proxy *Proxy) tcpListenerConfig() (*net.ListenConfig, error) {
|
||||||
|
return &net.ListenConfig{}, nil
|
||||||
|
}
|
30
dnscrypt-proxy/setsockopts_windows.go
Normal file
30
dnscrypt-proxy/setsockopts_windows.go
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (proxy *Proxy) udpListenerConfig() (*net.ListenConfig, error) {
|
||||||
|
return &net.ListenConfig{
|
||||||
|
Control: func(network, address string, c syscall.RawConn) error {
|
||||||
|
_ = c.Control(func(fd uintptr) {
|
||||||
|
_ = syscall.SetsockoptInt(syscall.Handle(fd), syscall.IPPROTO_IP, syscall.IP_TOS, 0x70)
|
||||||
|
_ = syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_RCVBUF, 4096)
|
||||||
|
_ = syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_SNDBUF, 4096)
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (proxy *Proxy) tcpListenerConfig() (*net.ListenConfig, error) {
|
||||||
|
return &net.ListenConfig{
|
||||||
|
Control: func(network, address string, c syscall.RawConn) error {
|
||||||
|
_ = c.Control(func(fd uintptr) {
|
||||||
|
_ = syscall.SetsockoptInt(syscall.Handle(fd), syscall.IPPROTO_IP, syscall.IP_TOS, 0x70)
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue