mirror of
https://github.com/DNSCrypt/dnscrypt-proxy.git
synced 2024-12-25 00:00:55 +01:00
Refresh certificates
We may later want to register a stamp even if no certificate was found
This commit is contained in:
parent
7279699210
commit
4dc3fee9a0
@ -30,7 +30,8 @@ func NewProxy(listenAddrStr string, serverName string, serverAddrStr string, ser
|
||||
log.Fatal(err)
|
||||
}
|
||||
curve25519.ScalarBaseMult(&proxy.proxyPublicKey, &proxy.proxySecretKey)
|
||||
proxy.serversInfo.registerServer(&proxy, serverName, serverAddrStr, serverPkStr, providerName)
|
||||
stamp, _ := NewServerStampFromLegacy(serverName, serverAddrStr, serverPkStr, providerName)
|
||||
proxy.serversInfo.registerServer(&proxy, serverName, stamp)
|
||||
listenUDPAddr, err := net.ResolveUDPAddr("udp", listenAddrStr)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@ -47,7 +48,7 @@ func NewProxy(listenAddrStr string, serverName string, serverAddrStr string, ser
|
||||
}()
|
||||
for {
|
||||
time.Sleep(30 * time.Minute)
|
||||
// Refresh certificates
|
||||
proxy.serversInfo.refresh(&proxy)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net"
|
||||
@ -45,24 +46,35 @@ type ServersInfo struct {
|
||||
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)
|
||||
func (serversInfo *ServersInfo) registerServer(proxy *Proxy, name string, stamp ServerStamp) error {
|
||||
serversInfo.Lock()
|
||||
defer serversInfo.Unlock()
|
||||
newServer, err := serversInfo.fetchServerInfo(proxy, name, stamp)
|
||||
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()
|
||||
serversInfo.serverStamps = append(serversInfo.serverStamps, stamp)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (serversInfo *ServersInfo) refresh(proxy *Proxy) {
|
||||
fmt.Println("Refreshing certificates")
|
||||
serversInfo.RLock()
|
||||
stamps := serversInfo.serverStamps
|
||||
serversInfo.RUnlock()
|
||||
for _, stamp := range stamps {
|
||||
serversInfo.registerServer(proxy, stamp.name, stamp)
|
||||
_ = stamp
|
||||
}
|
||||
}
|
||||
|
||||
func (serversInfo *ServersInfo) getOne() *ServerInfo {
|
||||
serversInfo.RLock()
|
||||
serverInfo := &serversInfo.inner[rand.Intn(len(serversInfo.inner))]
|
||||
@ -70,20 +82,20 @@ func (serversInfo *ServersInfo) getOne() *ServerInfo {
|
||||
return serverInfo
|
||||
}
|
||||
|
||||
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 {
|
||||
func (serversInfo *ServersInfo) fetchServerInfo(proxy *Proxy, name string, stamp ServerStamp) (ServerInfo, error) {
|
||||
serverPk, err := hex.DecodeString(strings.Replace(stamp.serverPkStr, ":", "", -1))
|
||||
if err != nil || len(serverPk) != ed25519.PublicKeySize {
|
||||
log.Fatal("Invalid public key")
|
||||
}
|
||||
certInfo, err := FetchCurrentCert(proxy, serverPublicKey, serverAddrStr, providerName)
|
||||
certInfo, err := FetchCurrentCert(proxy, serverPk, stamp.serverAddrStr, stamp.providerName)
|
||||
if err != nil {
|
||||
return ServerInfo{}, err
|
||||
}
|
||||
remoteUDPAddr, err := net.ResolveUDPAddr("udp", serverAddrStr)
|
||||
remoteUDPAddr, err := net.ResolveUDPAddr("udp", stamp.serverAddrStr)
|
||||
if err != nil {
|
||||
return ServerInfo{}, err
|
||||
}
|
||||
remoteTCPAddr, err := net.ResolveTCPAddr("tcp", serverAddrStr)
|
||||
remoteTCPAddr, err := net.ResolveTCPAddr("tcp", stamp.serverAddrStr)
|
||||
if err != nil {
|
||||
return ServerInfo{}, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user