Refresh certificates

We may later want to register a stamp even if no certificate was found
This commit is contained in:
Frank Denis 2018-01-09 17:34:19 +01:00
parent 7279699210
commit 4dc3fee9a0
2 changed files with 26 additions and 13 deletions

View File

@ -30,7 +30,8 @@ func NewProxy(listenAddrStr string, serverName string, serverAddrStr string, ser
log.Fatal(err) log.Fatal(err)
} }
curve25519.ScalarBaseMult(&proxy.proxyPublicKey, &proxy.proxySecretKey) 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) listenUDPAddr, err := net.ResolveUDPAddr("udp", listenAddrStr)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -47,7 +48,7 @@ func NewProxy(listenAddrStr string, serverName string, serverAddrStr string, ser
}() }()
for { for {
time.Sleep(30 * time.Minute) time.Sleep(30 * time.Minute)
// Refresh certificates proxy.serversInfo.refresh(&proxy)
} }
} }

View File

@ -2,6 +2,7 @@ package main
import ( import (
"encoding/hex" "encoding/hex"
"fmt"
"log" "log"
"math/rand" "math/rand"
"net" "net"
@ -45,24 +46,35 @@ type ServersInfo struct {
serverStamps []ServerStamp serverStamps []ServerStamp
} }
func (serversInfo *ServersInfo) registerServer(proxy *Proxy, name string, serverAddrStr string, serverPkStr string, providerName string) error { func (serversInfo *ServersInfo) registerServer(proxy *Proxy, name string, stamp ServerStamp) error {
newServer, err := serversInfo.fetchServerInfo(proxy, name, serverAddrStr, serverPkStr, providerName) serversInfo.Lock()
defer serversInfo.Unlock()
newServer, err := serversInfo.fetchServerInfo(proxy, name, stamp)
if err != nil { if err != nil {
return err return err
} }
serversInfo.Lock()
for i, oldServer := range serversInfo.inner { for i, oldServer := range serversInfo.inner {
if oldServer.Name == newServer.Name { if oldServer.Name == newServer.Name {
serversInfo.inner[i] = newServer serversInfo.inner[i] = newServer
serversInfo.Unlock()
return nil return nil
} }
} }
serversInfo.inner = append(serversInfo.inner, newServer) serversInfo.inner = append(serversInfo.inner, newServer)
serversInfo.Unlock() serversInfo.serverStamps = append(serversInfo.serverStamps, stamp)
return nil 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 { func (serversInfo *ServersInfo) getOne() *ServerInfo {
serversInfo.RLock() serversInfo.RLock()
serverInfo := &serversInfo.inner[rand.Intn(len(serversInfo.inner))] serverInfo := &serversInfo.inner[rand.Intn(len(serversInfo.inner))]
@ -70,20 +82,20 @@ func (serversInfo *ServersInfo) getOne() *ServerInfo {
return serverInfo return serverInfo
} }
func (serversInfo *ServersInfo) fetchServerInfo(proxy *Proxy, name string, serverAddrStr string, serverPkStr string, providerName string) (ServerInfo, error) { func (serversInfo *ServersInfo) fetchServerInfo(proxy *Proxy, name string, stamp ServerStamp) (ServerInfo, error) {
serverPublicKey, err := hex.DecodeString(strings.Replace(serverPkStr, ":", "", -1)) serverPk, err := hex.DecodeString(strings.Replace(stamp.serverPkStr, ":", "", -1))
if err != nil || len(serverPublicKey) != ed25519.PublicKeySize { if err != nil || len(serverPk) != ed25519.PublicKeySize {
log.Fatal("Invalid public key") log.Fatal("Invalid public key")
} }
certInfo, err := FetchCurrentCert(proxy, serverPublicKey, serverAddrStr, providerName) certInfo, err := FetchCurrentCert(proxy, serverPk, stamp.serverAddrStr, stamp.providerName)
if err != nil { if err != nil {
return ServerInfo{}, err return ServerInfo{}, err
} }
remoteUDPAddr, err := net.ResolveUDPAddr("udp", serverAddrStr) remoteUDPAddr, err := net.ResolveUDPAddr("udp", stamp.serverAddrStr)
if err != nil { if err != nil {
return ServerInfo{}, err return ServerInfo{}, err
} }
remoteTCPAddr, err := net.ResolveTCPAddr("tcp", serverAddrStr) remoteTCPAddr, err := net.ResolveTCPAddr("tcp", stamp.serverAddrStr)
if err != nil { if err != nil {
return ServerInfo{}, err return ServerInfo{}, err
} }