透過 nginx 代理程式提升你的 Webogram 實例

嘿哈布爾!

最近,我發現自己處於這樣一種情況:我必須在公司網路內工作,而網路存取受到限制,正如你從標題中猜到的那樣,Telegram 在那裡被封鎖了。我相信很多人都熟悉這種情況。

我可以不用即時通訊工具,但我需要 Telegram 來運作。無法在工作機器上安裝客戶端,也無法使用個人筆記型電腦。另一個解決方案似乎是使用它 官方網頁版,但正如您所猜測的,它也不可用。我立即劃掉了尋找非官方鏡像的選項(我希望原因很明顯)。

幸運的是,Webogram 是一個開源項目,其原始碼可在 GitHub上 它的作者(我非常感謝他!)
安裝和啟動本身並不困難,但是,如果您在阻止訪問 Telegram 伺服器的網路中使用它,您很可能會感到失望而不是成功,因為 Web 版本會從​​使用者的機器向 Telegram 伺服器發送請求。

幸運的是,這是一個相當簡單(但不是很明顯)的修復方法。我想警告你,我不是這個決定的作者。我設法找到了它 分支,其中討論了與我類似的問題。 GitHub 用戶建議的解決方案 特克諾喬克,它對我幫助很大,但是,我確信它也可以對其他人有所幫助,所以我決定寫這個教程。

下面您將找到有關設定 Webogram 映像和使用 nginx 設定其對 Telegram 伺服器的請求代理程式的逐步指南。

作為範例,我選擇了新安裝和更新的 Ubuntu Server 18.04.3。

警告: 本教學不包含如何在 nginx 中設定域的說明。這必須獨立完成。本教學假設您已經擁有一個配置了 SSL 的網域,並且您計劃配置它的伺服器可以存取 Telegram 伺服器(以您喜歡的任何方式)

假設該伺服器的IP為10.23.0.3,網域為mywebogram.localhost

根據這些約定,我將給出配置範例。不要忘記將值更改為您自己的值。

那麼就讓我們開始吧:

要執行 Webogram,我們需要 nodejs。預設情況下,如果從 Ubuntu 儲存庫安裝它,我們將獲得 nodejs 版本 8.x。我們需要 12.x:

curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - 
sudo apt update && sudo apt -y install nodejs

我們選擇 Webogram 的所在地。

例如,讓我們將其放在主目錄的根目錄中。為此,我們將官方儲存庫克隆到我們的​​伺服器:

cd ~ && git clone https://github.com/zhukov/webogram.git

下一步是安裝運行應用程式所需的所有相依性:

cd webogram && npm install

讓我們嘗試進行試運行。運行以下命令:

npm start

之後,我們嘗試在瀏覽器中開啟它

 http://10.23.0.3:8000/app/index.html

如果您到目前為止已正確完成所有操作,則 Webogram 授權頁面將會開啟。

現在我們需要配置應用程式以作為服務運行。為此,我們將創建一個文件

sudo touch /lib/systemd/system/webogram.service

在任何編輯器中開啟它並賦予它以下外觀(輸入您的 WorkDirectory 路徑)

[Unit]
Description=Webogram mirror
[Service]
WorkingDirectory=/home/tg/webogram
ExecStart=/usr/bin/npm start
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target

之後我們執行以下命令:

應用程式變更

sudo systemctl daemon-reload

啟用自動啟動:

sudo systemctl enable webogram.service

讓我們啟動服務:

sudo systemctl start webogram.service

完成這些步驟後,Webogram 將繼續在連接埠 8000 上可用。

由於我們將透過 nginx 配置對 Webogram 的訪問,因此我們將關閉連接埠 8000 以接收來自外部的請求。

我們為此使用 udf 實用程式(或任何對您方便的方法):

sudo ufw deny 8000

如果您仍然決定使用 udf,但它在伺服器上被停用,請添加更多規則(以便一切不會崩潰)並啟用 udf:

sudo ufw allow ssh
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable

接下來,我們繼續更改 nginx 配置。

正如我上面警告的那樣,假設您的伺服器已經配置了 SSL 的網域。我僅提醒您注意需要添加到網域設定檔中的內容才能正確運行:


server {
...
  location ^~ /pluto/apiw1/ {
    proxy_pass https://pluto.web.telegram.org/apiw1/;
  }
  location ^~ /venus/apiw1/ {
    proxy_pass https://venus.web.telegram.org/apiw1/;
  }
  location ^~ /aurora/apiw1/ {
    proxy_pass https://aurora.web.telegram.org/apiw1/;
  }
  location ^~ /vesta/apiw1/ {
    proxy_pass https://vesta.web.telegram.org/apiw1/;
  }
  location ^~ /flora/apiw1/ {
    proxy_pass https://flora.web.telegram.org/apiw1/;
  }
  location ^~ /pluto-1/apiw1/ {
    proxy_pass https://pluto-1.web.telegram.org/apiw1/;
  }
  location ^~ /venus-1/apiw1/ {
    proxy_pass https://venus-1.web.telegram.org/apiw1/;
  }
  location ^~ /aurora-1/apiw1/ {
    proxy_pass https://aurora-1.web.telegram.org/apiw1/;
  }
  location ^~ /vesta-1/apiw1/ {
    proxy_pass https://vesta-1.web.telegram.org/apiw1/;
  }
  location ^~ /flora-1/apiw1/ {
    proxy_pass https://flora-1.web.telegram.org/apiw1/;
  }
  location ^~ /DC1/ {
    proxy_pass http://149.154.175.10:80/;
  }
  location ^~ /DC2/ {
    proxy_pass http://149.154.167.40:80/;
  }
  location ^~ /DC3/ {
    proxy_pass http://149.154.175.117:80/;
  }
  location ^~ /DC4/ {
    proxy_pass http://149.154.175.50:80/;
  }
  location ^~ /DC5/ {
    proxy_pass http://149.154.167.51:80/;
  }
  location ^~ /DC6/ {
    proxy_pass http://149.154.175.100:80/;
  }
  location ^~ /DC7/ {
    proxy_pass http://149.154.167.91:80/;
  }
  location ^~ /DC8/ {
    proxy_pass http://149.154.171.5:80/;
  }
 location / {
    auth_basic "tg";
    auth_basic_user_file /etc/nginx/passwd.htpasswd;
    proxy_pass http://localhost:8000/;
    proxy_read_timeout 90s;
    proxy_connect_timeout 90s;
    proxy_send_timeout 90s;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
  }
}

我們在 nginx 設定中新增的內容:

  • 我們更改根位置,這將代理請求到端口 8000,Webogram 響應該端口
  • 我們使用 basic-auth 關閉根位置。這純粹是像徵性的一步,旨在防止我們的應用程式被窺探和機器人入侵。 (這樣就不會出現阻塞問題)
  • 一堆具有到 Telegram 伺服器的 proxy_path 的位置是我們的端點,我們將透過它們代理我們的請求。

另外,讓我們建立一個文件 /etc/nginx/passwd.htpasswd;,以便 nginx 可以與使用者密碼進行比較。

sudo apt install apache2-utils
sudo htpasswd -c /etc/nginx/passwd.htpasswd tg

透過 nginx 代理程式提升你的 Webogram 實例

重啟 nginx:

sudo systemctl restart nginx

Webogram 現在只在 mywebogram.localhost/app/index.html 輸入建立 htpasswd 指令時定義的登入名稱和密碼後。

只剩下一點點了:讓我們對專案本身做一些小小的改動。

在編輯器中開啟文件 ~/webogram/app/js/lib/mtproto.js

並將其開頭改為以下形式:

/*!
 * Webogram v0.7.0 - messaging web application for MTProto
 * https://github.com/zhukov/webogram
 * Copyright (C) 2014 Igor Zhukov <igor.beatle@gmail.com>
 * https://github.com/zhukov/webogram/blob/master/LICENSE
 */

angular.module('izhukov.mtproto', ['izhukov.utils'])

  .factory('MtpDcConfigurator', function () {
    var sslSubdomains = ['pluto', 'venus', 'aurora', 'vesta', 'flora']

    var dcOptions = Config.Modes.test
      ? [
        {id: 1, host: 'mywebogram.localhost/DC1',  port: 80},
        {id: 2, host: 'mywebogram.localhost/DC2',  port: 80},
        {id: 3, host: 'mywebogram.localhost/DC3', port: 80}
      ]
      : [
        {id: 1, host: 'mywebogram.localhost/DC4',  port: 80},
        {id: 2, host: 'mywebogram.localhost/DC5',  port: 80},
        {id: 3, host: 'mywebogram.localhost/DC6', port: 80},
        {id: 4, host: 'mywebogram.localhost/DC7',  port: 80},
        {id: 5, host: 'mywebogram.localhost/DC8',   port: 80}
      ]

    var chosenServers = {}

    function chooseServer (dcID, upload) {
      if (chosenServers[dcID] === undefined) {
        var chosenServer = false,
          i, dcOption

        if (Config.Modes.ssl || !Config.Modes.http) {
          var subdomain = sslSubdomains[dcID - 1] + (upload ? '-1' : '')
          var path = Config.Modes.test ? 'apiw_test1' : '/apiw1/'
          chosenServer = 'https://mywebogram.localhost/' + subdomain + path
          return chosenServer
        }
       for (i = 0; i < dcOptions.length; i++) {
          dcOption = dcOptions[i]
          if (dcOption.id == dcID) {
            chosenServer = 'http://' + dcOption.host + '/apiw1'
            break
          }
        }
        chosenServers[dcID] = chosenServer
      }
...
 

此後,您需要使用瀏覽器中的應用程式刷新頁面。

打開瀏覽器控制台並查看應用程式的網路請求。如果一切正常且 XHR 請求到達您的伺服器,那麼一切都正確完成,並且 Webogram 現在透過 nginx 代理程式。

透過 nginx 代理程式提升你的 Webogram 實例

我希望本教程對我以外的其他人也有用。

非常感謝所有讀到最後的讀者。

如果有人遇到任何困難或我有任何不準確的表達,我會很樂意在評論或 PM 中回答並盡力幫助您。

來源: www.habr.com

添加評論