解决内网拉取企微会话存档代理问题的一种办法

问题:客户的服务都是内网的,不能直接访问外网;访问外网的话需要走kong网关才能出去。

会话存档官网说可以使用socket5http方式拉取会话存档;我这边尝试了直接使用kong网关的ip和端口配置进去,是访问不了的

我后面就又尝试了使用nginxsquid 做正向代理的方式使用http方式访问,也是没有成功;这两种做代理服务器为什么不能成功,没有理解;有知道的朋友可以告诉我一下,有成功使用这两个做代理服务器拉取消息成功的朋友可以分享下经验。

搞一个socket5的代理服务器是可以成功,我没有试过,但是问过踩过坑的人说这种方式可行。

如果懒得麻烦再搭一个代理服务器,那就可以自己动手写一个简单的代理服务器搞定它。

大概的交互流程如下:
在这里插入图片描述

下面是全部的代码,拷过去,调一调应该就能用了

ForwardProxyApplication

import xxx.ProxyService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.Resource;

@SpringBootApplication
public class ForwardProxyApplication implements CommandLineRunner {

    @Resource
    private ProxyService proxyService;

    public static void main(String[] args) {
        SpringApplication.run(ForwardProxyApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        proxyService.start(args);
    }
}

ProxyConfig

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ProxyConfig {

    @Value("${socket.port}")
    public Integer socketPort;

    @Value("${proxy.host}")
    public String proxyHost;

    @Value("${proxy.port}")
    public Integer proxyPort;
}

ProxyService

import xxx.config.ProxyConfig;
import xxx.thread.ProxyHandleThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

@Service
public class ProxyService {

    private static final Logger logger = LoggerFactory.getLogger(ProxyService.class);

    @Resource
    private ProxyConfig proxyConfig;

    public void start(String[] args) {
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(proxyConfig.socketPort);
            logger.info("Server running at http://0.0.0.0:{}", proxyConfig.socketPort);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        while (true) {
            try {
                assert ss != null;
                Socket socket = ss.accept();
                socket.setSoTimeout(1000 * 60);
                String line = "";
                InputStream is = socket.getInputStream();
                String tempHost = "", host;
                int port = 80;
                String type = null;
                OutputStream os = socket.getOutputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));

                int temp = 1;
                StringBuilder sb = new StringBuilder();
                while ((line = br.readLine()) != null) {
                    logger.info(line + "-----------------");
                    if (temp == 1) {  
                        type = line.split(" ")[0];
                        if (type == null) continue;
                    }
                    temp++;
                    String[] s1 = line.split(": ");
                    if (line.isEmpty()) {
                        break;
                    }
                    for (int i = 0; i < s1.length; i++) {
                        if (s1[i].equalsIgnoreCase("host")) {
                            tempHost = s1[i + 1];
                        }
                    }
                    sb.append(line).append("\r\n");
                    line = null;
                }
                sb.append("\r\n"); 
                if (tempHost.split(":").length > 1) {
                    port = Integer.parseInt(tempHost.split(":")[1]);
                }
                host = tempHost.split(":")[0];
                Socket proxySocket = null;
                if (host != null && !host.equals("")) {
                	// todo 这里最重要的
                    host = proxyConfig.proxyHost;
                    port = proxyConfig.proxyPort;
                    proxySocket = new Socket(host, port);
                    proxySocket.setSoTimeout(1000 * 60);
                    OutputStream proxyOs = proxySocket.getOutputStream();
                    InputStream proxyIs = proxySocket.getInputStream();
                    if (Objects.requireNonNull(type).equalsIgnoreCase("connect")) {  
                        os.write("HTTP/1.1 200 Connection Established\r\n\r\n".getBytes());
                        os.flush();
                    } else {
                        proxyOs.write(sb.toString().getBytes(StandardCharsets.UTF_8));
                        proxyOs.flush();
                    }
                    new ProxyHandleThread(is, proxyOs).start(); //监听客户端传来消息并转发给服务器
                    new ProxyHandleThread(proxyIs, os).start(); //监听服务器传来消息并转发给客户端
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

ProxyHandleThread

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketTimeoutException;

public class ProxyHandleThread extends Thread {
    private final InputStream input;
    private final OutputStream output;

    public ProxyHandleThread(InputStream input, OutputStream output) {
        this.input = input;
        this.output = output;
    }

    @Override
    public void run() {
        try {
            BufferedInputStream bis = new BufferedInputStream(input);
            byte[] buffer = new byte[1024];
            int length = -1;
            while ((length = bis.read(buffer)) != -1) {
                output.write(buffer, 0, length);
                length = -1;
            }
            output.flush();
            try {
                Thread.sleep(5000);     //不能执行完了,就把线程关掉,不然双方交互的数据还没有处理完,这里写了个睡眠 5s,可以酌情考虑
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } catch (SocketTimeoutException e) {
            try {
                input.close();
                output.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                input.close();
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

没有搞定的,可以加好友一起聊聊,请备注csdn加我的
在这里插入图片描述

感谢 https://blog.csdn.net/jumprn/article/details/90173852

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三书yjy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值