2025年6月26日 星期四

在 pwsh 中使用 Remoting 連入 linux

在 pwsh 中使用 Remoting 連入 linux

用習慣 pwsh 有時候沒要幹嘛只是做一點點事情,直接用 pwsh 會比較快,真的要做比較複雜的事情還是切 bash 好,因為網上的教學是以 bash 為主的。



linux 端的設定1:: 安裝 pwsh

安裝 pwsh 可以參考微軟的網站有教學
Installing PowerShell on Ubuntu - PowerShell | Microsoft Learn


範例用從 github 下載的方式 (因為我是安裝在 pi5 上)

載點記得選 gihub 上自己合適的系統與版本,我的是 arm64 如果一般選 x64 即可

# 下載
wget https://github.com/PowerShell/PowerShell/releases/download/v7.5.2/powershell-7.5.2-linux-arm64.tar.gz

# 刪除舊內容
sudo rm -rf /opt/powershell/7/*

# 解壓縮
sudo tar -xzf ~/powershell-7.5.2-linux-arm64.tar.gz -C /opt/powershell/7

# 建立連結
sudo ln -sf /opt/powershell/7/pwsh /usr/local/bin/pwsh

# 賦予執行權
sudo chmod +x /opt/powershell/7/pwsh

# 執行
pwsh

到這邊就可以正常執行

手動安裝差在沒辦法透過 apt-get 自動升級版本,好處是可以裝到最新板的。



linux 端的設定1:: 設定 ssh 的子系統

在 linux 端打開 Subsystem 即可 (不用像 Win 需要啟動 WinRM)

先到SSH設定中

sudo nano /etc/ssh/sshd_config

拉到最下面找到 # override default of no subsystems 這個註解
下面可能有 sftp 範例但是不管手動加上這行就行

Subsystem powershell    /usr/local/bin/pwsh -sshs -NoLogo

存檔後就設定好了



pwsh 連接方法

在連接之前先確保 ssh 金鑰已經配置完畢,可以無密碼登入,然後使用下面的指令連接

Enter-PSSession -HostName 192.168.3.88 -UserName chg

連入之後會長這個樣子




2025年6月22日 星期日

使用 SwitchyOmega 跳轉特定網址 IP

使用 SwitchyOmega 跳轉特定網址 IP

這是接續的文章最終實現的在瀏覽器上僅特定網址跳轉IP
  1. 在 Ubuntu 架設 WireGuard Client 用戶端跳轉IP
  2. 在 Ubuntu 用 dante 架設 sock5 代理 讓瀏覽器在特定網址跳轉IP
  3. 使用 SwitchyOmega 跳轉特定網址 IP

下面正文開始



這是瀏覽器的一個插件,可以透過 sock5 跳轉特定網址的IP
Proxy SwitchyOmega - Chrome 線上應用程式商店


安裝好之後進入設定頁面,填入你本地的 sock5 代理伺服器



然後儲存設定即可

再來進入你需要跳轉的網頁,右上角點出該插件,點擊加入條件



情境模式選擇 proxy 按下加入



再來重整一下就能跳轉了





如果要測速的話,speetest的流量沒辦法走 sock5 如果要測速可以試試看這個
インターネット回線の速度テスト | Fast.com




在 Ubuntu 用 dante 架設 sock5 代理 讓瀏覽器在特定網址跳轉IP

在 Ubuntu 用 dante 架設 sock5 代理 讓瀏覽器在特定網址跳轉IP

第二篇是代理伺服器,代理再搭配 Chrome 的插件 SwitchyOmega 就可以完成組合技,只在特定網只才跳轉IP



安裝

更新並安裝

sudo apt-get update && sudo apt install dante-server -y

然後我們先備份一下最初的原始設定範例

sudo cp -p /etc/danted.conf /etc/danted.orig.conf



更改設定

再來我們修改設定值

sudo nano /etc/danted.conf

輸入下面的基本範例

logoutput: /var/log/danted.log
internal: 0.0.0.0 port = 1080
external: wg1

method: none
user.notprivileged: nobody

client pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0
    log: connect disconnect error
}

pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0
    protocol: tcp udp
    log: connect disconnect error
}

輸入完畢按下 CTRL+S 存檔 CTRL+X 離開



啟動服務

設置好之後就可以啟動服務了

sudo systemctl start danted

如果有修改檔案需要重啟服務可以用以下的方式

sudo systemctl daemon-reload && sudo systemctl restart danted


啟動完畢之後檢查一下服務狀態是否在 runing 狀態

sudo systemctl status danted --no-pager -n 10

然後就可以測試一下 sock 代理是否可用了

curl --socks5 localhost:1080 http://ipinfo.io/ip; echo


最後確認一下經由外部IP的連接

curl --socks5 "$(hostname -I|cut -f1 -d' '):1080" http://ipinfo.io/ip; echo

這裡有成功顯示跳轉的IP就是完成了



設置開機時自動啟動

原則上加入 systemctl 就行了

sudo systemctl enable danted


從其他電腦測試

這邊選用 powershell 指令當範例,能連上就是可以用了

curl.exe --socks5 192.168.3.89:1080 http://ipinfo.io/ip

指令中的 IP: 192.168.3.89 記得改成你自己的機器的 IP

至此就完成摟~






進階:: 自動啟動 danted 時讓他等待 wg1 連接完成

更進階的讓 danted 自動啟動時會先等待 wg1 連線成功才啟動的方式

機器的性能不要太差不用特別設定不會卡到,有問題在設定也行。具體表現的情況就是重啟後 danted 啟動時不到虛擬網卡 wg1 而失敗 


首先編輯一下設定

sudo nano /lib/systemd/system/danted.service

然後找到 After=network.target 把它取代成下面這樣

#After=network.target
After=network.target wg-quick@wg1.service
Requires=wg-quick@wg1.service

這表示自動啟動 danted 時會順帶啟動 wg1 並且等待他完成

然後存檔離開,並重啟服務 (需要先關閉 wg1)

sudo wg-quick down wg1 && sudo systemctl daemon-reload && sudo systemctl restart danted


最後確認一下經由外部IP的連接

curl --socks5 "$(hostname -I|cut -f1 -d' '):1080" http://ipinfo.io/ip; echo

沒問題的話就完成了

重新啟動可以輸入 sudo reboot 重啟一下測試成果



進階2:: 向區網廣播自身的 hostname

由於IP可能會變動,或是不好記住可以多裝一個套件 avahi-daemon 廣播主機名稱

這樣一來下一步設定代理伺服器的時候就不用記住 IP 了,直接設置這台主機名即可


不需要什麼設定,就是安裝並設定開機啟動而已

sudo apt install avahi-daemon -y
sudo systemctl enable avahi-daemon
sudo systemctl start avahi-daemon

再來就可以不用輸入IP而是輸入本機名稱即可,不知道名稱是什麼可以打指令 hostname 即可以查詢




進階3:: 簡易管理批次檔案

自己寫了兩個方便切換的,沒特別考量什麼只是方便能切隨意地的東西而已

後來發現 sing-box 這個神器,好像即使像這開代理也不用跳轉本機流量的樣子,管理上也可以讓 wg 同時運行 server / client 的樣子,詳細還沒測


proxy-disable.sh

#!/bin/bash

set -e

DISABLE_BOOT=0

for arg in "$@"; do
    case "$arg" in
        -b|--disable-boot)
            DISABLE_BOOT=1
            ;;
    esac
done

# 先關掉 danted
sudo systemctl stop danted

if [[ $DISABLE_BOOT -eq 1 ]]; then
    sudo systemctl disable danted
fi

# 如果 wg0 存在,關掉 WireGuard
if ip link show wg0 &>/dev/null; then
    sudo wg-quick down wg0
fi
# 如果 wg1 存在,關掉 WireGuard
if ip link show wg1 &>/dev/null; then
    sudo wg-quick down wg1
fi

# 關閉開機自動啟動
if [[ $DISABLE_BOOT -eq 1 ]]; then
    sudo systemctl disable wg-quick@wg1
fi

echo -e "🌐 外部 IPv4:\033[1;33m$(curl -4 -s ifconfig.me)\033[0m"


proxy-enable.sh

#!/bin/bash

set -e

ENABLE_BOOT=0

for arg in "$@"; do
    case "$arg" in
        -b|--enable-boot)
            ENABLE_BOOT=1
            ;;
    esac
done

# 如果 wg0 存在,關掉 WireGuard
ip link show wg0 &>/dev/null && sudo wg-quick down wg0

# 開啟 WireGuard(如果沒開才開)
ip link show wg1 &>/dev/null || sudo wg-quick up wg1
# 重新載入 systemd 並重啟 danted
sudo systemctl daemon-reload
sudo systemctl restart danted

if [[ $ENABLE_BOOT -eq 1 ]]; then
    sudo systemctl enable wg-quick@wg1
    sudo systemctl enable danted
fi

sleep 1

proxy="$(hostname -I | cut -f1 -d' '):1080"
echo -ne "SOCKS5 代理:\033[1;90m$proxy\033[0m → IP:"
echo -ne "\033[38;5;39m"
curl --socks5 "$proxy" -s http://ipinfo.io/ip
echo -e "\033[0m"



2025年6月21日 星期六

在 Ubuntu 架設 WireGuard Client 用戶端跳轉IP

在 Ubuntu 架設 WireGuard Client 用戶端跳轉IP

這是接續的文章最終實現的在瀏覽器上僅特定網址跳轉IP
  1. 在 Ubuntu 架設 WireGuard Client 用戶端跳轉IP
  2. 在 Ubuntu 用 dante 架設 sock5 代理 讓瀏覽器在特定網址跳轉IP
  3. 使用 SwitchyOmega 跳轉特定網址 IP

下面正文開始



主要是為了做本地的跳轉用代理伺服器,選擇用 WireGuard 速度更佳。到時候我要丟到樹莓派上面跑的。

如果要用wsl測試記得要用 wsl2 版的,在 wsl1 中沒有 WireGuard 要的內核,跑不起來的。安裝 wsl2 要注意如果本機有裝夜神或VMware會影響到




安裝套件

sudo apt-get update
sudo apt install wireguard resolvconf -y

套件 resolvconf 是DNS相關的這個不裝連接會報錯


安裝設定檔

這邊自己把設定檔準備好複製進去,範例中 AX4200_wgs_client 這是我的設定檔名稱,記得改成自己的名字

然後執行下面的命令複製到正確的位置去,並設置權限

sudo cp AX4200_wgs_client.conf /etc/wireguard/wg1.conf
sudo chmod 600 /etc/wireguard/wg1.conf

這裡的 wg1.conf 最終會變成虛擬網卡的名稱,想改的話趁這時候換個名字


連線

然後就可以直接連上了

sudo wg-quick up wg1

連好之後可以確定一下狀態

sudo wg

要重新連接可以用這樣

sudo wg-quick down wg1 && sudo wg-quick up wg1 && sudo wg


測試對外IP

測試連線用 CURL 打出去看一下 IP

echo -e "🌐 外部 IPv4:\033[1;33m$(curl -4 -s ifconfig.me)\033[0m"


開機自動連結

最後測試好了可以設定成開機自動連接

sudo systemctl enable wg-quick@wg1

這樣開機的時候就會自動連上了




如何替 google部落格 中的標準 code 區塊 統一新增複製按鈕

如何替 google部落格 中的標準 code 區塊 統一新增複製按鈕

本站可以算是我自己的筆記儲存庫吧自己也經常用,大概最困擾的就是不斷的滑鼠拖曳然後按複製紐,研究了一下成功造出複製按鈕了,現在網站所有文章都有複製按鈕可以用了!

由於是用 JavaScript 動態插入的,所以所有文章都會生效,編輯習慣也不用特別改就是原本的 code 區塊即可不用改變


代碼從這個地方可以新增小工具並插入



然後選擇 HTML/JavaScript



在內容處以下的代碼,直接無腦貼上就行了

<style>
.code-copy-box {
  position: relative;
  margin: 1em 0;
}
.code-copy-btn {
  position: absolute;
  top: 8px;       /* 向上貼近一點 */
  right: 8px;     /* 向右貼近一點 */
  z-index: 3;
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 0;
  opacity: 0.74;
  transition: opacity 0.15s;
  line-height: 1;
}
.code-copy-btn:hover,
.code-copy-btn.copied {
  opacity: 1;
}
.code-copy-btn svg {
  display: block;
  width: 16px;    /* 比原本更小巧 */
  height: 16px;
  fill: #b7babd; /* 深色模式明亮灰 */
  transition: fill 0.18s;
}
.code-copy-btn.copied svg {
  fill: #79ff98; /* 成功後綠色 */
}
.code-copy-tooltip {
  display: none;
  position: absolute;
  top: 4px;
  right: 30px;   /* 跟右邊距離要比 svg 再多一點 */
  background: #22282f;
  color: #b7babd;
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 12px;
  font-family: inherit;
  box-shadow: 0 1px 8px rgba(0,0,0,0.18);
  pointer-events: none;
  z-index: 10;
  opacity: 0.99;
  align-items: center;
  gap: 5px;
  user-select: none;
}
.code-copy-btn.copied + .code-copy-tooltip {
  display: flex;
  animation: fadein 0.15s;
}
@keyframes fadein {
  from { opacity: 0; transform: translateY(-7px);}
  to   { opacity: 0.99; transform: translateY(0);}
}
</style>
<script>
document.addEventListener("DOMContentLoaded", function() {
  const COPY_SVG = `<svg aria-hidden="true" height="16" viewBox="0 0 16 16" width="16" class="octicon octicon-copy"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg>`;
  const CHECK_SVG = `<svg aria-hidden="true" height="16" viewBox="0 0 16 16" width="16" class="octicon octicon-check"><path fill="#79ff98" d="M13.78 5.22a.75.75 0 0 1 0 1.06l-5.25 5.25a.75.75 0 0 1-1.06 0l-2.25-2.25a.75.75 0 1 1 1.06-1.06l1.72 1.72 4.72-4.72a.75.75 0 0 1 1.06 0Z"></path></svg>`;

  document.querySelectorAll('code.hljs').forEach(function(code) {
    if (code.parentElement.classList.contains('code-copy-box')) return;
    var wrapper = document.createElement('div');
    wrapper.className = 'code-copy-box';
    code.parentNode.insertBefore(wrapper, code);
    wrapper.appendChild(code);

    var btn = document.createElement('button');
    btn.className = 'code-copy-btn';
    btn.type = 'button';
    btn.innerHTML = COPY_SVG;
    btn.setAttribute('aria-label', '複製程式碼');
    btn.setAttribute('title', '複製程式碼');
    wrapper.appendChild(btn);

    var tooltip = document.createElement('div');
    tooltip.className = 'code-copy-tooltip';
    tooltip.innerHTML = `Copied!`;
    wrapper.appendChild(tooltip);

    btn.addEventListener('click', function() {
      // 直接處理:移除非斷行空格,然後移除最後一個換行符(通常是多餘的)
      var textToCopy = code.innerText.replace(/\u00A0/g, ' ').replace(/\n$/, '');

      navigator.clipboard.writeText(textToCopy).then(function() {
        btn.classList.add('copied');
        btn.innerHTML = CHECK_SVG;
        setTimeout(function() {
          btn.classList.remove('copied');
          btn.innerHTML = COPY_SVG;
        }, 1200);
      });
    });
  });
});
</script>