Linux 學習筆記(一):內網穿透

軟件安裝

frp 是一個可用於內網穿透的高性能的反向代理應用,支持 tcp, udp, http, https 協議。

下載並安裝

  • 獲取 VPS 的處理器架構

依據處理器架構選擇對應版本的 frp、下載並解壓html

[jiong@centos7 ~]$ arch
[jiong@centos7 ~]$ wget https://github.com/fatedier/frp/releases/download/v0.21.0/frp_0.21.0_linux_amd64.tar.gz
[jiong@centos7 ~]$ tar -xzvf frp_0.21.0_linux_amd64.tar.gz

原理介紹

圖片描述

  • 在具備公網 IP 的 機器上使用 frps 服務
  • 在內網機器上使用 frpc 服務,將其「註冊」到 frps 的機器上
  • 外部用戶即可以經過具備公網 IP 的 frps 機器訪問到內網的機器,這實際上就是一個方向代理的過程

鏈接內網機器

# frpc.ini
[common]
server_addr = 123.123.123.123
server_port = 7000
log_file    = ./frps.log

[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000
  • 內網機器經過「x.x.x.x」與 frps 代理機器創建鏈接
  • 內網機器經過 「frpc.ini」在 frps 代理機器上「註冊」了一個代理服務,監聽 6000 端口
  • 當外部用戶經過 SSH 鏈接代理機器的 6000 端口,便鏈接到了內網機器,這是一個方向代理了內網機器的過程。

經過查看事實日子能夠看到 frp 的交互流程linux

[root@host frp]# tail -f frps.log 

2018/11/15 00:47:18 [I] [service.go:130] frps tcp listen on 0.0.0.0:7000
2018/11/15 00:47:18 [I] [root.go:207] Start frps success
2018/11/15 00:48:33 [I] [service.go:319] client login info: ip [112.5.201.153:52355] version [0.21.0] hostname [] os [windows] arch [amd64]
2018/11/15 00:49:17 [I] [service.go:319] client login info: ip [211.97.128.100:3722] version [0.21.0] hostname [] os [windows] arch [amd64]
2018/11/15 00:56:16 [I] [service.go:319] client login info: ip [112.5.201.153:52652] version [0.21.0] hostname [] os [linux] arch [amd64]
2018/11/15 00:56:16 [I] [proxy.go:217] [5571d58b034abea2] [ssh] tcp proxy listen port [6000]
2018/11/15 00:56:16 [I] [control.go:335] [5571d58b034abea2] new proxy [ssh] success
2018/11/15 00:57:33 [I] [proxy.go:87] [5571d58b034abea2] [ssh] get a new work connection: [112.5.201.153:52652]
2018/11/15 00:59:17 [I] [proxy.go:87] [5571d58b034abea2] [ssh] get a new work connection: [112.5.201.153:52652]
2018/11/15 01:00:55 [I] [proxy.go:87] [5571d58b034abea2] [ssh] get a new work connection: [112.5.201.153:52652]
2018/11/15 01:20:19 [I] [proxy.go:87] [5571d58b034abea2] [ssh] get a new work connection: [112.5.201.153:52652]
2018/11/15 01:22:21 [I] [control.go:220] [9f010fa9d8b9ea9b] control writer is closing
2018/11/15 01:22:21 [I] [control.go:292] [9f010fa9d8b9ea9b] client exit success
2018/11/15 01:23:03 [I] [proxy.go:87] [5571d58b034abea2] [ssh] get a new work connection: [112.5.201.153:52652]
2018/11/15 01:23:48 [I] [proxy.go:87] [5571d58b034abea2] [ssh] get a new work connection: [112.5.201.153:52652]
2018/11/15 01:24:35 [I] [service.go:319] client login info: ip [211.97.128.100:3724] version [0.21.0] hostname [] os [windows] arch [amd64]
2018/11/15 02:01:13 [I] [control.go:220] [3166cd00e061258b] control writer is closing
2018/11/15 02:01:13 [I] [control.go:292] [3166cd00e061258b] client exit success
2018/11/15 02:08:16 [I] [control.go:220] [4972517f0d8c09db] control writer is closing
2018/11/15 02:08:16 [I] [control.go:292] [4972517f0d8c09db] client exit success
2018/11/15 02:08:18 [I] [service.go:319] client login info: ip [112.5.201.153:52900] version [0.21.0] hostname [] os [windows] arch [amd64]
2018/11/15 02:08:19 [W] [control.go:332] [9168626ea7c7ca07] new proxy [ssh] error: port already used
2018/11/15 02:08:19 [I] [proxy.go:401] [9168626ea7c7ca07] [home] stcp proxy custom listen success

以上配置實現了外部用戶 SSH 鏈接內網無公共 IP 的虛擬機git

外部用戶指:未容許 frp 的機器github

配置儀表盤

儀表盤: Dashboard
[common]
server_addr    = 123.123.123.123
server_port    = 7000
log_file       = ./frps.log
dashboard_port = 7500
dashboard_user = admin
dashboard_pwd  = admin

自定義帳戶密碼,使用服務端IP:Dashboard端口即可訪問web

123.123.123.123:7500

在儀表盤界面能夠很方便地看到鏈接池地狀態鏈接數等segmentfault

代理遠程桌面

服務名稱、轉發端口不能重複,由於都是註冊在「frps」上的windows

  1. 「frpc-1」在「frps」上註冊了「home」、「company_visitor」這兩個服務
  2. 當「frpc-2」在「frps」上某一註冊服務的「sk」與「frpc-1」的「sk」一致時,代理到「frpc-1」的「3389端口」
# frpc-1

[common]
server_addr = 93.179.97.24
server_port = 7000
log_file    = ./frpc.log

[home]
type            = stcp
sk              = mstsc
local_ip        = 127.0.0.1
local_port      = 3389
use_encryption  = true
use_compression = true

[company_visitor]
type            = stcp
sk              = mstsc
role            = visitor
server_name     = company
bind_addr       = 127.0.0.1
bind_port       = 1116
use_encryption  = true
use_compression = true
  1. 「frpc-2」在「frps」上註冊了「company」、「home_visitor」這兩個服務
  2. 當「frpc-1」在「frps」上某一註冊服務的「sk」與「frpc-1」的「sk」一致時,代理到「frpc-2」的「3389端口」
# frpc-2

[common]
server_addr = 93.179.97.24
server_port = 7000
log_file    = ./frpc.log

[company]
type            = stcp
sk              = mstsc
local_ip        = 127.0.0.1
local_port      = 3389
use_encryption  = true
use_compression = true

[home_visitor]
type            = stcp
sk              = mstsc
role            = visitor
server_name     = home
bind_addr       = 127.0.0.1
bind_port       = 1118
use_encryption  = true
use_compression = true

以上配置實現了「frpc-2」、「frpc-1」互相遠程桌面centos

後臺運行

  • WINDOWS 後臺運行——註冊服務

簡而言之,winsw 能將 windows 的程序註冊到服務中,此處用來自啓及後臺運行 frpcapi

下載winsw,在相應目錄下編寫 XML 配置文件服務器

<service>
    <id>frp</id>
    <name>內網穿透</name>
    <description>內網穿透</description>
    <executable>frpc</executable>
    <arguments>-c frpc.ini</arguments>
    <onfailure action="restart" delay="60 sec"/>
    <onfailure action="restart" delay="120 sec"/>
    <logmode>reset</logmode>
</service>

執行命令註冊爲 windows 服務

winsw install
  • WINDOWS 後臺運行——計劃任務

建立 bat 腳本

@echo off

mode con cols=60 lines=20

color a

title 服務監聽工具

:frpc

D:\frpc\frpc.exe -c d:\frpc\frpc.ini

ping -n 2 127.1 >nul

cls

goto frpc

控制面板建立計劃任務,具體參見:建立windows計劃任務使FRP開機啓動

  • Linux 後臺運行

後臺運行的四種方法

supervisor
rc.local
systemd
nohup

使用 systemctl 設置開機自啓

[root@host system]# vi /etc/systemd/system/frps.service

編寫 frps.service 使用 systemctl 設置開機自啓

[Unit]
Description=frps daemon

[Service]
Type=simple
ExecStart=/opt/frps/frps -c /opt/frps/frps.ini

[Install]
WantedBy=multi-user.target
[root@host system]# systemctl start frps
[root@host system]# systemctl enable frps

熱更新

[common]
admin_addr = 127.0.0.1
admin_port = 7400

啓用了 admin 端口後

frpc reload -c ./frpc.ini

# reload success

須要注意的是,在windows下使用服務自啓或計劃任務自啓時,使用 reload 會提示一下信息

frpc reload error: Get http://127.0.0.1:7400/api/reload: dial tcp 127.0.0.1:7400: connectex: No connection could be made because the target machine actively refused it.

解決辦法是:關閉進程重啓

查看代理狀態

一樣須要啓動 admin 端口

[common]
admin_addr = 127.0.0.1
admin_port = 7400

啓用了 admin 端口後

frpc status -c ./frpc.ini

# Proxy Status...
# TCP
# Name    Status   LocalAddr           Plugin  RemoteAddr         Error
# vmshop  running  192.168.108.129:80          93.179.97.24:3333

# STCP
# Name    Status   LocalAddr          Plugin  RemoteAddr  Error
# home    running  127.0.0.1:3389

代理內網服務

使用的也是 tcp

[common]
server_addr = 93.179.97.24
server_port = 7000
log_file    = ./frpc.log

...

[web]
type        = tcp
local_ip    = 192.168.0.1
local_port  = 80
remote_port = 6000

因而在外部就能夠使用

93.179.97.24:6000

訪問到 web

相關文章

Linux 學習筆記(一):內網穿透
Linux 學習筆記(二):搭建我的Git服務器
Linux 學習筆記(三):Ubuntu 操做系統
Linux 學習筆記(四):Docker
Linux 學習筆記(五):Redis
Linux 學習筆記(六):Linux

相關文章
相關標籤/搜索