顯示具有 VPS 標籤的文章。 顯示所有文章
顯示具有 VPS 標籤的文章。 顯示所有文章

2021年6月2日 星期三

frp 心得

 概覽

frp是內網穿透的反向代理工具,支持多種服務(HTTP、SSH、RDP),需要準備一台VPS

相關連接

文檔

下載


服務器

查CPU型號(AMD、Intel、ARM)

# grep -i core /proc/cpuinfo
model name      : AMD EPYC 7601 32-Core Processor

下載、解壓縮、進入目錄

# wget https://github.com/fatedier/frp/releases/download/v0.36.2/frp_0.36.2_linux_amd64.tar.gz
# tar zxvf frp_0.36.2_linux_amd64.tar.gz
# cd frp_0.36.2_linux_amd64/

編輯 frps.ini


[common]
# frp监听的端口,默认是7000,可以改成其他的
bind_port = 7000
# 授权码,请改成更复杂的
token = 12345678
# HTTP 类型代理监听的端口
vhost_http_port = 8080
# frpc 設置 tls_enable = true 才能連,連線做tls加密
tls_only = true
# tls_trusted_ca_file 内容是有效的话,客戶端要配置tls_cert_file和tls_key_file 才能連
tls_trusted_ca_file = /frp_tls_files/ca.crt

# frp管理后台端口,请按自己需求更改
dashboard_port = 7500
# frp管理后台用户名和密码,请改成自己的
dashboard_user = admin
dashboard_pwd = admin
enable_prometheus = true
# 二级域名后缀
subdomain_host = yourdomain.com

# frp日志配置
log_file = /var/log/frps.log
log_level = info
log_max_days = 3
s

設置和啟動frp服務

在frp目錄下執行
mkdir -p /etc/frp
cp frps.ini /etc/frp
cp frps /usr/bin
cp systemd/frps.service /usr/lib/systemd/system/
systemctl enable frps
systemctl start frps
s

防火墻放行端口

# 添加监听端口
firewall-cmd --permanent --add-port=7000/tcp
# 添加管理后台端口
firewall-cmd --permanent --add-port=7500/tcp
firewall-cmd --reload
s

訪問frp後台

瀏覽器打開"http://服務器IP:後台管理端口" ,輸入用戶名和密碼可以查看連接狀態:



客戶端

下載frp

到  https://github.com/fatedier/frp/releases ,下載最新版客戶端
windows:32位系統 frp_0.36.2_windows_386.zip ,64位系統  frp_0.36.2_windows_amd64.zip  ,不知道系統多少下載32位

解壓縮下載的壓縮包,進入文件夾內

編輯 frpc.ini 

(提供服務的遠程電腦)
# 服务端配置
[common]
server_addr = 服务器ip
# 请换成设置的服务器端口
server_port = 7000
token = 12345678
# 自定义 TLS 协议加密
tls_enable = true
tls_cert_file = C:\frp_tls_files\client.crt
tls_key_file = C:\frp_tls_files\client.key

# 配置http服务,可用于小程序开发、远程调试等
[web]
type = http
local_ip = 127.0.0.1
local_port = 80
subdomain = win
# 将 frpc 与 frps 之间的通信内容加密传输,将会有效防止传输内容被截取。
use_encryption = true
# 对传输内容进行压缩,可以有效减小 frpc 与 frps 之间的网络流量,加快流量转发速度,但是会额外消耗一些 CPU 资源。
use_compression = true
# 设置 BasicAuth 鉴权
http_user = abc
http_pwd = abc

# 配置遠程桌面服務
[secret_rdp]
# stcp(secret tcp) 类型的代理可以避免让任何人都能访问到要穿透的服务,但是访问者也需要运行另外一个 frpc 客户端。
type = stcp
# 只有 sk 一致的用户才能访问到此服务
sk = abcdefg
local_ip = 127.0.0.1
local_port = 3389
use_encryption = true
use_compression = true
s
ps.
1. 一個服務端可以同時給多個客戶端使用
2. [ssh]這樣的名稱必須全局唯一,即就算有多個客戶端,也只能使用一次,其他的可以用[ssh2]、[ssh3]等;
3. 除了type為http/https,端口只能被一個服務使用

客戶端連上服務端

git bash 進入frp目錄,然後執行
$ ./frpc.exe -c frpc.ini

後台查狀態

登錄frp管理後台,應該可以看到客戶端已經連上來了

firewalld

firewalld 啟動失敗

原本用iptables,要改用firewalld,但是 firewalld 啟動失敗
# systemctl start firewalld
Failed to start firewalld.service: Unit is masked.

解法

# systemctl unmask --now firewalld
Removed symlink /etc/systemd/system/firewalld.service.
# systemctl enable firewalld
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/basic.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
# systemctl start firewalld


檢查 firewalld 設定

使用 --list-all 檢查
# firewall-cmd --list-all
查可用的zones
# firewall-cmd  --get-zones
查預設的zone
# firewall-cmd --get-default-zone
public
查當前zone開放的端口
# firewall-cmd --list-ports
新增可用的端口
# firewall-cmd --add-port=port-number/port-type
如:
# firewall-cmd --add-port=8080/tcp
讓新的設定永久生效
# firewall-cmd --runtime-to-permanent


openssl升級

因為使用到證書登錄,證書登錄用到了openssl,但是openssl舊版有 Heartbleed bug 如果你的openssl太舊,需要升級openssl,到 https://www.openssl.org/source/ 下載最新的openssl

安裝

# wget https://www.openssl.org/source/openssl-1.1.1k.tar.gz
# tar zxf openssl-1.1.1k.tar.gz
# cd openssl-1.1.1k/
# ./config
# make
# make test
# make install

測試新裝的openssl

# /usr/local/bin/openssl version
/usr/local/bin/openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory

解法

# ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/
# ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/

再次測試

# /usr/local/bin/openssl version
OpenSSL 1.1.1k  25 Mar 2021

替換新舊版本

# mv /usr/bin/openssl  /usr/bin/openssl-old
# ln -s /usr/local/bin/openssl /usr/bin/openssl
# openssl version
OpenSSL 1.1.1k  25 Mar 2021

自定義 TLS 協議加密

生成服務器私鑰

# openssl genrsa -out ca.key 1024
Generating RSA private key, 1024 bit long modulus (2 primes)
.....+++++
.......+++++
e is 65537 (0x010001)

# ls
ca.key

根據私鑰生成證書申請文件csr

# openssl req -new -key ca.key -out ca.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Shanghai
Locality Name (eg, city) []:Shanghai
Organization Name (eg, company) [Internet Widgits Pty Ltd]:JY
Organizational Unit Name (eg, section) []:JY
Common Name (e.g. server FQDN or YOUR name) []:*.yourdomain.com   # 這裡輸入你的域名,*.yourdomain.com生成通配符域名證書
Email Address []:yourname@email.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:  # 這裡輸入密碼,沒有密碼可以空
An optional company name []:JY

# ls
ca.csr  ca.key

私鑰對證書申請進行簽名從而生成證書

# openssl x509 -req -in ca.csr -out ca.crt -signkey ca.key -days 3650
Signature ok
subject=C = CN, ST = Shanghai, L = Shanghai, O = JY, OU = JY, CN = *.yourdomain.com, emailAddress = yourname@email.com
Getting Private key

# ls
ca.crt  ca.csr  ca.key

生成 frpc 的私鑰

# openssl genrsa -out client.key 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
..........+++++
................................................+++++
e is 65537 (0x010001)

# ls
ca.crt  ca.csr  ca.key  client.key

準備默認 OpenSSL 配置文件於當前目錄

# cp /etc/pki/tls/openssl.cnf ./my-openssl.cnf
# ls
ca.crt  ca.csr  ca.key  client.key  my-openssl.cnf

根據frpc的私鑰生成frpc的證書申請文件csr

# openssl req -new -sha256 -key client.key \
>     -subj "/C=XX/ST=DEFAULT/L=DEFAULT/O=DEFAULT/CN=client.com" \
>     -reqexts SAN \
>     -config <(cat my-openssl.cnf <(printf "\n[SAN]\nsubjectAltName=DNS:client.com,DNS:example.client.com")) \
>     -out client.csr
# ls
ca.crt  ca.csr  ca.key  client.csr  client.key  my-openssl.cnf

ps. 使用 client.com 沒事,即使這是無效的域名,不影響使用

生成 frpc 的證書(用到ca.crtca.key

# openssl x509 -req -days 365 \
>     -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial \
> -extfile <(printf "subjectAltName=DNS:client.com,DNS:example.client.com") \
> -out client.crt
Signature ok
subject=C = XX, ST = DEFAULT, L = DEFAULT, O = DEFAULT, CN = client.com
Getting CA Private Key
# ls
ca.crt  ca.csr  ca.key  ca.srl  client.crt  client.csr  client.key  my-openssl.cnf


當 服務器的frps.ini 配置的 tls_trusted_ca_file 是有效的 ca.crt 時,客戶端frpc.ini 的 tls_cert_file 和 tls_key_file 必須是有效的 client.crt 和 client.key 。完成frps 單向驗證 frpc 的身份

nginx反向代理frp後台

因為cloudflare免費的只能代理80端口,想要用cloudflare的https訪問frp後台,可以這樣做
nginx新增配置:
  
server
{
    server_name  frp.yourdomain.com;

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

在想要訪問內網服務的機器上也部署 frpc

frpc.ini配置如下:
  
# 服务端配置
[common]
(同提供服務的遠程電腦的frpc.ini)

[secret_ssh_visitor]
type = stcp
# stcp 的访问者
role = visitor
# 要访问的 stcp 代理的名字
server_name = secret_rdp
sk = abcdefg
# 绑定本地端口用于访问 SSH 服务
bind_addr = 127.0.0.1
bind_port = 13389
  
s

訪問內網穿透服務

web

提供服務的遠程電腦打開web服務,外網任意一台電腦可直接訪問 https://win.yourdomain.com/ ,即使遠程電腦的web服務

secret_rdp

外網電腦打開Windows 遠端桌面連線,輸入  127.0.0.1:13389 就可以連接到本地Windows
注意一定要開啟windows電腦的【啟用遠端桌面】

將 frp 封裝成 windows 後台服務

使用 NSSM 將 frp 封裝成 windows 服務,可以在後台運行,並且開機自啟動

安裝nssm且新增frpc服務

下載 nssm 2.24  後解壓縮,(打開git bash)進入 nssm-2.24 目錄
/path/nssm-2.24/win64
$ ./nssm.exe install frpc
Administrator access is needed to install a service.


在彈出的對話框中填寫
Path: C:\path\frp_0.36.2_windows_386\frpc.exe
Startup directory: C:\path\frp_0.36.2_windows_386
Arguments: -c C:\path\frp_0.36.2_windows_386\frpc.ini

Details頁簽可以設定是否開機(Startup type)自動啟動:automatic / manual 

編輯服務配置

$ ./nssm.exe edit frpc

啟動和停止

用命令【啟動】和【停止】實際測試無效
$ ./nssm.exe start frpc
Can't open service!
OpenService(): 存取被拒。
$ ./nssm.exe stop frpc
Can't open service!
OpenService(): 存取被拒。

要用這方式停止
運行 services.msc ,可以打開 windows 的服務管理器。在這裡面手動停用/啟動

刪除服務

$ ./nssm.exe remove frpc
Administrator access is needed to remove a service.







參考資料

https://www.vpsjxw.com/vps_use/vps_frp_intro/  vps+frp内网穿透,外网远程访问局域网内主机的ssh、远程桌面、网站服务
https://tlanyan.me/frp-tunnel-tutorial/  frp内网穿透教程(主要)
https://bobcares.com/blog/failed-to-start-firewalld-service-unit-is-masked/  Failed to start firewalld service unit is masked – How we fix it!
https://www.4spaces.org/how-to-upgrade-openssl-on-centos-7/  CentOS如何升级openssl到最新版本
https://ningyu1.github.io/site/post/51-ssl-cert/  Openssl生成自签名证书,简单步骤
https://www.sohu.com/a/416379503_610671  教你用FRP做内网穿透 使用远程桌面连接家里的windows电脑








2016年9月16日 星期五

使用nginx限制連線IP

目的
為了只讓VPN連線進來的連線能訪問網站

編輯nginx設定檔
# vim /etc/nginx/sites-available/test.yourdomain.com.conf
在 location / { } 區塊中新增
location / {
    ...
    allow 10.12.0.0/24; # Openconnect / iPhone,你VPN後的IP區段
    deny all;
}
要注意 location ~ \.php$ { }區塊中沒設的話, 訪問php頁面不會受影響

最後重啟nginx


參考資料:
http://www.cyberciti.biz/faq/linux-unix-nginx-access-control-howto/  Nginx Block And Deny IP Address OR Network Subnets


設定Private DNS


有些網址不想讓外面人訪問,所以設定了Private DNS,讓使用VPN連線進來的設備可以直接打開網址訪問私有站。這篇的情況是iPhone 使用AnyConnect 連線openconnect,server系統為 CentOS 7

安裝bind9
# yum install bind bind-utils
編輯 /etc/named.conf 設定檔
# /etc/named.conf
新增 ACL "trusted"區段
acl "trusted" {
        10.12.0.1;    # ns1 - DNS server
        ...
        10.12.0.190;  # iPhone LAN IP
        ...
        10.12.0.254;
};
因為VPN連進來,會自動分配虛擬IP,所以只好窮舉
編輯 options 區段
options {
    listen-on port 53 { 127.0.0.1; 10.12.0.1; };
    # listen-on-v6 port 53 { ::1; };
    ...
    allow-query     { trusted; };  # allows queries from "trusted" clients
    ...
}
10.12.0.1為我連上VPN後的主要DNS IP
在最底下加入
include "/etc/named/named.conf.local";

設定 local file
# vim /etc/named/named.conf.local
zone "test2.yourdomain.com" {
    type master;
    file "/etc/named/zones/db.test2.yourdomain.com";
};
zone "test3.yourdomain.com" {
    type master;
    file "/etc/named/zones/db.test3.yourdomain.com";
};
zone "12.10.in-addr.arpa" {
    type master;
    file "/etc/named/zones/db.10.12";
};

因為我的 test.yourdomain.com DNS解析是設在CloudFlare 公開的,如果這邊zone只設 yourdomain.com ,然後在正解和反解檔案分別設 test2.yourdomain.com 、test3.yourdomain.com,這樣子連vpn後使用私有VPN 雖然查得到 test2.yourdomain.comtest3.yourdomain.com 但設在代管上的test.yourdomain.com會查詢不到

產生Forward Zone File
# chmod 755 /etc/named
# mkdir /etc/named/zones
編輯 test2.yourdomain.com 正解設定
# vim /etc/named/zones/db.test2.yourdomain.com
$TTL    604800
@       IN      SOA     ns1.test2.yourdomain.com. admin.test2.yourdomain.com. (
                  3       ; Serial
             604800     ; Refresh
              86400     ; Retry
            2419200     ; Expire
             604800 )   ; Negative Cache TTL
;
; name servers - NS records
     IN      NS      ns1.test2.yourdomain.com.

; name servers - A records
ns1.test2.yourdomain.com.          IN      A       10.12.0.1

; 10.12.0.0/16 - A records
test2.yourdomain.com.        IN      A      10.12.0.1 # 因為我web server和DNS在同一台

編輯 test3.yourdomain.com 正解設定
# vim /etc/named/zones/db.test3.yourdomain.com
$TTL    604800
@       IN      SOA     ns1.test3.yourdomain.com. admin.test3.yourdomain.com. (
                  3       ; Serial
             604800     ; Refresh
              86400     ; Retry
            2419200     ; Expire
             604800 )   ; Negative Cache TTL
;
; name servers - NS records
     IN      NS      ns1.test3.yourdomain.com.

; name servers - A records
ns1.test3.yourdomain.com.          IN      A       10.12.0.1

; 10.12.0.0/16 - A records
test3.yourdomain.com.        IN      A      10.12.0.1

編輯反解設定
# vim /etc/named/zones/db.10.12
$TTL    604800
@       IN      SOA     test2.yourdomain.com. test3.yourdomain.com. (
                              3         ; Serial
                         604800         ; Refresh
                          86400         ; Retry
                        2419200         ; Expire
                         604800 )       ; Negative Cache TTL
; name servers
      IN      NS      ns1.test2.yourdomain.com.
      IN      NS      ns1.test3.yourdomain.com.

; PTR Records
0.1   IN      PTR     ns1.test2.yourdomain.com.    ; 10.12.0.1
0.1   IN      PTR     ns1.test3.yourdomain.com.    ; 10.12.0.1
0.1   IN      PTR       test2.yourdomain.com.  ; 10.12.0.1

0.1   IN      PTR       test3.yourdomain.com.  ; 10.12.0.1

檢查bind 設定檔語法
# named-checkconf
檢查zone file
# named-checkzone test2.yourdomain.com /etc/named/zones/db.test2.yourdomain.com
zone test2.yourdomain.com/IN: loaded serial 3
OK
這邊 檢查時需特別注意,named-checkzone後的test2.yourdomain.com需換成你正確的域名,否則會報 ignoring out-of-zone data 的警告
# named-checkzone  12.10.in-addr.arpa /etc/named/zones/db.10.12
zone 12.10.in-addr.arpa/IN: loaded serial 3
OK

啟動bind9
# service named start

最後用nslookup檢查是否設定成功
# nslookup
> server 10.12.0.1
Default server: 10.12.0.1
Address: 10.12.0.1#53
> test2.yourdomain.com
Server:         10.12.0.1
Address:        10.12.0.1#53

編輯 /etc/ocserv/ocserv.conf ,讓手機連上VPN後優先選擇內網的DNS
dns = 10.12.0.1
dns = 8.8.8.8
dns = 8.8.4.4
然後重啟oscerv

最後手機連上VPN後訪問 test2.yourdomain.com,成功


參考資料:
https://www.digitalocean.com/community/tutorials/how-to-configure-bind-as-a-private-network-dns-server-on-centos-7  How To Configure BIND as a Private Network DNS Server on CentOS 7
http://blog.ltns.info/linux/openwrt_ocserv_vpn_client_smart_proxy/  OpenWrt路由器搭建和配置ocserv实现VPN客户端智能代理


2016年8月16日 星期二

MySQL SSH Tunnel

聽Jethro和Ike討論 Naticat的強大,如:SSH Tunnel、structure sync 

因為我常用的是phpmyadmin,所以找一下SSH Tunnel + phpmyadmin的解決方法

我有一台VPS機器,ssh需要私鑰登入,MySQL port沒有對外,如何使用phpmyadmin連線?

打開ssh通道
$ ssh -N <username>@<Bastian server IP> -L 3307:<internal ip of mysql server>:3306
-N 你不執行任何命令,僅端口轉發( port forward )
3307 本地要轉發的port
Bastian server IP 必須能連到MySQL 伺服器
3306 遠端機器的MySQL端口

因為我電腦使用Windows 7 ,所以這道命令在虛擬機上下的
ex. 
$ ssh -i vps.key -N user@vps.ip -L 3307:localhost:3306
3307:localhost:3306 是指虛擬機的3307端口 轉發到 vps.ip上的localhost 3306端口

檢測
$ telnet localhost 3307
...
5.6.28-76.1-log ... mysql_native_password
VPS上的mysql版本是5.6.28 沒錯

連線
$ mysql -u root -p  -h localhost -P 3307
經過幾次測試後, -P 是無效的,因為虛擬機和VPS的MySQL密碼和版本不一樣,但是都要輸入虛擬機的密碼,只能連到虛擬機的MySQL
原因:
當使用 localhost 參數時, MySQL使用sockets。請改用127.0.0.1
$ mysql -u root -p  -h 127.0.0.1 -P 3307

後記,因為虛擬機上沒裝phpmyadmin,所以Windows 7 在 config.inc.php 設不了 127.0.0.1:3307,只能設 192.168.x.x:3307 (虛擬機IP) ,去連VPS的MySQL。但是VPS的MySQL只允許 localhost或127.0.0.1登入

結論,使用Naticat的SSH Tunnel功能在此場景會比較方便,不過私鑰記得轉成ppk去連線





2016年5月10日 星期二

Linode CentOS 7主機搭建Cisco AnyConnect VPN

iPhone的shadowsocks在牆外似乎不作用,所以找了一個除了PPTP以外的代理方式

1. 安裝ocserv ( OpenConnect server )

因為ocserv已經在epel 的庫中提供了,可以直接用yum安裝
ocserv.x86_64 : OpenConnect SSL VPN server
直接用yum裝即可
# yum install epel-release (如果你還沒裝epel的話)
# yum install ocserv

2. 準備證書

# cd ~
# mkdir certificates
# cd certificates
在此目錄下新建一個 ca.tmpl的CA證書模版,內容為:
cn = "xx.xx.xx.xx"
organization = "xx.xx.xx.xx"
serial = 1
expiration_days = 3650
ca
signing_key
cert_signing_key
crl_signing_key

生成CA密鑰
$ certtool --generate-privkey --outfile ca-key.pem
生成CA證書
$ certtool --generate-self-signed --load-privkey ca-key.pem --template ca.tmpl --outfile ca-cert.pem
生成伺服器憑證,這裡注意cn項必須對應你伺服器的功能變數名稱或IP,server.tmpl
cn = "Your hostname or IP"
organization = "xx.xx.xx.xx"
expiration_days = 3650
signing_key
encryption_key
tls_www_server

生成密鑰
$ certtool --generate-privkey --outfile server-key.pem
生成server證書
$ certtool --generate-certificate --load-privkey server-key.pem --load-ca-certificate ca-cert.pem --load-ca-privkey ca-key.pem --template server.tmpl --outfile server-cert.pem
把證書移動到合適的地方
$ cp ca-cert.pem /etc/ocserv
$ cp server-cert.pem /etc/ocserv
$ cp server-key.pem /etc/ocserv

3. 準備配置文件

修改 /etc/ocserv/ocserv.conf
# 登陸方式,目前先用密碼登錄
auth = "plain[/etc/ocserv/ocpasswd]"
 
# 允許同時連接的用戶端數量
max-clients = 4
 
# 限制同一用戶端的並行登陸數量
max-same-clients = 2
 
# 服務監聽的IP(伺服器IP,可不設置)
listen-host = 1.2.3.4
 
# 服務監聽的TCP/UDP埠(選擇你喜歡的數位)
tcp-port = 4433
udp-port = 4434
 
# 自動優化VPN的網路性能
try-mtu-discovery = true
 
# 確保伺服器正確讀取用戶證書(後面會用到用戶證書)
cert-user-oid = 2.5.4.3
 
# 伺服器憑證與金鑰
server-cert = /etc/ssl/private/my-server-cert.pem
server-key = /etc/ssl/private/my-server-key.pem
 
# 用戶端連上vpn後使用的dns
dns = 8.8.8.8
dns = 8.8.4.4
 
# 注釋掉所有的route,讓伺服器成為gateway
#route = 192.168.1.0/255.255.255.0
 
# 啟用cisco用戶端相容性支援
cisco-client-compat = true

這邊要注意tcp-portudp-port不要設9000,以免跟php-fpm的port衝突

4. 測試server

創建測試帳號(test)
$ ocpasswd -c /etc/ocserv/ocpasswd test
Enter password:
Re-enter password:

開啟NAT轉發:(這步我略過,查機器上的設定檔,net.ipv4.ip_forward已開啟)
$ sudo sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g' /etc/sysctl.conf
$ sudo sysctl -p

啟動ocserv服務
# ocserv -f -d 1
然後打開手機上的Cisco Anyconnect新建一個VPN,添加服務器:
IP:端口
ex.
xx.xx.xx.xx:4433
連線就能輸入密碼運行

4. 創建客戶端證書,省得老輸入密碼

$ cd ~/certificates/
$ vim user.tmpl
cn = "some random name"
unit = "some random unit"
expiration_days = 365
signing_key
tls_www_client

創建user密鑰
$ certtool --generate-privkey --outfile user-key.pem
創建user證書
$ certtool --generate-certificate --load-privkey user-key.pem --load-ca-certificate ca-cert.pem --load-ca-privkey ca-key.pem --template user.tmpl --outfile user-cert.pem
將證書和金鑰轉為PKCS12的格式,好導入Anyconnect,期間會要求你輸入帳號密碼,就輸入用ocpasswd產生的帳號密碼
$ certtool --to-p12 --load-privkey user-key.pem --pkcs-cipher 3des-pkcs12 --load-certificate user-cert.pem --outfile user.p12 --outder
Generating a PKCS #12 structure...
Loading private key list...
Loaded 1 private keys.
Enter a name for the key: test
Enter password:
Confirm password:

然後把user.p12這個證書放到一個可以直接被訪問的地方,用safari打開安裝,導入成功後將對應的VPN => 進階 => 憑證 => 選擇你導入的憑證

為了讓伺服器能夠認得這張證書,我們再來修改一下/etc/ocserv/ocserv.conf配置
$ vim /etc/ocserv/ocserv.conf
# 改為證書登陸,注釋掉原來的登陸模式
auth = "certificate"
 
# 證書認證不支援這個選項,注釋掉這行
#listen-clear-file = /var/run/ocserv-conn.socket
 
# 啟用證書驗證
ca-cert = /etc/ocserv/ca-cert.pem

重啟ocserv
# kill -9 ocserv_pid
# ocserv -f -d 1

使用客戶端證書這方式我只有在公司網路環境才成功走代理,在家裡和3G網路VPN Log只顯示連線成功,但是瀏覽器打開iplocationfinder.com 檢查結果是失敗的 => 查不到原因,所以可能還是改為輸入帳號密碼登入為主  => 20160920已解決

5. 智能分流

因為人在大陸直接全局翻牆會造成大陸國內網站訪問巨慢的問題,所以必須在服務器端設route table推送到客戶端
https://github.com/CNMan/ocserv-cn-no-route
直接將 cn-no-route.txt 之中的 no-route 寫進 /etc/ocserv/ocserv.conf

最後一樣重啟ocserv服務

Windows客戶端下載
https://openconnect.github.io/openconnect-gui/

參考資料:
http://ifreedomlife.com/2015/04/20/Setup-Cisco-AnyConnect-VPN-on-CentOS7/ 在 CentOS 7 上搭建 Cisco AnyConnect VPN
https://www.logcg.com/archives/1343.html  使用ocserv搭建 Cisco Anyconnect 服务器 (主要)


2016年2月26日 星期五

nginx 跑CI 產生500錯誤空白頁

環境:Linode
系統:CentOS 7

把在VM上用Apache跑的CI 2.2.1專案搬到新的Linode上,打開後跑500錯誤空白頁,而且nginx error log 沒有報錯

原因:php-mysql沒有安裝
# php -i | grep mysql
PHP Warning:  Unknown: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in Unknown on line 0
安裝 php-mysql
# yum install php-mysql
重啟php-fpm(只重啟nginx無效)
# service php-fpm restart

參考資料:
http://serverfault.com/questions/412683/configuring-nginx-php5-fpm-to-work-with-codeigniter  Configuring Nginx & php5-fpm to work with Codeigniter


2015年12月29日 星期二

linode Centos 7 安裝pptp 伺服器

因為linode是新的,且centos系列不熟,參考網路上說要安裝pptpd,但是yum 一直搜不到 pptpd
# yum search pptp
Loaded plugins: fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
Loading mirror speeds from cached hostfile
 * base: mirrors.linode.com
 * extras: mirrors.linode.com
 * updates: mirrors.linode.com
====================================================== N/S matched: pptp ======================================================
pptp.x86_64 : Point-to-Point Tunneling Protocol (PPTP) Client
pptp-setup.x86_64 : PPTP Tunnel Configuration Script

  Name and summary matches only, use "search all" for everything.

解法:
安裝epel-release
# yum install epel-release

最後yum搜尋得到pptpd了,
# yum search pptp
...
NetworkManager-pptp.x86_64 : NetworkManager VPN plugin for PPTP
NetworkManager-pptp-gnome.x86_64 : NetworkManager VPN plugin for PPTP - GNOME files
pptp.x86_64 : Point-to-Point Tunneling Protocol (PPTP) Client
pptp-setup.x86_64 : PPTP Tunnel Configuration Script
pptpd.x86_64 : PoPToP Point to Point Tunneling Server
pptpd-sysvinit.noarch : PoPToP Point to Point Tunneling Server

因為linode的centos 7 原本就有裝ppp和iptables了,所以只需要裝pptpd就好

安裝pptpd
# yum install pptpd

編輯/etc/pptpd.conf
# vim  /etc/pptpd.conf
將最底下localip和remoteip 的註解拿掉
localip 192.168.0.1
remoteip 192.168.0.234-238,192.168.0.245

編輯/etc/ppp/options.pptpd
# vim /etc/ppp/options.pptpd
搜尋 ms-dns,加入下面這兩行
ms-dns 8.8.8.8
ms-dns 8.8.4.4

編輯 /etc/ppp/chap-secrets,設定帳號密碼
# vim /etc/ppp/chap-secrets
username pptpd password *

編輯/etc/sysctl.conf
# vim /etc/sysctl.conf
加入
net.ipv4.ip_forward=1
然後改變設定
# sysctl -p

設定iptables
# iptables -A INPUT -i eth0 -p tcp --dport 1723 -j ACCEPT
# iptables -A INPUT -i eth0 -p gre -j ACCEPT
# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
# iptables -A FORWARD -i ppp+ -o eth0 -j ACCEPT
# iptables -A FORWARD -i eth0 -o ppp+ -j ACCEPT
# service iptables save
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.
# service iptables restart
Redirecting to /bin/systemctl restart  iptables.service
Failed to issue method call: Unit iptables.service failed to load: No such file or directory.

重啟pptpd
# service pptpd restart
Redirecting to /bin/systemctl restart  pptpd.service

最後用手機連vpn後打開http://www.iplocationfinder.com/,檢測翻牆有沒有成功

重開機啟動pptpd服務後連不上
http://linux.it.net.cn/CentOS/fast/2014/1102/7635.html
With RHEL 7 / CentOS 7, firewalld was introduced to manage iptables. IMHO, firewalld is more suited for workstations than for server environments.
It is possible to go back to a more classic iptables setup. First, stop and mask the firewalld service:
RHEL 7/CentOS 7 firewalld是被做為管理iptables,述我直言,firewalld比較適合工作站而不是服務器環境。
這有可能把他調回傳統的iptables設定
查firewalld狀態
# systemctl status firewalld
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled)
   Active: inactive (dead)
啟動iptables報錯
# systemctl start iptables
Failed to issue method call: Unit iptables.service failed to load: No such file or directory.
stop和mask firewalld服務
# systemctl stop firewalld
# systemctl mask firewalld
安裝search iptables-services
# yum install iptables-services
開機時啟動iptables的服務
systemctl enable iptables
儲存防火牆設定
# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
啟動iptables服務(這次無報錯)
# systemctl start iptables.service

某天iPhone手機連不上PPTP服務
在CentOS 7 server查pptpd的log
方法1
# journalctl -f
方法2
# tail -f /var/log/messages
有看到 CTRL: PTY read or GRE write failed (pty,gre)=(6,7) 錯誤, 但是查這篇 https://www.lidaren.com/archives/1229 後,試著把 /etc/pptpd.conf 的 logwtmp 註解掉,但手機仍然連不上
結果:
使用Windows7電腦連上了,看來是手機的問題


參考資料:
https://www.photonvps.com/billing/knowledgebase.php?action=displayarticle&id=58   How to setup VPN server (PPTP on CentOS, RedHat and Ubuntu)?

2015年3月24日 星期二

阿里雲和網站備案實戰

在中國只要開80 port就需要備案,以下以企業網站備案為實例

買網址
http://www.net.cn/ 萬網
在首頁可以搜尋網址,用支付寶(淘寶)登錄,然後用支付寶付費
买域名时需要填的资料:
域名所有者单位名称:*
域名管理联系人:*
所属区域:*
通讯地址:*
邮编:*
联系电话:*
手机:*
电子邮箱:*
企业管理人:
所属行业:
之後要填《网站备案信息真实性核验单》(需負責人簽名蓋公章)


設定網址聯繫人,以便即時提醒通知(如網址快過期了)
登錄 http://www.net.cn/ 萬網後
上方 "产品管理/续费" => 我的域名 => 在你買的網址後面點"管理" => 進入"域名控制台"頁面
裡面有"基本信息"、"域名信息修改"、"域名所有者实名认证" 、"域名证书打印(備案時需要打印出來寄送)" ...等,可以修改聯繫信箱電話、查詢網址实名认证结果

網址綁定IP
在 "域名控制台" 中 點上方的"域名解析" 裡面可以"設置解析"

買虛擬主機
http://www.net.cn/hosting/basic/
基础版
网页空间: 2G
单月流量: 20GB
并发连接数: 150个
价格:288元/年

進入虛擬主機管理控制台
登錄 http://www.net.cn/ 萬網後
上方 "产品管理/续费" => "我的主机" =>  在你買的主機後面點"管理"( 在這個列表頁可以看到我們買的虛擬主機IP ) => 進入"主機管理控制台"頁面
站点信息 => 基础环境设置 => 域名绑定 - 可以綁定網址,但基本上還是要在你"域名控制台"上設定才對
站点信息 => 高级环境设置 => PHP版本设置 - 可以把PHP設成4, 5.2, 5.3,但要"停止站点"後重啟才會生效
站点信息 => 高级环境设置 => PHP.ini设置 - 可以弱弱的設定一點點PHP設定,
数据库管理 => 管理 - 進入phpmyadmin( 版本有點舊 )管理你虛擬主機的資料庫

發票索取
http://help.www.net.cn/knowledge_detail.htm?knowledgeId=5867323

缺點:
無法ssh,不能修改php.ini設定,云虚拟主机仅支持1个站点。

因為Joomla 3 在PHP5.3以下php.ini需要關閉 magic_quotes_gpc這設定,但虛擬主機沒辦法設定,所以買了阿里雲ECS

買阿里雲ECS (類似Linode的VPS,可以ssh)
進入阿里雲網頁 http://www.aliyun.com/ ,可用淘寶(支付寶)帳號登錄
产品 => 弹性计算 => 云服务器ECS http://www.aliyun.com/product/ecs/ 這邊有介紹阿里 雲ECS => 點"立即购买"後可以配置你要買的ECS
我選
包年包月 ( 買10個月送兩個月 ) 的青島主機
CPU: 1核 内存: 1GB 数据盘: 0G 带宽: 1Mbps(不限流量)
"镜像类型"選"市场镜像"然後可以選人家配置好的主機環境(有點類似docker的image)
並在此步驟設定ssh時root的密碼
以下是我選的鏡像:
PHP运行环境(Ubuntu 64位 | PHP5.4 | Apache)
Ubuntu12.04 64位 来源:上海驻云信息科技有限公司
集成软件:PHP5.4.27、Apache2.4.9、MySQL5.5.37 ... etc

進入阿里雲ECS後台
進入阿里雲網頁 http://www.aliyun.com/ => "管理控制台" => 已开通的产品与服务: "云服务器ECS" => "实例" (列表中可以看到你買的ECS的IP,啟動後可以ssh登錄) => "管理"
可以用網頁的ssh ,只是要設網頁的登錄密碼,而且不好用,還是建議用putty連線
停機後可以更改作業系統,如CentOS改Ubuntu (資料會不見)

新增雲主機聯繫人
会员中心 => 会员信息 => 联系人管理

找查阿里雲ECS的MySQL密碼(以我上面選的市場鏡像為例)
重設密碼
$ bash /alidata/init/initPasswd.sh # 重設mysql和ftp密碼
MySQL和FTP密碼放在
/alidata/account.log

備案
http://beian.aliyun.com/ 備案流程圖
1. 登录备案系统 - 
https://beian.gein.cn 阿里雲ICP 代備案管理系統
這邊帳號和淘寶是分開的,需重新申請
注意:這邊必須寫業主的資料,如手機、email..等。如果將來要改資料,還要重新跑一次備案流程。如檢核點拍照、重新郵寄資料...
2. 填写备案信息 - 
企业备案请提前准备好企业证件负责人证件扫描件或照片,例如营业执照组织机构代码证
3. 提交初审 - 
阿里云审核时长为1个工作日结果将以短信和邮件形式通知
4. 邮寄资料和办理拍照 - 
http://help.aliyun.com/knowledge_detail.htm?knowledgeId=5974581 办理拍照流程说明
邮寄资料 - 
a. 网站备案信息真实性核验单 请打印3份,按照样例分别填写并全部邮寄(前面签名盖公章的那份) - 必須打印三份後分別簽名蓋章,不能簽名蓋章後打印三份,否則會被退
b. 域名证书 - 在"域名控制台" 中打印出來一份
c. 邮寄地址 - 略,備案網頁流程上會跟你說
办理拍照 - 前往拍照核验点免费办理拍照
a. 请 網站負責人 本人携带 身份证原件 和 记录备案订单号(你將會知道) 前往核验点(你將會知道)
5. 等待管局审核 - 大概要等兩個禮拜(實測約16天)
6. 备案成功


參考資料:
http://beian.aliyun.com/ 備案流程
http://www.net.cn/ 萬網,阿里雲的,買網址、租虛擬主機
https://beian.gein.cn 阿里雲ICP 代備案管理系統
http://cp.hichina.com/ 萬網 - (虛擬主機)主机管理控制台
http://www.aliyun.com/ 阿里雲 - 租雲服務器ECS ( 可ssh )