解决 wsl2 ip 的问题

解决问题最简单粗暴的方法就是解决提出问题的人,但是我不能解决我自己,哈哈
前面 wsl1 用得挺好的,但是 vscode 推了好几次 wsl2,仔细研究了下觉得,问题应该不大,那就升级了
升级完用着用着发现,这个 ip 是个大问题,试过修改交换机为 nat,但是重启后又回去了,搞的有时候网络连接没有启用 ipv4,神奇
行,那就想办法把 ip 更新到 hosts,试过 https://github.com/shayne/go-wsl2-host 不起作用,没辙那就继续折腾
分析下问题,就是获取到 wsl2 的 ip,然后更新到 hosts 里,然后想办法把这个动作搞成 wsl2 一启动就触发,折腾了好几天,找到了最佳实践

$hostname = "wsl.dev"

# find ip of eth0
$ifconfig = (wsl -- ip -4 addr show eth0)
$ipPattern = "((\d+\.?){4})"
$ip = ([regex]"inet $ipPattern").Match($ifconfig).Groups[1].Value
if (-not $ip) {
    exit
}
Write-Host $ip

$hostsPath = "$env:windir/system32/drivers/etc/hosts"

$hosts = (Get-Content -Path $hostsPath -Raw -ErrorAction Ignore)
if ($null -eq $hosts) {
    $hosts = ""
}
$hosts = $hosts.Trim()

# update or add wsl ip
$find = "$ipPattern\s+$hostname"
$entry = "$ip $hostname"
$found = $hosts -match $find
if ($found) {
    $old_wsl_ip = $matches[0] -replace " $hostname", ''
    $hosts = $hosts -replace $old_wsl_ip, $ip
} else {
    $hosts = "$hosts`n$entry".Trim()
}

try {
    $temp = "$hostsPath.new"
    New-Item -Path $temp -ItemType File -Force | Out-Null
    Set-Content -Path $temp $hosts

    Move-Item -Path $temp -Destination $hostsPath -Force
} catch {
    Write-Error "cannot update wsl ip"
}

复制上面的内容保存为 wslhost.ps1 ,然后手动关闭下wsl

wsl --shutdown

打开事件查看器,依次展开 Windows 日志 > 系统,找到事件 ID 为 102 的记录
然后选中,右键打开菜单,选择 “将任务附加到此事件”
再按照对话框填就行了,如果 powershell 的执行策略,不允许执行 .ps1 文件,打开 powershell 执行下面的语句

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

这样,每次 wsl 启动时都会更新 ip 到 hosts 文件

asin 2 ean final solution

通过一个asin找到对应的ean,然后用ean在亚马逊还能找到对应的asin,太不容易了。。。
Find ean by a asin, then use this ean can searched out the right asin.

有个神奇的比价网站 https://camelcamelcamel.com/ 粘贴亚马逊链接或者asin,关键词,找出商品。神奇的地方来了,它会给出ean //ps: 拿着这个ean去亚马逊是搜不出来的,因为少了校验位
There is a amazing website https://camelcamelcamel.com/ paste asin, amazon links or keywords to find product. It’s will show the ean. //ps this ean can’t search in amazon, it’s missing valid bits

camelcamelcamel

这个校验位的算法,网上都有,这里给个在线计算的 https://eancheck.com/ 支持批量计算,毕竟只是简单的计算。。。
https://eancheck.com/ can calcate valid bit, and support batch calucate.

eancheck

我们拿着计算出的ean,就可以回到亚马逊去搜了,查看搜到的ean和前面的是一样的
We can use that ean to search in amazon.

amazon-de

在服务器上装个sourcegraph看代码

先介绍下 sourcegraph
Sourcegraph is a web-based code search and navigation tool for dev teams.
用于团队开发,基于网页的代码搜索,导航工具

Code search: fast, cross-repository, on any commit/branch (no indexing delay), with support for regexps, diffs, and filters
快速 跨仓库 任意提交/分支(没有索引延迟) 支持正则表达式,比较,过滤

Code navigation: go-to-definition and find-references for all major languages
所有主流语言支持:转到定义 查找引用

Deep integrations with GitHub, GitHub Enterprise, GitLab, Bitbucket Server, Phabricator, etc., plus a powerful extension API
深度集成巴拉巴拉

Open-source, self-hosted, and free (Enterprise upgrade available)
开源,可以自己搭建,免费,也可以升级企业版

最后一点就值得去装。

当然官网也是支持使用他们的服务,免费的。
自己搭建,需要服务器装有docker, 可用内存最低差不多1g,能跑起来的情况下。。。

docker run -d –publish 127.0.0.1:8880:7080 –publish 127.0.0.1:8443:7443 –publish 8633:2633 –name sg –restart unless-stopped –volume /root/sourcegraph/config:/etc/sourcegraph –volume /root/sourcegraph/data:/var/opt/sourcegraph sourcegraph/server:3.2.2
解释下上面的第一个 publish 参数:将主机的127.0.0.1:8880绑定到容器内的7080 http访问,因为再往上一层要用nginx代理访问容器,所以绑定到本地就行了
第二个 publish 参数:参考上一条。。https访问 这个端口其实用不到。。。
第三个 publish 参数:将主机的8633绑定到容器的2633 进管理控制台需要用到,而且还稍微有点麻烦。。。
两个目录,一个是配置,一个是数据
配置指容器内nginx等的配置
数据是裸代码仓库
截止2019-04-14时最新稳定版本是3.2.2

跑起来之后就是nginx配置代理了

    location / {
        proxy_pass http://127.0.0.1:8880;
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
    }

自己再加上https证书就更完美了

然后我们就访问它,第一次访问好像是注册管理员
然后登录进去进行设置代码仓

{
  "url": "https://github.com",
  "token": "token",
  "repos": [
    "directus/directus",
    "WordPress/WordPress",
    "woocommerce/woocommerce"
  ]
}

仓库直接在里面加就行,记得再回到列表页enable那个仓库

如果是自建的git服务,url可以使用 https://name:pwd@host/

禁止注册就需要进入管理控制台了,强制https。。。可以禁用https,我是直接用vpn组网,然后直接访问的内网,简单粗暴

{
  // ...,
  "auth.providers": [{ "type": "builtin", "allowSignup": true }]
}

禁用内置认证注册之后可以开启 github 认证等,也可以管理员直接在后台添加用户

嗯,界面语言,没折腾过,不清楚有没有中文界面,不过主要就是和代码打交道,界面啥语言也无所谓了
看代码神器,sourceinsight可以被换掉了。。。

一个手动更新phpMyAdmin的脚本

#!/bin/bash

wget https://files.phpmyadmin.net/phpMyAdmin/"$1"/phpMyAdmin-"$1"-all-languages.zip
unzip phpMyAdmin-"$1"-all-languages.zip
if [ -f 'phpmyadmin/config.inc.php' ]; then
        cp phpmyadmin/config.inc.php phpMyAdmin-"$1"-all-languages/
fi
rm -rf phpmyadmin
mv phpMyAdmin-"$1"-all-languages phpmyadmin
chown -R www-data:www-data phpmyadmin

复制到phpmyadmin的上一层目录
chmod +x update_phpmyadmin.php
./update_phpmyadmin 4.8.5
就可以安装到指定版本了…
不止一个服务器的更新,以前一条命令一条命令的敲,好烦啊,这下好多了,清静了

装个pi-hole做dns去广告

pi-hole wikipedia 它的原理是dns沉洞,对于广告服务的域名返回错误的dns响应.例如,返回pi-hole的服务器ip,这样就不会请求到广告,美滋滋.
安装也很简单,一条命令一把梭

curl -sSL https://install.pi-hole.net | sudo bash

剩下跟着脚本走就行了,对了,因为是做dns服务器,所以,必须要选择你的设备能够访问的一个ip.你的设备是指手机,pad,电脑等dns客户端.
还有,如果装了web界面,最后会有自动设置的密码.不过,忘了也没关系

pihole -a -p

嗯,下面是别人录制的安装视频,可以参考下
install pihole

pihole会针对屏蔽的广告地址,设置一个页面,看到的页面会是这样

这就是/var/www/html/pihole 下面的index.php的功劳了

嗯,dns会影响网速,如果觉得网速慢了,可以试试换dns