@@ -2,7 +2,6 @@ package main
22
33import (
44 "context"
5- crypto_rand "crypto/rand"
65 "encoding/binary"
76 "net"
87 "os"
@@ -15,61 +14,63 @@ import (
1514 "github.com/jedisct1/dlog"
1615 clocksmith "github.com/jedisct1/go-clocksmith"
1716 stamps "github.com/jedisct1/go-dnsstamps"
18- "golang.org/x/crypto/curve25519"
1917 netproxy "golang.org/x/net/proxy"
2018)
2119
2220type Proxy struct {
23- pluginsGlobals PluginsGlobals
24- serversInfo ServersInfo
25- questionSizeEstimator QuestionSizeEstimator
26- registeredServers []RegisteredServer
27- dns64Resolvers []string
28- dns64Prefixes []string
29- serversBlockingFragments []string
30- ednsClientSubnets []* net.IPNet
31- queryLogIgnoredQtypes []string
32- localDoHListeners []* net.TCPListener
33- queryMeta []string
34- enableHotReload bool
35- udpListeners []* net.UDPConn
36- sources []* Source
37- tcpListeners []* net.TCPListener
38- registeredRelays []RegisteredServer
39- listenAddresses []string
40- localDoHListenAddresses []string
41- monitoringUI MonitoringUIConfig
42- monitoringInstance * MonitoringUI
43- xTransport * XTransport
44- allWeeklyRanges * map [string ]WeeklyRanges
45- routes * map [string ][]string
46- captivePortalMap * CaptivePortalMap
47- nxLogFormat string
48- localDoHCertFile string
49- localDoHCertKeyFile string
50- captivePortalMapFile string
51- localDoHPath string
52- cloakFile string
53- forwardFile string
54- blockIPFormat string
55- blockIPLogFile string
56- allowedIPFile string
57- allowedIPFormat string
58- allowedIPLogFile string
59- queryLogFormat string
60- blockIPFile string
61- allowNameFile string
62- allowNameFormat string
63- allowNameLogFile string
64- blockNameLogFile string
65- blockNameFormat string
66- blockNameFile string
67- queryLogFile string
68- blockedQueryResponse string
69- userName string
70- nxLogFile string
71- proxySecretKey [32 ]byte
72- proxyPublicKey [32 ]byte
21+ pluginsGlobals PluginsGlobals
22+ serversInfo ServersInfo
23+ questionSizeEstimator QuestionSizeEstimator
24+ registeredServers []RegisteredServer
25+ dns64Resolvers []string
26+ dns64Prefixes []string
27+ serversBlockingFragments []string
28+ ednsClientSubnets []* net.IPNet
29+ queryLogIgnoredQtypes []string
30+ localDoHListeners []* net.TCPListener
31+ queryMeta []string
32+ enableHotReload bool
33+ udpListeners []* net.UDPConn
34+ sources []* Source
35+ tcpListeners []* net.TCPListener
36+ registeredRelays []RegisteredServer
37+ listenAddresses []string
38+ localDoHListenAddresses []string
39+ monitoringUI MonitoringUIConfig
40+ monitoringInstance * MonitoringUI
41+ xTransport * XTransport
42+ allWeeklyRanges * map [string ]WeeklyRanges
43+ routes * map [string ][]string
44+ captivePortalMap * CaptivePortalMap
45+ nxLogFormat string
46+ localDoHCertFile string
47+ localDoHCertKeyFile string
48+ captivePortalMapFile string
49+ localDoHPath string
50+ cloakFile string
51+ forwardFile string
52+ blockIPFormat string
53+ blockIPLogFile string
54+ allowedIPFile string
55+ allowedIPFormat string
56+ allowedIPLogFile string
57+ queryLogFormat string
58+ blockIPFile string
59+ allowNameFile string
60+ allowNameFormat string
61+ allowNameLogFile string
62+ blockNameLogFile string
63+ blockNameFormat string
64+ blockNameFile string
65+ queryLogFile string
66+ blockedQueryResponse string
67+ userName string
68+ nxLogFile string
69+ proxySecretKey [32 ]byte
70+ proxyPublicKey [32 ]byte
71+ // cryptoKeyMu guards proxySecretKey, proxyPublicKey, and the classic
72+ // SharedKey of every ServerInfo while the client key is rotated.
73+ cryptoKeyMu sync.RWMutex
7374 ServerNames []string
7475 DisabledServerNames []string
7576 requiredProps stamps.ServerInformalProperties
@@ -130,6 +131,17 @@ func (proxy *Proxy) registerLocalDoHListener(listener *net.TCPListener) {
130131 proxy .listenersMu .Unlock ()
131132}
132133
134+ func (proxy * Proxy ) handleNetworkChange () {
135+ if proxy .ephemeralKeys {
136+ return
137+ }
138+ if err := proxy .rotateDNSCryptClientKey (); err != nil {
139+ dlog .Errorf ("Unable to rotate DNSCrypt client key after network change: %v" , err )
140+ return
141+ }
142+ dlog .Notice ("Rotated DNSCrypt client key after network change" )
143+ }
144+
133145func (proxy * Proxy ) addDNSListener (listenAddrStr string ) {
134146 udp := "udp"
135147 tcp := "tcp"
@@ -264,11 +276,11 @@ func (proxy *Proxy) StartProxy() {
264276 proxy .questionSizeEstimator = NewQuestionSizeEstimator ()
265277 proxy .netMonitor = newNetworkMonitor ()
266278 proxy .netMonitor .init ()
267- go proxy .netMonitor .start ( context . Background (), defaultNetworkMonitorInterval )
268- if _ , err := crypto_rand . Read ( proxy .proxySecretKey [:] ); err != nil {
279+ proxy .netMonitor .onChange = proxy . handleNetworkChange
280+ if err := proxy .initDNSCryptClientKey ( ); err != nil {
269281 dlog .Fatal (err )
270282 }
271- curve25519 . ScalarBaseMult ( & proxy . proxyPublicKey , & proxy . proxySecretKey )
283+ go proxy . netMonitor . start ( context . Background (), defaultNetworkMonitorInterval )
272284
273285 // Initialize and start the monitoring UI if enabled
274286 if proxy .monitoringUI .Enabled {
0 commit comments