Backend: abstract version information
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
		
							parent
							
								
									24605c9c01
								
							
						
					
					
						commit
						8d27570eea
					
				| @ -46,6 +46,10 @@ public class Application extends android.app.Application { | |||||||
|         return get().asyncWorker; |         return get().asyncWorker; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     public static Backend getBackend() { | ||||||
|  |         return get().backend; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     public static Class getBackendType() { |     public static Class getBackendType() { | ||||||
|         return get().backend.getClass(); |         return get().backend.getClass(); | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -62,4 +62,19 @@ public interface Backend { | |||||||
|      * @return The updated state of the tunnel. |      * @return The updated state of the tunnel. | ||||||
|      */ |      */ | ||||||
|     State setState(Tunnel tunnel, State state) throws Exception; |     State setState(Tunnel tunnel, State state) throws Exception; | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Determine version of underlying backend. | ||||||
|  |      * | ||||||
|  |      * @return The version of the backend. | ||||||
|  |      * @throws Exception | ||||||
|  |      */ | ||||||
|  |     String getVersion() throws Exception; | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Determine type name of underlying backend. | ||||||
|  |      * | ||||||
|  |      * @return Type name | ||||||
|  |      */ | ||||||
|  |     String getTypeName(); | ||||||
| } | } | ||||||
|  | |||||||
| @ -58,7 +58,11 @@ public final class GoBackend implements Backend { | |||||||
| 
 | 
 | ||||||
|     private static native String wgVersion(); |     private static native String wgVersion(); | ||||||
| 
 | 
 | ||||||
|     public static String getVersion() { return wgVersion(); } |     @Override | ||||||
|  |     public String getVersion() { return wgVersion(); } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getTypeName() { return "Go userspace"; } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public Config applyConfig(final Tunnel tunnel, final Config config) throws Exception { |     public Config applyConfig(final Tunnel tunnel, final Config config) throws Exception { | ||||||
|  | |||||||
| @ -13,8 +13,6 @@ import com.wireguard.android.Application; | |||||||
| import com.wireguard.android.model.Tunnel; | import com.wireguard.android.model.Tunnel; | ||||||
| import com.wireguard.android.model.Tunnel.State; | import com.wireguard.android.model.Tunnel.State; | ||||||
| import com.wireguard.android.model.Tunnel.Statistics; | import com.wireguard.android.model.Tunnel.Statistics; | ||||||
| import com.wireguard.android.util.RootShell; |  | ||||||
| import com.wireguard.android.util.ToolsInstaller; |  | ||||||
| import com.wireguard.config.Config; | import com.wireguard.config.Config; | ||||||
| 
 | 
 | ||||||
| import java.io.File; | import java.io.File; | ||||||
| @ -41,6 +39,18 @@ public final class WgQuickBackend implements Backend { | |||||||
|         localTemporaryDir = new File(context.getCacheDir(), "tmp"); |         localTemporaryDir = new File(context.getCacheDir(), "tmp"); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getVersion() throws Exception { | ||||||
|  |         final List<String> output = new ArrayList<>(); | ||||||
|  |         if (Application.getRootShell() | ||||||
|  |                 .run(output, "cat /sys/module/wireguard/version") != 0 || output.isEmpty()) | ||||||
|  |             throw new Exception("Unable to determine kernel module version"); | ||||||
|  |         return output.get(0); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String getTypeName() { return "Kernel module"; } | ||||||
|  | 
 | ||||||
|     @Override |     @Override | ||||||
|     public Config applyConfig(final Tunnel tunnel, final Config config) throws Exception { |     public Config applyConfig(final Tunnel tunnel, final Config config) throws Exception { | ||||||
|         if (tunnel.getState() == State.UP) { |         if (tunnel.getState() == State.UP) { | ||||||
|  | |||||||
| @ -14,6 +14,7 @@ import android.util.AttributeSet; | |||||||
| import com.wireguard.android.Application; | import com.wireguard.android.Application; | ||||||
| import com.wireguard.android.BuildConfig; | import com.wireguard.android.BuildConfig; | ||||||
| import com.wireguard.android.R; | import com.wireguard.android.R; | ||||||
|  | import com.wireguard.android.backend.Backend; | ||||||
| import com.wireguard.android.backend.GoBackend; | import com.wireguard.android.backend.GoBackend; | ||||||
| import com.wireguard.android.backend.WgQuickBackend; | import com.wireguard.android.backend.WgQuickBackend; | ||||||
| 
 | 
 | ||||||
| @ -26,24 +27,15 @@ public class VersionPreference extends Preference { | |||||||
|     public VersionPreference(final Context context, final AttributeSet attrs) { |     public VersionPreference(final Context context, final AttributeSet attrs) { | ||||||
|         super(context, attrs); |         super(context, attrs); | ||||||
| 
 | 
 | ||||||
|         if (Application.getBackendType() == GoBackend.class) { |         final Backend backend = Application.getBackend(); | ||||||
|             versionSummary = getContext().getString(R.string.version_userspace_summary, GoBackend.getVersion()); |         versionSummary = getContext().getString(R.string.version_summary_checking, backend.getTypeName().toLowerCase()); | ||||||
|         } else if (Application.getBackendType() == WgQuickBackend.class) { |         Application.getAsyncWorker().supplyAsync(backend::getVersion).whenComplete((version, exception) -> { | ||||||
|             versionSummary = getContext().getString(R.string.version_kernel_summary_checking); |  | ||||||
|             Application.getAsyncWorker().supplyAsync(() -> { |  | ||||||
|                 final List<String> output = new ArrayList<>(); |  | ||||||
|                 if (Application.getRootShell() |  | ||||||
|                         .run(output, "cat /sys/module/wireguard/version") != 0 || output.isEmpty()) |  | ||||||
|                     throw new RuntimeException("Unable to determine kernel module version"); |  | ||||||
|                 return output.get(0); |  | ||||||
|             }).whenComplete((version, exception) -> { |  | ||||||
|             versionSummary = exception == null |             versionSummary = exception == null | ||||||
|                         ? getContext().getString(R.string.version_kernel_summary, version) |                     ? getContext().getString(R.string.version_summary, backend.getTypeName(), version) | ||||||
|                         : getContext().getString(R.string.version_kernel_summary_unknown); |                     : getContext().getString(R.string.version_summary_unknown, backend.getTypeName().toLowerCase()); | ||||||
|             notifyChanged(); |             notifyChanged(); | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public CharSequence getSummary() { |     public CharSequence getSummary() { | ||||||
|  | |||||||
| @ -82,10 +82,9 @@ | |||||||
|     <string name="tunnel_rename_error">Unable to rename tunnel: %s</string> |     <string name="tunnel_rename_error">Unable to rename tunnel: %s</string> | ||||||
|     <string name="tunnel_rename_success">Successfully renamed tunnel to “%s”</string> |     <string name="tunnel_rename_success">Successfully renamed tunnel to “%s”</string> | ||||||
|     <string name="version_title">WireGuard for Android v%s"</string> |     <string name="version_title">WireGuard for Android v%s"</string> | ||||||
|     <string name="version_kernel_summary">Kernel module backend v%s</string> |     <string name="version_summary">%s backend v%s</string> | ||||||
|     <string name="version_kernel_summary_checking">Checking kernel module backend version</string> |     <string name="version_summary_checking">Checking %s backend version</string> | ||||||
|     <string name="version_kernel_summary_unknown">Unknown kernel module version</string> |     <string name="version_summary_unknown">Unknown %s version</string> | ||||||
|     <string name="version_userspace_summary">Go userspace backend v%s</string> |  | ||||||
|     <string name="zip_exporter_title">Export tunnels to zip file</string> |     <string name="zip_exporter_title">Export tunnels to zip file</string> | ||||||
|     <string name="zip_export_error">Unable to export tunnels: %s</string> |     <string name="zip_export_error">Unable to export tunnels: %s</string> | ||||||
|     <string name="zip_export_success">Saved to %s</string> |     <string name="zip_export_success">Saved to %s</string> | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user