2019-10-20 12:03:20 +02:00
// +build !android
2019-10-03 16:11:02 +02:00
2018-01-24 15:14:48 +01:00
package main
import (
2019-10-11 11:54:00 +02:00
"fmt"
2019-10-31 17:36:59 +01:00
"io"
2018-01-24 15:14:48 +01:00
"net"
"github.com/coreos/go-systemd/activation"
"github.com/jedisct1/dlog"
)
2019-10-31 17:36:59 +01:00
type multiCloser [ ] io . Closer
func ( mc multiCloser ) Close ( ) ( err error ) {
for _ , c := range mc {
err = c . Close ( )
}
return err
}
func ( proxy * Proxy ) SystemDListeners ( ) ( io . Closer , error ) {
2018-06-13 16:49:32 +02:00
files := activation . Files ( true )
2019-09-16 15:46:39 +02:00
if len ( files ) > 0 {
2019-10-03 15:46:37 +02:00
if len ( proxy . userName ) > 0 || proxy . child {
2019-09-16 15:46:39 +02:00
dlog . Fatal ( "Systemd activated sockets are incompatible with privilege dropping. Remove activated sockets and fill `listen_addresses` in the dnscrypt-proxy configuration file instead." )
}
dlog . Warn ( "Systemd sockets are untested and unsupported - use at your own risk" )
2018-07-07 19:44:11 +02:00
}
2019-10-31 17:36:59 +01:00
var mc multiCloser
2018-06-13 16:49:32 +02:00
for i , file := range files {
2019-10-11 11:54:00 +02:00
defer file . Close ( )
ok := false
2018-06-13 16:49:32 +02:00
if listener , err := net . FileListener ( file ) ; err == nil {
dlog . Noticef ( "Wiring systemd TCP socket #%d, %s, %s" , i , file . Name ( ) , listener . Addr ( ) )
2019-10-11 11:54:00 +02:00
ok = true
2019-10-31 17:36:59 +01:00
mc = append ( mc , listener )
2018-06-13 16:49:32 +02:00
go proxy . tcpListener ( listener . ( * net . TCPListener ) )
} else if pc , err := net . FilePacketConn ( file ) ; err == nil {
dlog . Noticef ( "Wiring systemd UDP socket #%d, %s, %s" , i , file . Name ( ) , pc . LocalAddr ( ) )
2019-10-11 11:54:00 +02:00
ok = true
2019-10-31 17:36:59 +01:00
mc = append ( mc , pc )
2018-06-13 16:49:32 +02:00
go proxy . udpListener ( pc . ( * net . UDPConn ) )
2018-01-24 15:14:48 +01:00
}
2019-10-11 11:54:00 +02:00
if ! ok {
2019-10-31 17:36:59 +01:00
return nil , fmt . Errorf ( "Could not wire systemd socket #%d, %s" , i , file . Name ( ) )
2019-10-11 11:54:00 +02:00
}
2018-01-24 15:14:48 +01:00
}
2018-06-13 16:49:32 +02:00
2019-10-31 17:36:59 +01:00
return mc , nil
2018-01-24 15:14:48 +01:00
}