2018-11-09 11:23:52 +00:00
// S P D X - L i c e n s e - I d e n t i f i e r : M I T
// C o p y r i g h t © 2 0 1 8 W i r e G u a r d L L C . A l l R i g h t s R e s e r v e d .
import NetworkExtension
class ErrorNotifier {
2018-12-13 20:54:53 +00:00
let activationAttemptId : String ?
weak var tunnelProvider : NEPacketTunnelProvider ?
2018-12-21 10:10:04 +00:00
var tunnelName : String ?
var isActivateOnDemandEnabled = false
2018-12-13 20:54:53 +00:00
init ( activationAttemptId : String ? , tunnelProvider : NEPacketTunnelProvider ) {
self . activationAttemptId = activationAttemptId
self . tunnelProvider = tunnelProvider
ErrorNotifier . removeLastErrorFile ( )
}
func errorMessage ( for error : PacketTunnelProviderError ) -> ( String , String ) ? {
2018-12-12 18:28:27 +00:00
switch error {
2018-11-09 11:23:52 +00:00
case . savedProtocolConfigurationIsInvalid :
2018-12-21 10:10:04 +00:00
return ( " Activation failure " , " Could not retrieve tunnel information from the saved configuration. " )
case . dnsResolutionFailure :
return ( " DNS resolution failure " , " One or more endpoint domains could not be resolved. " )
2018-11-09 11:23:52 +00:00
case . couldNotStartWireGuard :
2018-12-21 10:10:04 +00:00
return ( " Activation failure " , " WireGuard backend could not be started. " )
2018-11-09 11:23:52 +00:00
case . coultNotSetNetworkSettings :
2018-12-21 10:10:04 +00:00
return ( " Activation failure " , " Error applying network settings on the tunnel. " )
2018-11-09 11:23:52 +00:00
}
}
2018-12-13 20:54:53 +00:00
func notify ( _ error : PacketTunnelProviderError ) {
guard let ( title , message ) = errorMessage ( for : error ) else { return }
if let activationAttemptId = activationAttemptId , let lastErrorFilePath = FileManager . networkExtensionLastErrorFileURL ? . path {
// T h e t u n n e l w a s s t a r t e d f r o m t h e a p p
2018-12-21 10:10:04 +00:00
let onDemandMessage = isActivateOnDemandEnabled ? " This tunnel has Activate On Demand enabled, so this tunnel might be activated automatically. You may turn off Activate On Demand in the WireGuard app by navigating to: ' \( tunnelName ? ? " tunnel " ) ' > Edit. " : " "
let errorMessageData = " \( activationAttemptId ) \n \( title ) \n \( message ) \( onDemandMessage ) " . data ( using : . utf8 )
2018-12-13 20:54:53 +00:00
FileManager . default . createFile ( atPath : lastErrorFilePath , contents : errorMessageData , attributes : nil )
} else {
2018-12-21 10:10:04 +00:00
// T h e t u n n e l w a s p r o b a b l y s t a r t e d f r o m i O S S e t t i n g s a p p o r a c t i v a t e d o n - d e m a n d
2018-12-13 20:54:53 +00:00
if let tunnelProvider = self . tunnelProvider {
// d i s p l a y M e s s a g e ( ) i s d e p r e c a t e d , b u t t h e r e ' s n o b e t t e r a l t e r n a t i v e i f i n v o k e d f r o m i O S S e t t i n g s
2018-12-21 10:10:04 +00:00
if ! isActivateOnDemandEnabled { // I f u s i n g a c t i v a t e - o n - d e m a n d , d o n ' t u s e d i s p l a y M e s s a g e
tunnelProvider . displayMessage ( " \( title ) : \( message ) " ) { _ in }
}
2018-12-13 20:54:53 +00:00
}
}
}
static func removeLastErrorFile ( ) {
if let lastErrorFileURL = FileManager . networkExtensionLastErrorFileURL {
_ = FileManager . deleteFile ( at : lastErrorFileURL )
}
2018-11-09 11:23:52 +00:00
}
}