2018-01-09 00:24:51 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2018-01-09 16:40:37 +01:00
|
|
|
"encoding/binary"
|
2018-01-09 00:24:51 +01:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/curve25519"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Proxy struct {
|
2018-01-09 16:40:37 +01:00
|
|
|
proxyPublicKey [32]byte
|
|
|
|
proxySecretKey [32]byte
|
|
|
|
questionSizeEstimator QuestionSizeEstimator
|
2018-01-09 16:59:06 +01:00
|
|
|
serversInfo ServersInfo
|
2018-01-09 16:40:37 +01:00
|
|
|
timeout time.Duration
|
2018-01-09 00:24:51 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 13:35:10 +01:00
|
|
|
func main() {
|
|
|
|
log.SetFlags(0)
|
2018-01-09 17:15:07 +01:00
|
|
|
NewProxy("127.0.0.1:5399", "dnscrypt.org-fr", "212.47.228.136:443", "E801:B84E:A606:BFB0:BAC0:CE43:445B:B15E:BA64:B02F:A3C4:AA31:AE10:636A:0790:324D", "2.dnscrypt-cert.fr.dnscrypt.org")
|
2018-01-09 00:24:51 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 17:15:07 +01:00
|
|
|
func NewProxy(listenAddrStr string, serverName string, serverAddrStr string, serverPkStr string, providerName string) {
|
2018-01-09 16:40:37 +01:00
|
|
|
proxy := Proxy{questionSizeEstimator: NewQuestionSizeEstimator(), timeout: TimeoutMax}
|
2018-01-09 00:24:51 +01:00
|
|
|
if _, err := rand.Read(proxy.proxySecretKey[:]); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
curve25519.ScalarBaseMult(&proxy.proxyPublicKey, &proxy.proxySecretKey)
|
2018-01-09 17:34:19 +01:00
|
|
|
stamp, _ := NewServerStampFromLegacy(serverName, serverAddrStr, serverPkStr, providerName)
|
|
|
|
proxy.serversInfo.registerServer(&proxy, serverName, stamp)
|
2018-01-09 16:40:37 +01:00
|
|
|
listenUDPAddr, err := net.ResolveUDPAddr("udp", listenAddrStr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
listenTCPAddr, err := net.ResolveTCPAddr("tcp", listenAddrStr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
proxy.udpListener(listenUDPAddr)
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
proxy.tcpListener(listenTCPAddr)
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
time.Sleep(30 * time.Minute)
|
2018-01-09 17:34:19 +01:00
|
|
|
proxy.serversInfo.refresh(&proxy)
|
2018-01-09 13:35:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:40:37 +01:00
|
|
|
func (proxy *Proxy) udpListener(listenAddr *net.UDPAddr) error {
|
|
|
|
clientPc, err := net.ListenUDP("udp", listenAddr)
|
2018-01-09 00:24:51 +01:00
|
|
|
if err != nil {
|
2018-01-09 13:35:10 +01:00
|
|
|
return err
|
2018-01-09 00:24:51 +01:00
|
|
|
}
|
|
|
|
defer clientPc.Close()
|
2018-01-09 16:40:37 +01:00
|
|
|
fmt.Printf("Now listening to %v [UDP]\n", listenAddr)
|
2018-01-09 00:24:51 +01:00
|
|
|
for {
|
2018-01-09 16:40:37 +01:00
|
|
|
buffer := make([]byte, MaxDNSPacketSize-1)
|
2018-01-09 00:24:51 +01:00
|
|
|
length, clientAddr, err := clientPc.ReadFrom(buffer)
|
|
|
|
if err != nil {
|
2018-01-09 13:35:10 +01:00
|
|
|
return err
|
2018-01-09 00:24:51 +01:00
|
|
|
}
|
|
|
|
packet := buffer[:length]
|
|
|
|
go func() {
|
2018-01-09 16:59:06 +01:00
|
|
|
proxy.processIncomingQuery(proxy.serversInfo.getOne(), packet, &clientAddr, clientPc)
|
2018-01-09 16:40:37 +01:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (proxy *Proxy) tcpListener(listenAddr *net.TCPAddr) error {
|
|
|
|
acceptPc, err := net.ListenTCP("tcp", listenAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer acceptPc.Close()
|
|
|
|
fmt.Printf("Now listening to %v [TCP]\n", listenAddr)
|
|
|
|
for {
|
|
|
|
clientPc, err := acceptPc.Accept()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
defer clientPc.Close()
|
|
|
|
clientPc.SetDeadline(time.Now().Add(proxy.timeout))
|
|
|
|
buffer := make([]byte, 2+MaxDNSPacketSize-1)
|
|
|
|
length, err := clientPc.Read(buffer)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
innerLength := binary.BigEndian.Uint16(buffer[0:2])
|
|
|
|
if int(innerLength) > length-2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
packet := buffer[2:length]
|
2018-01-09 16:59:06 +01:00
|
|
|
proxy.processIncomingQuery(proxy.serversInfo.getOne(), packet, nil, clientPc)
|
2018-01-09 00:24:51 +01:00
|
|
|
}()
|
|
|
|
}
|
2018-01-09 13:27:03 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:40:37 +01:00
|
|
|
func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, packet []byte, clientAddr *net.Addr, clientPc net.Conn) {
|
2018-01-09 13:27:03 +01:00
|
|
|
if len(packet) < MinDNSPacketSize {
|
|
|
|
return
|
|
|
|
}
|
2018-01-09 16:40:37 +01:00
|
|
|
encrypted, clientNonce, err := proxy.Encrypt(serverInfo, packet, "udp")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-01-09 00:24:51 +01:00
|
|
|
pc, err := net.DialUDP("udp", nil, serverInfo.UDPAddr)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pc.SetDeadline(time.Now().Add(serverInfo.Timeout))
|
|
|
|
pc.Write(encrypted)
|
2018-01-09 13:27:03 +01:00
|
|
|
|
|
|
|
encrypted = make([]byte, MaxDNSPacketSize)
|
|
|
|
length, err := pc.Read(encrypted)
|
2018-01-09 16:40:37 +01:00
|
|
|
pc.Close()
|
2018-01-09 00:24:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-01-09 13:27:03 +01:00
|
|
|
encrypted = encrypted[:length]
|
|
|
|
packet, err = proxy.Decrypt(serverInfo, encrypted, clientNonce)
|
2018-01-09 00:24:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-01-09 16:40:37 +01:00
|
|
|
if clientAddr != nil {
|
|
|
|
clientPc.(net.PacketConn).WriteTo(packet, *clientAddr)
|
|
|
|
} else {
|
|
|
|
packet = append(append(packet, 0), 0)
|
|
|
|
copy(packet[2:], packet[:len(packet)-2])
|
|
|
|
binary.BigEndian.PutUint16(packet[0:2], uint16(len(packet)-2))
|
|
|
|
clientPc.Write(packet)
|
|
|
|
}
|
2018-01-09 13:27:03 +01:00
|
|
|
if HasTCFlag(packet) {
|
2018-01-09 16:40:37 +01:00
|
|
|
proxy.questionSizeEstimator.blindAdjust()
|
2018-01-09 00:24:51 +01:00
|
|
|
}
|
|
|
|
}
|