2025年5月10日星期六

为Dify 的 ssrf_proxy 配置上游代理

 # 为Dify 的 ssrf_proxy 配置上游代理

在使用 Dify 过程中,可能会遇到需要通过代理服务器来访问外部网络的场景。Dify 使用 ssrf_proxy(Squid ) 来提升sandbox的安全性。通过配置 Squid 代理,可以方便地实现这一需求。在本文中,我们将介绍如何通过配置 Squid 的 cache_peer 和 never_direct 指令,实现所有 HTTP 请求都通过上游代理转发的目标。

#### 什么是 Squid?


Squid 是一款流行的代理服务器软件,它能够缓存 HTTP、FTP 等协议的请求,减少带宽消耗并提升访问速度。Squid 还可以充当代理服务器,将用户的请求转发给上游代理,从而为应用程序提供额外的隐私和控制功能。在配置 Dify 的 `ssrf_proxy` 服务时,我们可以利用 Squid 将 HTTP 请求通过上游代理进行转发。


#### 需求背景


在配置 `ssrf_proxy` 时,我们需要让 HTTP 请求通过 Squid 代理服务器的上游代理进行访问,避免直接访问外部网络。通过配置 Squid 的 `cache_peer` 和 `never_direct` 指令,我们可以轻松实现这个目标。


#### 配置步骤


##### 1. 打开 Squid 配置文件


在 Dify 的 Docker 配置目录中,找到 `squid.conf.template` 文件。这个文件位于 `dify/docker/` 目录下。使用任意文本编辑器打开此文件,准备进行修改。


```bash

cd dify/docker/ssrf_proxy

nano squid.conf.template

```


##### 2. 配置 `cache_peer`


`cache_peer` 指令用于指定 Squid 代理的上游代理服务器。基本语法如下:


```bash

cache_peer <hostname> <type> <http-port> <icp-port> [options]

```


- `<hostname>`:上游代理服务器的主机名或 IP 地址。

- `<type>`:代理类型,通常为 `parent`,表示这是一个上游代理。

- `<http-port>`:上游代理服务器的 HTTP 端口(如 3128)。

- `<icp-port>`:ICP 端口,如果不需要可以设置为 0。

- `[options]`:额外的选项,如 `no-query` 禁用 ICP 查询,`default` 设为默认上游代理。


例如,如果你的上游代理的 IP 地址为 `192.168.1.100`,HTTP 端口为 `3128`,则可以这样配置:


```bash

cache_peer 192.168.1.100 parent 3128 0 no-query default

```


此配置将所有请求发送到位于 `192.168.1.100` 的上游代理,并禁用了 ICP 查询功能。


##### 3. 添加 `never_direct allow all`


为了确保 Squid 代理始终通过上游代理转发请求,而不直接连接外部网络,我们需要在配置文件中添加 `never_direct allow all` 指令。这一指令强制所有请求都通过上游代理处理。


在 `squid.conf.template` 文件的合适位置(通常在 `cache_peer` 指令附近)添加以下内容:


```bash

never_direct allow all

```


##### 4. 完整配置示例


在完成上述步骤后,`squid.conf.template` 文件的完整配置可能如下所示:


```bash

# 定义上游代理

cache_peer 192.168.1.100 parent 3128 0 no-query default


# 禁止直接连接,强制使用上游代理

never_direct allow all

```


##### 5. 保存并重启服务


在完成配置后,保存文件并退出编辑器。然后,重新启动 Dify 及其 `ssrf_proxy` 服务,以应用新的 Squid 配置。你可以使用如下命令重启服务:


```bash

docker-compose down

docker-compose up -d

```


通过这种方式,你的 Squid 代理服务器就会将所有请求都转发给配置的上游代理,而不会直接访问目标服务器。


#### 结论


通过配置 `cache_peer` 和 `never_direct allow all`,我们可以强制 Squid 代理服务器将所有 HTTP 请求都通过上游代理转发。这种配置非常适合需要通过特定代理进行网络访问的场景,尤其是在企业网络或需要遵守特定网络策略的环境中。


希望这篇文章能帮助你顺利完成 Dify 的 `ssrf_proxy` 服务配置。如果你有任何疑问或进一步的需求,欢迎在评论区留言交流!

2023年4月2日星期日

autossh exit when working background

 Try to add -N to your arguments.

autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -f -N -R :5678:127.0.0.1:5678 root@<server_ip>

Through a socks5 proxy

autossh -M 0 -o "ProxyCommand nc -x 127.0.0.1:7891 -X 5 %h %p" -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -R :5678:127.0.0.1:5678 root@<server_ip>

2023年3月1日星期三

Change default user of WSL1&2

Recently, I moved my WSL Ubuntu distribution out of my C drive due to insufficient disk space.

After the "export-import" operation, my default login user for WSL changed to root.

Normally, as a virtual environment, it shouldn't bother me at all. However, my Python packages are installed under a normal user account, which was the default login user.

I tried to find the /etc/wsl.conf file but failed, so I decided to do a small trick to save time.

 Append following code to /root/.bashrc.

PP_NAME=`ps -p $PPID -o comm=`
#echo "PP_NAME: $PP_NAME"
if [ "$PP_NAME" == "init" ]
then
    su <YOUR_USERNAME>
    exit
fi

But it only works when we start wsl from cmd. VSCode's wsl will still start with root user.

Thus, we still need to use the /etc/wsl.conf .

If there is no /etc/wsl.conf, then create it. And fill the file with:

[user] default=username

If you are using WSL 1, just close all your WSL instances. And restart WSL.

If you are using WSL 2, you need to use 'wsl --shutdown' command to stop your WSL2 VM first, then restart your  WSL.


2023年2月21日星期二

ChatGPT对于普通人来说,是个灾难。

 

最近ChatGPT的火爆,让所有人,包括我们这些外行都见识到了大模型的厉害。

于是,根据历来传统,想要自主可控的欲望越发的强烈。笔者在自己的老爷机器(i7 4790,32G RAM,gtx1080 8G)上尝试跑了一下GPT2的预训练模型(如果有人感兴趣,可以给大伙弄个简单的流程说明)。

效果嘛:





典型的人工智障。

来看看ChatGPT是咋说的:




为啥和ChatGPT差距这么大?

主要原因还是在模型规模上。

为啥不搞个更大的模型啊?

因为笔者不配。。。

我们知道,神经网络或者是深度神经网络的三大要素:模型、数据以及算力。

就GPT来说:

  • 模型基本公开。
  • 数据就是自然语言。麻烦主要在标注部分,但是一定能解决(毕竟笔者这样的廉价劳动力有的是)。
  • 算力,也就是我们的炼丹炉,在大模型上来说就比较难搞了。我们来讨论一下。

目前chatgpt的GPT-3.5还没有具体的信息。所以我们还是以目前公开领域能看到的GPT-3为例。这玩意有1750亿的参数。这是个什么概念呢?

我们用有具体数据的开源模型来估计吧。一个60亿参数的GPT-J模型,其预训练参数的zip包大概60GB。一款3A大作也就这样了。1750亿,扩大了将近30倍!

有人可能会觉得:还不到2TB,现在硬盘也不贵,这有什么?

小伙汁,这就naive啦。这玩意做推理和训练的时候,是要载入内存哒。而且,这么大的模型,要在可接受的时间内计算出结果,用CPU也是不行哒,要用并行计算来加速。一般来说就是GPU了。

仍旧以GPT-J为估算基础,加载这个模型内存要40-50G以上,显存要在13G以上。使用rtx3090 tuning(注意,是tuning,还不是training),需要12个小时。之后的模型参数还会膨胀一些,大概要到80G。

那么GPT-3炼丹大概会是一个什么地狱场景,想必大家心里也应该有数了吧(据说需要几百块A100,训练时间以天计算)。更可怕的是GPT-3现在已经过时了。。。

chatgpt背后的模型据说比GPT-3还要大。这就是我之前文章中认为中小企业无法拥有自己的LLM的主要原因。

分析了一圈。一块A100,便宜的也要6万多。对于一个普通人来说,别说数百块,一块就破产。想要自己跑一个可用的GPT,基本是没什么希望的。

ChatGPT的所作所为大伙最近应该已经看了够多的了。作为一个普通人,要想不被AI卷死,就只能用大企业的服务,老老实实的交AI税。但是别无选择的服务和奴役又有什么区别?

2022年5月8日星期日

为qbittorrent-nox配置jackett搜索插件(config jackett search plugin for qbittorrent-nox)

1 download jackett plugin(installing the url in webui directly won't work!)

https://raw.githubusercontent.com/qbittorrent/search-plugins/master/nova3/engines/jackett.py
2 move jackett.py to 

~/.local/share/data/qBittorrent/nova3/engines/, or ~/.local/share/qBittorrent/nova3/engines/

3 create a jackett.json file in the same directory with the content below:

{ "api_key": "YOUR_API_KEY_HERE", "tracker_first": false, "url": "http://127.0.0.1:9117" }

4 restart your qbittorrent-nox. 

2022年4月10日星期日

Install docker on ubuntu [Short version]

 Tl;Dr

sudo apt-get remove docker docker-engine docker.io containerd runc
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

 

For ones who have plenty of time to wast :

 https://docs.docker.com/engine/install/ubuntu/

 

Config dockerd to use proxy:

 

sudo mkdir -p /etc/systemd/system/docker.service.d
sudo vi /etc/systemd/system/docker.service.d/http-proxy.conf 
[Service]
Environment="HTTP_PROXY=http://
192.168.1.66:7890"
Environment="HTTPS_PROXY=http://
192.168.1.66:7890"
Environment="NO_PROXY=localhost,127.0.0.1,192.168.1.66,192.168.1.108"  
 
sudo systemctl daemon-reload
sudo systemctl restart docker 
sudo systemctl show --property=Environment docker 

2021年7月2日星期五

Extract shsq

 

As we know the normal Magic number is 0x73717368("hsqs").

But some vendors modified the squashfs format to support LZMA, and changed the magic to 0x71736873("shsq").

But now, squashfs has already supported LZMA. Thus we can modify our binary image and let unsquashfs do its work.

  1. Open image with a hex edtor or create your own tool.
  2. Change the magic "shsq" to "hsqs" at the beginning of the image.
  3. Change compression_id(image_header[20]) from "1-GZIP" to "2-LZMA"
  4. run unsquashfs as usual.
ref:https://dr-emann.github.io/squashfs/