一、背景
最近公司在做網路安全整改,對大資料環境的網路安全要求更高,要求所有的大資料環境的機器不能訪問外網,但是可以訪問公司的非大資料環境的其他服務器,我們組有四臺單獨的機器在大資料環境中,相關的python腳本都部署在這4臺機器上,這些python腳本都是做大資料相關分析的,同時很多都有訪問外網的需求,比如訪問天氣api查詢歷史天氣、訪問百度距離api查詢、根據統計資料定時發郵件等,這些都是訪問外網的,

大資料環境的機器不能訪問外網,但是可以訪問公司非大資料環境的機器,非大資料環境的機器可以訪問外網,在考慮整改帶來的代碼入侵性和公司網路現狀的情況下,準備申請一臺在公司非大資料環境的機器做外網出口的正向代理,將所有的外網訪問地址統一轉到這臺機器上,然后由這一臺機器統一訪問外網;這樣及可以滿足安全需求,也可以較少的修改代碼,只需要替換現有外網地址對應的ip和埠即可,
二、整改程序
2.1.現狀分析
統計現在四臺機器的外網訪問情況,發現主要有外網訪問:一是呼叫外部的天氣API和百度距離API,另一種是定時發郵件到相關同事的郵箱中;所以需要做兩類代理:http代理、郵件服務器代理,
2.2.申請機器,搭建Nginx
需要申請一臺不在大資料環境但是在公司網路環境的機器,并且大資料機器可以訪問這臺機器,同時這臺機器可以訪問外網,(目前申請一臺機器,未考慮高可用),
申請的機器地址:101.147.192.179, 作用:安裝nginx,做為代理,后續不管是訪問http外部介面還是發郵件,都使用這個ip,
公司真實的郵件服務器地址:mail.xxxxxx.cn
搭建nginx:
[ops@dis-algo data]$
[ops@dis-algo data]$ mkdir nginx
[ops@dis-algo data]$
[ops@dis-algo data]$ cd nginx/
[ops@dis-algo nginx]$
# 下載nginx
[ops@dis-algo nginx]$ wget http://nginx.org/download/nginx-1.20.1.tar.gz
[ops@dis-algo nginx]$ ls
nginx-1.20.1.tar.gz
[ops@dis-algo nginx]$
# 解壓
[ops@dis-algo nginx]$ tar -zxvf nginx-1.20.1.tar.gz
[ops@dis-algo nginx]$ ll
total 1040
drwxr-xr-x 8 ops ops 158 May 25 20:35 nginx-1.20.1
-rw-rw-r-- 1 ops ops 1061461 May 25 23:34 nginx-1.20.1.tar.gz
[ops@dis-algo nginx]$
[ops@dis-algo nginx]$ cd nginx-1.20.1/
[ops@dis-algo nginx-1.20.1]$ ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[ops@dis-algo nginx-1.20.1]$
[ops@dis-algo nginx-1.20.1]$
[ops@dis-algo nginx-1.20.1]$
[ops@dis-algo nginx-1.20.1]$
# 配置,由于是使用stream方式代理郵件服務器,所以需要--with-stream模塊,不要--with-mail模塊
# 同時需要http代理相關的模塊
# 安裝的路徑在當前目錄的上一級 --prefix=.. 直接在當前目錄安裝由于檔案沖突會報錯
[ops@dis-algo nginx-1.20.1]$ ./configure --prefix=.. --with-http_stub_status_module --with-http_ssl_module --with-stream
[ops@dis-algo nginx-1.20.1]$
[ops@dis-algo nginx-1.20.1]$
# 編譯、安裝
[ops@dis-algo nginx-1.20.1]$ make
[ops@dis-algo nginx-1.20.1]$ make install
[ops@dis-algo nginx-1.20.1]$
[ops@dis-algo nginx-1.20.1]$ ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[ops@dis-algo nginx-1.20.1]$
[ops@dis-algo nginx-1.20.1]$
[ops@dis-algo nginx-1.20.1]$
[ops@dis-algo nginx-1.20.1]$
# 回到上一級的安裝目錄
[ops@dis-algo nginx-1.20.1]$ cd ..
[ops@dis-algo nginx]$ ls
conf html logs nginx-1.20.1 nginx-1.20.1.tar.gz sbin
[ops@dis-algo nginx]$
[ops@dis-algo nginx]$
2.3.配置http代理和郵件代理
(base) [ops@dis-algo85 nginx]$
(base) [ops@dis-algo85 nginx]$ cd conf/
# 找到組態檔 nginx.conf
(base) [ops@dis-algo85 conf]$ ls
fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf
fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default
(base) [ops@dis-algo85 conf]$
(base) [ops@dis-algo85 conf]$
# 根據具體情況修改組態檔 完成http代理和郵件代理的配置
(base) [ops@dis-algo85 conf]$ vim nginx.conf
(base) [ops@dis-algo85 conf]$
(base) [ops@dis-algo85 conf]$ cat nginx.conf
worker_processes 8;
error_log /data/nginx/logs/error.log;
pid /data/nginx/logs/nginx.pid;
events {
worker_connections 1024;
}
# http代理
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /data/nginx/logs/access.log main;
# DNS服務器
resolver 8.8.8.8;
server {
# 后續外部http請求都使用101.147.192.179:18080訪問
server_name 101.147.192.179;
listen 18080;
keepalive_requests 120;
# 對每個http介面做代理
location ^~ /caiyunapp/ {
proxy_pass https://api.caiyunapp.com/;
}
# get傳參使用$request_uri
location = /routematrix/v2/riding {
proxy_pass http://api.map.baidu.com$request_uri;
}
location ^~ /tianqihoubao {
proxy_pass http://www.tianqihoubao.com/;
}
location ^~ /weather {
proxy_pass http://www.weather.com.cn/;
}
}
}
# 郵件服務器代理 需要使用--with-stream模塊
stream {
server {
listen 25; # 這里必須使用25 其他的埠號我沒有測通;使用25埠要求啟動使用root權限
proxy_pass mail.xxxxxx.cn:25; # 原來的公司郵件服務器
}
}
(base) [ops@dis-algo85 conf]$
# 測驗組態檔是否正確
(base) [ops@dis-algo85 conf]$ ../sbin/nginx -t
nginx: the configuration file ../conf/nginx.conf syntax is ok
nginx: configuration file ../conf/nginx.conf test is successful
(base) [ops@dis-algo85 conf]$
# 啟動 報錯
# 需要執行nginx -c nginx.conf命令指定組態檔
(base) [ops@dis-algo85 conf]$ ../sbin/nginx -s reload
nginx: [error] invalid PID number "" in "/data/nginx/logs/nginx.pid"
(base) [ops@dis-algo85 conf]$
# 指定組態檔, 要求使用sudo,因為郵件代理使用的是25埠,這個埠要求root
(base) [ops@dis-algo85 conf]$ sudo ../sbin/nginx -c /data/nginx/conf/nginx.conf
(base) [ops@dis-algo85 conf]$
# 切換root用戶前,先關閉nginx
(base) [ops@dis-algo85 conf]$
(base) [ops@dis-algo85 conf]$ ../sbin/nginx -s stop
(base) [ops@dis-algo85 conf]$
# 以root用戶重啟 要求先以root用戶身份指定組態檔(sudo nginx -c )
(base) [ops@dis-algo85 conf]$ sudo ../sbin/nginx -s reload
(base) [ops@dis-algo85 conf]$
# 查看是否重啟成功
(base) [ops@dis-algo85 conf]$ netstat -nltp | grep 25
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN -
(base) [ops@dis-algo85 conf]$
2.4.測驗
在大資料環境的四臺機器上測驗,
2.4.1.Http代理測驗
1.測驗api.caiyunapp.com
原來的地址:
curl https://api.caiyunapp.com/v2.5/YRVvqVfC3j0K794p/119.292776,26.075473/realtime.json
代理的地址:
curl http://101.147.192.179:18080/caiyunapp/v2.5/YRVvqVfC3j0K794p/119.292776,26.075473/realtime.json
2.測驗api.map.baidu.com
原來的地址:
curl http://api.map.baidu.com/routematrix/v2/riding?output=json\&riding_type=1\&origins=30.903624,104.259426\&destinations=30.892165,104.254266\&ak=5xYpGVj7A9WqnHERyB9U4QtM6uRTO75T
代理的地址:
curl http://101.147.192.179:18080/routematrix/v2/riding?output=json\&riding_type=1\&origins=30.903624,104.259426\&destinations=30.892165,104.254266\&ak=5xYpGVj7A9WqnHERyB9U4QtM6uRTO75T
3.測驗www.tianqihoubao.com
原來的地址:
curl http://www.tianqihoubao.com/lishi/wuxi/month/201101.html
代理的地址:
curl http://101.147.192.179:18080/tianqihoubao/lishi/wuxi/month/201101.html
4.測驗www.weather.com.cn
原來的地址:
curl http://www.weather.com.cn/weather15d/101230101.shtml
代理的地址:
curl http://101.147.192.179:18080/weather/weather15d/101230101.shtml
2.4.2.郵件代理測驗
1.引入jar包
<!--發郵件-->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.6</version>
</dependency>
2.java測驗
package com.wuxiaolong;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
public class TestEmail {
private final static String TIMEOUT_MS = "20000";
public static void main(String[] args) throws Exception{
// 這是公司原來的郵件服務器 代理之前使用這個
// String host = "mail.xxxxxx.cn";
// 這是代理服務器(nginx) 代理之后使用這個
String host = "101.147.192.179";
String port = "25";// SMTP郵件服務器默認埠
// 公司的郵箱發郵件
String user = "wuxl3@abc.cn";
String password = "XXXXXX";
// 163郵箱收郵件
String recipients = "wuxiaolongah@163.com";
String subject = "郵件發送測驗";
String content = "郵件正文:<br>你好 proxy!";
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", port);
props.put("mail.smtp.host", host);
props.put("mail.smtp.timeout", TIMEOUT_MS);
Authenticator auth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
};
Session session = Session.getInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(user));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
msg.setSubject(subject);
// 向multipart物件中添加郵件的各個部分內容,包括文本內容和附件
Multipart multipart = new MimeMultipart();
// 添加郵件正文
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent(content, "text/html;charset=UTF-8");
multipart.addBodyPart(contentPart);
// 將multipart物件放到message中
msg.setContent(multipart);
// 保存郵件
msg.saveChanges();
Transport.send(msg, msg.getAllRecipients());
}
}
測驗結果:

2.5.修改代碼
相關的python代碼按照上面2.4的測驗的代理地址進行修改,
2.6.后續注意事項
- 后續這四臺機器上有新的外網地址訪問,需要提前加入到這臺代理機器上,
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/307197.html
標籤:其他
上一篇:MySQL ERROR 1064 (42000)——不管怎樣grant總是報錯,怎么回事?
下一篇:666-集群聊天服務器專案總結
