Store upstream names, prepare for dnscrypt v3 stamps

This commit is contained in:
Frank Denis 2018-01-09 17:15:07 +01:00
parent 721313a603
commit 805c8a822f
3 changed files with 46 additions and 20 deletions

View File

@ -1,7 +1,6 @@
package main
import (
"net"
"time"
)
@ -24,16 +23,6 @@ var (
TimeoutMax = 5 * time.Second
)
type ServerInfo struct {
MagicQuery [8]byte
ServerPk [32]byte
SharedKey [32]byte
CryptoConstruction CryptoConstruction
Timeout time.Duration
UDPAddr *net.UDPAddr
TCPAddr *net.TCPAddr
}
func HasTCFlag(packet []byte) bool {
return packet[2]&2 == 2
}

View File

@ -21,16 +21,16 @@ type Proxy struct {
func main() {
log.SetFlags(0)
NewProxy("127.0.0.1:5399", "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")
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")
}
func NewProxy(listenAddrStr string, serverAddrStr string, serverPkStr string, providerName string) {
func NewProxy(listenAddrStr string, serverName string, serverAddrStr string, serverPkStr string, providerName string) {
proxy := Proxy{questionSizeEstimator: NewQuestionSizeEstimator(), timeout: TimeoutMax}
if _, err := rand.Read(proxy.proxySecretKey[:]); err != nil {
log.Fatal(err)
}
curve25519.ScalarBaseMult(&proxy.proxyPublicKey, &proxy.proxySecretKey)
proxy.serversInfo.registerServer(&proxy, serverAddrStr, serverPkStr, providerName)
proxy.serversInfo.registerServer(&proxy, serverName, serverAddrStr, serverPkStr, providerName)
listenUDPAddr, err := net.ResolveUDPAddr("udp", listenAddrStr)
if err != nil {
log.Fatal(err)

View File

@ -6,21 +6,57 @@ import (
"net"
"strings"
"sync"
"time"
"golang.org/x/crypto/ed25519"
)
type ServersInfo struct {
sync.RWMutex
inner []ServerInfo
type ServerStamp struct {
name string
serverAddrStr string
serverPkStr string
providerName string
}
func (serversInfo *ServersInfo) registerServer(proxy *Proxy, serverAddrStr string, serverPkStr string, providerName string) error {
newServer, err := serversInfo.fetchServerInfo(proxy, serverAddrStr, serverPkStr, providerName)
func NewServerStampFromLegacy(name string, serverAddrStr string, serverPkStr string, providerName string) (ServerStamp, error) {
return ServerStamp{
name: name,
serverAddrStr: serverAddrStr,
serverPkStr: serverPkStr,
providerName: providerName,
}, nil
}
type ServerInfo struct {
MagicQuery [8]byte
ServerPk [32]byte
SharedKey [32]byte
CryptoConstruction CryptoConstruction
Name string
Timeout time.Duration
UDPAddr *net.UDPAddr
TCPAddr *net.TCPAddr
}
type ServersInfo struct {
sync.RWMutex
inner []ServerInfo
serverStamps []ServerStamp
}
func (serversInfo *ServersInfo) registerServer(proxy *Proxy, name string, serverAddrStr string, serverPkStr string, providerName string) error {
newServer, err := serversInfo.fetchServerInfo(proxy, name, serverAddrStr, serverPkStr, providerName)
if err != nil {
return err
}
serversInfo.Lock()
for i, oldServer := range serversInfo.inner {
if oldServer.Name == newServer.Name {
serversInfo.inner[i] = newServer
serversInfo.Unlock()
return nil
}
}
serversInfo.inner = append(serversInfo.inner, newServer)
serversInfo.Unlock()
return nil
@ -33,7 +69,7 @@ func (serversInfo *ServersInfo) getOne() *ServerInfo {
return serverInfo
}
func (serversInfo *ServersInfo) fetchServerInfo(proxy *Proxy, serverAddrStr string, serverPkStr string, providerName string) (ServerInfo, error) {
func (serversInfo *ServersInfo) fetchServerInfo(proxy *Proxy, name string, serverAddrStr string, serverPkStr string, providerName string) (ServerInfo, error) {
serverPublicKey, err := hex.DecodeString(strings.Replace(serverPkStr, ":", "", -1))
if err != nil || len(serverPublicKey) != ed25519.PublicKeySize {
log.Fatal("Invalid public key")
@ -55,6 +91,7 @@ func (serversInfo *ServersInfo) fetchServerInfo(proxy *Proxy, serverAddrStr stri
ServerPk: certInfo.ServerPk,
SharedKey: certInfo.SharedKey,
CryptoConstruction: certInfo.CryptoConstruction,
Name: name,
Timeout: TimeoutMin,
UDPAddr: remoteUDPAddr,
TCPAddr: remoteTCPAddr,