@@ -60,7 +60,7 @@ type Proxy struct {
6060 forwardFile string
6161 cloakFile string
6262 pluginsGlobals PluginsGlobals
63- urlsToPrefetch []URLToPrefetch
63+ urlsToPrefetch []* URLToPrefetch
6464 clientsCount uint32
6565 maxClients uint32
6666 xTransport * XTransport
@@ -74,7 +74,7 @@ type Proxy struct {
7474 showCerts bool
7575}
7676
77- func (proxy * Proxy ) StartProxy () {
77+ func (proxy * Proxy ) StartProxy (quit <- chan struct {} ) {
7878 proxy .questionSizeEstimator = NewQuestionSizeEstimator ()
7979 if _ , err := crypto_rand .Read (proxy .proxySecretKey [:]); err != nil {
8080 dlog .Fatal (err )
@@ -96,12 +96,16 @@ func (proxy *Proxy) StartProxy() {
9696
9797 // if 'userName' is not set, continue as before
9898 if ! (len (proxy .userName ) > 0 ) {
99- if err := proxy .udpListenerFromAddr (listenUDPAddr ); err != nil {
99+ udpCloser , err := proxy .udpListenerFromAddr (listenUDPAddr )
100+ if err != nil {
100101 dlog .Fatal (err )
101102 }
102- if err := proxy .tcpListenerFromAddr (listenTCPAddr ); err != nil {
103+ tcpCloser , err := proxy .tcpListenerFromAddr (listenTCPAddr )
104+ if err != nil {
103105 dlog .Fatal (err )
104106 }
107+ defer udpCloser .Close ()
108+ defer tcpCloser .Close ()
105109 } else {
106110 // if 'userName' is set and we are the parent process
107111 if ! proxy .child {
@@ -156,9 +160,11 @@ func (proxy *Proxy) StartProxy() {
156160 if len (proxy .userName ) > 0 && ! proxy .child {
157161 proxy .dropPrivilege (proxy .userName , FileDescriptors )
158162 }
159- if err := proxy .SystemDListeners (); err != nil {
163+ sdc , err := proxy .SystemDListeners ()
164+ if err != nil {
160165 dlog .Fatal (err )
161166 }
167+ defer sdc .Close ()
162168 liveServers , err := proxy .serversInfo .refresh (proxy )
163169 if liveServers > 0 {
164170 proxy .certIgnoreTimestamp = false
@@ -177,7 +183,7 @@ func (proxy *Proxy) StartProxy() {
177183 dlog .Error (err )
178184 dlog .Notice ("dnscrypt-proxy is waiting for at least one server to be reachable" )
179185 }
180- proxy .prefetcher (& proxy . urlsToPrefetch )
186+ go proxy .prefetcher ()
181187 if len (proxy .serversInfo .registeredServers ) > 0 {
182188 go func () {
183189 for {
@@ -193,30 +199,27 @@ func (proxy *Proxy) StartProxy() {
193199 }
194200 }()
195201 }
202+ <- quit
196203}
197204
198- func (proxy * Proxy ) prefetcher (urlsToPrefetch * []URLToPrefetch ) {
199- go func () {
200- for {
201- now := time .Now ()
202- for i := range * urlsToPrefetch {
203- urlToPrefetch := & (* urlsToPrefetch )[i ]
204- if now .After (urlToPrefetch .when ) {
205- dlog .Debugf ("Prefetching [%s]" , urlToPrefetch .url )
206- if err := PrefetchSourceURL (proxy .xTransport , urlToPrefetch ); err != nil {
207- dlog .Debugf ("Prefetching [%s] failed: %s" , urlToPrefetch .url , err )
208- } else {
209- dlog .Debugf ("Prefetching [%s] succeeded. Next refresh scheduled for %v" , urlToPrefetch .url , urlToPrefetch .when )
210- }
205+ func (proxy * Proxy ) prefetcher () {
206+ for {
207+ now := time .Now ()
208+ for _ , urlToPrefetch := range proxy .urlsToPrefetch {
209+ if now .After (urlToPrefetch .when ) {
210+ dlog .Debugf ("Prefetching [%s]" , urlToPrefetch .url )
211+ if err := PrefetchSourceURL (proxy .xTransport , urlToPrefetch ); err != nil {
212+ dlog .Debugf ("Prefetching [%s] failed: %s" , urlToPrefetch .url , err )
213+ } else {
214+ dlog .Debugf ("Prefetching [%s] succeeded. Next refresh scheduled for %v" , urlToPrefetch .url , urlToPrefetch .when )
211215 }
212216 }
213- clocksmith .Sleep (60 * time .Second )
214217 }
215- }()
218+ clocksmith .Sleep (60 * time .Second )
219+ }
216220}
217221
218222func (proxy * Proxy ) udpListener (clientPc * net.UDPConn ) {
219- defer clientPc .Close ()
220223 for {
221224 buffer := make ([]byte , MaxDNSPacketSize - 1 )
222225 length , clientAddr , err := clientPc .ReadFrom (buffer )
@@ -236,18 +239,17 @@ func (proxy *Proxy) udpListener(clientPc *net.UDPConn) {
236239 }
237240}
238241
239- func (proxy * Proxy ) udpListenerFromAddr (listenAddr * net.UDPAddr ) error {
242+ func (proxy * Proxy ) udpListenerFromAddr (listenAddr * net.UDPAddr ) (io. Closer , error ) {
240243 clientPc , err := net .ListenUDP ("udp" , listenAddr )
241244 if err != nil {
242- return err
245+ return nil , err
243246 }
244247 dlog .Noticef ("Now listening to %v [UDP]" , listenAddr )
245248 go proxy .udpListener (clientPc )
246- return nil
249+ return clientPc , nil
247250}
248251
249252func (proxy * Proxy ) tcpListener (acceptPc * net.TCPListener ) {
250- defer acceptPc .Close ()
251253 for {
252254 clientPc , err := acceptPc .Accept ()
253255 if err != nil {
@@ -272,14 +274,14 @@ func (proxy *Proxy) tcpListener(acceptPc *net.TCPListener) {
272274 }
273275}
274276
275- func (proxy * Proxy ) tcpListenerFromAddr (listenAddr * net.TCPAddr ) error {
277+ func (proxy * Proxy ) tcpListenerFromAddr (listenAddr * net.TCPAddr ) (io. Closer , error ) {
276278 acceptPc , err := net .ListenTCP ("tcp" , listenAddr )
277279 if err != nil {
278- return err
280+ return nil , err
279281 }
280282 dlog .Noticef ("Now listening to %v [TCP]" , listenAddr )
281283 go proxy .tcpListener (acceptPc )
282- return nil
284+ return acceptPc , nil
283285}
284286
285287func (proxy * Proxy ) prepareForRelay (ip net.IP , port int , encryptedQuery * []byte ) {
0 commit comments