1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
import android.annotation.TargetApi; import android.content.Context; import android.net.ConnectivityManager; import android.net.LinkProperties; import android.net.Network; import android.net.NetworkInfo; import android.net.RouteInfo; import android.os.Build; import android.util.Log; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.lang.reflect.Method; import java.net.InetAddress; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; /** * DNS servers detector * <p> * IMPORTANT: don't cache the result. * <p> * Or if you want to cache the result make sure you invalidate the cache * on any network change. * <p> * It is always better to use a new instance of the detector when you need * current DNS servers otherwise you may get into troubles because of invalid/changed * DNS servers. * <p> * This class combines various methods and solutions from: * Dnsjava http://www.xbill.org/dnsjava/ * Minidns https://github.com/MiniDNS/minidns * <p> * Unfortunately both libraries are not aware of Orero changes so new method was added to fix this. * <p> * Created by Madalin Grigore-Enescu on 2/24/18. */ public class DnsServersDetector { private static final String TAG = "DnsServersDetector"; /** * Holds some default DNS servers used in case all DNS servers detection methods fail. * Can be set to null if you want caller to fail in this situation. */ private static final String[] FACTORY_DNS_SERVERS = { "8.8.8.8", "8.8.4.4" }; /** * Properties delimiter used in exec method of DNS servers detection */ private static final String METHOD_EXEC_PROP_DELIM = "]: ["; /** * Holds context this was created under */ private Context context; //region - public ////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////// /** * Constructor */ public DnsServersDetector(Context context) { this.context = context; } /** * Returns android DNS servers used for current connected network * * @return Dns servers array */ public String[] getServers() { // METHOD 1: old deprecated system properties String[] result = getServersMethodSystemProperties(); if (result != null && result.length > 0) { return result; } // METHOD 2 - use connectivity manager result = getServersMethodConnectivityManager(); if (result != null && result.length > 0) { return result; } // LAST METHOD: detect android DNS servers by executing getprop string command in a separate process // This method fortunately works in Oreo too but many people may want to avoid exec // so it's used only as a failsafe scenario result = getServersMethodExec(); if (result != null && result.length > 0) { return result; } // Fall back on factory DNS servers return FACTORY_DNS_SERVERS; } //endregion //region - private ///////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////// /** * Detect android DNS servers by using connectivity manager * <p> * This method is working in android LOLLIPOP or later * * @return Dns servers array */ private String[] getServersMethodConnectivityManager() { // This code only works on LOLLIPOP and higher if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { try { final ArrayList<String> priorityServersArrayList = new ArrayList<>(); final ArrayList<String> serversArrayList = new ArrayList<>(); final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(android.content.Context.CONNECTIVITY_SERVICE); if (connectivityManager != null) { // Iterate all networks // Notice that android LOLLIPOP or higher allow iterating multiple connected networks of SAME type for (Network network : connectivityManager.getAllNetworks()) { final NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network); if (networkInfo.isConnected()) { final LinkProperties linkProperties = connectivityManager.getLinkProperties(network); final List<InetAddress> dnsServersList = linkProperties.getDnsServers(); // Prioritize the DNS servers for link which have a default route if (linkPropertiesHasDefaultRoute(linkProperties)) { for (InetAddress element : dnsServersList) { final String dnsHost = element.getHostAddress(); priorityServersArrayList.add(dnsHost); } } else { for (InetAddress element : dnsServersList) { final String dnsHost = element.getHostAddress(); serversArrayList.add(dnsHost); } } } } } // Append secondary arrays only if priority is empty if (priorityServersArrayList.isEmpty()) { priorityServersArrayList.addAll(serversArrayList); } // Stop here if we have at least one DNS server if (priorityServersArrayList.size() > 0) { return priorityServersArrayList.toArray(new String[0]); } } catch (Exception ex) { Log.d(TAG, "Exception detecting DNS servers using ConnectivityManager method", ex); } } // Failure return null; } /** * Detect android DNS servers by using old deprecated system properties * <p> * This method is NOT working anymore in Android 8.0 * Official Android documentation state this in the article Android 8.0 Behavior Changes. * The system properties net.dns1, net.dns2, net.dns3, and net.dns4 are no longer available, * a change that improves privacy on the platform. * <p> * https://developer.android.com/about/versions/oreo/android-8.0-changes.html#o-pri * * @return Dns servers array */ private String[] getServersMethodSystemProperties() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { // This originally looked for all lines containing .dns; but // http://code.google.com/p/android/issues/detail?id=2207#c73 // indicates that net.dns* should always be the active nameservers, so // we use those. final String re1 = "^\\d+(\\.\\d+){3}$"; final String re2 = "^[0-9a-f]+(:[0-9a-f]*)+:[0-9a-f]+$"; ArrayList<String> serversArrayList = new ArrayList<>(); try { Class<?> SystemProperties = Class.forName("android.os.SystemProperties"); Method method = SystemProperties.getMethod("get", new Class[]{String.class}); final String[] netdns = new String[]{"net.dns1", "net.dns2", "net.dns3", "net.dns4"}; for (String netdn : netdns) { Object[] args = new Object[]{netdn}; String v = (String) method.invoke(null, args); if (v != null && (v.matches(re1) || v.matches(re2)) && !serversArrayList.contains(v)) { serversArrayList.add(v); } } // Stop here if we have at least one DNS server if (serversArrayList.size() > 0) { return serversArrayList.toArray(new String[0]); } } catch (Exception e) { Log.d(TAG, "Exception detecting DNS servers using SystemProperties method", e); } } // Failed return null; } /** * Detect android DNS servers by executing getprop string command in a separate process * <p> * Notice there is an android bug when Runtime.exec() hangs without providing a Process object. * This problem is fixed in Jelly Bean (Android 4.1) but not in ICS (4.0.4) and probably it will never be fixed in ICS. * https://stackoverflow.com/questions/8688382/runtime-exec-bug-hangs-without-providing-a-process-object/11362081 * * @return Dns servers array */ private String[] getServersMethodExec() { // We are on the safe side and avoid any bug if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { try { Process process = Runtime.getRuntime().exec("getprop"); InputStream inputStream = process.getInputStream(); LineNumberReader lineNumberReader = new LineNumberReader(new InputStreamReader(inputStream)); Set<String> serversSet = methodExecParseProps(lineNumberReader); if (serversSet != null && serversSet.size() > 0) { return serversSet.toArray(new String[0]); } } catch (Exception e) { Log.d(TAG, "Exception in getServersMethodExec", e); } } // Failed return null; } /** * Parse properties produced by executing getprop command * * @param lineNumberReader lineNumberReader * @return Set of parsed properties * @throws Exception Exception */ private Set<String> methodExecParseProps(BufferedReader lineNumberReader) throws Exception { String line; Set<String> serversSet = new HashSet<String>(10); while ((line = lineNumberReader.readLine()) != null) { int split = line.indexOf(METHOD_EXEC_PROP_DELIM); if (split == -1) { continue; } String property = line.substring(1, split); int valueStart = split + METHOD_EXEC_PROP_DELIM.length(); int valueEnd = line.length() - 1; if (valueEnd < valueStart) { // This can happen if a newline sneaks in as the first character of the property value. For example // "[propName]: [\n…]". Log.d(TAG, "Malformed property detected: \"" + line + '"'); continue; } String value = line.substring(valueStart, valueEnd); if (value.isEmpty()) { continue; } if (property.endsWith(".dns") || property.endsWith(".dns1") || property.endsWith(".dns2") || property.endsWith(".dns3") || property.endsWith(".dns4")) { // normalize the address InetAddress ip = InetAddress.getByName(value); if (ip == null) continue; value = ip.getHostAddress(); if (value == null) continue; if (value.length() == 0) continue; serversSet.add(value); } } return serversSet; } /** * Returns true if the specified link properties have any default route * * @param linkProperties linkProperties * @return true if the specified link properties have default route or false otherwise */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) private boolean linkPropertiesHasDefaultRoute(LinkProperties linkProperties) { for (RouteInfo route : linkProperties.getRoutes()) { if (route.isDefaultRoute()) { return true; } } return false; } //endregion } |
Android DNS更新与DNS-Prefetch
一、什么是DNS
DNS(Domain Name System,域名系统),dns用于将域名解析解析为ip地址。
例如:给你www.baidu.com的主机名,你给
我查出对应的ip地址:163.177.151.109。一些主机名还会有别名,如www.baidu.com就
有别名www.a.shifen.com,甚至不止一个别名,或一个别名有2个ip地址。在linux机子
上,运行nslookup(name service lookup)就是进行域名解析。如下面:
1 2 3 4 5 6 7 8 9 10 |
~$ nslookup www.baidu.com Server: 127.0.0.1 Address: 127.0.0.1#53 Non-authoritative answer: www.baidu.com canonical name = www.a.shifen.com. Name: www.a.shifen.com Address: 163.177.151.109 Name: www.a.shifen.com Address: 163.177.151.110 |
DNS工作方式分为递归查询和迭代查询,具体可参考下图
ubuntu 18.04 Apache2启用HTTP/2
HTTP/2 可以让我们的应用更快、更简单、更稳定 – 这几词凑到一块是很罕见的!HTTP/2 将很多以前我们在应用中针对 HTTP/1.1 想出来的“歪招儿”一笔勾销,把解决那些问题的方案内置在了传输层中。不仅如此,它还为我们进一步优化应用和提升性能提供了全新的机会!
——《Web 性能权威指南》
Ubuntu18.04 官方源已经包含带有HTTP2模块的 Apache/2.4.29 ,所以我们可以很简单的启用 HTTP2。
1 |
$ sudo a2enmod http2 |
首先启用 http2 模块,然后在虚拟主机的配置文件中或者在Apache2的全局配置文件中加入:
1 |
Protocols h2 http/1.1 |
当客户端支持时优先使用 HTTP2 ,其次是 http/1.1,表明了一种优先顺序。目前为止就已经启用完成了,但如果你是使用的 Apache2+PHP 架构的应用程序,由于 mpm_prefork 模块不支持 mod_http2,所以我们需要切换 mpm_prefork、mod_php 到 mpm_event 和 php-fpm 来解决这个问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Install php-fpm and enable $ sudo apt install php-fpm $ sudo systemctl enable php7.2-fpm # To enable PHP 7.2 FPM in Apache2 $ sudo a2enmod proxy_fcgi setenvif $ sudo a2enconf php7.2-fpm # First to disable PHP 7.2/ mpm_prefork to avoid conflicts,then enable. $ sudo a2dismod php7.2 mpm_prefork $ sudo a2enmod mpm_event |
解决GitHub的raw.githubusercontent.com无法连接问题
https://site.ip138.com/raw.Githubusercontent.com/
输入raw.githubusercontent.com
查询IP地址
修改hosts Ubuntu,CentOS及macOS直接在终端输入
1 |
$ sudo vi /etc/hosts |
添加以下内容保存即可 (IP地址查询后相应修改,可以ping不同IP的延时 选择最佳IP地址)
# GitHub Start
52.74.223.119 github.com
192.30.253.119 gist.github.com
54.169.195.247 api.github.com
185.199.111.153 assets-cdn.github.com
199.232.96.133 raw.githubusercontent.com
#151.101.76.133 raw.githubusercontent.com
151.101.108.133 user-images.githubusercontent.com
151.101.76.133 gist.githubusercontent.com
151.101.76.133 cloud.githubusercontent.com
151.101.76.133 camo.githubusercontent.com
151.101.76.133 avatars0.githubusercontent.com
151.101.76.133 avatars1.githubusercontent.com
151.101.76.133 avatars2.githubusercontent.com
151.101.76.133 avatars3.githubusercontent.com
151.101.76.133 avatars4.githubusercontent.com
151.101.76.133 avatars5.githubusercontent.com
151.101.76.133 avatars6.githubusercontent.com
151.101.76.133 avatars7.githubusercontent.com
151.101.76.133 avatars8.githubusercontent.com
# GitHub End
参考链接
ubuntu 20.04 & ubuntu18.04快速开启TCP BBR实现高效单边加速
Linux Kernel
内核升级到 4.9
及以上版本可以实现 BBR
加速,由于Ubuntu 18.04
默认的内核就是 4.15
版本的内核,由于Ubuntu 20.04
默认的内核就是 5.4
版本的内核,并已经默认编译了 TCP BBR
模块,所以可以直接通过参数开启。
新的 TCP
拥塞控制算法 BBR
(Bottleneck Bandwidth and RTT) 可以让服务器的带宽尽量跑慢,并且尽量不要有排队的情况,让网络服务更佳稳定和高效。
修改系统变量:
1 2 3 |
$ echo 'net.core.default_qdisc=fq' | sudo tee -a /etc/sysctl.conf $ echo 'net.ipv4.tcp_congestion_control=bbr' | sudo tee -a /etc/sysctl.conf |
保存生效
1 |
$ sudo sysctl -p |
执行
1 |
$ sysctl net.ipv4.tcp_available_congestion_control |
如果结果是这样
1 2 |
$ sysctl net.ipv4.tcp_available_congestion_control net.ipv4.tcp_available_congestion_control = bbr cubic reno |
就开启了。 执行 lsmod | grep bbr
,以检测 BBR 是否开启。
一般建议重启系统,重启后执行
1 2 |
$ sysctl net.ipv4.tcp_congestion_control net.ipv4.tcp_congestion_control = bbr |
参考链接
【解决】Unable to locate adb
Flutter Flare(Rive)动画
Flare
(Rive
)是一家可以快速制Flutter SVG
动画的网站,提供专门的Flutter Weight
承载网站导出的动画文件,相当于Android
的Lottie
,Flare
的首次出现非常惊艳,是在Flutter
发布大会上,一下就火了。
flutter动态申请权限
https://pub.flutter-io.cn/packages/permission_handler
https://www.jianshu.com/p/fa68876fbdfd
例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Future requestPermission() async { // 申请权限 Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([PermissionGroup.storage]); // 申请结果 PermissionStatus permission = await PermissionHandler() .checkPermissionStatus(PermissionGroup.storage); if (permission == PermissionStatus.granted) { print("权限申请通过"); } else { print("权限申请通过"); } } } |
参考链接
Flutter 1.22最新的多国语支持配置和使用
Flutter 1.22现已发布
最新版本中,随之发布了很多新的特性以及修复了一些BUG,现在我们说说其中的新的多国语插件。个人感觉新多国语操作,多了一些模板性的东西,但是也方便了很多东西。
话不多说,现在我们进入正题,如何使用新插件呢?
配置项目的yaml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
dependencies: flutter: sdk: flutter flutter_localizations: #配置多国语支持 sdk: flutter intl: ^0.16.1 #多国语插件,要求高于0.16.1版本 flutter: # The following line ensures that the Material Icons font is # included with your application, so that you can use the icons in # the material Icons class. uses-material-design: true generate: true #新增配置,自动多国语代码生成 |
AAPT: error: resource android:attr/fontVariationSettings not found
最近在编译Flutter
项目的时候,报错如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:processDebugResources'. > Android resource linking failed Output: /Users/xxxx/Source/developer_quest/build/app/intermediates/incremental/mergeDebugResources/merged.dir/values/values.xml:86: error: resource android:attr/fontVariationSettings not found. /Users/xxxx/Source/developer_quest/build/app/intermediates/incremental/mergeDebugResources/merged.dir/values/values.xml:87: error: resource android:attr/ttcIndex not found. error: failed linking references. Command: /Users/xxxx/.gradle/caches/transforms-1/files-1.1/aapt2-3.2.1-4818971-osx.jar/ace5a2b7765511437e6add51febae423/aapt2-3.2.1-4818971-osx/aapt2 link -I\ /Users/xxxx/Library/Android/sdk/platforms/android-27/android.jar\ --manifest\ /Users/xxxx/Source/developer_quest/build/app/intermediates/merged_manifests/debug/processDebugManifest/merged/AndroidManifest.xml\ -o\ /Users/xxxx/Source/developer_quest/build/app/intermediates/processed_res/debug/processDebugResources/out/resources-debug.ap_\ -R\ @/Users/xxxx/Source/developer_quest/build/app/intermediates/incremental/processDebugResources/resources-list-for-resources-debug.ap_.txt\ --auto-add-overlay\ --java\ /Users/xxxx/Source/developer_quest/build/app/generated/not_namespaced_r_class_sources/debug/processDebugResources/r\ --custom-package\ dev.flutter.devrpg\ -0\ apk\ --output-text-symbols\ /Users/xxxx/Source/developer_quest/build/app/intermediates/symbols/debug/R.txt\ --no-version-vectors Daemon: AAPT2 aapt2-3.2.1-4818971-osx Daemon #0 Output: /Users/xxxx/.gradle/caches/transforms-1/files-1.1/core-1.1.0.aar/becc1f5b1538f3fe61f1f959fb2bd672/res/values/values.xml:174:5-210:25: AAPT: error: resource android:attr/fontVariationSettings not found. /Users/xxxx/.gradle/caches/transforms-1/files-1.1/core-1.1.0.aar/becc1f5b1538f3fe61f1f959fb2bd672/res/values/values.xml:174:5-210:25: AAPT: error: resource android:attr/ttcIndex not found. error: failed linking references. Command: /Users/xxxx/.gradle/caches/transforms-1/files-1.1/aapt2-3.2.1-4818971-osx.jar/ace5a2b7765511437e6add51febae423/aapt2-3.2.1-4818971-osx/aapt2 link -I\ /Users/xxxx/Library/Android/sdk/platforms/android-27/android.jar\ --manifest\ /Users/xxxx/Source/developer_quest/build/app/intermediates/merged_manifests/debug/processDebugManifest/merged/AndroidManifest.xml\ -o\ /Users/xxxx/Source/developer_quest/build/app/intermediates/processed_res/debug/processDebugResources/out/resources-debug.ap_\ -R\ @/Users/xxxx/Source/developer_quest/build/app/intermediates/incremental/processDebugResources/resources-list-for-resources-debug.ap_.txt\ --auto-add-overlay\ --java\ /Users/xxxx/Source/developer_quest/build/app/generated/not_namespaced_r_class_sources/debug/processDebugResources/r\ --custom-package\ dev.flutter.devrpg\ -0\ apk\ --output-text-symbols\ /Users/xxxx/Source/developer_quest/build/app/intermediates/symbols/debug/R.txt\ --no-version-vectors Daemon: AAPT2 aapt2-3.2.1-4818971-osx Daemon #0 * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 13s Exception: Gradle task assembleDebug failed with exit code 1 |
解决方案:
try to change the compileSdkVersion above 27:
compileSdkVersion 28