1
13693261870
2022-09-16 fee60c3e25fac0982f3b8cb8feea7225c4ed22f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.terra.proxy.servlet;
 
import com.terra.proxy.bean.VistorBean;
import org.apache.commons.lang3.StringUtils;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMethod;
 
import javax.servlet.http.HttpServletRequest;
import java.net.HttpURLConnection;
import java.net.URL;
 
public class MyProxyServlet extends ProxyServlet {
 
    private boolean flag;
 
    private Logger log = LoggerFactory.getLogger(MyProxyServlet.class);
 
    @Override
    protected String rewriteUrlFromRequest(HttpServletRequest servletRequest) {
 
        flag = Boolean.parseBoolean(this.getConfigParam("flag"));
        // VistorBean 操作日志入库信息
        VistorBean vistorBean = new VistorBean();
        // IP
        vistorBean.setRequestip(servletRequest.getRemoteHost());
        // URI
        if (StringUtils.isNotEmpty(servletRequest.getRequestURI())) {
            vistorBean.setServerurl(servletRequest.getRequestURI());
        }
        //
        StringBuffer queryString2 = new StringBuffer();
        //
        StringBuffer uri = new StringBuffer();
        // servlet.json 配置了 targetUri
        if (StringUtils.isNotEmpty(getTargetUri(servletRequest))) {
            uri.append(getTargetUri(servletRequest));
        }
        // pathInfo
        if (StringUtils.isNotEmpty(servletRequest.getPathInfo())) {
            uri.append(servletRequest.getPathInfo());
        }
        // queryString
        String queryString = servletRequest.getQueryString();
        if (StringUtils.isNotEmpty(queryString)) {
            String[] params = queryString.split("&");
            for (int i = 0; i < params.length; i++) {
                queryString2 = queryString2.append("&").append(params[i]);
            }
        } else {
            if (flag) {
                vistorBean.setResult("Fail");
                // 返回页面 500
                return "http://" + "localhost" + ":" + servletRequest.getLocalPort() + "/500.html";
            }
        }
        //
        String fragment = null;
        if (StringUtils.isNotEmpty(queryString)) {
            int fragIdx = queryString.indexOf('#');
            if (fragIdx >= 0) {
                fragment = queryString.substring(fragIdx + 1);
            }
        }
        if (StringUtils.isNotEmpty(queryString2)) {
            uri.append('?').append(encodeUriQuery(queryString2));
        }
        if ((this.doSendUrlFragment) && (fragment != null)) {
            uri.append('#').append(encodeUriQuery(fragment));
        }
        //
        vistorBean.setResult("ok");
 
        String url = uri.toString();
        boolean result = checkHttpUrlConnection(getTargetUri());
        if (!result) {
            url = url.replace("71.3.251.28", "71.3.251.29");
        }
        log.info("*******************************");
        log.info("FINAL HTTP URL : " + url);
        log.info("*******************************");
 
        return url;
    }
 
    private boolean checkHttpUrlConnection(String url) {
        boolean result = false;
        try {
            URL testUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) testUrl.openConnection();
            connection.setRequestMethod(String.valueOf(RequestMethod.GET));
            connection.setUseCaches(false);
            connection.setConnectTimeout(3000);
            connection.connect();
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                result = true;
            }
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
 
 
}